Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 2 | # Copyright (c) 2011 The Chromium OS Authors. |
| 3 | # |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 4 | |
| 5 | import command |
| 6 | import re |
| 7 | import os |
| 8 | import series |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 9 | import subprocess |
| 10 | import sys |
| 11 | import terminal |
| 12 | |
Simon Glass | 0c3e9c1 | 2014-04-20 10:50:13 -0600 | [diff] [blame] | 13 | import checkpatch |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 14 | import settings |
| 15 | |
Simon Glass | 6af913d | 2014-08-09 15:33:11 -0600 | [diff] [blame] | 16 | # True to use --no-decorate - we check this in Setup() |
| 17 | use_no_decorate = True |
| 18 | |
Simon Glass | b9dbcb4 | 2014-08-09 15:33:10 -0600 | [diff] [blame] | 19 | def LogCmd(commit_range, git_dir=None, oneline=False, reverse=False, |
| 20 | count=None): |
| 21 | """Create a command to perform a 'git log' |
| 22 | |
| 23 | Args: |
| 24 | commit_range: Range expression to use for log, None for none |
| 25 | git_dir: Path to git repositiory (None to use default) |
| 26 | oneline: True to use --oneline, else False |
| 27 | reverse: True to reverse the log (--reverse) |
| 28 | count: Number of commits to list, or None for no limit |
| 29 | Return: |
| 30 | List containing command and arguments to run |
| 31 | """ |
| 32 | cmd = ['git'] |
| 33 | if git_dir: |
| 34 | cmd += ['--git-dir', git_dir] |
Simon Glass | 5f4e00d | 2014-08-28 09:43:37 -0600 | [diff] [blame] | 35 | cmd += ['--no-pager', 'log', '--no-color'] |
Simon Glass | b9dbcb4 | 2014-08-09 15:33:10 -0600 | [diff] [blame] | 36 | if oneline: |
| 37 | cmd.append('--oneline') |
Simon Glass | 6af913d | 2014-08-09 15:33:11 -0600 | [diff] [blame] | 38 | if use_no_decorate: |
| 39 | cmd.append('--no-decorate') |
Simon Glass | 299b909 | 2014-08-14 21:59:11 -0600 | [diff] [blame] | 40 | if reverse: |
| 41 | cmd.append('--reverse') |
Simon Glass | b9dbcb4 | 2014-08-09 15:33:10 -0600 | [diff] [blame] | 42 | if count is not None: |
| 43 | cmd.append('-n%d' % count) |
| 44 | if commit_range: |
| 45 | cmd.append(commit_range) |
Simon Glass | 642e9a6 | 2016-03-12 18:50:31 -0700 | [diff] [blame] | 46 | |
| 47 | # Add this in case we have a branch with the same name as a directory. |
| 48 | # This avoids messages like this, for example: |
| 49 | # fatal: ambiguous argument 'test': both revision and filename |
| 50 | cmd.append('--') |
Simon Glass | b9dbcb4 | 2014-08-09 15:33:10 -0600 | [diff] [blame] | 51 | return cmd |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 52 | |
| 53 | def CountCommitsToBranch(): |
| 54 | """Returns number of commits between HEAD and the tracking branch. |
| 55 | |
| 56 | This looks back to the tracking branch and works out the number of commits |
| 57 | since then. |
| 58 | |
| 59 | Return: |
| 60 | Number of patches that exist on top of the branch |
| 61 | """ |
Simon Glass | b9dbcb4 | 2014-08-09 15:33:10 -0600 | [diff] [blame] | 62 | pipe = [LogCmd('@{upstream}..', oneline=True), |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 63 | ['wc', '-l']] |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 64 | stdout = command.RunPipe(pipe, capture=True, oneline=True).stdout |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 65 | patch_count = int(stdout) |
| 66 | return patch_count |
| 67 | |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 68 | def NameRevision(commit_hash): |
| 69 | """Gets the revision name for a commit |
| 70 | |
| 71 | Args: |
| 72 | commit_hash: Commit hash to look up |
| 73 | |
| 74 | Return: |
| 75 | Name of revision, if any, else None |
| 76 | """ |
| 77 | pipe = ['git', 'name-rev', commit_hash] |
| 78 | stdout = command.RunPipe([pipe], capture=True, oneline=True).stdout |
| 79 | |
| 80 | # We expect a commit, a space, then a revision name |
| 81 | name = stdout.split(' ')[1].strip() |
| 82 | return name |
| 83 | |
| 84 | def GuessUpstream(git_dir, branch): |
| 85 | """Tries to guess the upstream for a branch |
| 86 | |
| 87 | This lists out top commits on a branch and tries to find a suitable |
| 88 | upstream. It does this by looking for the first commit where |
| 89 | 'git name-rev' returns a plain branch name, with no ! or ^ modifiers. |
| 90 | |
| 91 | Args: |
| 92 | git_dir: Git directory containing repo |
| 93 | branch: Name of branch |
| 94 | |
| 95 | Returns: |
| 96 | Tuple: |
| 97 | Name of upstream branch (e.g. 'upstream/master') or None if none |
| 98 | Warning/error message, or None if none |
| 99 | """ |
| 100 | pipe = [LogCmd(branch, git_dir=git_dir, oneline=True, count=100)] |
| 101 | result = command.RunPipe(pipe, capture=True, capture_stderr=True, |
| 102 | raise_on_error=False) |
| 103 | if result.return_code: |
| 104 | return None, "Branch '%s' not found" % branch |
| 105 | for line in result.stdout.splitlines()[1:]: |
| 106 | commit_hash = line.split(' ')[0] |
| 107 | name = NameRevision(commit_hash) |
| 108 | if '~' not in name and '^' not in name: |
| 109 | if name.startswith('remotes/'): |
| 110 | name = name[8:] |
| 111 | return name, "Guessing upstream as '%s'" % name |
| 112 | return None, "Cannot find a suitable upstream for branch '%s'" % branch |
| 113 | |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 114 | def GetUpstream(git_dir, branch): |
| 115 | """Returns the name of the upstream for a branch |
| 116 | |
| 117 | Args: |
| 118 | git_dir: Git directory containing repo |
| 119 | branch: Name of branch |
| 120 | |
| 121 | Returns: |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 122 | Tuple: |
| 123 | Name of upstream branch (e.g. 'upstream/master') or None if none |
| 124 | Warning/error message, or None if none |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 125 | """ |
Simon Glass | d2e9538 | 2013-05-08 08:06:08 +0000 | [diff] [blame] | 126 | try: |
| 127 | remote = command.OutputOneLine('git', '--git-dir', git_dir, 'config', |
| 128 | 'branch.%s.remote' % branch) |
| 129 | merge = command.OutputOneLine('git', '--git-dir', git_dir, 'config', |
| 130 | 'branch.%s.merge' % branch) |
| 131 | except: |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 132 | upstream, msg = GuessUpstream(git_dir, branch) |
| 133 | return upstream, msg |
Simon Glass | d2e9538 | 2013-05-08 08:06:08 +0000 | [diff] [blame] | 134 | |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 135 | if remote == '.': |
Simon Glass | 7e92f5c | 2015-01-29 11:35:16 -0700 | [diff] [blame] | 136 | return merge, None |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 137 | elif remote and merge: |
| 138 | leaf = merge.split('/')[-1] |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 139 | return '%s/%s' % (remote, leaf), None |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 140 | else: |
Paul Burton | f14a131 | 2016-09-27 16:03:51 +0100 | [diff] [blame] | 141 | raise ValueError("Cannot determine upstream branch for branch " |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 142 | "'%s' remote='%s', merge='%s'" % (branch, remote, merge)) |
| 143 | |
| 144 | |
| 145 | def GetRangeInBranch(git_dir, branch, include_upstream=False): |
| 146 | """Returns an expression for the commits in the given branch. |
| 147 | |
| 148 | Args: |
| 149 | git_dir: Directory containing git repo |
| 150 | branch: Name of branch |
| 151 | Return: |
| 152 | Expression in the form 'upstream..branch' which can be used to |
Simon Glass | d2e9538 | 2013-05-08 08:06:08 +0000 | [diff] [blame] | 153 | access the commits. If the branch does not exist, returns None. |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 154 | """ |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 155 | upstream, msg = GetUpstream(git_dir, branch) |
Simon Glass | d2e9538 | 2013-05-08 08:06:08 +0000 | [diff] [blame] | 156 | if not upstream: |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 157 | return None, msg |
| 158 | rstr = '%s%s..%s' % (upstream, '~' if include_upstream else '', branch) |
| 159 | return rstr, msg |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 160 | |
Simon Glass | 5eeef46 | 2014-12-01 17:33:57 -0700 | [diff] [blame] | 161 | def CountCommitsInRange(git_dir, range_expr): |
| 162 | """Returns the number of commits in the given range. |
| 163 | |
| 164 | Args: |
| 165 | git_dir: Directory containing git repo |
| 166 | range_expr: Range to check |
| 167 | Return: |
| 168 | Number of patches that exist in the supplied rangem or None if none |
| 169 | were found |
| 170 | """ |
| 171 | pipe = [LogCmd(range_expr, git_dir=git_dir, oneline=True)] |
| 172 | result = command.RunPipe(pipe, capture=True, capture_stderr=True, |
| 173 | raise_on_error=False) |
| 174 | if result.return_code: |
| 175 | return None, "Range '%s' not found or is invalid" % range_expr |
| 176 | patch_count = len(result.stdout.splitlines()) |
| 177 | return patch_count, None |
| 178 | |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 179 | def CountCommitsInBranch(git_dir, branch, include_upstream=False): |
| 180 | """Returns the number of commits in the given branch. |
| 181 | |
| 182 | Args: |
| 183 | git_dir: Directory containing git repo |
| 184 | branch: Name of branch |
| 185 | Return: |
Simon Glass | d2e9538 | 2013-05-08 08:06:08 +0000 | [diff] [blame] | 186 | Number of patches that exist on top of the branch, or None if the |
| 187 | branch does not exist. |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 188 | """ |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 189 | range_expr, msg = GetRangeInBranch(git_dir, branch, include_upstream) |
Simon Glass | d2e9538 | 2013-05-08 08:06:08 +0000 | [diff] [blame] | 190 | if not range_expr: |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 191 | return None, msg |
Simon Glass | 5eeef46 | 2014-12-01 17:33:57 -0700 | [diff] [blame] | 192 | return CountCommitsInRange(git_dir, range_expr) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 193 | |
| 194 | def CountCommits(commit_range): |
| 195 | """Returns the number of commits in the given range. |
| 196 | |
| 197 | Args: |
| 198 | commit_range: Range of commits to count (e.g. 'HEAD..base') |
| 199 | Return: |
| 200 | Number of patches that exist on top of the branch |
| 201 | """ |
Simon Glass | b9dbcb4 | 2014-08-09 15:33:10 -0600 | [diff] [blame] | 202 | pipe = [LogCmd(commit_range, oneline=True), |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 203 | ['wc', '-l']] |
| 204 | stdout = command.RunPipe(pipe, capture=True, oneline=True).stdout |
| 205 | patch_count = int(stdout) |
| 206 | return patch_count |
| 207 | |
| 208 | def Checkout(commit_hash, git_dir=None, work_tree=None, force=False): |
| 209 | """Checkout the selected commit for this build |
| 210 | |
| 211 | Args: |
| 212 | commit_hash: Commit hash to check out |
| 213 | """ |
| 214 | pipe = ['git'] |
| 215 | if git_dir: |
| 216 | pipe.extend(['--git-dir', git_dir]) |
| 217 | if work_tree: |
| 218 | pipe.extend(['--work-tree', work_tree]) |
| 219 | pipe.append('checkout') |
| 220 | if force: |
| 221 | pipe.append('-f') |
| 222 | pipe.append(commit_hash) |
Simon Glass | f1bf686 | 2014-09-05 19:00:09 -0600 | [diff] [blame] | 223 | result = command.RunPipe([pipe], capture=True, raise_on_error=False, |
| 224 | capture_stderr=True) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 225 | if result.return_code != 0: |
Paul Burton | f14a131 | 2016-09-27 16:03:51 +0100 | [diff] [blame] | 226 | raise OSError('git checkout (%s): %s' % (pipe, result.stderr)) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 227 | |
| 228 | def Clone(git_dir, output_dir): |
| 229 | """Checkout the selected commit for this build |
| 230 | |
| 231 | Args: |
| 232 | commit_hash: Commit hash to check out |
| 233 | """ |
| 234 | pipe = ['git', 'clone', git_dir, '.'] |
Simon Glass | f1bf686 | 2014-09-05 19:00:09 -0600 | [diff] [blame] | 235 | result = command.RunPipe([pipe], capture=True, cwd=output_dir, |
| 236 | capture_stderr=True) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 237 | if result.return_code != 0: |
Paul Burton | f14a131 | 2016-09-27 16:03:51 +0100 | [diff] [blame] | 238 | raise OSError('git clone: %s' % result.stderr) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 239 | |
| 240 | def Fetch(git_dir=None, work_tree=None): |
| 241 | """Fetch from the origin repo |
| 242 | |
| 243 | Args: |
| 244 | commit_hash: Commit hash to check out |
| 245 | """ |
| 246 | pipe = ['git'] |
| 247 | if git_dir: |
| 248 | pipe.extend(['--git-dir', git_dir]) |
| 249 | if work_tree: |
| 250 | pipe.extend(['--work-tree', work_tree]) |
| 251 | pipe.append('fetch') |
Simon Glass | f1bf686 | 2014-09-05 19:00:09 -0600 | [diff] [blame] | 252 | result = command.RunPipe([pipe], capture=True, capture_stderr=True) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 253 | if result.return_code != 0: |
Paul Burton | f14a131 | 2016-09-27 16:03:51 +0100 | [diff] [blame] | 254 | raise OSError('git fetch: %s' % result.stderr) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 255 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 256 | def CreatePatches(start, count, series): |
| 257 | """Create a series of patches from the top of the current branch. |
| 258 | |
| 259 | The patch files are written to the current directory using |
| 260 | git format-patch. |
| 261 | |
| 262 | Args: |
| 263 | start: Commit to start from: 0=HEAD, 1=next one, etc. |
| 264 | count: number of commits to include |
| 265 | Return: |
| 266 | Filename of cover letter |
| 267 | List of filenames of patch files |
| 268 | """ |
| 269 | if series.get('version'): |
| 270 | version = '%s ' % series['version'] |
Masahiro Yamada | 41d176f | 2015-08-31 01:23:32 +0900 | [diff] [blame] | 271 | cmd = ['git', 'format-patch', '-M', '--signoff'] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 272 | if series.get('cover'): |
| 273 | cmd.append('--cover-letter') |
| 274 | prefix = series.GetPatchPrefix() |
| 275 | if prefix: |
| 276 | cmd += ['--subject-prefix=%s' % prefix] |
| 277 | cmd += ['HEAD~%d..HEAD~%d' % (start + count, start)] |
| 278 | |
| 279 | stdout = command.RunList(cmd) |
| 280 | files = stdout.splitlines() |
| 281 | |
| 282 | # We have an extra file if there is a cover letter |
| 283 | if series.get('cover'): |
| 284 | return files[0], files[1:] |
| 285 | else: |
| 286 | return None, files |
| 287 | |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 288 | def BuildEmailList(in_list, tag=None, alias=None, raise_on_error=True): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 289 | """Build a list of email addresses based on an input list. |
| 290 | |
| 291 | Takes a list of email addresses and aliases, and turns this into a list |
| 292 | of only email address, by resolving any aliases that are present. |
| 293 | |
| 294 | If the tag is given, then each email address is prepended with this |
| 295 | tag and a space. If the tag starts with a minus sign (indicating a |
| 296 | command line parameter) then the email address is quoted. |
| 297 | |
| 298 | Args: |
| 299 | in_list: List of aliases/email addresses |
| 300 | tag: Text to put before each address |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 301 | alias: Alias dictionary |
| 302 | raise_on_error: True to raise an error when an alias fails to match, |
| 303 | False to just print a message. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 304 | |
| 305 | Returns: |
| 306 | List of email addresses |
| 307 | |
| 308 | >>> alias = {} |
| 309 | >>> alias['fred'] = ['f.bloggs@napier.co.nz'] |
| 310 | >>> alias['john'] = ['j.bloggs@napier.co.nz'] |
| 311 | >>> alias['mary'] = ['Mary Poppins <m.poppins@cloud.net>'] |
| 312 | >>> alias['boys'] = ['fred', ' john'] |
| 313 | >>> alias['all'] = ['fred ', 'john', ' mary '] |
| 314 | >>> BuildEmailList(['john', 'mary'], None, alias) |
| 315 | ['j.bloggs@napier.co.nz', 'Mary Poppins <m.poppins@cloud.net>'] |
| 316 | >>> BuildEmailList(['john', 'mary'], '--to', alias) |
| 317 | ['--to "j.bloggs@napier.co.nz"', \ |
| 318 | '--to "Mary Poppins <m.poppins@cloud.net>"'] |
| 319 | >>> BuildEmailList(['john', 'mary'], 'Cc', alias) |
| 320 | ['Cc j.bloggs@napier.co.nz', 'Cc Mary Poppins <m.poppins@cloud.net>'] |
| 321 | """ |
| 322 | quote = '"' if tag and tag[0] == '-' else '' |
| 323 | raw = [] |
| 324 | for item in in_list: |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 325 | raw += LookupEmail(item, alias, raise_on_error=raise_on_error) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 326 | result = [] |
| 327 | for item in raw: |
| 328 | if not item in result: |
| 329 | result.append(item) |
| 330 | if tag: |
| 331 | return ['%s %s%s%s' % (tag, quote, email, quote) for email in result] |
| 332 | return result |
| 333 | |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 334 | def EmailPatches(series, cover_fname, args, dry_run, raise_on_error, cc_fname, |
Mateusz Kulikowski | 80c2ebc | 2016-01-14 20:37:41 +0100 | [diff] [blame] | 335 | self_only=False, alias=None, in_reply_to=None, thread=False): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 336 | """Email a patch series. |
| 337 | |
| 338 | Args: |
| 339 | series: Series object containing destination info |
| 340 | cover_fname: filename of cover letter |
| 341 | args: list of filenames of patch files |
| 342 | dry_run: Just return the command that would be run |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 343 | raise_on_error: True to raise an error when an alias fails to match, |
| 344 | False to just print a message. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 345 | cc_fname: Filename of Cc file for per-commit Cc |
| 346 | self_only: True to just email to yourself as a test |
Doug Anderson | 06f27ac | 2013-03-17 10:31:04 +0000 | [diff] [blame] | 347 | in_reply_to: If set we'll pass this to git as --in-reply-to. |
| 348 | Should be a message ID that this is in reply to. |
Mateusz Kulikowski | 80c2ebc | 2016-01-14 20:37:41 +0100 | [diff] [blame] | 349 | thread: True to add --thread to git send-email (make |
| 350 | all patches reply to cover-letter or first patch in series) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 351 | |
| 352 | Returns: |
| 353 | Git command that was/would be run |
| 354 | |
Doug Anderson | 51d7321 | 2012-11-26 15:21:40 +0000 | [diff] [blame] | 355 | # For the duration of this doctest pretend that we ran patman with ./patman |
| 356 | >>> _old_argv0 = sys.argv[0] |
| 357 | >>> sys.argv[0] = './patman' |
| 358 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 359 | >>> alias = {} |
| 360 | >>> alias['fred'] = ['f.bloggs@napier.co.nz'] |
| 361 | >>> alias['john'] = ['j.bloggs@napier.co.nz'] |
| 362 | >>> alias['mary'] = ['m.poppins@cloud.net'] |
| 363 | >>> alias['boys'] = ['fred', ' john'] |
| 364 | >>> alias['all'] = ['fred ', 'john', ' mary '] |
| 365 | >>> alias[os.getenv('USER')] = ['this-is-me@me.com'] |
| 366 | >>> series = series.Series() |
| 367 | >>> series.to = ['fred'] |
| 368 | >>> series.cc = ['mary'] |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 369 | >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \ |
| 370 | False, alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 371 | 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \ |
| 372 | "m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" cover p1 p2' |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 373 | >>> EmailPatches(series, None, ['p1'], True, True, 'cc-fname', False, \ |
| 374 | alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 375 | 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \ |
| 376 | "m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" p1' |
| 377 | >>> series.cc = ['all'] |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 378 | >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \ |
| 379 | True, alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 380 | 'git send-email --annotate --to "this-is-me@me.com" --cc-cmd "./patman \ |
| 381 | --cc-cmd cc-fname" cover p1 p2' |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 382 | >>> EmailPatches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \ |
| 383 | False, alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 384 | 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \ |
| 385 | "f.bloggs@napier.co.nz" --cc "j.bloggs@napier.co.nz" --cc \ |
| 386 | "m.poppins@cloud.net" --cc-cmd "./patman --cc-cmd cc-fname" cover p1 p2' |
Doug Anderson | 51d7321 | 2012-11-26 15:21:40 +0000 | [diff] [blame] | 387 | |
| 388 | # Restore argv[0] since we clobbered it. |
| 389 | >>> sys.argv[0] = _old_argv0 |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 390 | """ |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 391 | to = BuildEmailList(series.get('to'), '--to', alias, raise_on_error) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 392 | if not to: |
Simon Glass | c55e056 | 2016-07-25 18:59:00 -0600 | [diff] [blame] | 393 | git_config_to = command.Output('git', 'config', 'sendemail.to', |
| 394 | raise_on_error=False) |
Masahiro Yamada | d91f5b9 | 2014-07-18 14:23:20 +0900 | [diff] [blame] | 395 | if not git_config_to: |
| 396 | print ("No recipient.\n" |
| 397 | "Please add something like this to a commit\n" |
| 398 | "Series-to: Fred Bloggs <f.blogs@napier.co.nz>\n" |
| 399 | "Or do something like this\n" |
| 400 | "git config sendemail.to u-boot@lists.denx.de") |
| 401 | return |
Peter Tyser | c6af802 | 2015-01-26 11:42:21 -0600 | [diff] [blame] | 402 | cc = BuildEmailList(list(set(series.get('cc')) - set(series.get('to'))), |
| 403 | '--cc', alias, raise_on_error) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 404 | if self_only: |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 405 | to = BuildEmailList([os.getenv('USER')], '--to', alias, raise_on_error) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 406 | cc = [] |
| 407 | cmd = ['git', 'send-email', '--annotate'] |
Doug Anderson | 06f27ac | 2013-03-17 10:31:04 +0000 | [diff] [blame] | 408 | if in_reply_to: |
Simon Glass | 22c5834 | 2017-05-29 15:31:23 -0600 | [diff] [blame] | 409 | if type(in_reply_to) != str: |
| 410 | in_reply_to = in_reply_to.encode('utf-8') |
Doug Anderson | 06f27ac | 2013-03-17 10:31:04 +0000 | [diff] [blame] | 411 | cmd.append('--in-reply-to="%s"' % in_reply_to) |
Mateusz Kulikowski | 80c2ebc | 2016-01-14 20:37:41 +0100 | [diff] [blame] | 412 | if thread: |
| 413 | cmd.append('--thread') |
Doug Anderson | 06f27ac | 2013-03-17 10:31:04 +0000 | [diff] [blame] | 414 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 415 | cmd += to |
| 416 | cmd += cc |
| 417 | cmd += ['--cc-cmd', '"%s --cc-cmd %s"' % (sys.argv[0], cc_fname)] |
| 418 | if cover_fname: |
| 419 | cmd.append(cover_fname) |
| 420 | cmd += args |
Simon Glass | 47e308e | 2017-05-29 15:31:25 -0600 | [diff] [blame] | 421 | cmdstr = ' '.join(cmd) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 422 | if not dry_run: |
Simon Glass | 47e308e | 2017-05-29 15:31:25 -0600 | [diff] [blame] | 423 | os.system(cmdstr) |
| 424 | return cmdstr |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 425 | |
| 426 | |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 427 | def LookupEmail(lookup_name, alias=None, raise_on_error=True, level=0): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 428 | """If an email address is an alias, look it up and return the full name |
| 429 | |
| 430 | TODO: Why not just use git's own alias feature? |
| 431 | |
| 432 | Args: |
| 433 | lookup_name: Alias or email address to look up |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 434 | alias: Dictionary containing aliases (None to use settings default) |
| 435 | raise_on_error: True to raise an error when an alias fails to match, |
| 436 | False to just print a message. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 437 | |
| 438 | Returns: |
| 439 | tuple: |
| 440 | list containing a list of email addresses |
| 441 | |
| 442 | Raises: |
| 443 | OSError if a recursive alias reference was found |
| 444 | ValueError if an alias was not found |
| 445 | |
| 446 | >>> alias = {} |
| 447 | >>> alias['fred'] = ['f.bloggs@napier.co.nz'] |
| 448 | >>> alias['john'] = ['j.bloggs@napier.co.nz'] |
| 449 | >>> alias['mary'] = ['m.poppins@cloud.net'] |
| 450 | >>> alias['boys'] = ['fred', ' john', 'f.bloggs@napier.co.nz'] |
| 451 | >>> alias['all'] = ['fred ', 'john', ' mary '] |
| 452 | >>> alias['loop'] = ['other', 'john', ' mary '] |
| 453 | >>> alias['other'] = ['loop', 'john', ' mary '] |
| 454 | >>> LookupEmail('mary', alias) |
| 455 | ['m.poppins@cloud.net'] |
| 456 | >>> LookupEmail('arthur.wellesley@howe.ro.uk', alias) |
| 457 | ['arthur.wellesley@howe.ro.uk'] |
| 458 | >>> LookupEmail('boys', alias) |
| 459 | ['f.bloggs@napier.co.nz', 'j.bloggs@napier.co.nz'] |
| 460 | >>> LookupEmail('all', alias) |
| 461 | ['f.bloggs@napier.co.nz', 'j.bloggs@napier.co.nz', 'm.poppins@cloud.net'] |
| 462 | >>> LookupEmail('odd', alias) |
| 463 | Traceback (most recent call last): |
| 464 | ... |
| 465 | ValueError: Alias 'odd' not found |
| 466 | >>> LookupEmail('loop', alias) |
| 467 | Traceback (most recent call last): |
| 468 | ... |
| 469 | OSError: Recursive email alias at 'other' |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 470 | >>> LookupEmail('odd', alias, raise_on_error=False) |
Simon Glass | b0cd341 | 2014-08-28 09:43:35 -0600 | [diff] [blame] | 471 | Alias 'odd' not found |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 472 | [] |
| 473 | >>> # In this case the loop part will effectively be ignored. |
| 474 | >>> LookupEmail('loop', alias, raise_on_error=False) |
Simon Glass | b0cd341 | 2014-08-28 09:43:35 -0600 | [diff] [blame] | 475 | Recursive email alias at 'other' |
| 476 | Recursive email alias at 'john' |
| 477 | Recursive email alias at 'mary' |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 478 | ['j.bloggs@napier.co.nz', 'm.poppins@cloud.net'] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 479 | """ |
| 480 | if not alias: |
| 481 | alias = settings.alias |
| 482 | lookup_name = lookup_name.strip() |
| 483 | if '@' in lookup_name: # Perhaps a real email address |
| 484 | return [lookup_name] |
| 485 | |
| 486 | lookup_name = lookup_name.lower() |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 487 | col = terminal.Color() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 488 | |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 489 | out_list = [] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 490 | if level > 10: |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 491 | msg = "Recursive email alias at '%s'" % lookup_name |
| 492 | if raise_on_error: |
Paul Burton | f14a131 | 2016-09-27 16:03:51 +0100 | [diff] [blame] | 493 | raise OSError(msg) |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 494 | else: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 495 | print(col.Color(col.RED, msg)) |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 496 | return out_list |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 497 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 498 | if lookup_name: |
| 499 | if not lookup_name in alias: |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 500 | msg = "Alias '%s' not found" % lookup_name |
| 501 | if raise_on_error: |
Paul Burton | f14a131 | 2016-09-27 16:03:51 +0100 | [diff] [blame] | 502 | raise ValueError(msg) |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 503 | else: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 504 | print(col.Color(col.RED, msg)) |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 505 | return out_list |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 506 | for item in alias[lookup_name]: |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 507 | todo = LookupEmail(item, alias, raise_on_error, level + 1) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 508 | for new_item in todo: |
| 509 | if not new_item in out_list: |
| 510 | out_list.append(new_item) |
| 511 | |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 512 | #print("No match for alias '%s'" % lookup_name) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 513 | return out_list |
| 514 | |
| 515 | def GetTopLevel(): |
| 516 | """Return name of top-level directory for this git repo. |
| 517 | |
| 518 | Returns: |
| 519 | Full path to git top-level directory |
| 520 | |
| 521 | This test makes sure that we are running tests in the right subdir |
| 522 | |
Doug Anderson | 51d7321 | 2012-11-26 15:21:40 +0000 | [diff] [blame] | 523 | >>> os.path.realpath(os.path.dirname(__file__)) == \ |
| 524 | os.path.join(GetTopLevel(), 'tools', 'patman') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 525 | True |
| 526 | """ |
| 527 | return command.OutputOneLine('git', 'rev-parse', '--show-toplevel') |
| 528 | |
| 529 | def GetAliasFile(): |
| 530 | """Gets the name of the git alias file. |
| 531 | |
| 532 | Returns: |
| 533 | Filename of git alias file, or None if none |
| 534 | """ |
Simon Glass | 519fad2 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 535 | fname = command.OutputOneLine('git', 'config', 'sendemail.aliasesfile', |
| 536 | raise_on_error=False) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 537 | if fname: |
| 538 | fname = os.path.join(GetTopLevel(), fname.strip()) |
| 539 | return fname |
| 540 | |
Vikram Narayanan | 12fb29a | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 541 | def GetDefaultUserName(): |
| 542 | """Gets the user.name from .gitconfig file. |
| 543 | |
| 544 | Returns: |
| 545 | User name found in .gitconfig file, or None if none |
| 546 | """ |
| 547 | uname = command.OutputOneLine('git', 'config', '--global', 'user.name') |
| 548 | return uname |
| 549 | |
| 550 | def GetDefaultUserEmail(): |
| 551 | """Gets the user.email from the global .gitconfig file. |
| 552 | |
| 553 | Returns: |
| 554 | User's email found in .gitconfig file, or None if none |
| 555 | """ |
| 556 | uemail = command.OutputOneLine('git', 'config', '--global', 'user.email') |
| 557 | return uemail |
| 558 | |
Wu, Josh | 9873b91 | 2015-04-15 10:25:18 +0800 | [diff] [blame] | 559 | def GetDefaultSubjectPrefix(): |
| 560 | """Gets the format.subjectprefix from local .git/config file. |
| 561 | |
| 562 | Returns: |
| 563 | Subject prefix found in local .git/config file, or None if none |
| 564 | """ |
| 565 | sub_prefix = command.OutputOneLine('git', 'config', 'format.subjectprefix', |
| 566 | raise_on_error=False) |
| 567 | |
| 568 | return sub_prefix |
| 569 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 570 | def Setup(): |
| 571 | """Set up git utils, by reading the alias files.""" |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 572 | # Check for a git alias file also |
Simon Glass | 81bcca8 | 2014-08-28 09:43:45 -0600 | [diff] [blame] | 573 | global use_no_decorate |
| 574 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 575 | alias_fname = GetAliasFile() |
| 576 | if alias_fname: |
| 577 | settings.ReadGitAliases(alias_fname) |
Simon Glass | 6af913d | 2014-08-09 15:33:11 -0600 | [diff] [blame] | 578 | cmd = LogCmd(None, count=0) |
| 579 | use_no_decorate = (command.RunPipe([cmd], raise_on_error=False) |
| 580 | .return_code == 0) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 581 | |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 582 | def GetHead(): |
| 583 | """Get the hash of the current HEAD |
| 584 | |
| 585 | Returns: |
| 586 | Hash of HEAD |
| 587 | """ |
| 588 | return command.OutputOneLine('git', 'show', '-s', '--pretty=format:%H') |
| 589 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 590 | if __name__ == "__main__": |
| 591 | import doctest |
| 592 | |
| 593 | doctest.testmod() |