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, of, until, deadline, verbose=False): | |
| 15 | |
| 16 hash = of | |
|
Michael Achenbach
2015/04/27 13:09:43
not liked
Michael Hablich
2015/04/27 15:39:00
Done.
| |
| 17 | |
| 18 all_commits_raw = (_git_execute( | |
| 19 git_working_dir, | |
| 20 ["rev-list", "--reverse", hash + ".." + until], verbose)) | |
| 21 if verbose: | |
| 22 print "All commits between <of> and <until>: " + all_commits_raw | |
| 23 | |
| 24 all_commits = 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(git_working_dir, of, until, deadline, verbose=False): | |
| 42 | |
| 43 hash = of | |
|
Michael Achenbach
2015/04/27 13:09:43
not liked
Michael Hablich
2015/04/27 15:39:00
Done.
| |
| 44 | |
| 45 if deadline: | |
| 46 | |
| 47 commits_between = (_git_execute( | |
| 48 git_working_dir, | |
| 49 ["rev-list", "--reverse", hash + ".." + deadline], | |
| 50 verbose)) | |
| 51 if commits_between.strip() == "": | |
| 52 return [] | |
| 53 | |
| 54 #Extract commit position | |
| 55 original_message = _git_execute(git_working_dir, | |
| 56 ["show", "-s", "--format=%B", hash], verbose) | |
| 57 title = original_message.splitlines()[0] | |
| 58 | |
| 59 matches = re.search("(\{#)([0-9]*)(\})", original_message) | |
| 60 commit_position = matches.group(2) | |
| 61 if verbose: | |
| 62 print "1.) Commit position to look for: " + commit_position | |
| 63 | |
| 64 search_range = hash + ".." + until | |
| 65 | |
| 66 found_by_hash = (_git_execute( | |
| 67 git_working_dir, ( | |
|
Michael Achenbach
2015/04/27 13:09:43
You don't need additional parentheses in python as
Michael Hablich
2015/04/27 15:39:00
Done.
| |
| 68 ["log", "--reverse", | |
| 69 search_range, | |
|
Michael Achenbach
2015/04/27 13:09:43
nit: indentation, align list items with content in
| |
| 70 "--grep=" + hash, "--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,( | |
| 79 ["log", "--reverse", | |
| 80 search_range, | |
| 81 "--grep=" + commit_position, | |
| 82 "--format=%H"]), | |
| 83 verbose)) | |
| 84 | |
| 85 found_by_commit_pos = found_by_commit_pos.strip() | |
| 86 | |
| 87 if verbose: | |
| 88 print "3.) Found by commit position: " + found_by_commit_pos | |
| 89 | |
| 90 #Replace brackets or else they are wrongly interpreted by --grep | |
| 91 title = title.replace("[", "\\[") | |
| 92 title = title.replace("]", "\\]") | |
| 93 | |
| 94 found_by_title = (_git_execute( | |
| 95 git_working_dir,( | |
| 96 ["log", "--reverse", | |
| 97 search_range, | |
| 98 '--grep=' + title, | |
| 99 "--format=%H"]), | |
| 100 verbose)) | |
| 101 | |
| 102 found_by_title = found_by_title.strip() | |
| 103 | |
| 104 if verbose: | |
| 105 print "4.) Found by title: " + found_by_title | |
| 106 | |
| 107 hits = ( | |
| 108 _convert_to_array(found_by_hash) + | |
| 109 _convert_to_array(found_by_commit_pos) + | |
| 110 _convert_to_array(found_by_title)) | |
| 111 hits = _remove_duplicates(hits) | |
| 112 | |
| 113 return hits | |
| 114 | |
| 115 def _convert_to_array(string_of_hashes): | |
| 116 if len(string_of_hashes) == 0: | |
| 117 return [] | |
| 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, commands, verbose=False): | |
| 128 | |
| 129 fullCommand = ["git", "-C", working_dir] + commands | |
| 130 if verbose: | |
| 131 print "Git working dir: " + working_dir | |
| 132 print "Executing git command:" + str(fullCommand) | |
| 133 p = Popen(args=fullCommand, stdin=PIPE, | |
| 134 stdout=PIPE, stderr=PIPE) | |
| 135 output, err = p.communicate() | |
| 136 rc = p.returncode | |
| 137 if rc != 0: | |
| 138 raise Exception(err) | |
| 139 if verbose: | |
| 140 print "Git return value: " + output | |
| 141 return output | |
| 142 | |
| 143 def _pretty_print_entry(hash, pre_text, verbose): | |
|
Michael Achenbach
2015/04/27 13:09:43
Format and readability - how about:
output = _g
Michael Hablich
2015/04/27 15:39:00
Done.
| |
| 144 | |
| 145 text_to_print = pre_text + ( | |
| 146 (_git_execute( | |
| 147 options.git_dir, | |
| 148 ( | |
| 149 ["show", | |
| 150 "--quiet", | |
| 151 "--date=iso", | |
| 152 hash, | |
| 153 "--format=%ad # %H # %s"]), | |
| 154 verbose)).strip()) | |
| 155 print text_to_print | |
| 156 | |
| 157 if __name__ == "__main__": # pragma: no cover | |
| 158 parser = argparse.ArgumentParser( | |
| 159 ("This tool searches the git repository for " | |
| 160 "commits which are related to the commit <of>.")) | |
| 161 parser.add_argument("-g", "--git-dir", required=False, default=".", | |
| 162 help="The path to your git working directory.") | |
| 163 parser.add_argument("--verbose", action="store_true", | |
| 164 help="Enables verbose output") | |
| 165 parser.add_argument("of", nargs=1, | |
| 166 help="Hash of the commit to be searched.") | |
| 167 parser.add_argument("until", nargs=1, | |
| 168 help="Commit when searching should stop") | |
| 169 parser.add_argument("--all", action="store_true", | |
| 170 help=("Searches for related commits in all " | |
| 171 "commits between <of> and <until>")) | |
| 172 parser.add_argument("--deadline", required=False, | |
| 173 help=("The script will only list related commits " | |
| 174 "which are separated by hash <--deadline>.")) | |
| 175 parser.add_argument("--prettyprint", action="store_true", | |
| 176 help=("Pretty prints the output")) | |
| 177 | |
| 178 args = sys.argv[1:] | |
| 179 options = parser.parse_args(args) | |
| 180 if options.all: | |
| 181 all_related_commits = search_all_related_commits( | |
| 182 options.git_dir, | |
| 183 options.of[0], | |
| 184 options.until[0], | |
| 185 options.deadline, | |
| 186 options.verbose) | |
| 187 | |
| 188 high_level_commits = sorted(all_related_commits.keys(), key = lambda x: ( | |
| 189 (_git_execute(options.git_dir, | |
| 190 ["show", "--quiet", "--date=iso", x, "--format=%ad"], | |
| 191 options.verbose)).strip())) | |
| 192 | |
| 193 for current_key in high_level_commits: | |
| 194 if options.prettyprint: | |
| 195 _pretty_print_entry(current_key, "+", options.verbose) | |
| 196 else: | |
| 197 print "+" + current_key | |
| 198 | |
| 199 found_commits = all_related_commits[current_key] | |
| 200 for current_commit in found_commits: | |
| 201 if options.prettyprint: | |
| 202 _pretty_print_entry(current_commit, "| ", options.verbose) | |
| 203 else: | |
| 204 print "| " + current_commit | |
| 205 else: | |
| 206 hits = search_related_commits(options.git_dir, options.of[0], | |
| 207 options.until[0], options.deadline, options.verbose) | |
| 208 if len(hits) > 0: | |
| 209 print "\n".join(hits) | |
| OLD | NEW |