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.
7 Invokes the specified (quoted) command for all files modified
8 between the current git branch and the specified branch or commit.
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.
13 Deleted files are not included. Neither are untracked files.
16 %prog [-b BRANCH] [-d] [-x EXTENSIONS|-c|-g] [-t TOKEN] QUOTED_COMMAND
19 %prog -x gyp,gypi "tools/format_xml.py [[FILENAME]]"
20 %prog -c "tools/sort-headers.py [[FILENAME]]"
21 %prog -g "tools/sort_sources.py [[FILENAME]]"
22 %prog -t "~~BINGO~~" "echo I modified ~~BINGO~~"
31 # List of C++-like source file extensions.
32 _CPP_EXTENSIONS
= ('h', 'hh', 'hpp', 'c', 'cc', 'cpp', 'cxx', 'mm',)
33 # List of build file extensions.
34 _BUILD_EXTENSIONS
= ('gyp', 'gypi', 'gn',)
37 def GitShell(args
, ignore_return
=False):
38 """A shell invocation suitable for communicating with git. Returns
39 output as list of lines, raises exception on error.
41 job
= subprocess
.Popen(args
,
43 stdout
=subprocess
.PIPE
,
44 stderr
=subprocess
.STDOUT
)
45 (out
, err
) = job
.communicate()
46 if job
.returncode
!= 0 and not ignore_return
:
48 raise Exception("Error %d running command %s" % (
49 job
.returncode
, args
))
50 return out
.split('\n')
53 def FilenamesFromGit(branch_name
, extensions
):
54 """Provides a list of all new and modified files listed by [git diff
55 branch_name] where branch_name can be blank to get a diff of the
58 Excludes deleted files.
60 If extensions is not an empty list, include only files with one of
61 the extensions on the list.
63 lines
= GitShell('git diff --stat=600,500 %s' % branch_name
)
67 # Avoid summary line, and files that have been deleted (no plus).
68 if line
.find('|') != -1 and line
.find('+') != -1:
69 filename
= line
.split()[0]
71 filename
= filename
.rstrip()
72 ext
= filename
.rsplit('.')[-1]
73 if not extensions
or ext
in extensions
:
74 filenames
.append(filename
)
78 def ForAllTouchedFiles(branch_name
, extensions
, token
, command
):
79 """For each new or modified file output by [git diff branch_name],
80 run command with token replaced with the filename. If extensions is
81 not empty, do this only for files with one of the extensions in that
84 filenames
= FilenamesFromGit(branch_name
, extensions
)
85 for filename
in filenames
:
86 os
.system(command
.replace(token
, filename
))
90 parser
= optparse
.OptionParser(usage
=__doc__
)
91 parser
.add_option('-x', '--extensions', default
='', dest
='extensions',
92 help='Limits to files with given extensions '
94 parser
.add_option('-c', '--cpp', default
=False, action
='store_true',
96 help='Runs your command only on C++-like source files.')
97 # -g stands for GYP and GN.
98 parser
.add_option('-g', '--build', default
=False, action
='store_true',
100 help='Runs your command only on build files.')
101 parser
.add_option('-t', '--token', default
='[[FILENAME]]', dest
='token',
102 help='Sets the token to be replaced for each file '
103 'in your command (default [[FILENAME]]).')
104 parser
.add_option('-b', '--branch', default
='origin/master', dest
='branch',
105 help='Sets what to diff to (default origin/master). Set '
106 'to empty to diff workspace against HEAD.')
107 opts
, args
= parser
.parse_args()
113 if opts
.cpp_only
and opts
.build_only
:
114 parser
.error("--cpp and --build are mutually exclusive")
116 extensions
= opts
.extensions
118 extensions
= _CPP_EXTENSIONS
120 extensions
= _BUILD_EXTENSIONS
122 ForAllTouchedFiles(opts
.branch
, extensions
, opts
.token
, args
[0])
125 if __name__
== '__main__':