Chromium Code Reviews| 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 = _find_commits_inbetween( | |
| 17 start_hash, until, git_working_dir, verbose) | |
| 18 if verbose: | |
| 19 print "All commits between <of> and <until>: " + all_commits_raw | |
| 20 | |
| 21 #Adding start hash too | |
|
Michael Achenbach
2015/05/04 08:57:53
nit: space after # - also below
Michael Hablich
2015/05/04 12:17:31
Done.
| |
| 22 all_commits = [start_hash] | |
| 23 all_commits.extend(all_commits_raw.splitlines()) | |
| 24 all_related_commits = {} | |
| 25 already_treated_commits = [] | |
|
Michael Achenbach
2015/05/04 08:57:53
Make already_treated_commits a set as it has lots
Michael Hablich
2015/05/04 12:17:31
Acknowledged.
| |
| 26 for commit in all_commits: | |
| 27 if commit in already_treated_commits: | |
| 28 continue | |
| 29 | |
| 30 related_commits = search_related_commits( | |
| 31 git_working_dir, commit, until, deadline, verbose) | |
| 32 if len(related_commits) > 0: | |
| 33 all_related_commits[commit] = related_commits | |
| 34 already_treated_commits.extend(related_commits) | |
| 35 | |
| 36 already_treated_commits.append(commit) | |
| 37 | |
| 38 return all_related_commits | |
| 39 | |
| 40 def search_related_commits( | |
| 41 git_working_dir, start_hash, until, deadline, verbose=False): | |
| 42 | |
| 43 if deadline: | |
| 44 commits_between = _find_commits_inbetween( | |
| 45 start_hash, deadline, git_working_dir, verbose) | |
| 46 if commits_between == "": | |
| 47 return [] | |
| 48 | |
| 49 #Extract commit position | |
| 50 original_message = _git_execute( | |
| 51 git_working_dir, | |
| 52 ["show", "-s", "--format=%B", start_hash], | |
| 53 verbose) | |
| 54 title = original_message.splitlines()[0] | |
| 55 | |
| 56 matches = re.search("(\{#)([0-9]*)(\})", original_message) | |
| 57 commit_position = matches.group(2) | |
| 58 if verbose: | |
| 59 print "1.) Commit position to look for: " + commit_position | |
| 60 | |
| 61 search_range = start_hash + ".." + until | |
| 62 | |
| 63 def git_args(grep_pattern): | |
| 64 return [ | |
| 65 "log", | |
| 66 "--reverse", | |
| 67 "--grep=" + grep_pattern, | |
| 68 "--format=%H", | |
| 69 search_range, | |
| 70 ] | |
| 71 | |
| 72 found_by_hash = _git_execute( | |
| 73 git_working_dir, git_args(start_hash), verbose).strip() | |
| 74 | |
| 75 if verbose: | |
| 76 print "2.) Found by hash: " + found_by_hash | |
| 77 | |
| 78 found_by_commit_pos = _git_execute( | |
| 79 git_working_dir, git_args(commit_position), verbose).strip() | |
| 80 | |
| 81 if verbose: | |
| 82 print "3.) Found by commit position: " + found_by_commit_pos | |
| 83 | |
| 84 # Replace brackets or else they are wrongly interpreted by --grep | |
|
Michael Achenbach
2015/05/04 08:57:53
Are there no other things that need to be escaped?
Michael Hablich
2015/05/04 12:17:31
The String is already escaped when it is given to
| |
| 85 title = title.replace("[", "\\[") | |
| 86 title = title.replace("]", "\\]") | |
| 87 | |
| 88 found_by_title = _git_execute( | |
| 89 git_working_dir, git_args(title), verbose).strip() | |
| 90 | |
| 91 if verbose: | |
| 92 print "4.) Found by title: " + found_by_title | |
| 93 | |
| 94 hits = ( | |
| 95 _convert_to_array(found_by_hash) + | |
| 96 _convert_to_array(found_by_commit_pos) + | |
| 97 _convert_to_array(found_by_title)) | |
| 98 hits = _remove_duplicates(hits) | |
| 99 | |
| 100 if deadline: | |
| 101 for current_hit in hits: | |
| 102 commits_between = _find_commits_inbetween( | |
| 103 deadline, current_hit, git_working_dir, verbose) | |
| 104 if commits_between != "": | |
| 105 return hits | |
| 106 return [] | |
| 107 | |
| 108 return hits | |
| 109 | |
| 110 def _find_commits_inbetween(start_hash, end_hash, git_working_dir, verbose): | |
| 111 commits_between = _git_execute( | |
| 112 git_working_dir, | |
| 113 ["rev-list", "--reverse", start_hash + ".." + end_hash], | |
| 114 verbose) | |
| 115 return commits_between.strip() | |
| 116 | |
| 117 def _convert_to_array(string_of_hashes): | |
| 118 return string_of_hashes.splitlines() | |
| 119 | |
| 120 def _remove_duplicates(array): | |
| 121 no_duplicates = [] | |
| 122 for current in array: | |
| 123 if not current in no_duplicates: | |
| 124 no_duplicates.append(current) | |
| 125 return no_duplicates | |
| 126 | |
| 127 def _git_execute(working_dir, args, verbose=False): | |
| 128 command = ["git", "-C", working_dir] + args | |
| 129 if verbose: | |
| 130 print "Git working dir: " + working_dir | |
| 131 print "Executing git command:" + str(command) | |
| 132 p = Popen(args=command, stdin=PIPE, | |
| 133 stdout=PIPE, stderr=PIPE) | |
| 134 output, err = p.communicate() | |
| 135 rc = p.returncode | |
| 136 if rc != 0: | |
| 137 raise Exception(err) | |
| 138 if verbose: | |
| 139 print "Git return value: " + output | |
| 140 return output | |
| 141 | |
| 142 def _pretty_print_entry(hash, git_dir, pre_text, verbose): | |
| 143 text_to_print = _git_execute( | |
| 144 git_dir, | |
| 145 ["show", | |
| 146 "--quiet", | |
| 147 "--date=iso", | |
| 148 hash, | |
| 149 "--format=%ad # %H # %s"], | |
| 150 verbose) | |
| 151 return pre_text + text_to_print.strip() | |
| 152 | |
| 153 def main(options): | |
| 154 | |
| 155 output = [] | |
| 156 if options.all: | |
| 157 all_related_commits = search_all_related_commits( | |
| 158 options.git_dir, | |
| 159 options.of[0], | |
| 160 options.until[0], | |
| 161 options.deadline, | |
| 162 options.verbose) | |
| 163 | |
| 164 sort_key = lambda x: ( | |
| 165 _git_execute( | |
| 166 options.git_dir, | |
| 167 ["show", "--quiet", "--date=iso", x, "--format=%ad"], | |
| 168 options.verbose)).strip() | |
| 169 | |
| 170 high_level_commits = sorted(all_related_commits.keys(), key=sort_key) | |
| 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( | |
| 188 options.git_dir, options.of[0], | |
| 189 options.until[0], options.deadline, options.verbose) | |
| 190 if hits: | |
| 191 output.append("\n".join(hits)) | |
|
Michael Achenbach
2015/05/04 08:57:53
As you _use_ main() like a generator, how about ma
Michael Hablich
2015/05/04 12:17:31
sgtm
| |
| 192 | |
| 193 return output | |
| 194 | |
| 195 if __name__ == "__main__": # pragma: no cover | |
| 196 parser = argparse.ArgumentParser( | |
| 197 "This tool searches the git repository for " | |
|
Michael Achenbach
2015/05/04 08:57:53
Is this description accurate? Isn't the tool searc
Michael Hablich
2015/05/04 12:17:31
Updated description.
| |
| 198 "commits which are related to the commit <of>.") | |
| 199 parser.add_argument("-g", "--git-dir", required=False, default=".", | |
| 200 help="The path to your git working directory.") | |
| 201 parser.add_argument("--verbose", action="store_true", | |
|
Michael Achenbach
2015/05/04 08:57:53
This is quite chatty. How about calling this "--de
Michael Hablich
2015/05/04 12:17:31
The intention is to keep in line with the rest of
| |
| 202 help="Enables verbose output") | |
| 203 parser.add_argument("of", nargs=1, | |
| 204 help="Hash of the commit to be searched.") | |
| 205 parser.add_argument("until", nargs=1, | |
| 206 help="Commit when searching should stop") | |
| 207 parser.add_argument("--all", action="store_true", | |
| 208 help="Searches for related commits in all " | |
| 209 "commits between <of> and <until>") | |
| 210 parser.add_argument("--deadline", required=False, | |
|
Michael Achenbach
2015/05/04 08:57:53
"deadline" and "until" are very similar in their w
Michael Hablich
2015/05/04 12:17:31
separator sounds good.
| |
| 211 help="The script will only list related commits " | |
| 212 "which are separated by hash <--deadline>.") | |
| 213 parser.add_argument("--prettyprint", action="store_true", | |
| 214 help="Pretty prints the output") | |
| 215 | |
| 216 args = sys.argv[1:] | |
| 217 options = parser.parse_args(args) | |
| 218 for current_line in main(options): | |
| 219 print current_line | |
| OLD | NEW |