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