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 os |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 6 | import sys |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 7 | |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 8 | from patman import settings |
Simon Glass | 131444f | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 9 | from u_boot_pylib import command |
| 10 | from u_boot_pylib import terminal |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 11 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 12 | # True to use --no-decorate - we check this in setup() |
Simon Glass | 6af913d | 2014-08-09 15:33:11 -0600 | [diff] [blame] | 13 | use_no_decorate = True |
| 14 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 15 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 16 | def log_cmd(commit_range, git_dir=None, oneline=False, reverse=False, |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 17 | count=None): |
Simon Glass | b9dbcb4 | 2014-08-09 15:33:10 -0600 | [diff] [blame] | 18 | """Create a command to perform a 'git log' |
| 19 | |
| 20 | Args: |
| 21 | commit_range: Range expression to use for log, None for none |
Anatolij Gustschin | f2bcb32 | 2019-10-27 17:55:04 +0100 | [diff] [blame] | 22 | git_dir: Path to git repository (None to use default) |
Simon Glass | b9dbcb4 | 2014-08-09 15:33:10 -0600 | [diff] [blame] | 23 | oneline: True to use --oneline, else False |
| 24 | reverse: True to reverse the log (--reverse) |
| 25 | count: Number of commits to list, or None for no limit |
| 26 | Return: |
| 27 | List containing command and arguments to run |
| 28 | """ |
| 29 | cmd = ['git'] |
| 30 | if git_dir: |
| 31 | cmd += ['--git-dir', git_dir] |
Simon Glass | 5f4e00d | 2014-08-28 09:43:37 -0600 | [diff] [blame] | 32 | cmd += ['--no-pager', 'log', '--no-color'] |
Simon Glass | b9dbcb4 | 2014-08-09 15:33:10 -0600 | [diff] [blame] | 33 | if oneline: |
| 34 | cmd.append('--oneline') |
Simon Glass | 6af913d | 2014-08-09 15:33:11 -0600 | [diff] [blame] | 35 | if use_no_decorate: |
| 36 | cmd.append('--no-decorate') |
Simon Glass | 299b909 | 2014-08-14 21:59:11 -0600 | [diff] [blame] | 37 | if reverse: |
| 38 | cmd.append('--reverse') |
Simon Glass | b9dbcb4 | 2014-08-09 15:33:10 -0600 | [diff] [blame] | 39 | if count is not None: |
| 40 | cmd.append('-n%d' % count) |
| 41 | if commit_range: |
| 42 | cmd.append(commit_range) |
Simon Glass | 642e9a6 | 2016-03-12 18:50:31 -0700 | [diff] [blame] | 43 | |
| 44 | # Add this in case we have a branch with the same name as a directory. |
| 45 | # This avoids messages like this, for example: |
| 46 | # fatal: ambiguous argument 'test': both revision and filename |
| 47 | cmd.append('--') |
Simon Glass | b9dbcb4 | 2014-08-09 15:33:10 -0600 | [diff] [blame] | 48 | return cmd |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 49 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 50 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 51 | def count_commits_to_branch(branch): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 52 | """Returns number of commits between HEAD and the tracking branch. |
| 53 | |
| 54 | This looks back to the tracking branch and works out the number of commits |
| 55 | since then. |
| 56 | |
Simon Glass | 2eb4da7 | 2020-07-05 21:41:51 -0600 | [diff] [blame] | 57 | Args: |
| 58 | branch: Branch to count from (None for current branch) |
| 59 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 60 | Return: |
| 61 | Number of patches that exist on top of the branch |
| 62 | """ |
Simon Glass | 2eb4da7 | 2020-07-05 21:41:51 -0600 | [diff] [blame] | 63 | if branch: |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 64 | us, msg = get_upstream('.git', branch) |
Simon Glass | 2eb4da7 | 2020-07-05 21:41:51 -0600 | [diff] [blame] | 65 | rev_range = '%s..%s' % (us, branch) |
| 66 | else: |
| 67 | rev_range = '@{upstream}..' |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 68 | pipe = [log_cmd(rev_range, oneline=True)] |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 69 | result = command.run_pipe(pipe, capture=True, capture_stderr=True, |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 70 | oneline=True, raise_on_error=False) |
Simon Glass | 1c1f207 | 2020-10-29 21:46:34 -0600 | [diff] [blame] | 71 | if result.return_code: |
| 72 | raise ValueError('Failed to determine upstream: %s' % |
| 73 | result.stderr.strip()) |
| 74 | patch_count = len(result.stdout.splitlines()) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 75 | return patch_count |
| 76 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 77 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 78 | def name_revision(commit_hash): |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 79 | """Gets the revision name for a commit |
| 80 | |
| 81 | Args: |
| 82 | commit_hash: Commit hash to look up |
| 83 | |
| 84 | Return: |
| 85 | Name of revision, if any, else None |
| 86 | """ |
| 87 | pipe = ['git', 'name-rev', commit_hash] |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 88 | stdout = command.run_pipe([pipe], capture=True, oneline=True).stdout |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 89 | |
| 90 | # We expect a commit, a space, then a revision name |
| 91 | name = stdout.split(' ')[1].strip() |
| 92 | return name |
| 93 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 94 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 95 | def guess_upstream(git_dir, branch): |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 96 | """Tries to guess the upstream for a branch |
| 97 | |
| 98 | This lists out top commits on a branch and tries to find a suitable |
| 99 | upstream. It does this by looking for the first commit where |
| 100 | 'git name-rev' returns a plain branch name, with no ! or ^ modifiers. |
| 101 | |
| 102 | Args: |
| 103 | git_dir: Git directory containing repo |
| 104 | branch: Name of branch |
| 105 | |
| 106 | Returns: |
| 107 | Tuple: |
| 108 | Name of upstream branch (e.g. 'upstream/master') or None if none |
| 109 | Warning/error message, or None if none |
| 110 | """ |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 111 | pipe = [log_cmd(branch, git_dir=git_dir, oneline=True, count=100)] |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 112 | result = command.run_pipe(pipe, capture=True, capture_stderr=True, |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 113 | raise_on_error=False) |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 114 | if result.return_code: |
| 115 | return None, "Branch '%s' not found" % branch |
| 116 | for line in result.stdout.splitlines()[1:]: |
| 117 | commit_hash = line.split(' ')[0] |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 118 | name = name_revision(commit_hash) |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 119 | if '~' not in name and '^' not in name: |
| 120 | if name.startswith('remotes/'): |
| 121 | name = name[8:] |
| 122 | return name, "Guessing upstream as '%s'" % name |
| 123 | return None, "Cannot find a suitable upstream for branch '%s'" % branch |
| 124 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 125 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 126 | def get_upstream(git_dir, branch): |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 127 | """Returns the name of the upstream for a branch |
| 128 | |
| 129 | Args: |
| 130 | git_dir: Git directory containing repo |
| 131 | branch: Name of branch |
| 132 | |
| 133 | Returns: |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 134 | Tuple: |
| 135 | Name of upstream branch (e.g. 'upstream/master') or None if none |
| 136 | Warning/error message, or None if none |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 137 | """ |
Simon Glass | d2e9538 | 2013-05-08 08:06:08 +0000 | [diff] [blame] | 138 | try: |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 139 | remote = command.output_one_line('git', '--git-dir', git_dir, 'config', |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 140 | 'branch.%s.remote' % branch) |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 141 | merge = command.output_one_line('git', '--git-dir', git_dir, 'config', |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 142 | 'branch.%s.merge' % branch) |
| 143 | except Exception: |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 144 | upstream, msg = guess_upstream(git_dir, branch) |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 145 | return upstream, msg |
Simon Glass | d2e9538 | 2013-05-08 08:06:08 +0000 | [diff] [blame] | 146 | |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 147 | if remote == '.': |
Simon Glass | 7e92f5c | 2015-01-29 11:35:16 -0700 | [diff] [blame] | 148 | return merge, None |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 149 | elif remote and merge: |
Simon Glass | 978c1fb | 2023-10-30 10:22:30 -0700 | [diff] [blame] | 150 | # Drop the initial refs/heads from merge |
| 151 | leaf = merge.split('/', maxsplit=2)[2:] |
| 152 | return '%s/%s' % (remote, '/'.join(leaf)), None |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 153 | else: |
Paul Burton | f14a131 | 2016-09-27 16:03:51 +0100 | [diff] [blame] | 154 | raise ValueError("Cannot determine upstream branch for branch " |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 155 | "'%s' remote='%s', merge='%s'" |
| 156 | % (branch, remote, merge)) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 157 | |
| 158 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 159 | def get_range_in_branch(git_dir, branch, include_upstream=False): |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 160 | """Returns an expression for the commits in the given branch. |
| 161 | |
| 162 | Args: |
| 163 | git_dir: Directory containing git repo |
| 164 | branch: Name of branch |
| 165 | Return: |
| 166 | Expression in the form 'upstream..branch' which can be used to |
Simon Glass | d2e9538 | 2013-05-08 08:06:08 +0000 | [diff] [blame] | 167 | access the commits. If the branch does not exist, returns None. |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 168 | """ |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 169 | upstream, msg = get_upstream(git_dir, branch) |
Simon Glass | d2e9538 | 2013-05-08 08:06:08 +0000 | [diff] [blame] | 170 | if not upstream: |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 171 | return None, msg |
| 172 | rstr = '%s%s..%s' % (upstream, '~' if include_upstream else '', branch) |
| 173 | return rstr, msg |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 174 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 175 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 176 | def count_commits_in_range(git_dir, range_expr): |
Simon Glass | 5eeef46 | 2014-12-01 17:33:57 -0700 | [diff] [blame] | 177 | """Returns the number of commits in the given range. |
| 178 | |
| 179 | Args: |
| 180 | git_dir: Directory containing git repo |
| 181 | range_expr: Range to check |
| 182 | Return: |
Anatolij Gustschin | f2bcb32 | 2019-10-27 17:55:04 +0100 | [diff] [blame] | 183 | 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] | 184 | were found |
| 185 | """ |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 186 | pipe = [log_cmd(range_expr, git_dir=git_dir, oneline=True)] |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 187 | result = command.run_pipe(pipe, capture=True, capture_stderr=True, |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 188 | raise_on_error=False) |
Simon Glass | 5eeef46 | 2014-12-01 17:33:57 -0700 | [diff] [blame] | 189 | if result.return_code: |
| 190 | return None, "Range '%s' not found or is invalid" % range_expr |
| 191 | patch_count = len(result.stdout.splitlines()) |
| 192 | return patch_count, None |
| 193 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 194 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 195 | def count_commits_in_branch(git_dir, branch, include_upstream=False): |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 196 | """Returns the number of commits in the given branch. |
| 197 | |
| 198 | Args: |
| 199 | git_dir: Directory containing git repo |
| 200 | branch: Name of branch |
| 201 | Return: |
Simon Glass | d2e9538 | 2013-05-08 08:06:08 +0000 | [diff] [blame] | 202 | Number of patches that exist on top of the branch, or None if the |
| 203 | branch does not exist. |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 204 | """ |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 205 | range_expr, msg = get_range_in_branch(git_dir, branch, include_upstream) |
Simon Glass | d2e9538 | 2013-05-08 08:06:08 +0000 | [diff] [blame] | 206 | if not range_expr: |
Simon Glass | f204ab1 | 2014-12-01 17:33:54 -0700 | [diff] [blame] | 207 | return None, msg |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 208 | return count_commits_in_range(git_dir, range_expr) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 209 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 210 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 211 | def count_commits(commit_range): |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 212 | """Returns the number of commits in the given range. |
| 213 | |
| 214 | Args: |
| 215 | commit_range: Range of commits to count (e.g. 'HEAD..base') |
| 216 | Return: |
| 217 | Number of patches that exist on top of the branch |
| 218 | """ |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 219 | pipe = [log_cmd(commit_range, oneline=True), |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 220 | ['wc', '-l']] |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 221 | stdout = command.run_pipe(pipe, capture=True, oneline=True).stdout |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 222 | patch_count = int(stdout) |
| 223 | return patch_count |
| 224 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 225 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 226 | def checkout(commit_hash, git_dir=None, work_tree=None, force=False): |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 227 | """Checkout the selected commit for this build |
| 228 | |
| 229 | Args: |
| 230 | commit_hash: Commit hash to check out |
| 231 | """ |
| 232 | pipe = ['git'] |
| 233 | if git_dir: |
| 234 | pipe.extend(['--git-dir', git_dir]) |
| 235 | if work_tree: |
| 236 | pipe.extend(['--work-tree', work_tree]) |
| 237 | pipe.append('checkout') |
| 238 | if force: |
| 239 | pipe.append('-f') |
| 240 | pipe.append(commit_hash) |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 241 | result = command.run_pipe([pipe], capture=True, raise_on_error=False, |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 242 | capture_stderr=True) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 243 | if result.return_code != 0: |
Paul Burton | f14a131 | 2016-09-27 16:03:51 +0100 | [diff] [blame] | 244 | raise OSError('git checkout (%s): %s' % (pipe, result.stderr)) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 245 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 246 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 247 | def clone(git_dir, output_dir): |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 248 | """Checkout the selected commit for this build |
| 249 | |
| 250 | Args: |
| 251 | commit_hash: Commit hash to check out |
| 252 | """ |
| 253 | pipe = ['git', 'clone', git_dir, '.'] |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 254 | result = command.run_pipe([pipe], capture=True, cwd=output_dir, |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 255 | capture_stderr=True) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 256 | if result.return_code != 0: |
Paul Burton | f14a131 | 2016-09-27 16:03:51 +0100 | [diff] [blame] | 257 | raise OSError('git clone: %s' % result.stderr) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 258 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 259 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 260 | def fetch(git_dir=None, work_tree=None): |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 261 | """Fetch from the origin repo |
| 262 | |
| 263 | Args: |
| 264 | commit_hash: Commit hash to check out |
| 265 | """ |
| 266 | pipe = ['git'] |
| 267 | if git_dir: |
| 268 | pipe.extend(['--git-dir', git_dir]) |
| 269 | if work_tree: |
| 270 | pipe.extend(['--work-tree', work_tree]) |
| 271 | pipe.append('fetch') |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 272 | result = command.run_pipe([pipe], capture=True, capture_stderr=True) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 273 | if result.return_code != 0: |
Paul Burton | f14a131 | 2016-09-27 16:03:51 +0100 | [diff] [blame] | 274 | raise OSError('git fetch: %s' % result.stderr) |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 275 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 276 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 277 | def check_worktree_is_available(git_dir): |
Alper Nebi Yasak | fede44a | 2020-09-03 15:51:03 +0300 | [diff] [blame] | 278 | """Check if git-worktree functionality is available |
| 279 | |
| 280 | Args: |
| 281 | git_dir: The repository to test in |
| 282 | |
| 283 | Returns: |
| 284 | True if git-worktree commands will work, False otherwise. |
| 285 | """ |
| 286 | pipe = ['git', '--git-dir', git_dir, 'worktree', 'list'] |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 287 | result = command.run_pipe([pipe], capture=True, capture_stderr=True, |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 288 | raise_on_error=False) |
Alper Nebi Yasak | fede44a | 2020-09-03 15:51:03 +0300 | [diff] [blame] | 289 | return result.return_code == 0 |
| 290 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 291 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 292 | def add_worktree(git_dir, output_dir, commit_hash=None): |
Alper Nebi Yasak | fede44a | 2020-09-03 15:51:03 +0300 | [diff] [blame] | 293 | """Create and checkout a new git worktree for this build |
| 294 | |
| 295 | Args: |
| 296 | git_dir: The repository to checkout the worktree from |
| 297 | output_dir: Path for the new worktree |
| 298 | commit_hash: Commit hash to checkout |
| 299 | """ |
| 300 | # We need to pass --detach to avoid creating a new branch |
| 301 | pipe = ['git', '--git-dir', git_dir, 'worktree', 'add', '.', '--detach'] |
| 302 | if commit_hash: |
| 303 | pipe.append(commit_hash) |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 304 | result = command.run_pipe([pipe], capture=True, cwd=output_dir, |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 305 | capture_stderr=True) |
Alper Nebi Yasak | fede44a | 2020-09-03 15:51:03 +0300 | [diff] [blame] | 306 | if result.return_code != 0: |
| 307 | raise OSError('git worktree add: %s' % result.stderr) |
| 308 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 309 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 310 | def prune_worktrees(git_dir): |
Alper Nebi Yasak | fede44a | 2020-09-03 15:51:03 +0300 | [diff] [blame] | 311 | """Remove administrative files for deleted worktrees |
| 312 | |
| 313 | Args: |
| 314 | git_dir: The repository whose deleted worktrees should be pruned |
| 315 | """ |
| 316 | pipe = ['git', '--git-dir', git_dir, 'worktree', 'prune'] |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 317 | result = command.run_pipe([pipe], capture=True, capture_stderr=True) |
Alper Nebi Yasak | fede44a | 2020-09-03 15:51:03 +0300 | [diff] [blame] | 318 | if result.return_code != 0: |
| 319 | raise OSError('git worktree prune: %s' % result.stderr) |
| 320 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 321 | |
| 322 | def create_patches(branch, start, count, ignore_binary, series, signoff=True): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 323 | """Create a series of patches from the top of the current branch. |
| 324 | |
| 325 | The patch files are written to the current directory using |
| 326 | git format-patch. |
| 327 | |
| 328 | Args: |
Simon Glass | 2eb4da7 | 2020-07-05 21:41:51 -0600 | [diff] [blame] | 329 | branch: Branch to create patches from (None for current branch) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 330 | start: Commit to start from: 0=HEAD, 1=next one, etc. |
| 331 | count: number of commits to include |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 332 | ignore_binary: Don't generate patches for binary files |
| 333 | series: Series object for this series (set of patches) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 334 | Return: |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 335 | Filename of cover letter (None if none) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 336 | List of filenames of patch files |
| 337 | """ |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 338 | cmd = ['git', 'format-patch', '-M'] |
Philipp Tomsich | 858531a | 2020-11-24 18:14:52 +0100 | [diff] [blame] | 339 | if signoff: |
| 340 | cmd.append('--signoff') |
Bin Meng | a04f121 | 2020-05-04 00:52:44 -0700 | [diff] [blame] | 341 | if ignore_binary: |
| 342 | cmd.append('--no-binary') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 343 | if series.get('cover'): |
| 344 | cmd.append('--cover-letter') |
| 345 | prefix = series.GetPatchPrefix() |
| 346 | if prefix: |
| 347 | cmd += ['--subject-prefix=%s' % prefix] |
Simon Glass | 2eb4da7 | 2020-07-05 21:41:51 -0600 | [diff] [blame] | 348 | brname = branch or 'HEAD' |
| 349 | cmd += ['%s~%d..%s~%d' % (brname, start + count, brname, start)] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 350 | |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 351 | stdout = command.run_list(cmd) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 352 | files = stdout.splitlines() |
| 353 | |
| 354 | # We have an extra file if there is a cover letter |
| 355 | if series.get('cover'): |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 356 | return files[0], files[1:] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 357 | else: |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 358 | return None, files |
| 359 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 360 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 361 | def build_email_list(in_list, tag=None, alias=None, warn_on_error=True): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 362 | """Build a list of email addresses based on an input list. |
| 363 | |
| 364 | Takes a list of email addresses and aliases, and turns this into a list |
| 365 | of only email address, by resolving any aliases that are present. |
| 366 | |
| 367 | If the tag is given, then each email address is prepended with this |
| 368 | tag and a space. If the tag starts with a minus sign (indicating a |
| 369 | command line parameter) then the email address is quoted. |
| 370 | |
| 371 | Args: |
| 372 | in_list: List of aliases/email addresses |
| 373 | tag: Text to put before each address |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 374 | alias: Alias dictionary |
Simon Glass | 1f975b9 | 2021-01-23 08:56:15 -0700 | [diff] [blame] | 375 | warn_on_error: True to raise an error when an alias fails to match, |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 376 | False to just print a message. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 377 | |
| 378 | Returns: |
| 379 | List of email addresses |
| 380 | |
| 381 | >>> alias = {} |
| 382 | >>> alias['fred'] = ['f.bloggs@napier.co.nz'] |
| 383 | >>> alias['john'] = ['j.bloggs@napier.co.nz'] |
| 384 | >>> alias['mary'] = ['Mary Poppins <m.poppins@cloud.net>'] |
| 385 | >>> alias['boys'] = ['fred', ' john'] |
| 386 | >>> alias['all'] = ['fred ', 'john', ' mary '] |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 387 | >>> build_email_list(['john', 'mary'], None, alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 388 | ['j.bloggs@napier.co.nz', 'Mary Poppins <m.poppins@cloud.net>'] |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 389 | >>> build_email_list(['john', 'mary'], '--to', alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 390 | ['--to "j.bloggs@napier.co.nz"', \ |
| 391 | '--to "Mary Poppins <m.poppins@cloud.net>"'] |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 392 | >>> build_email_list(['john', 'mary'], 'Cc', alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 393 | ['Cc j.bloggs@napier.co.nz', 'Cc Mary Poppins <m.poppins@cloud.net>'] |
| 394 | """ |
| 395 | quote = '"' if tag and tag[0] == '-' else '' |
| 396 | raw = [] |
| 397 | for item in in_list: |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 398 | raw += lookup_email(item, alias, warn_on_error=warn_on_error) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 399 | result = [] |
| 400 | for item in raw: |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 401 | if item not in result: |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 402 | result.append(item) |
| 403 | if tag: |
| 404 | return ['%s %s%s%s' % (tag, quote, email, quote) for email in result] |
| 405 | return result |
| 406 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 407 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 408 | def check_suppress_cc_config(): |
Nicolas Boichat | 0da9574 | 2020-07-13 10:50:00 +0800 | [diff] [blame] | 409 | """Check if sendemail.suppresscc is configured correctly. |
| 410 | |
| 411 | Returns: |
| 412 | True if the option is configured correctly, False otherwise. |
| 413 | """ |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 414 | suppresscc = command.output_one_line( |
| 415 | 'git', 'config', 'sendemail.suppresscc', raise_on_error=False) |
Nicolas Boichat | 0da9574 | 2020-07-13 10:50:00 +0800 | [diff] [blame] | 416 | |
| 417 | # Other settings should be fine. |
| 418 | if suppresscc == 'all' or suppresscc == 'cccmd': |
| 419 | col = terminal.Color() |
| 420 | |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 421 | print((col.build(col.RED, "error") + |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 422 | ": git config sendemail.suppresscc set to %s\n" |
| 423 | % (suppresscc)) + |
| 424 | " patman needs --cc-cmd to be run to set the cc list.\n" + |
| 425 | " Please run:\n" + |
| 426 | " git config --unset sendemail.suppresscc\n" + |
| 427 | " Or read the man page:\n" + |
| 428 | " git send-email --help\n" + |
| 429 | " and set an option that runs --cc-cmd\n") |
Nicolas Boichat | 0da9574 | 2020-07-13 10:50:00 +0800 | [diff] [blame] | 430 | return False |
| 431 | |
| 432 | return True |
| 433 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 434 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 435 | def email_patches(series, cover_fname, args, dry_run, warn_on_error, cc_fname, |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 436 | self_only=False, alias=None, in_reply_to=None, thread=False, |
Maxim Cournoyer | 3ef23e9 | 2022-12-20 00:28:46 -0500 | [diff] [blame] | 437 | smtp_server=None, get_maintainer_script=None): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 438 | """Email a patch series. |
| 439 | |
| 440 | Args: |
| 441 | series: Series object containing destination info |
| 442 | cover_fname: filename of cover letter |
| 443 | args: list of filenames of patch files |
| 444 | dry_run: Just return the command that would be run |
Simon Glass | 1f975b9 | 2021-01-23 08:56:15 -0700 | [diff] [blame] | 445 | warn_on_error: True to print a warning when an alias fails to match, |
| 446 | False to ignore it. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 447 | cc_fname: Filename of Cc file for per-commit Cc |
| 448 | self_only: True to just email to yourself as a test |
Doug Anderson | 06f27ac | 2013-03-17 10:31:04 +0000 | [diff] [blame] | 449 | in_reply_to: If set we'll pass this to git as --in-reply-to. |
| 450 | Should be a message ID that this is in reply to. |
Mateusz Kulikowski | 80c2ebc | 2016-01-14 20:37:41 +0100 | [diff] [blame] | 451 | thread: True to add --thread to git send-email (make |
| 452 | all patches reply to cover-letter or first patch in series) |
Simon Glass | 8137e30 | 2018-06-19 09:56:07 -0600 | [diff] [blame] | 453 | smtp_server: SMTP server to use to send patches |
Maxim Cournoyer | 3ef23e9 | 2022-12-20 00:28:46 -0500 | [diff] [blame] | 454 | get_maintainer_script: File name of script to get maintainers emails |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 455 | |
| 456 | Returns: |
| 457 | Git command that was/would be run |
| 458 | |
Doug Anderson | 51d7321 | 2012-11-26 15:21:40 +0000 | [diff] [blame] | 459 | # For the duration of this doctest pretend that we ran patman with ./patman |
| 460 | >>> _old_argv0 = sys.argv[0] |
| 461 | >>> sys.argv[0] = './patman' |
| 462 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 463 | >>> alias = {} |
| 464 | >>> alias['fred'] = ['f.bloggs@napier.co.nz'] |
| 465 | >>> alias['john'] = ['j.bloggs@napier.co.nz'] |
| 466 | >>> alias['mary'] = ['m.poppins@cloud.net'] |
| 467 | >>> alias['boys'] = ['fred', ' john'] |
| 468 | >>> alias['all'] = ['fred ', 'john', ' mary '] |
| 469 | >>> alias[os.getenv('USER')] = ['this-is-me@me.com'] |
Simon Glass | 794c259 | 2020-06-07 06:45:47 -0600 | [diff] [blame] | 470 | >>> series = {} |
| 471 | >>> series['to'] = ['fred'] |
| 472 | >>> series['cc'] = ['mary'] |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 473 | >>> email_patches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \ |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 474 | False, alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 475 | 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \ |
Simon Glass | 1ee91c1 | 2020-11-03 13:54:10 -0700 | [diff] [blame] | 476 | "m.poppins@cloud.net" --cc-cmd "./patman send --cc-cmd cc-fname" cover p1 p2' |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 477 | >>> email_patches(series, None, ['p1'], True, True, 'cc-fname', False, \ |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 478 | alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 479 | 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \ |
Simon Glass | 1ee91c1 | 2020-11-03 13:54:10 -0700 | [diff] [blame] | 480 | "m.poppins@cloud.net" --cc-cmd "./patman send --cc-cmd cc-fname" p1' |
Simon Glass | 794c259 | 2020-06-07 06:45:47 -0600 | [diff] [blame] | 481 | >>> series['cc'] = ['all'] |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 482 | >>> email_patches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \ |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 483 | True, alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 484 | 'git send-email --annotate --to "this-is-me@me.com" --cc-cmd "./patman \ |
Simon Glass | 1ee91c1 | 2020-11-03 13:54:10 -0700 | [diff] [blame] | 485 | send --cc-cmd cc-fname" cover p1 p2' |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 486 | >>> email_patches(series, 'cover', ['p1', 'p2'], True, True, 'cc-fname', \ |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 487 | False, alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 488 | 'git send-email --annotate --to "f.bloggs@napier.co.nz" --cc \ |
| 489 | "f.bloggs@napier.co.nz" --cc "j.bloggs@napier.co.nz" --cc \ |
Simon Glass | 1ee91c1 | 2020-11-03 13:54:10 -0700 | [diff] [blame] | 490 | "m.poppins@cloud.net" --cc-cmd "./patman send --cc-cmd cc-fname" cover p1 p2' |
Doug Anderson | 51d7321 | 2012-11-26 15:21:40 +0000 | [diff] [blame] | 491 | |
| 492 | # Restore argv[0] since we clobbered it. |
| 493 | >>> sys.argv[0] = _old_argv0 |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 494 | """ |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 495 | to = build_email_list(series.get('to'), '--to', alias, warn_on_error) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 496 | if not to: |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 497 | git_config_to = command.output('git', 'config', 'sendemail.to', |
Simon Glass | c55e056 | 2016-07-25 18:59:00 -0600 | [diff] [blame] | 498 | raise_on_error=False) |
Masahiro Yamada | d91f5b9 | 2014-07-18 14:23:20 +0900 | [diff] [blame] | 499 | if not git_config_to: |
Simon Glass | 23b8a19 | 2019-05-14 15:53:36 -0600 | [diff] [blame] | 500 | print("No recipient.\n" |
| 501 | "Please add something like this to a commit\n" |
| 502 | "Series-to: Fred Bloggs <f.blogs@napier.co.nz>\n" |
| 503 | "Or do something like this\n" |
| 504 | "git config sendemail.to u-boot@lists.denx.de") |
Masahiro Yamada | d91f5b9 | 2014-07-18 14:23:20 +0900 | [diff] [blame] | 505 | return |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 506 | cc = build_email_list(list(set(series.get('cc')) - set(series.get('to'))), |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 507 | '--cc', alias, warn_on_error) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 508 | if self_only: |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 509 | to = build_email_list([os.getenv('USER')], '--to', |
| 510 | alias, warn_on_error) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 511 | cc = [] |
| 512 | cmd = ['git', 'send-email', '--annotate'] |
Simon Glass | 8137e30 | 2018-06-19 09:56:07 -0600 | [diff] [blame] | 513 | if smtp_server: |
| 514 | cmd.append('--smtp-server=%s' % smtp_server) |
Doug Anderson | 06f27ac | 2013-03-17 10:31:04 +0000 | [diff] [blame] | 515 | if in_reply_to: |
Simon Glass | 9dfb311 | 2020-11-08 20:36:18 -0700 | [diff] [blame] | 516 | cmd.append('--in-reply-to="%s"' % in_reply_to) |
Mateusz Kulikowski | 80c2ebc | 2016-01-14 20:37:41 +0100 | [diff] [blame] | 517 | if thread: |
| 518 | cmd.append('--thread') |
Doug Anderson | 06f27ac | 2013-03-17 10:31:04 +0000 | [diff] [blame] | 519 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 520 | cmd += to |
| 521 | cmd += cc |
Simon Glass | 1ee91c1 | 2020-11-03 13:54:10 -0700 | [diff] [blame] | 522 | cmd += ['--cc-cmd', '"%s send --cc-cmd %s"' % (sys.argv[0], cc_fname)] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 523 | if cover_fname: |
| 524 | cmd.append(cover_fname) |
| 525 | cmd += args |
Simon Glass | 47e308e | 2017-05-29 15:31:25 -0600 | [diff] [blame] | 526 | cmdstr = ' '.join(cmd) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 527 | if not dry_run: |
Simon Glass | 47e308e | 2017-05-29 15:31:25 -0600 | [diff] [blame] | 528 | os.system(cmdstr) |
| 529 | return cmdstr |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 530 | |
| 531 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 532 | def lookup_email(lookup_name, alias=None, warn_on_error=True, level=0): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 533 | """If an email address is an alias, look it up and return the full name |
| 534 | |
| 535 | TODO: Why not just use git's own alias feature? |
| 536 | |
| 537 | Args: |
| 538 | lookup_name: Alias or email address to look up |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 539 | alias: Dictionary containing aliases (None to use settings default) |
Simon Glass | 1f975b9 | 2021-01-23 08:56:15 -0700 | [diff] [blame] | 540 | warn_on_error: True to print a warning when an alias fails to match, |
| 541 | False to ignore it. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 542 | |
| 543 | Returns: |
| 544 | tuple: |
| 545 | list containing a list of email addresses |
| 546 | |
| 547 | Raises: |
| 548 | OSError if a recursive alias reference was found |
| 549 | ValueError if an alias was not found |
| 550 | |
| 551 | >>> alias = {} |
| 552 | >>> alias['fred'] = ['f.bloggs@napier.co.nz'] |
| 553 | >>> alias['john'] = ['j.bloggs@napier.co.nz'] |
| 554 | >>> alias['mary'] = ['m.poppins@cloud.net'] |
| 555 | >>> alias['boys'] = ['fred', ' john', 'f.bloggs@napier.co.nz'] |
| 556 | >>> alias['all'] = ['fred ', 'john', ' mary '] |
| 557 | >>> alias['loop'] = ['other', 'john', ' mary '] |
| 558 | >>> alias['other'] = ['loop', 'john', ' mary '] |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 559 | >>> lookup_email('mary', alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 560 | ['m.poppins@cloud.net'] |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 561 | >>> lookup_email('arthur.wellesley@howe.ro.uk', alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 562 | ['arthur.wellesley@howe.ro.uk'] |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 563 | >>> lookup_email('boys', alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 564 | ['f.bloggs@napier.co.nz', 'j.bloggs@napier.co.nz'] |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 565 | >>> lookup_email('all', alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 566 | ['f.bloggs@napier.co.nz', 'j.bloggs@napier.co.nz', 'm.poppins@cloud.net'] |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 567 | >>> lookup_email('odd', alias) |
Simon Glass | 1f975b9 | 2021-01-23 08:56:15 -0700 | [diff] [blame] | 568 | Alias 'odd' not found |
| 569 | [] |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 570 | >>> lookup_email('loop', alias) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 571 | Traceback (most recent call last): |
| 572 | ... |
| 573 | OSError: Recursive email alias at 'other' |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 574 | >>> lookup_email('odd', alias, warn_on_error=False) |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 575 | [] |
| 576 | >>> # In this case the loop part will effectively be ignored. |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 577 | >>> lookup_email('loop', alias, warn_on_error=False) |
Simon Glass | b0cd341 | 2014-08-28 09:43:35 -0600 | [diff] [blame] | 578 | Recursive email alias at 'other' |
| 579 | Recursive email alias at 'john' |
| 580 | Recursive email alias at 'mary' |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 581 | ['j.bloggs@napier.co.nz', 'm.poppins@cloud.net'] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 582 | """ |
| 583 | if not alias: |
| 584 | alias = settings.alias |
| 585 | lookup_name = lookup_name.strip() |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 586 | if '@' in lookup_name: # Perhaps a real email address |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 587 | return [lookup_name] |
| 588 | |
| 589 | lookup_name = lookup_name.lower() |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 590 | col = terminal.Color() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 591 | |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 592 | out_list = [] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 593 | if level > 10: |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 594 | msg = "Recursive email alias at '%s'" % lookup_name |
Simon Glass | 1f975b9 | 2021-01-23 08:56:15 -0700 | [diff] [blame] | 595 | if warn_on_error: |
Paul Burton | f14a131 | 2016-09-27 16:03:51 +0100 | [diff] [blame] | 596 | raise OSError(msg) |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 597 | else: |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 598 | print(col.build(col.RED, msg)) |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 599 | return out_list |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 600 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 601 | if lookup_name: |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 602 | if lookup_name not in alias: |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 603 | msg = "Alias '%s' not found" % lookup_name |
Simon Glass | 1f975b9 | 2021-01-23 08:56:15 -0700 | [diff] [blame] | 604 | if warn_on_error: |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 605 | print(col.build(col.RED, msg)) |
Simon Glass | 1f975b9 | 2021-01-23 08:56:15 -0700 | [diff] [blame] | 606 | return out_list |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 607 | for item in alias[lookup_name]: |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 608 | todo = lookup_email(item, alias, warn_on_error, level + 1) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 609 | for new_item in todo: |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 610 | if new_item not in out_list: |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 611 | out_list.append(new_item) |
| 612 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 613 | return out_list |
| 614 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 615 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 616 | def get_top_level(): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 617 | """Return name of top-level directory for this git repo. |
| 618 | |
| 619 | Returns: |
| 620 | Full path to git top-level directory |
| 621 | |
| 622 | This test makes sure that we are running tests in the right subdir |
| 623 | |
Doug Anderson | 51d7321 | 2012-11-26 15:21:40 +0000 | [diff] [blame] | 624 | >>> os.path.realpath(os.path.dirname(__file__)) == \ |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 625 | os.path.join(get_top_level(), 'tools', 'patman') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 626 | True |
| 627 | """ |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 628 | return command.output_one_line('git', 'rev-parse', '--show-toplevel') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 629 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 630 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 631 | def get_alias_file(): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 632 | """Gets the name of the git alias file. |
| 633 | |
| 634 | Returns: |
| 635 | Filename of git alias file, or None if none |
| 636 | """ |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 637 | fname = command.output_one_line('git', 'config', 'sendemail.aliasesfile', |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 638 | raise_on_error=False) |
Brian Norris | 94c775d | 2022-01-07 15:15:55 -0800 | [diff] [blame] | 639 | if not fname: |
| 640 | return None |
| 641 | |
| 642 | fname = os.path.expanduser(fname.strip()) |
| 643 | if os.path.isabs(fname): |
| 644 | return fname |
| 645 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 646 | return os.path.join(get_top_level(), fname) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 647 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 648 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 649 | def get_default_user_name(): |
Vikram Narayanan | 12fb29a | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 650 | """Gets the user.name from .gitconfig file. |
| 651 | |
| 652 | Returns: |
| 653 | User name found in .gitconfig file, or None if none |
| 654 | """ |
Fei Shao | 7871223 | 2023-09-08 22:15:59 +0800 | [diff] [blame] | 655 | uname = command.output_one_line('git', 'config', '--global', '--includes', 'user.name') |
Vikram Narayanan | 12fb29a | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 656 | return uname |
| 657 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 658 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 659 | def get_default_user_email(): |
Vikram Narayanan | 12fb29a | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 660 | """Gets the user.email from the global .gitconfig file. |
| 661 | |
| 662 | Returns: |
| 663 | User's email found in .gitconfig file, or None if none |
| 664 | """ |
Fei Shao | 7871223 | 2023-09-08 22:15:59 +0800 | [diff] [blame] | 665 | uemail = command.output_one_line('git', 'config', '--global', '--includes', 'user.email') |
Vikram Narayanan | 12fb29a | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 666 | return uemail |
| 667 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 668 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 669 | def get_default_subject_prefix(): |
Wu, Josh | 9873b91 | 2015-04-15 10:25:18 +0800 | [diff] [blame] | 670 | """Gets the format.subjectprefix from local .git/config file. |
| 671 | |
| 672 | Returns: |
| 673 | Subject prefix found in local .git/config file, or None if none |
| 674 | """ |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 675 | sub_prefix = command.output_one_line( |
| 676 | 'git', 'config', 'format.subjectprefix', raise_on_error=False) |
Wu, Josh | 9873b91 | 2015-04-15 10:25:18 +0800 | [diff] [blame] | 677 | |
| 678 | return sub_prefix |
| 679 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 680 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 681 | def setup(): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 682 | """Set up git utils, by reading the alias files.""" |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 683 | # Check for a git alias file also |
Simon Glass | 81bcca8 | 2014-08-28 09:43:45 -0600 | [diff] [blame] | 684 | global use_no_decorate |
| 685 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 686 | alias_fname = get_alias_file() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 687 | if alias_fname: |
| 688 | settings.ReadGitAliases(alias_fname) |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 689 | cmd = log_cmd(None, count=0) |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 690 | use_no_decorate = (command.run_pipe([cmd], raise_on_error=False) |
Simon Glass | 6af913d | 2014-08-09 15:33:11 -0600 | [diff] [blame] | 691 | .return_code == 0) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 692 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 693 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 694 | def get_head(): |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 695 | """Get the hash of the current HEAD |
| 696 | |
| 697 | Returns: |
| 698 | Hash of HEAD |
| 699 | """ |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 700 | return command.output_one_line('git', 'show', '-s', '--pretty=format:%H') |
Simon Glass | 11aba51 | 2012-12-15 10:42:07 +0000 | [diff] [blame] | 701 | |
Maxim Cournoyer | 9a196fb | 2022-12-19 17:32:38 -0500 | [diff] [blame] | 702 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 703 | if __name__ == "__main__": |
| 704 | import doctest |
| 705 | |
| 706 | doctest.testmod() |