OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2015 the V8 project authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 import argparse | |
7 import os | |
8 import sys | |
9 import re | |
10 import operator | |
11 from subprocess import Popen, PIPE | |
12 | |
13 def search_all_related_commits( | |
14 git_working_dir, start_hash, until, deadline, verbose=False): | |
15 | |
16 all_commits_raw = (_git_execute( | |
17 git_working_dir, | |
18 ["rev-list", "--reverse", start_hash + ".." + until], verbose)) | |
19 if verbose: | |
20 print "All commits between <of> and <until>: " + all_commits_raw | |
21 | |
22 #Adding start hash too | |
Michael Achenbach
2015/04/28 21:23:51
nit: Space after # - end comments with periods.
Michael Hablich
2015/04/29 09:22:42
Done.
| |
23 all_commits = [start_hash] | |
24 all_commits.extend(all_commits_raw.splitlines()) | |
25 all_related_commits = {} | |
26 already_treated_commits = [] | |
27 for commit in all_commits: | |
28 if commit in already_treated_commits: | |
29 continue | |
30 | |
31 related_commits = (search_related_commits( | |
32 git_working_dir, commit, until, deadline, verbose)) | |
33 if len(related_commits) > 0: | |
34 all_related_commits[commit] = related_commits | |
35 already_treated_commits.extend(related_commits) | |
36 | |
37 already_treated_commits.append(commit) | |
38 | |
39 return all_related_commits | |
40 | |
41 def search_related_commits( | |
42 git_working_dir, start_hash, until, deadline, verbose=False): | |
43 | |
44 if deadline: | |
45 commits_between = (_git_execute( | |
46 git_working_dir, | |
47 ["rev-list", "--reverse", start_hash + ".." + deadline], | |
48 verbose)) | |
49 if commits_between.strip() == "": | |
50 return [] | |
51 | |
52 #Extract commit position | |
53 original_message = _git_execute(git_working_dir, | |
54 ["show", "-s", "--format=%B", start_hash], verbose) | |
55 title = original_message.splitlines()[0] | |
56 | |
57 matches = re.search("(\{#)([0-9]*)(\})", original_message) | |
58 commit_position = matches.group(2) | |
59 if verbose: | |
60 print "1.) Commit position to look for: " + commit_position | |
61 | |
62 search_range = start_hash + ".." + until | |
63 | |
64 found_by_hash = _git_execute( | |
65 git_working_dir, ( | |
66 ["log", | |
67 "--reverse", | |
68 search_range, | |
69 "--grep=" + start_hash, | |
70 "--format=%H"]), | |
71 verbose) | |
72 found_by_hash = found_by_hash.strip() | |
73 | |
74 if verbose: | |
75 print "2.) Found by hash: " + found_by_hash | |
76 | |
77 found_by_commit_pos = (_git_execute( | |
78 git_working_dir,( | |
Michael Achenbach
2015/04/28 21:23:51
nit: space after comma
Michael Hablich
2015/04/29 09:22:42
Done.
| |
79 ["log", | |
80 "--reverse", | |
81 search_range, | |
82 "--grep=" + commit_position, | |
83 "--format=%H"]), | |
84 verbose)) | |
85 | |
86 found_by_commit_pos = found_by_commit_pos.strip() | |
87 | |
88 if verbose: | |
89 print "3.) Found by commit position: " + found_by_commit_pos | |
90 | |
91 #Replace brackets or else they are wrongly interpreted by --grep | |
92 title = title.replace("[", "\\[") | |
93 title = title.replace("]", "\\]") | |
94 | |
95 found_by_title = (_git_execute( | |
96 git_working_dir,( | |
97 ["log", "--reverse", | |
98 search_range, | |
99 '--grep=' + title, | |
100 "--format=%H"]), | |
101 verbose)) | |
102 | |
103 found_by_title = found_by_title.strip() | |
104 | |
105 if verbose: | |
106 print "4.) Found by title: " + found_by_title | |
107 | |
108 hits = ( | |
109 _convert_to_array(found_by_hash) + | |
110 _convert_to_array(found_by_commit_pos) + | |
111 _convert_to_array(found_by_title)) | |
112 hits = _remove_duplicates(hits) | |
113 | |
114 return hits | |
115 | |
116 def _convert_to_array(string_of_hashes): | |
117 if len(string_of_hashes) == 0: | |
118 return [] | |
119 return string_of_hashes.splitlines() | |
120 | |
121 def _remove_duplicates(array): | |
122 no_duplicates = [] | |
123 for current in array: | |
124 if not current in no_duplicates: | |
125 no_duplicates.append(current) | |
126 return no_duplicates | |
127 | |
128 def _git_execute(working_dir, commands, verbose=False): | |
129 | |
130 fullCommand = ["git", "-C", working_dir] + commands | |
131 if verbose: | |
132 print "Git working dir: " + working_dir | |
133 print "Executing git command:" + str(fullCommand) | |
134 p = Popen(args=fullCommand, stdin=PIPE, | |
135 stdout=PIPE, stderr=PIPE) | |
136 output, err = p.communicate() | |
137 rc = p.returncode | |
138 if rc != 0: | |
139 raise Exception(err) | |
140 if verbose: | |
141 print "Git return value: " + output | |
142 return output | |
143 | |
144 def _pretty_print_entry(hash, git_dir, pre_text, verbose): | |
145 | |
146 text_to_print = _git_execute( | |
147 git_dir, | |
148 ["show", | |
149 "--quiet", | |
150 "--date=iso", | |
151 hash, | |
152 "--format=%ad # %H # %s"], | |
153 verbose) | |
154 return pre_text + text_to_print.strip() | |
155 | |
156 def main(options): | |
157 | |
158 output = [] | |
159 if options.all: | |
160 all_related_commits = search_all_related_commits( | |
161 options.git_dir, | |
162 options.of[0], | |
163 options.until[0], | |
164 options.deadline, | |
165 options.verbose) | |
166 | |
167 high_level_commits = sorted(all_related_commits.keys(), key = lambda x: ( | |
168 (_git_execute(options.git_dir, | |
169 ["show", "--quiet", "--date=iso", x, "--format=%ad"], | |
170 options.verbose)).strip())) | |
171 | |
172 for current_key in high_level_commits: | |
173 if options.prettyprint: | |
174 output.append(_pretty_print_entry( | |
175 current_key, options.git_dir, "+", options.verbose)) | |
176 else: | |
177 output.append("+" + current_key) | |
178 | |
179 found_commits = all_related_commits[current_key] | |
180 for current_commit in found_commits: | |
181 if options.prettyprint: | |
182 output.append(_pretty_print_entry( | |
183 current_commit, options.git_dir, "| ", options.verbose)) | |
184 else: | |
185 output.append("| " + current_commit) | |
186 else: | |
187 hits = search_related_commits(options.git_dir, options.of[0], | |
188 options.until[0], options.deadline, options.verbose) | |
189 if len(hits) > 0: | |
190 output.append("\n".join(hits)) | |
191 | |
192 return output | |
193 | |
194 if __name__ == "__main__": # pragma: no cover | |
195 parser = argparse.ArgumentParser( | |
196 ("This tool searches the git repository for " | |
197 "commits which are related to the commit <of>.")) | |
198 parser.add_argument("-g", "--git-dir", required=False, default=".", | |
199 help="The path to your git working directory.") | |
200 parser.add_argument("--verbose", action="store_true", | |
201 help="Enables verbose output") | |
202 parser.add_argument("of", nargs=1, | |
203 help="Hash of the commit to be searched.") | |
204 parser.add_argument("until", nargs=1, | |
205 help="Commit when searching should stop") | |
206 parser.add_argument("--all", action="store_true", | |
207 help=("Searches for related commits in all " | |
208 "commits between <of> and <until>")) | |
209 parser.add_argument("--deadline", required=False, | |
210 help=("The script will only list related commits " | |
211 "which are separated by hash <--deadline>.")) | |
212 parser.add_argument("--prettyprint", action="store_true", | |
213 help=("Pretty prints the output")) | |
214 | |
215 args = sys.argv[1:] | |
216 options = parser.parse_args(args) | |
217 for current_line in main(options): | |
218 print current_line | |
OLD | NEW |