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