diff mbox series

[v2,26/30] patman: Detect missing upstream in CountCommitsToBranch

Message ID 20201026010442.1606893-27-sjg@chromium.org
State Not Applicable
Headers show
Series [v2,01/30] patman: Correct operation of -n | expand

Commit Message

Simon Glass Oct. 26, 2020, 1:04 a.m. UTC
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>
---

(no changes since v1)

 tools/patman/func_test.py | 19 +++++++++++++++++++
 tools/patman/gitutil.py   | 10 +++++++---
 2 files changed, 26 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/tools/patman/func_test.py b/tools/patman/func_test.py
index 702ec407b7f..56181f8bbaa 100644
--- a/tools/patman/func_test.py
+++ b/tools/patman/func_test.py
@@ -610,3 +610,22 @@  Series-changes: 2
         self.assertEqual(
             ["Tag 'Series-changes' should be before sign-off / Change-Id"],
             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 27a0a9fbc1f..3a2366bcf59 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):