OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 # Copyright (c) 2011 The Chromium 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 """ | |
7 Invokes the specified (quoted) command for all files modified | |
8 between the current git branch and the specified branch or commit. | |
9 | |
10 The special token [[FILENAME]] (or whatever you choose using the -t | |
11 flag) is replaced with each of the filenames of new or modified files. | |
12 | |
13 Deleted files are not included. Neither are untracked files. | |
14 | |
15 Synopsis: | |
16 %prog [-b BRANCH] [-d] [-x EXTENSIONS|-c] [-t TOKEN] QUOTED_COMMAND | |
17 | |
18 Examples: | |
19 %prog -x gyp,gypi "tools/format_xml.py [[FILENAME]]" | |
20 %prog -c "tools/sort-headers.py [[FILENAME]]" | |
21 %prog -t "~~BINGO~~" "echo I modified ~~BINGO~~" | |
22 """ | |
23 | |
24 import optparse | |
25 import os | |
26 import subprocess | |
27 import sys | |
28 | |
29 | |
30 # List of C++-like source file extensions. | |
31 _CPP_EXTENSIONS = ('h', 'hh', 'hpp', 'c', 'cc', 'cpp', 'cxx', 'mm',) | |
32 | |
33 | |
34 def GitShell(args, ignore_return=False): | |
35 """A shell invocation suitable for communicating with git. Returns | |
36 output as list of lines, raises exception on error. | |
37 """ | |
38 job = subprocess.Popen(args, | |
39 shell=True, | |
40 stdout=subprocess.PIPE, | |
41 stderr=subprocess.STDOUT) | |
42 (out, err) = job.communicate() | |
43 if job.returncode != 0 and not ignore_return: | |
44 print out | |
45 raise Exception("Error %d running command %s" % ( | |
46 job.returncode, args)) | |
47 return out.split('\n') | |
48 | |
49 | |
50 def FilenamesFromGit(branch_name, extensions): | |
(unused - use chromium)
2011/10/31 18:19:56
needs docstring
Jói
2011/10/31 18:32:08
Done.
| |
51 lines = GitShell('git diff --stat=600,500 %s' % branch_name) | |
52 filenames = [] | |
53 for line in lines: | |
54 line = line.lstrip() | |
55 # Avoid summary line, and files that have been deleted (no plus). | |
56 if line.find('|') != -1 and line.find('+') != -1: | |
57 filename = line.split()[0] | |
58 if filename: | |
59 filename = filename.rstrip() | |
60 ext = filename.rsplit('.')[-1] | |
61 if not extensions or ext in extensions: | |
62 filenames.append(filename) | |
63 return filenames | |
64 | |
65 | |
66 def ForAllTouchedFiles(branch_name, extensions, token, command): | |
67 """For each new or modified file output by [git diff branch_name], | |
68 run command with token replaced with the filename. If extensions is | |
69 not empty, do this only for files with one of the extensions in that | |
70 list. | |
71 """ | |
72 filenames = FilenamesFromGit(branch_name, extensions) | |
73 for filename in filenames: | |
74 os.system(command.replace(token, filename)) | |
75 | |
76 | |
77 def main(): | |
78 parser = optparse.OptionParser(usage=__doc__) | |
(unused - use chromium)
2011/10/31 18:19:56
usage=__doc__ is a nice trick, didn't know about t
| |
79 parser.add_option( | |
(unused - use chromium)
2011/10/31 18:19:56
Format this like the other add_option() calls belo
Jói
2011/10/31 18:32:08
Done.
| |
80 '-x', '--extensions', | |
81 default='', | |
82 dest='extensions', | |
83 help='Limits to files with given extensions (comma-separated).') | |
84 parser.add_option('-c', '--cpp', default=False, action='store_true', | |
85 dest='cpp_only', | |
86 help='Runs your command only on C++-like source files.') | |
87 parser.add_option('-t', '--token', default='[[FILENAME]]', dest='token', | |
88 help=('Sets the token to be replaced for each file ' | |
89 'in your command (default [[FILENAME]]).')) | |
90 parser.add_option('-b', '--branch', default='origin/master', dest='branch', | |
91 help=('Sets what to diff to (default origin/master). Set ' | |
(unused - use chromium)
2011/10/31 18:19:56
nit: Since this is already in a function call, you
Jói
2011/10/31 18:32:08
Done.
| |
92 'to empty to diff workspace against HEAD.')) | |
93 opts, args = parser.parse_args() | |
94 | |
95 if not args: | |
96 parser.print_help() | |
97 sys.exit(1) | |
98 | |
99 extensions = opts.extensions | |
100 if opts.cpp_only: | |
101 extensions = _CPP_EXTENSIONS | |
102 | |
103 ForAllTouchedFiles(opts.branch, extensions, opts.token, args[0]) | |
104 | |
105 | |
106 if __name__ == '__main__': | |
107 main() | |
OLD | NEW |