3
3
import sys
4
4
5
5
6
- def validate (repo , parent ):
7
- master_commits = repo .git .rev_list ('master' ).split ()
8
- if parent not in set (master_commits ):
6
+ def validate (repo , parent , branch ):
7
+ branch_commits = repo .git .rev_list (branch ).split ()
8
+ if parent not in set (branch_commits ):
9
9
raise ValueError (
10
- 'This commit has not actually been merged into master.' )
10
+ 'This commit has not actually been merged into %s' % branch )
11
11
12
12
first_parent = repo .git .rev_list (
13
- 'master' , first_parent = True ).split ()
13
+ branch , first_parent = True ).split ()
14
14
if parent in set (first_parent ):
15
15
raise ValueError (
16
- 'This commit was originally made on the master branch?' )
16
+ 'This commit was originally made on the %s branch?' % branch )
17
17
18
18
19
- def get_first_merge_into (repo , parent ):
19
+ def get_first_merge_into (repo , parent , branch ):
20
20
"""
21
21
Stupid algorithm which works most of the time.
22
22
23
23
Follow the commit downstream and return the first merge into another
24
24
branch, as opposed to a merge from another branch. Hopefully, this other
25
- branch is master .
25
+ branch is <branch> .
26
26
27
27
Abort if the graph starts branching or terminates.
28
28
"""
29
29
children_dict = {}
30
- for line in repo .git .rev_list ('master' , children = True ).split ('\n ' ):
30
+ for line in repo .git .rev_list (branch , children = True ).split ('\n ' ):
31
31
commits = line .split ()
32
32
children_dict [commits [0 ]] = commits [1 :]
33
33
34
34
parents_dict = {}
35
- for line in repo .git .rev_list ('master' , parents = True ).split ('\n ' ):
35
+ for line in repo .git .rev_list (branch , parents = True ).split ('\n ' ):
36
36
commits = line .split ()
37
37
parents_dict [commits [0 ]] = commits [1 :]
38
38
@@ -51,21 +51,21 @@ def is_second_parent(child, parent):
51
51
parent = child
52
52
53
53
54
- def get_ancestry_path_first_parent_match (repo , parent ):
54
+ def get_ancestry_path_first_parent_match (repo , parent , branch ):
55
55
"""
56
56
Find the earliest common commit between ancestry-path and first-parent.
57
57
58
58
Source: http://stackoverflow.com/a/8492711
59
59
60
60
This is the correct algorithm assuming a properly-maintained git
61
61
history. However, if there has ever been a fast-forward merge of a feature
62
- branch into master , the first-parent history of master will have been
62
+ branch into <branch> , the first-parent history of <branch> will have been
63
63
tampered with and this "correct" approach could fail.
64
64
"""
65
65
ancestry_path = repo .git .rev_list (
66
- parent + '..master' , ancestry_path = True ).split ()
66
+ parent + '..' + branch , ancestry_path = True ).split ()
67
67
first_parent = repo .git .rev_list (
68
- 'master' , first_parent = True ).split ()
68
+ branch , first_parent = True ).split ()
69
69
70
70
for commit in reversed (ancestry_path ):
71
71
if commit in first_parent :
@@ -74,8 +74,27 @@ def get_ancestry_path_first_parent_match(repo, parent):
74
74
75
75
76
76
def get_merge ():
77
+ if 'help' in sys .argv :
78
+ print ("""usage: git get-merge <sha> [branch]
79
+
80
+ Attempt to find when commit <sha> was merged to <branch>, where <branch> is
81
+ `master` by default. Two methods are used:
82
+
83
+ # method 1
84
+ %s
85
+
86
+ # method 2
87
+ %s
88
+ """ % (get_first_merge_into .__doc__ , get_ancestry_path_first_parent_match .__doc__ ))
89
+ return 0
90
+
77
91
repo = git .Repo (os .getcwd ())
78
92
93
+ try :
94
+ branch = sys .argv [2 ]
95
+ except IndexError :
96
+ branch = 'master'
97
+
79
98
try :
80
99
parent = repo .git .rev_parse (sys .argv [1 ])
81
100
repo .git .show (sys .argv [1 ]) # validate existence
@@ -84,13 +103,13 @@ def get_merge():
84
103
return 1
85
104
86
105
try :
87
- validate (repo , parent )
106
+ validate (repo , parent , branch )
88
107
except ValueError as err :
89
108
print ('\n ' .join (err .args ))
90
109
return 1
91
110
92
- guess1 = get_first_merge_into (repo , parent )
93
- guess2 = get_ancestry_path_first_parent_match (repo , parent )
111
+ guess1 = get_first_merge_into (repo , parent , branch )
112
+ guess2 = get_ancestry_path_first_parent_match (repo , parent , branch )
94
113
if not (guess1 or guess2 ):
95
114
print ('Unable to resolve.' )
96
115
return 1
0 commit comments