diff options
author | Victor Boivie <victor.boivie@sonyericsson.com> | 2010-11-11 20:36:39 +0100 |
---|---|---|
committer | Shawn O. Pearce <sop@google.com> | 2011-04-07 17:19:06 -0400 |
commit | d572a13021b0430eddf83e9caedc9d5add693c62 (patch) | |
tree | a0dfe1a74aded3d9266eef72ee9ceec4cc9ef5a4 /subcmds/cherry_pick.py | |
parent | 3ba5f95b46f06ed3d7382346de29ac550d749ec9 (diff) | |
download | git-repo-d572a13021b0430eddf83e9caedc9d5add693c62.tar.gz |
Added repo cherry-pick commandv1.7.4.3
It is undesired to have the same Change-Id:-line for two separate
commits, and when cherry-picking, the user must manually change it.
If this is not done, bad things may happen (such as when the user
is uploading the cherry-picked commit to Gerrit, it will instead
see it as a new patch-set for the original change, or worse).
repo cherry-pick works the same was as git cherry-pick, except that
it replaces the Change-Id with a new one and adds a reference
back to the commit from where it was picked.
On failures (when git can not successfully apply the cherry-picked
commit), instructions will be written to the user.
Change-Id: I5a38b89839f91848fad43386d43cae2f6cdabf83
Diffstat (limited to 'subcmds/cherry_pick.py')
-rw-r--r-- | subcmds/cherry_pick.py | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/subcmds/cherry_pick.py b/subcmds/cherry_pick.py new file mode 100644 index 00000000..8da3a750 --- /dev/null +++ b/subcmds/cherry_pick.py | |||
@@ -0,0 +1,114 @@ | |||
1 | # | ||
2 | # Copyright (C) 2010 The Android Open Source Project | ||
3 | # | ||
4 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | # you may not use this file except in compliance with the License. | ||
6 | # You may obtain a copy of the License at | ||
7 | # | ||
8 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | # | ||
10 | # Unless required by applicable law or agreed to in writing, software | ||
11 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | # See the License for the specific language governing permissions and | ||
14 | # limitations under the License. | ||
15 | |||
16 | import sys, re, string, random, os | ||
17 | from command import Command | ||
18 | from git_command import GitCommand | ||
19 | |||
20 | CHANGE_ID_RE = re.compile(r'^\s*Change-Id: I([0-9a-f]{40})\s*$') | ||
21 | |||
22 | class CherryPick(Command): | ||
23 | common = True | ||
24 | helpSummary = "Cherry-pick a change." | ||
25 | helpUsage = """ | ||
26 | %prog <sha1> | ||
27 | """ | ||
28 | helpDescription = """ | ||
29 | '%prog' cherry-picks a change from one branch to another. | ||
30 | The change id will be updated, and a reference to the old | ||
31 | change id will be added. | ||
32 | """ | ||
33 | |||
34 | def _Options(self, p): | ||
35 | pass | ||
36 | |||
37 | def Execute(self, opt, args): | ||
38 | if len(args) != 1: | ||
39 | self.Usage() | ||
40 | |||
41 | reference = args[0] | ||
42 | |||
43 | p = GitCommand(None, | ||
44 | ['rev-parse', '--verify', reference], | ||
45 | capture_stdout = True, | ||
46 | capture_stderr = True) | ||
47 | if p.Wait() != 0: | ||
48 | print >>sys.stderr, p.stderr | ||
49 | sys.exit(1) | ||
50 | sha1 = p.stdout.strip() | ||
51 | |||
52 | p = GitCommand(None, ['cat-file', 'commit', sha1], capture_stdout=True) | ||
53 | if p.Wait() != 0: | ||
54 | print >>sys.stderr, "error: Failed to retrieve old commit message" | ||
55 | sys.exit(1) | ||
56 | old_msg = self._StripHeader(p.stdout) | ||
57 | |||
58 | p = GitCommand(None, | ||
59 | ['cherry-pick', sha1], | ||
60 | capture_stdout = True, | ||
61 | capture_stderr = True) | ||
62 | status = p.Wait() | ||
63 | |||
64 | print >>sys.stdout, p.stdout | ||
65 | print >>sys.stderr, p.stderr | ||
66 | |||
67 | if status == 0: | ||
68 | # The cherry-pick was applied correctly. We just need to edit the | ||
69 | # commit message. | ||
70 | new_msg = self._Reformat(old_msg, sha1) | ||
71 | |||
72 | p = GitCommand(None, ['commit', '--amend', '-F', '-'], | ||
73 | provide_stdin = True, | ||
74 | capture_stdout = True, | ||
75 | capture_stderr = True) | ||
76 | p.stdin.write(new_msg) | ||
77 | if p.Wait() != 0: | ||
78 | print >>sys.stderr, "error: Failed to update commit message" | ||
79 | sys.exit(1) | ||
80 | |||
81 | else: | ||
82 | print >>sys.stderr, """\ | ||
83 | NOTE: When committing (please see above) and editing the commit message, | ||
84 | please remove the old Change-Id-line and add: | ||
85 | """ | ||
86 | print >>sys.stderr, self._GetReference(sha1) | ||
87 | print >>sys.stderr | ||
88 | |||
89 | def _IsChangeId(self, line): | ||
90 | return CHANGE_ID_RE.match(line) | ||
91 | |||
92 | def _GetReference(self, sha1): | ||
93 | return "(cherry picked from commit %s)" % sha1 | ||
94 | |||
95 | def _StripHeader(self, commit_msg): | ||
96 | lines = commit_msg.splitlines() | ||
97 | return "\n".join(lines[lines.index("")+1:]) | ||
98 | |||
99 | def _Reformat(self, old_msg, sha1): | ||
100 | new_msg = [] | ||
101 | |||
102 | for line in old_msg.splitlines(): | ||
103 | if not self._IsChangeId(line): | ||
104 | new_msg.append(line) | ||
105 | |||
106 | # Add a blank line between the message and the change id/reference | ||
107 | try: | ||
108 | if new_msg[-1].strip() != "": | ||
109 | new_msg.append("") | ||
110 | except IndexError: | ||
111 | pass | ||
112 | |||
113 | new_msg.append(self._GetReference(sha1)) | ||
114 | return "\n".join(new_msg) | ||