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