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