[PATCH 02/20] u_boot_pylib: Provide directories to gitutil functions
Simon Glass
sjg at chromium.org
Thu May 8 09:28:26 CEST 2025
For testing it is useful to be able to set the current directory used
for git operations, as well as the git-repo directory. Update some of
the functions to support this.
Signed-off-by: Simon Glass <sjg at chromium.org>
---
tools/u_boot_pylib/command.py | 5 ++--
tools/u_boot_pylib/gitutil.py | 49 +++++++++++++++++++++++++----------
2 files changed, 38 insertions(+), 16 deletions(-)
diff --git a/tools/u_boot_pylib/command.py b/tools/u_boot_pylib/command.py
index 0e247355ef6..cb7ebf49ce5 100644
--- a/tools/u_boot_pylib/command.py
+++ b/tools/u_boot_pylib/command.py
@@ -203,7 +203,7 @@ def run_one(*cmd, **kwargs):
return run_pipe([cmd], **kwargs)
-def run_list(cmd):
+def run_list(cmd, **kwargs):
"""Run a command and return its output
Args:
@@ -211,8 +211,9 @@ def run_list(cmd):
Returns:
str: output of command
+ **kwargs (dict of args): Extra arguments to pass in
"""
- return run_pipe([cmd], capture=True).stdout
+ return run_pipe([cmd], capture=True, **kwargs).stdout
def stop_all():
diff --git a/tools/u_boot_pylib/gitutil.py b/tools/u_boot_pylib/gitutil.py
index e5a466c8cc6..f89576ef3b9 100644
--- a/tools/u_boot_pylib/gitutil.py
+++ b/tools/u_boot_pylib/gitutil.py
@@ -47,7 +47,7 @@ def log_cmd(commit_range, git_dir=None, oneline=False, reverse=False,
return cmd
-def count_commits_to_branch(branch):
+def count_commits_to_branch(branch, git_dir=None, end=None):
"""Returns number of commits between HEAD and the tracking branch.
This looks back to the tracking branch and works out the number of commits
@@ -55,16 +55,22 @@ def count_commits_to_branch(branch):
Args:
branch (str or None): Branch to count from (None for current branch)
+ git_dir (str): Path to git repository (None to use default)
+ end (str): End commit to stop before
Return:
Number of patches that exist on top of the branch
"""
- if branch:
- us, _ = get_upstream('.git', branch)
+ if end:
+ rev_range = f'{end}..{branch}'
+ elif branch:
+ us, msg = get_upstream(git_dir or '.git', branch)
+ if not us:
+ raise ValueError(msg)
rev_range = f'{us}..{branch}'
else:
rev_range = '@{upstream}..'
- cmd = log_cmd(rev_range, oneline=True)
+ cmd = log_cmd(rev_range, git_dir=git_dir, oneline=True)
result = command.run_one(*cmd, capture=True, capture_stderr=True,
oneline=True, raise_on_error=False)
if result.return_code:
@@ -321,7 +327,8 @@ def prune_worktrees(git_dir):
raise OSError(f'git worktree prune: {result.stderr}')
-def create_patches(branch, start, count, ignore_binary, series, signoff=True):
+def create_patches(branch, start, count, ignore_binary, series, signoff=True,
+ git_dir=None, cwd=None):
"""Create a series of patches from the top of the current branch.
The patch files are written to the current directory using
@@ -334,11 +341,16 @@ def create_patches(branch, start, count, ignore_binary, series, signoff=True):
ignore_binary (bool): Don't generate patches for binary files
series (Series): Series object for this series (set of patches)
signoff (bool): True to add signoff lines automatically
+ git_dir (str): Path to git repository (None to use default)
+ cwd (str): Path to use for git operations
Return:
Filename of cover letter (None if none)
List of filenames of patch files
"""
- cmd = ['git', 'format-patch', '-M']
+ cmd = ['git']
+ if git_dir:
+ cmd += ['--git-dir', git_dir]
+ cmd += ['format-patch', '-M']
if signoff:
cmd.append('--signoff')
if ignore_binary:
@@ -351,7 +363,7 @@ def create_patches(branch, start, count, ignore_binary, series, signoff=True):
brname = branch or 'HEAD'
cmd += [f'{brname}~{start + count}..{brname}~{start}']
- stdout = command.run_list(cmd)
+ stdout = command.run_list(cmd, cwd=cwd)
files = stdout.splitlines()
# We have an extra file if there is a cover letter
@@ -436,7 +448,7 @@ def check_suppress_cc_config():
def email_patches(series, cover_fname, args, dry_run, warn_on_error, cc_fname,
alias, self_only=False, in_reply_to=None, thread=False,
- smtp_server=None):
+ smtp_server=None, cwd=None):
"""Email a patch series.
Args:
@@ -456,6 +468,7 @@ def email_patches(series, cover_fname, args, dry_run, warn_on_error, cc_fname,
thread (bool): True to add --thread to git send-email (make
all patches reply to cover-letter or first patch in series)
smtp_server (str or None): SMTP server to use to send patches
+ cwd (str): Path to use for patch files (None to use current dir)
Returns:
Git command that was/would be run
@@ -528,7 +541,7 @@ send --cc-cmd cc-fname" cover p1 p2'
cmd.append(cover_fname)
cmd += args
if not dry_run:
- command.run(*cmd, capture=False, capture_stderr=False)
+ command.run(*cmd, capture=False, capture_stderr=False, cwd=cwd)
cmdstr = ' '.join([f'"{x}"' if ' ' in x and not '"' in x else x
for x in cmd])
return cmdstr
@@ -695,7 +708,7 @@ def setup():
.return_code == 0)
-def get_hash(spec):
+def get_hash(spec, git_dir=None):
"""Get the hash of a commit
Args:
@@ -704,8 +717,11 @@ def get_hash(spec):
Returns:
str: Hash of commit
"""
- return command.output_one_line('git', 'show', '-s', '--pretty=format:%H',
- spec)
+ cmd = ['git']
+ if git_dir:
+ cmd += ['--git-dir', git_dir]
+ cmd += ['show', '-s', '--pretty=format:%H', spec]
+ return command.output_one_line(*cmd)
def get_head():
@@ -717,13 +733,18 @@ def get_head():
return get_hash('HEAD')
-def get_branch():
+def get_branch(git_dir=None):
"""Get the branch we are currently on
Return:
str: branch name, or None if none
+ git_dir (str): Path to git repository (None to use default)
"""
- out = command.output_one_line('git', 'rev-parse', '--abbrev-ref', 'HEAD')
+ cmd = ['git']
+ if git_dir:
+ cmd += ['--git-dir', git_dir]
+ cmd += ['rev-parse', '--abbrev-ref', 'HEAD']
+ out = command.output_one_line(*cmd, raise_on_error=False)
if out == 'HEAD':
return None
return out
--
2.43.0
More information about the U-Boot
mailing list