From: Simon Glass <sjg@chromium.org>
Date: Fri, 30 Oct 2020 03:46:34 +0000 (-0600)
Subject: patman: Detect missing upstream in CountCommitsToBranch
X-Git-Tag: v2025.01-rc5-pxa1908~2136^2~4
X-Git-Url: http://git.dujemihanovic.xyz/img/html/static/%7B%7B%20%28.OutputFormats.Get?a=commitdiff_plain;h=be051c0c7741d67f5093f6b61b64c45eb200235b;p=u-boot.git

patman: Detect missing upstream in CountCommitsToBranch

At present if we fail to find the upstream then the error output is piped
to wc, resulting in bogus results. Avoid the pipe and check the output
directly.

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

diff --git a/tools/patman/func_test.py b/tools/patman/func_test.py
index b39e3f671d..cce3905c09 100644
--- a/tools/patman/func_test.py
+++ b/tools/patman/func_test.py
@@ -588,3 +588,22 @@ diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c
         self.assertEqual(
             ["Found possible blank line(s) at end of file 'lib/fdtdec.c'"],
             pstrm.commit.warn)
+
+    @unittest.skipIf(not HAVE_PYGIT2, 'Missing python3-pygit2')
+    def testNoUpstream(self):
+        """Test CountCommitsToBranch when there is no upstream"""
+        repo = self.make_git_tree()
+        target = repo.lookup_reference('refs/heads/base')
+        self.repo.checkout(target, strategy=pygit2.GIT_CHECKOUT_FORCE)
+
+        # Check that it can detect the current branch
+        try:
+            orig_dir = os.getcwd()
+            os.chdir(self.gitdir)
+            with self.assertRaises(ValueError) as exc:
+                gitutil.CountCommitsToBranch(None)
+            self.assertIn(
+                "Failed to determine upstream: fatal: no upstream configured for branch 'base'",
+                str(exc.exception))
+        finally:
+            os.chdir(orig_dir)
diff --git a/tools/patman/gitutil.py b/tools/patman/gitutil.py
index 27a0a9fbc1..3a2366bcf5 100644
--- a/tools/patman/gitutil.py
+++ b/tools/patman/gitutil.py
@@ -66,9 +66,13 @@ def CountCommitsToBranch(branch):
         rev_range = '%s..%s' % (us, branch)
     else:
         rev_range = '@{upstream}..'
-    pipe = [LogCmd(rev_range, oneline=True), ['wc', '-l']]
-    stdout = command.RunPipe(pipe, capture=True, oneline=True).stdout
-    patch_count = int(stdout)
+    pipe = [LogCmd(rev_range, oneline=True)]
+    result = command.RunPipe(pipe, capture=True, capture_stderr=True,
+                             oneline=True, raise_on_error=False)
+    if result.return_code:
+        raise ValueError('Failed to determine upstream: %s' %
+                         result.stderr.strip())
+    patch_count = len(result.stdout.splitlines())
     return patch_count
 
 def NameRevision(commit_hash):