[Concept,05/29] patman: Use gitutil helpers in review.py

Message ID 20260501110040.1874719-6-sjg@u-boot.org
State New
Headers
Series patman: Review-flow improvements and shared helpers |

Commit Message

Simon Glass May 1, 2026, 10:59 a.m. UTC
  From: Simon Glass <sjg@chromium.org>

review.py runs a handful of git commands directly through
command.output() and command.run_one(): a 'diff --stat' for the
review prompt, checkout/stash/stash-pop for branch-and-stash
bookkeeping around an apply, a 'rev-parse --verify' to choose
between the upstream's '/next' and '/master' branches, and
'rev-parse --abbrev-ref HEAD' to remember the current branch.

The helpers added in 'u_boot_pylib: Add gitutil helpers for
repo-aware git operations' cover all of these. Switch each call
site to the corresponding helper:

- diff_stat() for the review-prompt diffstat
- checkout_branch() and stash_pop() for the cleanup path
- ref_exists() for the upstream-branch probe
- current_branch() and stash_save(include_untracked=True) for the
  pre-apply bookkeeping

review.py no longer assembles 'git ...' argv lists itself.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 tools/patman/review.py | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
  

Patch

diff --git a/tools/patman/review.py b/tools/patman/review.py
index f0fa4e865e4..09614c85ed7 100644
--- a/tools/patman/review.py
+++ b/tools/patman/review.py
@@ -983,9 +983,8 @@  async def _review_single_patch(ctx, cmt, seq, all_commits):
         commit_msg = body
     else:
         commit_msg = (cmt.subject + '\n' + body).strip()
-    ctx.diffstat = command.output('git', 'diff', '--stat',
-                                  f'{cmt.hash}~..{cmt.hash}',
-                                  cwd=ctx.repo_path).strip()
+    ctx.diffstat = gitutil.diff_stat(f'{cmt.hash}~..{cmt.hash}',
+                                     ctx.repo_path).strip()
 
     previous_review = ctx.previous_reviews.get(seq)
     prompt = _build_review_prompt(ctx, cmt.hash, seq, all_commits,
@@ -1134,9 +1133,9 @@  def _git_restore(orig_branch, had_stash, repo_path):
 
     try:
         if orig_branch and repo_path:
-            command.output('git', 'checkout', orig_branch, cwd=repo_path)
+            gitutil.checkout_branch(orig_branch, repo_path)
         if had_stash:
-            command.output('git', 'stash', 'pop', cwd=repo_path)
+            gitutil.stash_pop(repo_path)
     except command.CommandExc:
         pass
 
@@ -1582,9 +1581,7 @@  def _get_upstream_branch(args, cser):
         ups = cser.db.upstream_get_default()
     if ups:
         branch = f'{ups}/next'
-        ret = command.run_one('git', 'rev-parse', '--verify', branch,
-            capture=True, raise_on_error=False)
-        if ret.return_code:
+        if not gitutil.ref_exists(branch):
             branch = f'{ups}/master'
         return branch
     return 'origin/master'
@@ -1607,9 +1604,9 @@  def _apply_and_check(ctx, link):
         ctx.upstream_branch, repo_path)
 
     if success:
-        applied = command.output('git', 'rev-list', '--count',
-            f'{ctx.upstream_branch}..{branch_name}', cwd=repo_path).strip()
-        if int(applied) == 0:
+        applied = gitutil.count_revs(
+            repo_path, f'{ctx.upstream_branch}..{branch_name}')
+        if not applied:
             success = False
 
     if not success:
@@ -1774,10 +1771,9 @@  def do_review(args, pwork, cser):
     try:
         ctx.upstream_branch = _get_upstream_branch(args, cser)
         ctx.repo_path = gitutil.get_top_level()
-        orig_branch = command.output('git', 'rev-parse', '--abbrev-ref',
-                                      'HEAD', cwd=ctx.repo_path).strip()
+        orig_branch = gitutil.current_branch(ctx.repo_path)
         try:
-            stash_out = command.output('git', 'stash', cwd=ctx.repo_path)
+            stash_out = gitutil.stash_save(ctx.repo_path)
             if 'No local changes' not in stash_out:
                 had_stash = True
         except command.CommandExc: