1 # Copyright (c) 2011 The Chromium OS Authors.
3 # See file CREDITS for list of people who contributed to this
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of
9 # the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston,
33 def CountCommitsToBranch():
34 """Returns number of commits between HEAD and the tracking branch.
36 This looks back to the tracking branch and works out the number of commits
40 Number of patches that exist on top of the branch
42 pipe = [['git', 'log', '--no-color', '--oneline', '@{upstream}..'],
44 stdout = command.RunPipe(pipe, capture=True, oneline=True).stdout
45 patch_count = int(stdout)
48 def GetUpstream(git_dir, branch):
49 """Returns the name of the upstream for a branch
52 git_dir: Git directory containing repo
53 branch: Name of branch
56 Name of upstream branch (e.g. 'upstream/master') or None if none
58 remote = command.OutputOneLine('git', '--git-dir', git_dir, 'config',
59 'branch.%s.remote' % branch)
60 merge = command.OutputOneLine('git', '--git-dir', git_dir, 'config',
61 'branch.%s.merge' % branch)
64 elif remote and merge:
65 leaf = merge.split('/')[-1]
66 return '%s/%s' % (remote, leaf)
68 raise ValueError, ("Cannot determine upstream branch for branch "
69 "'%s' remote='%s', merge='%s'" % (branch, remote, merge))
72 def GetRangeInBranch(git_dir, branch, include_upstream=False):
73 """Returns an expression for the commits in the given branch.
76 git_dir: Directory containing git repo
77 branch: Name of branch
79 Expression in the form 'upstream..branch' which can be used to
82 upstream = GetUpstream(git_dir, branch)
83 return '%s%s..%s' % (upstream, '~' if include_upstream else '', branch)
85 def CountCommitsInBranch(git_dir, branch, include_upstream=False):
86 """Returns the number of commits in the given branch.
89 git_dir: Directory containing git repo
90 branch: Name of branch
92 Number of patches that exist on top of the branch
94 range_expr = GetRangeInBranch(git_dir, branch, include_upstream)
95 pipe = [['git', '--git-dir', git_dir, 'log', '--oneline', range_expr],
97 result = command.RunPipe(pipe, capture=True, oneline=True)
98 patch_count = int(result.stdout)
101 def CountCommits(commit_range):
102 """Returns the number of commits in the given range.
105 commit_range: Range of commits to count (e.g. 'HEAD..base')
107 Number of patches that exist on top of the branch
109 pipe = [['git', 'log', '--oneline', commit_range],
111 stdout = command.RunPipe(pipe, capture=True, oneline=True).stdout
112 patch_count = int(stdout)
115 def Checkout(commit_hash, git_dir=None, work_tree=None, force=False):
116 """Checkout the selected commit for this build
119 commit_hash: Commit hash to check out
123 pipe.extend(['--git-dir', git_dir])
125 pipe.extend(['--work-tree', work_tree])
126 pipe.append('checkout')
129 pipe.append(commit_hash)
130 result = command.RunPipe([pipe], capture=True, raise_on_error=False)
131 if result.return_code != 0:
132 raise OSError, 'git checkout (%s): %s' % (pipe, result.stderr)
134 def Clone(git_dir, output_dir):
135 """Checkout the selected commit for this build
138 commit_hash: Commit hash to check out
140 pipe = ['git', 'clone', git_dir, '.']
141 result = command.RunPipe([pipe], capture=True, cwd=output_dir)
142 if result.return_code != 0:
143 raise OSError, 'git clone: %s' % result.stderr
145 def Fetch(git_dir=None, work_tree=None):
146 """Fetch from the origin repo
149 commit_hash: Commit hash to check out
153 pipe.extend(['--git-dir', git_dir])
155 pipe.extend(['--work-tree', work_tree])
157 result = command.RunPipe([pipe], capture=True)
158 if result.return_code != 0:
159 raise OSError, 'git fetch: %s' % result.stderr
161 def CreatePatches(start, count, series):
162 """Create a series of patches from the top of the current branch.
164 The patch files are written to the current directory using
168 start: Commit to start from: 0=HEAD, 1=next one, etc.
169 count: number of commits to include
171 Filename of cover letter
172 List of filenames of patch files
174 if series.get('version'):
175 version = '%s ' % series['version']
176 cmd = ['git', 'format-patch', '-M', '--signoff']
177 if series.get('cover'):
178 cmd.append('--cover-letter')
179 prefix = series.GetPatchPrefix()
181 cmd += ['--subject-prefix=%s' % prefix]
182 cmd += ['HEAD~%d..HEAD~%d' % (start + count, start)]
184 stdout = command.RunList(cmd)
185 files = stdout.splitlines()
187 # We have an extra file if there is a cover letter
188 if series.get('cover'):
189 return files[0], files[1:]
193 def ApplyPatch(verbose, fname):
194 """Apply a patch with git am to test it
196 TODO: Convert these to use command, with stderr option
199 fname: filename of patch file to apply
201 cmd = ['git', 'am', fname]
202 pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
203 stderr=subprocess.PIPE)
204 stdout, stderr = pipe.communicate()
205 re_error = re.compile('^error: patch failed: (.+):(\d+)')
206 for line in stderr.splitlines():
209 match = re_error.match(line)
211 print GetWarningMsg('warning', match.group(1), int(match.group(2)),
213 return pipe.returncode == 0, stdout
215 def ApplyPatches(verbose, args, start_point):
216 """Apply the patches with git am to make sure all is well
219 verbose: Print out 'git am' output verbatim
220 args: List of patch files to apply
221 start_point: Number of commits back from HEAD to start applying.
222 Normally this is len(args), but it can be larger if a start
226 col = terminal.Color()
228 # Figure out our current position
229 cmd = ['git', 'name-rev', 'HEAD', '--name-only']
230 pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
231 stdout, stderr = pipe.communicate()
233 str = 'Could not find current commit name'
234 print col.Color(col.RED, str)
237 old_head = stdout.splitlines()[0]
239 # Checkout the required start point
240 cmd = ['git', 'checkout', 'HEAD~%d' % start_point]
241 pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
242 stderr=subprocess.PIPE)
243 stdout, stderr = pipe.communicate()
245 str = 'Could not move to commit before patch series'
246 print col.Color(col.RED, str)
250 # Apply all the patches
252 ok, stdout = ApplyPatch(verbose, fname)
254 print col.Color(col.RED, 'git am returned errors for %s: will '
255 'skip this patch' % fname)
259 cmd = ['git', 'am', '--skip']
260 pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
261 stdout, stderr = pipe.communicate()
262 if pipe.returncode != 0:
263 print col.Color(col.RED, 'Unable to skip patch! Aborting...')
267 # Return to our previous position
268 cmd = ['git', 'checkout', old_head]
269 pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
270 stdout, stderr = pipe.communicate()
272 print col.Color(col.RED, 'Could not move back to head commit')
274 return error_count == 0
276 def BuildEmailList(in_list, tag=None, alias=None, raise_on_error=True):
277 """Build a list of email addresses based on an input list.
279 Takes a list of email addresses and aliases, and turns this into a list
280 of only email address, by resolving any aliases that are present.
282 If the tag is given, then each email address is prepended with this
283 tag and a space. If the tag starts with a minus sign (indicating a
284 command line parameter) then the email address is quoted.
287 in_list: List of aliases/email addresses
288 tag: Text to put before each address
289 alias: Alias dictionary
290 raise_on_error: True to raise an error when an alias fails to match,
291 False to just print a message.
294 List of email addresses
297 >>> alias['fred'] = ['f.bloggs@napier.co.nz']
298 >>> alias['john'] = ['j.bloggs@napier.co.nz']
299 >>> alias['mary'] = ['Mary Poppins <m.poppins@cloud.net>']
300 >>> alias['boys'] = ['fred', ' john']
301 >>> alias['all'] = ['fred ', 'john', ' mary ']
302 >>> BuildEmailList(['john', 'mary'], None, alias)
303 ['j.bloggs@napier.co.nz', 'Mary Poppins <m.poppins@cloud.net>']
304 >>> BuildEmailList(['john', 'mary'], '--to', alias)
305 ['--to "j.bloggs@napier.co.nz"', \
306 '--to "Mary Poppins <m.poppins@cloud.net>"']
307 >>> BuildEmailList(['john', 'mary'], 'Cc', alias)
308 ['Cc j.bloggs@napier.co.nz', 'Cc Mary Poppins <m.poppins@cloud.net>']
310 quote = '"' if tag and tag[0] == '-' else ''
313 raw += LookupEmail(item, alias, raise_on_error=raise_on_error)
316 if not item in result:
319 return ['%s %s%s%s' % (tag, quote, email, quote) for email in result]
322 def EmailPatches(series, cover_fname, args, dry_run, raise_on_error, cc_fname,
323 self_only=False, alias=None, in_reply_to=None):
324 """Email a patch series.
327 series: Series object containing destination info
328 cover_fname: filename of cover letter
329 args: list of filenames of patch files
330 dry_run: Just return the command that would be run
331 raise_on_error: True to raise an error when an alias fails to match,
332 False to just print a message.
333 cc_fname: Filename of Cc file for per-commit Cc
334 self_only: True to just email to yourself as a test
335 in_reply_to: If set we'll pass this to git as --in-reply-to.
336 Should be a message ID that this is in reply to.
339 Git command that was/would be run
341 # For the duration of this doctest pretend that we ran patman with ./patman
342 >>> _old_argv0 = sys.argv[0]
343 >>> sys.argv[0] = './patman'
346 >>> alias['fred'] = ['f.bloggs@napier.co.nz']
347 >>> alias['john'] = ['j.bloggs@napier.co.nz']
348 >>> alias['mary'] = ['m.poppins@cloud.net']
349 >>> alias['boys'] = ['fred', ' john']
350 >>> alias['all'] = ['fred ', 'john', ' mary ']
351 >>> alias[os.getenv('USER')] = ['this-is-me@me.com']
352 >>> series = series.Series()
353 >>> series.to = ['fred']
354 >>> series.cc = ['mary']
355 >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \
357 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \
358 "m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" cover p1 p2'
359 >>> EmailPatches(series, None, ['p1'], True, True, 'cc-fname', False, \
361 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \
362 "m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" p1'
363 >>> series.cc = ['all']
364 >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \
366 'git send-email --annotate --to "this-is-me@me.com" --cc-cmd "./patman \
367 --cc-cmd cc-fname" cover p1 p2'
368 >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \
370 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \
371 "f.bloggs@napier.co.nz" --cc "j.bloggs@napier.co.nz" --cc \
372 "m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" cover p1 p2'
374 # Restore argv[0] since we clobbered it.
375 >>> sys.argv[0] = _old_argv0
377 to = BuildEmailList(series.get('to'), '--to', alias, raise_on_error)
379 print ("No recipient, please add something like this to a commit\n"
380 "Series-to: Fred Bloggs <f.blogs@napier.co.nz>")
382 cc = BuildEmailList(series.get('cc'), '--cc', alias, raise_on_error)
384 to = BuildEmailList([os.getenv('USER')], '--to', alias, raise_on_error)
386 cmd = ['git', 'send-email', '--annotate']
388 cmd.append('--in-reply-to="%s"' % in_reply_to)
392 cmd += ['--cc-cmd', '"%s --cc-cmd %s"' % (sys.argv[0], cc_fname)]
394 cmd.append(cover_fname)
402 def LookupEmail(lookup_name, alias=None, raise_on_error=True, level=0):
403 """If an email address is an alias, look it up and return the full name
405 TODO: Why not just use git's own alias feature?
408 lookup_name: Alias or email address to look up
409 alias: Dictionary containing aliases (None to use settings default)
410 raise_on_error: True to raise an error when an alias fails to match,
411 False to just print a message.
415 list containing a list of email addresses
418 OSError if a recursive alias reference was found
419 ValueError if an alias was not found
422 >>> alias['fred'] = ['f.bloggs@napier.co.nz']
423 >>> alias['john'] = ['j.bloggs@napier.co.nz']
424 >>> alias['mary'] = ['m.poppins@cloud.net']
425 >>> alias['boys'] = ['fred', ' john', 'f.bloggs@napier.co.nz']
426 >>> alias['all'] = ['fred ', 'john', ' mary ']
427 >>> alias['loop'] = ['other', 'john', ' mary ']
428 >>> alias['other'] = ['loop', 'john', ' mary ']
429 >>> LookupEmail('mary', alias)
430 ['m.poppins@cloud.net']
431 >>> LookupEmail('arthur.wellesley@howe.ro.uk', alias)
432 ['arthur.wellesley@howe.ro.uk']
433 >>> LookupEmail('boys', alias)
434 ['f.bloggs@napier.co.nz', 'j.bloggs@napier.co.nz']
435 >>> LookupEmail('all', alias)
436 ['f.bloggs@napier.co.nz', 'j.bloggs@napier.co.nz', 'm.poppins@cloud.net']
437 >>> LookupEmail('odd', alias)
438 Traceback (most recent call last):
440 ValueError: Alias 'odd' not found
441 >>> LookupEmail('loop', alias)
442 Traceback (most recent call last):
444 OSError: Recursive email alias at 'other'
445 >>> LookupEmail('odd', alias, raise_on_error=False)
446 \033[1;31mAlias 'odd' not found\033[0m
448 >>> # In this case the loop part will effectively be ignored.
449 >>> LookupEmail('loop', alias, raise_on_error=False)
450 \033[1;31mRecursive email alias at 'other'\033[0m
451 \033[1;31mRecursive email alias at 'john'\033[0m
452 \033[1;31mRecursive email alias at 'mary'\033[0m
453 ['j.bloggs@napier.co.nz', 'm.poppins@cloud.net']
456 alias = settings.alias
457 lookup_name = lookup_name.strip()
458 if '@' in lookup_name: # Perhaps a real email address
461 lookup_name = lookup_name.lower()
462 col = terminal.Color()
466 msg = "Recursive email alias at '%s'" % lookup_name
470 print col.Color(col.RED, msg)
474 if not lookup_name in alias:
475 msg = "Alias '%s' not found" % lookup_name
477 raise ValueError, msg
479 print col.Color(col.RED, msg)
481 for item in alias[lookup_name]:
482 todo = LookupEmail(item, alias, raise_on_error, level + 1)
483 for new_item in todo:
484 if not new_item in out_list:
485 out_list.append(new_item)
487 #print "No match for alias '%s'" % lookup_name
491 """Return name of top-level directory for this git repo.
494 Full path to git top-level directory
496 This test makes sure that we are running tests in the right subdir
498 >>> os.path.realpath(os.path.dirname(__file__)) == \
499 os.path.join(GetTopLevel(), 'tools', 'patman')
502 return command.OutputOneLine('git', 'rev-parse', '--show-toplevel')
505 """Gets the name of the git alias file.
508 Filename of git alias file, or None if none
510 fname = command.OutputOneLine('git', 'config', 'sendemail.aliasesfile',
511 raise_on_error=False)
513 fname = os.path.join(GetTopLevel(), fname.strip())
516 def GetDefaultUserName():
517 """Gets the user.name from .gitconfig file.
520 User name found in .gitconfig file, or None if none
522 uname = command.OutputOneLine('git', 'config', '--global', 'user.name')
525 def GetDefaultUserEmail():
526 """Gets the user.email from the global .gitconfig file.
529 User's email found in .gitconfig file, or None if none
531 uemail = command.OutputOneLine('git', 'config', '--global', 'user.email')
535 """Set up git utils, by reading the alias files."""
536 # Check for a git alias file also
537 alias_fname = GetAliasFile()
539 settings.ReadGitAliases(alias_fname)
542 """Get the hash of the current HEAD
547 return command.OutputOneLine('git', 'show', '-s', '--pretty=format:%H')
549 if __name__ == "__main__":