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