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