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

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

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 20, 2022, 16:18 UTC
Message-ID
<xmqqa6934v2c.fsf@gitster.g>
In-Reply-To
<69c9fd5fbec859c2cced95930ac4d427a09aee90.1658298900.git.gitgitgadget@gmail.com>
"Moritz Baumann via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Moritz Baumann <moritz.baumann@sap.com>
>
> The error handling code referenced a variable that does not exist.
> Also, the condition could never evaluate to True.
Show 15 quoted lines
>
> 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
This has to be initialized to False, because ...
Show 8 quoted lines
>          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))

... we flip it to True when we find an available unused name and break out ...

>                  break
> -
> -        if not found:
> -            sys.exit("gave up trying to rename existing branch {0}".format(sync.branch))
... so that we can complain when we didn't find anything usable.

So a minimum fix would be to initialize found correctly, and rewriting the logic to use "for ... else" is an unrelated style change. The version using "for ... else" may be more idiomatic Python, and I do not think people would mind it, but it should be mentioned in the proposed log mesage, perhaps like:

    The code tries to see if there is an available name by setting
    the variable 'found' to true when it finds one and breaks out of
    the loop, but because the variable is incorrectly initialized to
    true (it should be initialized to false), the code after the
    loop cannot tell if it found an available name or not.
    It would be the minimal fix to initialize the variable to false,
    but in modern Python it is more idiomatic to add else: clause
    after a loop to write what happens when the loop did not break
    out, so let's do that instead.
    Also, fix the error message that refers to a wrong variable
    name.
or something.
Thanks.  Will queue.
Show 5 quoted lines
> +        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
Previous: Moritz Baumann via GitGitGadgetNext: Junio C Hamano
Message 5 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.