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