git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/2] git-p4: fix error handling in P4Unshelve.renameBranch()

From
MGMoritz Baumann via GitGitGadget <gitgitgadget@gmail.com>
Date
Jul 20, 2022, 18:55 UTC
Message-ID
<f7566dd5fc36fd3ae648abf7054fc0e1269b6874.1658343330.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1297.v2.git.git.1658343330.gitgitgadget@gmail.com>
From: Moritz Baumann <moritz.baumann@sap.com>

The error handling code path is meant to be triggered when the loop does not exit early via "break". This fails, as the boolean variable "found", which is used to track whether the loop was exited early, is initialized incorrectly.

It would be possible to fix this issue by correcting the initialization, but Python supports a for:-else: control flow construct for this exact use case (executing code if a loop does not exit early), so it is more idiomatic to remove the tracking variable entirely.

In addition, the error message no longer refers to a variable that does not exist.

Signed-off-by: Moritz Baumann <moritz.baumann@sap.com>
---
 git-p4.py | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/git-p4.py b/git-p4.py
index 1de3d6f1cd4..8f20d15f272 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -4369,19 +4369,16 @@ class P4Unshelve(Command):
     def renameBranch(self, branch_name):
         """Rename the existing branch to branch_name.N ."""
 
-        found = True
         for i in range(0, 1000):
             backup_branch_name = "{0}.{1}".format(branch_name, i)
             if not gitBranchExists(backup_branch_name):
                 # Copy ref to backup
                 gitUpdateRef(backup_branch_name, branch_name)
                 gitDeleteRef(branch_name)
-                found = True
                 print("renamed old unshelve branch to {0}".format(backup_branch_name))
                 break
-
-        if not found:
-            sys.exit("gave up trying to rename existing branch {0}".format(sync.branch))
+        else:
+            sys.exit("gave up trying to rename existing branch {0}".format(branch_name))
 
     def findLastP4Revision(self, starting_point):
         """Look back from starting_point for the first commit created by git-p4
-- 
gitgitgadget
Previous: Moritz Baumann via GitGitGadgetNext: Junio C Hamano
Message 10 of 11 in “git-p4: fix two undefined variables”
  1. 0/2 git-p4: fix two undefined variablesMoritz Baumann via GitGitGadget, Jul 20, 2022
  2. 1/2 git-p4: fix typo in P4Submit.applyCommit()Moritz Baumann via GitGitGadget, Jul 20, 2022
  3. Junio C HamanoJul 20, 2022
  4. 2/2 git-p4: fix error handling in P4Unshelve.renameBranch()Moritz Baumann via GitGitGadget, Jul 20, 2022
  5. Junio C HamanoJul 20, 2022
  6. Junio C HamanoJul 20, 2022
  7. Baumann, MoritzJul 20, 2022
  8. 0/2 git-p4: fix two undefined variablesMoritz Baumann via GitGitGadget, Jul 20, 2022
  9. 1/2 git-p4: fix typo in P4Submit.applyCommit()Moritz Baumann via GitGitGadget, Jul 20, 2022
  10. 2/2 git-p4: fix error handling in P4Unshelve.renameBranch()Moritz Baumann via GitGitGadget, Jul 20, 2022
  11. Junio C HamanoJul 20, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.