blob: 80edc7c2c6ca0b1072abb247605b8d0ee023b7f1 [file] [log] [blame]
Simon Glass0d24de92012-01-14 15:12:45 +00001# Copyright (c) 2011 The Chromium OS Authors.
2#
Wolfgang Denk1a459662013-07-08 09:37:19 +02003# SPDX-License-Identifier: GPL-2.0+
Simon Glass0d24de92012-01-14 15:12:45 +00004#
5
6import command
7import re
8import os
9import series
Simon Glass0d24de92012-01-14 15:12:45 +000010import subprocess
11import sys
12import terminal
13
Simon Glass757f64a2014-04-20 10:50:13 -060014import checkpatch
Simon Glass5f6a1c42012-12-15 10:42:07 +000015import settings
16
Simon Glasse49f14a2014-08-09 15:33:11 -060017# True to use --no-decorate - we check this in Setup()
18use_no_decorate = True
19
Simon Glasscda2a612014-08-09 15:33:10 -060020def LogCmd(commit_range, git_dir=None, oneline=False, reverse=False,
21 count=None):
22 """Create a command to perform a 'git log'
23
24 Args:
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
30 Return:
31 List containing command and arguments to run
32 """
33 cmd = ['git']
34 if git_dir:
35 cmd += ['--git-dir', git_dir]
Simon Glass9447a6b2014-08-28 09:43:37 -060036 cmd += ['--no-pager', 'log', '--no-color']
Simon Glasscda2a612014-08-09 15:33:10 -060037 if oneline:
38 cmd.append('--oneline')
Simon Glasse49f14a2014-08-09 15:33:11 -060039 if use_no_decorate:
40 cmd.append('--no-decorate')
Simon Glass042a7322014-08-14 21:59:11 -060041 if reverse:
42 cmd.append('--reverse')
Simon Glasscda2a612014-08-09 15:33:10 -060043 if count is not None:
44 cmd.append('-n%d' % count)
45 if commit_range:
46 cmd.append(commit_range)
47 return cmd
Simon Glass0d24de92012-01-14 15:12:45 +000048
49def CountCommitsToBranch():
50 """Returns number of commits between HEAD and the tracking branch.
51
52 This looks back to the tracking branch and works out the number of commits
53 since then.
54
55 Return:
56 Number of patches that exist on top of the branch
57 """
Simon Glasscda2a612014-08-09 15:33:10 -060058 pipe = [LogCmd('@{upstream}..', oneline=True),
Simon Glass0d24de92012-01-14 15:12:45 +000059 ['wc', '-l']]
Simon Glassa10fd932012-12-15 10:42:04 +000060 stdout = command.RunPipe(pipe, capture=True, oneline=True).stdout
Simon Glass0d24de92012-01-14 15:12:45 +000061 patch_count = int(stdout)
62 return patch_count
63
Simon Glass5f6a1c42012-12-15 10:42:07 +000064def GetUpstream(git_dir, branch):
65 """Returns the name of the upstream for a branch
66
67 Args:
68 git_dir: Git directory containing repo
69 branch: Name of branch
70
71 Returns:
72 Name of upstream branch (e.g. 'upstream/master') or None if none
73 """
Simon Glasscce717a2013-05-08 08:06:08 +000074 try:
75 remote = command.OutputOneLine('git', '--git-dir', git_dir, 'config',
76 'branch.%s.remote' % branch)
77 merge = command.OutputOneLine('git', '--git-dir', git_dir, 'config',
78 'branch.%s.merge' % branch)
79 except:
80 return None
81
Simon Glass5f6a1c42012-12-15 10:42:07 +000082 if remote == '.':
83 return merge
84 elif remote and merge:
85 leaf = merge.split('/')[-1]
86 return '%s/%s' % (remote, leaf)
87 else:
88 raise ValueError, ("Cannot determine upstream branch for branch "
89 "'%s' remote='%s', merge='%s'" % (branch, remote, merge))
90
91
92def GetRangeInBranch(git_dir, branch, include_upstream=False):
93 """Returns an expression for the commits in the given branch.
94
95 Args:
96 git_dir: Directory containing git repo
97 branch: Name of branch
98 Return:
99 Expression in the form 'upstream..branch' which can be used to
Simon Glasscce717a2013-05-08 08:06:08 +0000100 access the commits. If the branch does not exist, returns None.
Simon Glass5f6a1c42012-12-15 10:42:07 +0000101 """
102 upstream = GetUpstream(git_dir, branch)
Simon Glasscce717a2013-05-08 08:06:08 +0000103 if not upstream:
104 return None
Simon Glass5f6a1c42012-12-15 10:42:07 +0000105 return '%s%s..%s' % (upstream, '~' if include_upstream else '', branch)
106
107def CountCommitsInBranch(git_dir, branch, include_upstream=False):
108 """Returns the number of commits in the given branch.
109
110 Args:
111 git_dir: Directory containing git repo
112 branch: Name of branch
113 Return:
Simon Glasscce717a2013-05-08 08:06:08 +0000114 Number of patches that exist on top of the branch, or None if the
115 branch does not exist.
Simon Glass5f6a1c42012-12-15 10:42:07 +0000116 """
117 range_expr = GetRangeInBranch(git_dir, branch, include_upstream)
Simon Glasscce717a2013-05-08 08:06:08 +0000118 if not range_expr:
119 return None
Simon Glasscda2a612014-08-09 15:33:10 -0600120 pipe = [LogCmd(range_expr, git_dir=git_dir, oneline=True),
Simon Glass5f6a1c42012-12-15 10:42:07 +0000121 ['wc', '-l']]
122 result = command.RunPipe(pipe, capture=True, oneline=True)
123 patch_count = int(result.stdout)
124 return patch_count
125
126def CountCommits(commit_range):
127 """Returns the number of commits in the given range.
128
129 Args:
130 commit_range: Range of commits to count (e.g. 'HEAD..base')
131 Return:
132 Number of patches that exist on top of the branch
133 """
Simon Glasscda2a612014-08-09 15:33:10 -0600134 pipe = [LogCmd(commit_range, oneline=True),
Simon Glass5f6a1c42012-12-15 10:42:07 +0000135 ['wc', '-l']]
136 stdout = command.RunPipe(pipe, capture=True, oneline=True).stdout
137 patch_count = int(stdout)
138 return patch_count
139
140def Checkout(commit_hash, git_dir=None, work_tree=None, force=False):
141 """Checkout the selected commit for this build
142
143 Args:
144 commit_hash: Commit hash to check out
145 """
146 pipe = ['git']
147 if git_dir:
148 pipe.extend(['--git-dir', git_dir])
149 if work_tree:
150 pipe.extend(['--work-tree', work_tree])
151 pipe.append('checkout')
152 if force:
153 pipe.append('-f')
154 pipe.append(commit_hash)
155 result = command.RunPipe([pipe], capture=True, raise_on_error=False)
156 if result.return_code != 0:
157 raise OSError, 'git checkout (%s): %s' % (pipe, result.stderr)
158
159def Clone(git_dir, output_dir):
160 """Checkout the selected commit for this build
161
162 Args:
163 commit_hash: Commit hash to check out
164 """
165 pipe = ['git', 'clone', git_dir, '.']
166 result = command.RunPipe([pipe], capture=True, cwd=output_dir)
167 if result.return_code != 0:
168 raise OSError, 'git clone: %s' % result.stderr
169
170def Fetch(git_dir=None, work_tree=None):
171 """Fetch from the origin repo
172
173 Args:
174 commit_hash: Commit hash to check out
175 """
176 pipe = ['git']
177 if git_dir:
178 pipe.extend(['--git-dir', git_dir])
179 if work_tree:
180 pipe.extend(['--work-tree', work_tree])
181 pipe.append('fetch')
182 result = command.RunPipe([pipe], capture=True)
183 if result.return_code != 0:
184 raise OSError, 'git fetch: %s' % result.stderr
185
Simon Glass0d24de92012-01-14 15:12:45 +0000186def CreatePatches(start, count, series):
187 """Create a series of patches from the top of the current branch.
188
189 The patch files are written to the current directory using
190 git format-patch.
191
192 Args:
193 start: Commit to start from: 0=HEAD, 1=next one, etc.
194 count: number of commits to include
195 Return:
196 Filename of cover letter
197 List of filenames of patch files
198 """
199 if series.get('version'):
200 version = '%s ' % series['version']
201 cmd = ['git', 'format-patch', '-M', '--signoff']
202 if series.get('cover'):
203 cmd.append('--cover-letter')
204 prefix = series.GetPatchPrefix()
205 if prefix:
206 cmd += ['--subject-prefix=%s' % prefix]
207 cmd += ['HEAD~%d..HEAD~%d' % (start + count, start)]
208
209 stdout = command.RunList(cmd)
210 files = stdout.splitlines()
211
212 # We have an extra file if there is a cover letter
213 if series.get('cover'):
214 return files[0], files[1:]
215 else:
216 return None, files
217
Simon Glassa1318f72013-03-26 13:09:42 +0000218def BuildEmailList(in_list, tag=None, alias=None, raise_on_error=True):
Simon Glass0d24de92012-01-14 15:12:45 +0000219 """Build a list of email addresses based on an input list.
220
221 Takes a list of email addresses and aliases, and turns this into a list
222 of only email address, by resolving any aliases that are present.
223
224 If the tag is given, then each email address is prepended with this
225 tag and a space. If the tag starts with a minus sign (indicating a
226 command line parameter) then the email address is quoted.
227
228 Args:
229 in_list: List of aliases/email addresses
230 tag: Text to put before each address
Simon Glassa1318f72013-03-26 13:09:42 +0000231 alias: Alias dictionary
232 raise_on_error: True to raise an error when an alias fails to match,
233 False to just print a message.
Simon Glass0d24de92012-01-14 15:12:45 +0000234
235 Returns:
236 List of email addresses
237
238 >>> alias = {}
239 >>> alias['fred'] = ['f.bloggs@napier.co.nz']
240 >>> alias['john'] = ['j.bloggs@napier.co.nz']
241 >>> alias['mary'] = ['Mary Poppins <m.poppins@cloud.net>']
242 >>> alias['boys'] = ['fred', ' john']
243 >>> alias['all'] = ['fred ', 'john', ' mary ']
244 >>> BuildEmailList(['john', 'mary'], None, alias)
245 ['j.bloggs@napier.co.nz', 'Mary Poppins <m.poppins@cloud.net>']
246 >>> BuildEmailList(['john', 'mary'], '--to', alias)
247 ['--to "j.bloggs@napier.co.nz"', \
248'--to "Mary Poppins <m.poppins@cloud.net>"']
249 >>> BuildEmailList(['john', 'mary'], 'Cc', alias)
250 ['Cc j.bloggs@napier.co.nz', 'Cc Mary Poppins <m.poppins@cloud.net>']
251 """
252 quote = '"' if tag and tag[0] == '-' else ''
253 raw = []
254 for item in in_list:
Simon Glassa1318f72013-03-26 13:09:42 +0000255 raw += LookupEmail(item, alias, raise_on_error=raise_on_error)
Simon Glass0d24de92012-01-14 15:12:45 +0000256 result = []
257 for item in raw:
258 if not item in result:
259 result.append(item)
260 if tag:
261 return ['%s %s%s%s' % (tag, quote, email, quote) for email in result]
262 return result
263
Simon Glassa1318f72013-03-26 13:09:42 +0000264def EmailPatches(series, cover_fname, args, dry_run, raise_on_error, cc_fname,
Doug Anderson6d819922013-03-17 10:31:04 +0000265 self_only=False, alias=None, in_reply_to=None):
Simon Glass0d24de92012-01-14 15:12:45 +0000266 """Email a patch series.
267
268 Args:
269 series: Series object containing destination info
270 cover_fname: filename of cover letter
271 args: list of filenames of patch files
272 dry_run: Just return the command that would be run
Simon Glassa1318f72013-03-26 13:09:42 +0000273 raise_on_error: True to raise an error when an alias fails to match,
274 False to just print a message.
Simon Glass0d24de92012-01-14 15:12:45 +0000275 cc_fname: Filename of Cc file for per-commit Cc
276 self_only: True to just email to yourself as a test
Doug Anderson6d819922013-03-17 10:31:04 +0000277 in_reply_to: If set we'll pass this to git as --in-reply-to.
278 Should be a message ID that this is in reply to.
Simon Glass0d24de92012-01-14 15:12:45 +0000279
280 Returns:
281 Git command that was/would be run
282
Doug Andersona9700482012-11-26 15:21:40 +0000283 # For the duration of this doctest pretend that we ran patman with ./patman
284 >>> _old_argv0 = sys.argv[0]
285 >>> sys.argv[0] = './patman'
286
Simon Glass0d24de92012-01-14 15:12:45 +0000287 >>> alias = {}
288 >>> alias['fred'] = ['f.bloggs@napier.co.nz']
289 >>> alias['john'] = ['j.bloggs@napier.co.nz']
290 >>> alias['mary'] = ['m.poppins@cloud.net']
291 >>> alias['boys'] = ['fred', ' john']
292 >>> alias['all'] = ['fred ', 'john', ' mary ']
293 >>> alias[os.getenv('USER')] = ['this-is-me@me.com']
294 >>> series = series.Series()
295 >>> series.to = ['fred']
296 >>> series.cc = ['mary']
Simon Glassa1318f72013-03-26 13:09:42 +0000297 >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \
298 False, alias)
Simon Glass0d24de92012-01-14 15:12:45 +0000299 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \
300"m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" cover p1 p2'
Simon Glassa1318f72013-03-26 13:09:42 +0000301 >>> EmailPatches(series, None, ['p1'], True, True, 'cc-fname', False, \
302 alias)
Simon Glass0d24de92012-01-14 15:12:45 +0000303 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \
304"m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" p1'
305 >>> series.cc = ['all']
Simon Glassa1318f72013-03-26 13:09:42 +0000306 >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \
307 True, alias)
Simon Glass0d24de92012-01-14 15:12:45 +0000308 'git send-email --annotate --to "this-is-me@me.com" --cc-cmd "./patman \
309--cc-cmd cc-fname" cover p1 p2'
Simon Glassa1318f72013-03-26 13:09:42 +0000310 >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \
311 False, alias)
Simon Glass0d24de92012-01-14 15:12:45 +0000312 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \
313"f.bloggs@napier.co.nz" --cc "j.bloggs@napier.co.nz" --cc \
314"m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" cover p1 p2'
Doug Andersona9700482012-11-26 15:21:40 +0000315
316 # Restore argv[0] since we clobbered it.
317 >>> sys.argv[0] = _old_argv0
Simon Glass0d24de92012-01-14 15:12:45 +0000318 """
Simon Glassa1318f72013-03-26 13:09:42 +0000319 to = BuildEmailList(series.get('to'), '--to', alias, raise_on_error)
Simon Glass0d24de92012-01-14 15:12:45 +0000320 if not to:
Masahiro Yamadaee860c62014-07-18 14:23:20 +0900321 git_config_to = command.Output('git', 'config', 'sendemail.to')
322 if not git_config_to:
323 print ("No recipient.\n"
324 "Please add something like this to a commit\n"
325 "Series-to: Fred Bloggs <f.blogs@napier.co.nz>\n"
326 "Or do something like this\n"
327 "git config sendemail.to u-boot@lists.denx.de")
328 return
Simon Glassa1318f72013-03-26 13:09:42 +0000329 cc = BuildEmailList(series.get('cc'), '--cc', alias, raise_on_error)
Simon Glass0d24de92012-01-14 15:12:45 +0000330 if self_only:
Simon Glassa1318f72013-03-26 13:09:42 +0000331 to = BuildEmailList([os.getenv('USER')], '--to', alias, raise_on_error)
Simon Glass0d24de92012-01-14 15:12:45 +0000332 cc = []
333 cmd = ['git', 'send-email', '--annotate']
Doug Anderson6d819922013-03-17 10:31:04 +0000334 if in_reply_to:
335 cmd.append('--in-reply-to="%s"' % in_reply_to)
336
Simon Glass0d24de92012-01-14 15:12:45 +0000337 cmd += to
338 cmd += cc
339 cmd += ['--cc-cmd', '"%s --cc-cmd %s"' % (sys.argv[0], cc_fname)]
340 if cover_fname:
341 cmd.append(cover_fname)
342 cmd += args
343 str = ' '.join(cmd)
344 if not dry_run:
345 os.system(str)
346 return str
347
348
Simon Glassa1318f72013-03-26 13:09:42 +0000349def LookupEmail(lookup_name, alias=None, raise_on_error=True, level=0):
Simon Glass0d24de92012-01-14 15:12:45 +0000350 """If an email address is an alias, look it up and return the full name
351
352 TODO: Why not just use git's own alias feature?
353
354 Args:
355 lookup_name: Alias or email address to look up
Simon Glassa1318f72013-03-26 13:09:42 +0000356 alias: Dictionary containing aliases (None to use settings default)
357 raise_on_error: True to raise an error when an alias fails to match,
358 False to just print a message.
Simon Glass0d24de92012-01-14 15:12:45 +0000359
360 Returns:
361 tuple:
362 list containing a list of email addresses
363
364 Raises:
365 OSError if a recursive alias reference was found
366 ValueError if an alias was not found
367
368 >>> alias = {}
369 >>> alias['fred'] = ['f.bloggs@napier.co.nz']
370 >>> alias['john'] = ['j.bloggs@napier.co.nz']
371 >>> alias['mary'] = ['m.poppins@cloud.net']
372 >>> alias['boys'] = ['fred', ' john', 'f.bloggs@napier.co.nz']
373 >>> alias['all'] = ['fred ', 'john', ' mary ']
374 >>> alias['loop'] = ['other', 'john', ' mary ']
375 >>> alias['other'] = ['loop', 'john', ' mary ']
376 >>> LookupEmail('mary', alias)
377 ['m.poppins@cloud.net']
378 >>> LookupEmail('arthur.wellesley@howe.ro.uk', alias)
379 ['arthur.wellesley@howe.ro.uk']
380 >>> LookupEmail('boys', alias)
381 ['f.bloggs@napier.co.nz', 'j.bloggs@napier.co.nz']
382 >>> LookupEmail('all', alias)
383 ['f.bloggs@napier.co.nz', 'j.bloggs@napier.co.nz', 'm.poppins@cloud.net']
384 >>> LookupEmail('odd', alias)
385 Traceback (most recent call last):
386 ...
387 ValueError: Alias 'odd' not found
388 >>> LookupEmail('loop', alias)
389 Traceback (most recent call last):
390 ...
391 OSError: Recursive email alias at 'other'
Simon Glassa1318f72013-03-26 13:09:42 +0000392 >>> LookupEmail('odd', alias, raise_on_error=False)
Simon Glasse752edc2014-08-28 09:43:35 -0600393 Alias 'odd' not found
Simon Glassa1318f72013-03-26 13:09:42 +0000394 []
395 >>> # In this case the loop part will effectively be ignored.
396 >>> LookupEmail('loop', alias, raise_on_error=False)
Simon Glasse752edc2014-08-28 09:43:35 -0600397 Recursive email alias at 'other'
398 Recursive email alias at 'john'
399 Recursive email alias at 'mary'
Simon Glassa1318f72013-03-26 13:09:42 +0000400 ['j.bloggs@napier.co.nz', 'm.poppins@cloud.net']
Simon Glass0d24de92012-01-14 15:12:45 +0000401 """
402 if not alias:
403 alias = settings.alias
404 lookup_name = lookup_name.strip()
405 if '@' in lookup_name: # Perhaps a real email address
406 return [lookup_name]
407
408 lookup_name = lookup_name.lower()
Simon Glassa1318f72013-03-26 13:09:42 +0000409 col = terminal.Color()
Simon Glass0d24de92012-01-14 15:12:45 +0000410
411 out_list = []
Simon Glassa1318f72013-03-26 13:09:42 +0000412 if level > 10:
413 msg = "Recursive email alias at '%s'" % lookup_name
414 if raise_on_error:
415 raise OSError, msg
416 else:
417 print col.Color(col.RED, msg)
418 return out_list
419
Simon Glass0d24de92012-01-14 15:12:45 +0000420 if lookup_name:
421 if not lookup_name in alias:
Simon Glassa1318f72013-03-26 13:09:42 +0000422 msg = "Alias '%s' not found" % lookup_name
423 if raise_on_error:
424 raise ValueError, msg
425 else:
426 print col.Color(col.RED, msg)
427 return out_list
Simon Glass0d24de92012-01-14 15:12:45 +0000428 for item in alias[lookup_name]:
Simon Glassa1318f72013-03-26 13:09:42 +0000429 todo = LookupEmail(item, alias, raise_on_error, level + 1)
Simon Glass0d24de92012-01-14 15:12:45 +0000430 for new_item in todo:
431 if not new_item in out_list:
432 out_list.append(new_item)
433
434 #print "No match for alias '%s'" % lookup_name
435 return out_list
436
437def GetTopLevel():
438 """Return name of top-level directory for this git repo.
439
440 Returns:
441 Full path to git top-level directory
442
443 This test makes sure that we are running tests in the right subdir
444
Doug Andersona9700482012-11-26 15:21:40 +0000445 >>> os.path.realpath(os.path.dirname(__file__)) == \
446 os.path.join(GetTopLevel(), 'tools', 'patman')
Simon Glass0d24de92012-01-14 15:12:45 +0000447 True
448 """
449 return command.OutputOneLine('git', 'rev-parse', '--show-toplevel')
450
451def GetAliasFile():
452 """Gets the name of the git alias file.
453
454 Returns:
455 Filename of git alias file, or None if none
456 """
Simon Glassdc191502012-12-15 10:42:05 +0000457 fname = command.OutputOneLine('git', 'config', 'sendemail.aliasesfile',
458 raise_on_error=False)
Simon Glass0d24de92012-01-14 15:12:45 +0000459 if fname:
460 fname = os.path.join(GetTopLevel(), fname.strip())
461 return fname
462
Vikram Narayanan87d65552012-05-23 09:01:06 +0000463def GetDefaultUserName():
464 """Gets the user.name from .gitconfig file.
465
466 Returns:
467 User name found in .gitconfig file, or None if none
468 """
469 uname = command.OutputOneLine('git', 'config', '--global', 'user.name')
470 return uname
471
472def GetDefaultUserEmail():
473 """Gets the user.email from the global .gitconfig file.
474
475 Returns:
476 User's email found in .gitconfig file, or None if none
477 """
478 uemail = command.OutputOneLine('git', 'config', '--global', 'user.email')
479 return uemail
480
Simon Glass0d24de92012-01-14 15:12:45 +0000481def Setup():
482 """Set up git utils, by reading the alias files."""
Simon Glass0d24de92012-01-14 15:12:45 +0000483 # Check for a git alias file also
Simon Glass0b703db2014-08-28 09:43:45 -0600484 global use_no_decorate
485
Simon Glass0d24de92012-01-14 15:12:45 +0000486 alias_fname = GetAliasFile()
487 if alias_fname:
488 settings.ReadGitAliases(alias_fname)
Simon Glasse49f14a2014-08-09 15:33:11 -0600489 cmd = LogCmd(None, count=0)
490 use_no_decorate = (command.RunPipe([cmd], raise_on_error=False)
491 .return_code == 0)
Simon Glass0d24de92012-01-14 15:12:45 +0000492
Simon Glass5f6a1c42012-12-15 10:42:07 +0000493def GetHead():
494 """Get the hash of the current HEAD
495
496 Returns:
497 Hash of HEAD
498 """
499 return command.OutputOneLine('git', 'show', '-s', '--pretty=format:%H')
500
Simon Glass0d24de92012-01-14 15:12:45 +0000501if __name__ == "__main__":
502 import doctest
503
504 doctest.testmod()