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