1 # Copyright (c) 2011 The Chromium OS Authors.
3 # SPDX-License-Identifier: GPL-2.0+
17 # True to use --no-decorate - we check this in Setup()
18 use_no_decorate = True
20 def LogCmd(commit_range, git_dir=None, oneline=False, reverse=False,
22 """Create a command to perform a 'git log'
25 commit_range: Range expression to use for log, None for none
26 git_dir: Path to git repositiory (None to use default)
27 oneline: True to use --oneline, else False
28 reverse: True to reverse the log (--reverse)
29 count: Number of commits to list, or None for no limit
31 List containing command and arguments to run
35 cmd += ['--git-dir', git_dir]
36 cmd += ['--no-pager', 'log', '--no-color']
38 cmd.append('--oneline')
40 cmd.append('--no-decorate')
42 cmd.append('--reverse')
44 cmd.append('-n%d' % count)
46 cmd.append(commit_range)
48 # Add this in case we have a branch with the same name as a directory.
49 # This avoids messages like this, for example:
50 # fatal: ambiguous argument 'test': both revision and filename
54 def CountCommitsToBranch():
55 """Returns number of commits between HEAD and the tracking branch.
57 This looks back to the tracking branch and works out the number of commits
61 Number of patches that exist on top of the branch
63 pipe = [LogCmd('@{upstream}..', oneline=True),
65 stdout = command.RunPipe(pipe, capture=True, oneline=True).stdout
66 patch_count = int(stdout)
69 def NameRevision(commit_hash):
70 """Gets the revision name for a commit
73 commit_hash: Commit hash to look up
76 Name of revision, if any, else None
78 pipe = ['git', 'name-rev', commit_hash]
79 stdout = command.RunPipe([pipe], capture=True, oneline=True).stdout
81 # We expect a commit, a space, then a revision name
82 name = stdout.split(' ')[1].strip()
85 def GuessUpstream(git_dir, branch):
86 """Tries to guess the upstream for a branch
88 This lists out top commits on a branch and tries to find a suitable
89 upstream. It does this by looking for the first commit where
90 'git name-rev' returns a plain branch name, with no ! or ^ modifiers.
93 git_dir: Git directory containing repo
94 branch: Name of branch
98 Name of upstream branch (e.g. 'upstream/master') or None if none
99 Warning/error message, or None if none
101 pipe = [LogCmd(branch, git_dir=git_dir, oneline=True, count=100)]
102 result = command.RunPipe(pipe, capture=True, capture_stderr=True,
103 raise_on_error=False)
104 if result.return_code:
105 return None, "Branch '%s' not found" % branch
106 for line in result.stdout.splitlines()[1:]:
107 commit_hash = line.split(' ')[0]
108 name = NameRevision(commit_hash)
109 if '~' not in name and '^' not in name:
110 if name.startswith('remotes/'):
112 return name, "Guessing upstream as '%s'" % name
113 return None, "Cannot find a suitable upstream for branch '%s'" % branch
115 def GetUpstream(git_dir, branch):
116 """Returns the name of the upstream for a branch
119 git_dir: Git directory containing repo
120 branch: Name of branch
124 Name of upstream branch (e.g. 'upstream/master') or None if none
125 Warning/error message, or None if none
128 remote = command.OutputOneLine('git', '--git-dir', git_dir, 'config',
129 'branch.%s.remote' % branch)
130 merge = command.OutputOneLine('git', '--git-dir', git_dir, 'config',
131 'branch.%s.merge' % branch)
133 upstream, msg = GuessUpstream(git_dir, branch)
138 elif remote and merge:
139 leaf = merge.split('/')[-1]
140 return '%s/%s' % (remote, leaf), None
142 raise ValueError, ("Cannot determine upstream branch for branch "
143 "'%s' remote='%s', merge='%s'" % (branch, remote, merge))
146 def GetRangeInBranch(git_dir, branch, include_upstream=False):
147 """Returns an expression for the commits in the given branch.
150 git_dir: Directory containing git repo
151 branch: Name of branch
153 Expression in the form 'upstream..branch' which can be used to
154 access the commits. If the branch does not exist, returns None.
156 upstream, msg = GetUpstream(git_dir, branch)
159 rstr = '%s%s..%s' % (upstream, '~' if include_upstream else '', branch)
162 def CountCommitsInRange(git_dir, range_expr):
163 """Returns the number of commits in the given range.
166 git_dir: Directory containing git repo
167 range_expr: Range to check
169 Number of patches that exist in the supplied rangem or None if none
172 pipe = [LogCmd(range_expr, git_dir=git_dir, oneline=True)]
173 result = command.RunPipe(pipe, capture=True, capture_stderr=True,
174 raise_on_error=False)
175 if result.return_code:
176 return None, "Range '%s' not found or is invalid" % range_expr
177 patch_count = len(result.stdout.splitlines())
178 return patch_count, None
180 def CountCommitsInBranch(git_dir, branch, include_upstream=False):
181 """Returns the number of commits in the given branch.
184 git_dir: Directory containing git repo
185 branch: Name of branch
187 Number of patches that exist on top of the branch, or None if the
188 branch does not exist.
190 range_expr, msg = GetRangeInBranch(git_dir, branch, include_upstream)
193 return CountCommitsInRange(git_dir, range_expr)
195 def CountCommits(commit_range):
196 """Returns the number of commits in the given range.
199 commit_range: Range of commits to count (e.g. 'HEAD..base')
201 Number of patches that exist on top of the branch
203 pipe = [LogCmd(commit_range, oneline=True),
205 stdout = command.RunPipe(pipe, capture=True, oneline=True).stdout
206 patch_count = int(stdout)
209 def Checkout(commit_hash, git_dir=None, work_tree=None, force=False):
210 """Checkout the selected commit for this build
213 commit_hash: Commit hash to check out
217 pipe.extend(['--git-dir', git_dir])
219 pipe.extend(['--work-tree', work_tree])
220 pipe.append('checkout')
223 pipe.append(commit_hash)
224 result = command.RunPipe([pipe], capture=True, raise_on_error=False,
226 if result.return_code != 0:
227 raise OSError, 'git checkout (%s): %s' % (pipe, result.stderr)
229 def Clone(git_dir, output_dir):
230 """Checkout the selected commit for this build
233 commit_hash: Commit hash to check out
235 pipe = ['git', 'clone', git_dir, '.']
236 result = command.RunPipe([pipe], capture=True, cwd=output_dir,
238 if result.return_code != 0:
239 raise OSError, 'git clone: %s' % result.stderr
241 def Fetch(git_dir=None, work_tree=None):
242 """Fetch from the origin repo
245 commit_hash: Commit hash to check out
249 pipe.extend(['--git-dir', git_dir])
251 pipe.extend(['--work-tree', work_tree])
253 result = command.RunPipe([pipe], capture=True, capture_stderr=True)
254 if result.return_code != 0:
255 raise OSError, 'git fetch: %s' % result.stderr
257 def CreatePatches(start, count, series):
258 """Create a series of patches from the top of the current branch.
260 The patch files are written to the current directory using
264 start: Commit to start from: 0=HEAD, 1=next one, etc.
265 count: number of commits to include
267 Filename of cover letter
268 List of filenames of patch files
270 if series.get('version'):
271 version = '%s ' % series['version']
272 cmd = ['git', 'format-patch', '-M', '--signoff']
273 if series.get('cover'):
274 cmd.append('--cover-letter')
275 prefix = series.GetPatchPrefix()
277 cmd += ['--subject-prefix=%s' % prefix]
278 cmd += ['HEAD~%d..HEAD~%d' % (start + count, start)]
280 stdout = command.RunList(cmd)
281 files = stdout.splitlines()
283 # We have an extra file if there is a cover letter
284 if series.get('cover'):
285 return files[0], files[1:]
289 def BuildEmailList(in_list, tag=None, alias=None, raise_on_error=True):
290 """Build a list of email addresses based on an input list.
292 Takes a list of email addresses and aliases, and turns this into a list
293 of only email address, by resolving any aliases that are present.
295 If the tag is given, then each email address is prepended with this
296 tag and a space. If the tag starts with a minus sign (indicating a
297 command line parameter) then the email address is quoted.
300 in_list: List of aliases/email addresses
301 tag: Text to put before each address
302 alias: Alias dictionary
303 raise_on_error: True to raise an error when an alias fails to match,
304 False to just print a message.
307 List of email addresses
310 >>> alias['fred'] = ['f.bloggs@napier.co.nz']
311 >>> alias['john'] = ['j.bloggs@napier.co.nz']
312 >>> alias['mary'] = ['Mary Poppins <m.poppins@cloud.net>']
313 >>> alias['boys'] = ['fred', ' john']
314 >>> alias['all'] = ['fred ', 'john', ' mary ']
315 >>> BuildEmailList(['john', 'mary'], None, alias)
316 ['j.bloggs@napier.co.nz', 'Mary Poppins <m.poppins@cloud.net>']
317 >>> BuildEmailList(['john', 'mary'], '--to', alias)
318 ['--to "j.bloggs@napier.co.nz"', \
319 '--to "Mary Poppins <m.poppins@cloud.net>"']
320 >>> BuildEmailList(['john', 'mary'], 'Cc', alias)
321 ['Cc j.bloggs@napier.co.nz', 'Cc Mary Poppins <m.poppins@cloud.net>']
323 quote = '"' if tag and tag[0] == '-' else ''
326 raw += LookupEmail(item, alias, raise_on_error=raise_on_error)
329 if not item in result:
332 return ['%s %s%s%s' % (tag, quote, email, quote) for email in result]
335 def EmailPatches(series, cover_fname, args, dry_run, raise_on_error, cc_fname,
336 self_only=False, alias=None, in_reply_to=None, thread=False):
337 """Email a patch series.
340 series: Series object containing destination info
341 cover_fname: filename of cover letter
342 args: list of filenames of patch files
343 dry_run: Just return the command that would be run
344 raise_on_error: True to raise an error when an alias fails to match,
345 False to just print a message.
346 cc_fname: Filename of Cc file for per-commit Cc
347 self_only: True to just email to yourself as a test
348 in_reply_to: If set we'll pass this to git as --in-reply-to.
349 Should be a message ID that this is in reply to.
350 thread: True to add --thread to git send-email (make
351 all patches reply to cover-letter or first patch in series)
354 Git command that was/would be run
356 # For the duration of this doctest pretend that we ran patman with ./patman
357 >>> _old_argv0 = sys.argv[0]
358 >>> sys.argv[0] = './patman'
361 >>> alias['fred'] = ['f.bloggs@napier.co.nz']
362 >>> alias['john'] = ['j.bloggs@napier.co.nz']
363 >>> alias['mary'] = ['m.poppins@cloud.net']
364 >>> alias['boys'] = ['fred', ' john']
365 >>> alias['all'] = ['fred ', 'john', ' mary ']
366 >>> alias[os.getenv('USER')] = ['this-is-me@me.com']
367 >>> series = series.Series()
368 >>> series.to = ['fred']
369 >>> series.cc = ['mary']
370 >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \
372 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \
373 "m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" cover p1 p2'
374 >>> EmailPatches(series, None, ['p1'], True, True, 'cc-fname', False, \
376 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \
377 "m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" p1'
378 >>> series.cc = ['all']
379 >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \
381 'git send-email --annotate --to "this-is-me@me.com" --cc-cmd "./patman \
382 --cc-cmd cc-fname" cover p1 p2'
383 >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \
385 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \
386 "f.bloggs@napier.co.nz" --cc "j.bloggs@napier.co.nz" --cc \
387 "m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" cover p1 p2'
389 # Restore argv[0] since we clobbered it.
390 >>> sys.argv[0] = _old_argv0
392 to = BuildEmailList(series.get('to'), '--to', alias, raise_on_error)
394 git_config_to = command.Output('git', 'config', 'sendemail.to',
395 raise_on_error=False)
396 if not git_config_to:
397 print ("No recipient.\n"
398 "Please add something like this to a commit\n"
399 "Series-to: Fred Bloggs <f.blogs@napier.co.nz>\n"
400 "Or do something like this\n"
401 "git config sendemail.to u-boot@lists.denx.de")
403 cc = BuildEmailList(list(set(series.get('cc')) - set(series.get('to'))),
404 '--cc', alias, raise_on_error)
406 to = BuildEmailList([os.getenv('USER')], '--to', alias, raise_on_error)
408 cmd = ['git', 'send-email', '--annotate']
410 cmd.append('--in-reply-to="%s"' % in_reply_to)
412 cmd.append('--thread')
416 cmd += ['--cc-cmd', '"%s --cc-cmd %s"' % (sys.argv[0], cc_fname)]
418 cmd.append(cover_fname)
426 def LookupEmail(lookup_name, alias=None, raise_on_error=True, level=0):
427 """If an email address is an alias, look it up and return the full name
429 TODO: Why not just use git's own alias feature?
432 lookup_name: Alias or email address to look up
433 alias: Dictionary containing aliases (None to use settings default)
434 raise_on_error: True to raise an error when an alias fails to match,
435 False to just print a message.
439 list containing a list of email addresses
442 OSError if a recursive alias reference was found
443 ValueError if an alias was not found
446 >>> alias['fred'] = ['f.bloggs@napier.co.nz']
447 >>> alias['john'] = ['j.bloggs@napier.co.nz']
448 >>> alias['mary'] = ['m.poppins@cloud.net']
449 >>> alias['boys'] = ['fred', ' john', 'f.bloggs@napier.co.nz']
450 >>> alias['all'] = ['fred ', 'john', ' mary ']
451 >>> alias['loop'] = ['other', 'john', ' mary ']
452 >>> alias['other'] = ['loop', 'john', ' mary ']
453 >>> LookupEmail('mary', alias)
454 ['m.poppins@cloud.net']
455 >>> LookupEmail('arthur.wellesley@howe.ro.uk', alias)
456 ['arthur.wellesley@howe.ro.uk']
457 >>> LookupEmail('boys', alias)
458 ['f.bloggs@napier.co.nz', 'j.bloggs@napier.co.nz']
459 >>> LookupEmail('all', alias)
460 ['f.bloggs@napier.co.nz', 'j.bloggs@napier.co.nz', 'm.poppins@cloud.net']
461 >>> LookupEmail('odd', alias)
462 Traceback (most recent call last):
464 ValueError: Alias 'odd' not found
465 >>> LookupEmail('loop', alias)
466 Traceback (most recent call last):
468 OSError: Recursive email alias at 'other'
469 >>> LookupEmail('odd', alias, raise_on_error=False)
470 Alias 'odd' not found
472 >>> # In this case the loop part will effectively be ignored.
473 >>> LookupEmail('loop', alias, raise_on_error=False)
474 Recursive email alias at 'other'
475 Recursive email alias at 'john'
476 Recursive email alias at 'mary'
477 ['j.bloggs@napier.co.nz', 'm.poppins@cloud.net']
480 alias = settings.alias
481 lookup_name = lookup_name.strip()
482 if '@' in lookup_name: # Perhaps a real email address
485 lookup_name = lookup_name.lower()
486 col = terminal.Color()
490 msg = "Recursive email alias at '%s'" % lookup_name
494 print(col.Color(col.RED, msg))
498 if not lookup_name in alias:
499 msg = "Alias '%s' not found" % lookup_name
501 raise ValueError, msg
503 print(col.Color(col.RED, msg))
505 for item in alias[lookup_name]:
506 todo = LookupEmail(item, alias, raise_on_error, level + 1)
507 for new_item in todo:
508 if not new_item in out_list:
509 out_list.append(new_item)
511 #print("No match for alias '%s'" % lookup_name)
515 """Return name of top-level directory for this git repo.
518 Full path to git top-level directory
520 This test makes sure that we are running tests in the right subdir
522 >>> os.path.realpath(os.path.dirname(__file__)) == \
523 os.path.join(GetTopLevel(), 'tools', 'patman')
526 return command.OutputOneLine('git', 'rev-parse', '--show-toplevel')
529 """Gets the name of the git alias file.
532 Filename of git alias file, or None if none
534 fname = command.OutputOneLine('git', 'config', 'sendemail.aliasesfile',
535 raise_on_error=False)
537 fname = os.path.join(GetTopLevel(), fname.strip())
540 def GetDefaultUserName():
541 """Gets the user.name from .gitconfig file.
544 User name found in .gitconfig file, or None if none
546 uname = command.OutputOneLine('git', 'config', '--global', 'user.name')
549 def GetDefaultUserEmail():
550 """Gets the user.email from the global .gitconfig file.
553 User's email found in .gitconfig file, or None if none
555 uemail = command.OutputOneLine('git', 'config', '--global', 'user.email')
558 def GetDefaultSubjectPrefix():
559 """Gets the format.subjectprefix from local .git/config file.
562 Subject prefix found in local .git/config file, or None if none
564 sub_prefix = command.OutputOneLine('git', 'config', 'format.subjectprefix',
565 raise_on_error=False)
570 """Set up git utils, by reading the alias files."""
571 # Check for a git alias file also
572 global use_no_decorate
574 alias_fname = GetAliasFile()
576 settings.ReadGitAliases(alias_fname)
577 cmd = LogCmd(None, count=0)
578 use_no_decorate = (command.RunPipe([cmd], raise_on_error=False)
582 """Get the hash of the current HEAD
587 return command.OutputOneLine('git', 'show', '-s', '--pretty=format:%H')
589 if __name__ == "__main__":