-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-multi-cherry-pick
executable file
·119 lines (98 loc) · 2.84 KB
/
git-multi-cherry-pick
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python2
#
# Get N commits, and try commiting them in the order given, and retry again the failed
# commits until reaching the end of the list.
#
# This means that the time complexity is O(N^2) in the number of commits.
#
import os
import sys
import tempfile
import time
import re
import cPickle
EDITOR="EDITOR.TEMP"
class TempFile(object):
filename = None
class Info(dict):
def __init__(self, cherry_picked_branch = None, original_branch = None):
self.cherry_picked_branch = cherry_picked_branch
self.original_branch = original_branch
@classmethod
def write(selfclass, v):
f = open(selfclass.tempfile, "w")
f.write(v)
f.close()
@classmethod
def read(selfclass):
return open(selfclass.tempfile).read()
@staticmethod
def load():
info = TempFile.Info()
vars(info).update(cPickle.loads(TempFile.read()))
return info
@staticmethod
def save(info):
TempFile.write(cPickle.dumps(vars(info)))
def editor_commit_editmsg(filename):
info = TempFile.load()
f = open(filename)
output = []
for line in f:
output.append(line)
output.append("(representive of original merge " + info.original_branch + ")\n")
f.close()
f = open(filename, "w")
f.write(''.join(output))
f.close()
TempFile.save(info)
def main():
if len(sys.argv) <= 1:
print "syntax: git-multi-cherry-pick [description-file]"
return
commits = []
for commit in open(sys.argv[1]).readlines():
commit = commit.strip()
if commit:
commits.append(commit)
commits_succeeded = []
commits_failed = []
while len(commits):
commits_retry = []
for commit in commits:
if '#' in commit:
commit_id = commit.split('#', 1)[0]
else:
commit_id = commit
print "-"*80
print "git-multi-cherry-pick: Trying to apply: %s" % (commit, )
print
ret = os.system("git cherry-pick %s" % (commit_id, ))
if ret != 0:
commits_retry.append(commit)
print
print "git-multi-cherry-pick: failed this time"
os.system("git cherry-pick --abort")
else:
commits_succeeded.append(commit)
if len(commits_retry) == len(commits):
commits_failed = commits_retry
break
else:
commits = commits_retry
if commits_succeeded:
print
print "Succeeded: "
print
for commit in commits_succeeded:
print commit
print
if commits_failed:
print
print "Failed: "
print
for commit in commits_failed:
print commit
print
if __name__ == "__main__":
main()