git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] git-p4: fix two undefined variables
@ 2022-07-20  6:34 Moritz Baumann via GitGitGadget
  2022-07-20  6:34 ` [PATCH 1/2] git-p4: fix typo in P4Submit.applyCommit() Moritz Baumann via GitGitGadget
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Moritz Baumann via GitGitGadget @ 2022-07-20  6:34 UTC (permalink / raw)
  To: git; +Cc: Luke Diamand, Moritz Baumann

Moritz Baumann (2):
  git-p4: fix typo in P4Submit.applyCommit()
  git-p4: fix error handling in P4Unshelve.renameBranch()

 git-p4.py | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)


base-commit: bbea4dcf42b28eb7ce64a6306cdde875ae5d09ca
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1297%2Fmbs-c%2Ffix-undefined-variables-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1297/mbs-c/fix-undefined-variables-v1
Pull-Request: https://github.com/git/git/pull/1297
-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/2] git-p4: fix typo in P4Submit.applyCommit()
  2022-07-20  6:34 [PATCH 0/2] git-p4: fix two undefined variables Moritz Baumann via GitGitGadget
@ 2022-07-20  6:34 ` Moritz Baumann via GitGitGadget
  2022-07-20 16:09   ` Junio C Hamano
  2022-07-20  6:34 ` [PATCH 2/2] git-p4: fix error handling in P4Unshelve.renameBranch() Moritz Baumann via GitGitGadget
  2022-07-20 18:55 ` [PATCH v2 0/2] git-p4: fix two undefined variables Moritz Baumann via GitGitGadget
  2 siblings, 1 reply; 11+ messages in thread
From: Moritz Baumann via GitGitGadget @ 2022-07-20  6:34 UTC (permalink / raw)
  To: git; +Cc: Luke Diamand, Moritz Baumann, Moritz Baumann

From: Moritz Baumann <moritz.baumann@sap.com>

Signed-off-by: Moritz Baumann <moritz.baumann@sap.com>
---
 git-p4.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/git-p4.py b/git-p4.py
index 8fbf6eb1fe3..1de3d6f1cd4 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -2226,7 +2226,7 @@ class P4Submit(Command, P4UserMap):
                                 raw=True):
                             if regexp.search(line):
                                 if verbose:
-                                    print("got keyword match on %s in %s in %s" % (regex.pattern, line, file))
+                                    print("got keyword match on %s in %s in %s" % (regexp.pattern, line, file))
                                 kwfiles[file] = regexp
                                 break
 
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 2/2] git-p4: fix error handling in P4Unshelve.renameBranch()
  2022-07-20  6:34 [PATCH 0/2] git-p4: fix two undefined variables Moritz Baumann via GitGitGadget
  2022-07-20  6:34 ` [PATCH 1/2] git-p4: fix typo in P4Submit.applyCommit() Moritz Baumann via GitGitGadget
@ 2022-07-20  6:34 ` Moritz Baumann via GitGitGadget
  2022-07-20 16:18   ` Junio C Hamano
  2022-07-20 18:55 ` [PATCH v2 0/2] git-p4: fix two undefined variables Moritz Baumann via GitGitGadget
  2 siblings, 1 reply; 11+ messages in thread
From: Moritz Baumann via GitGitGadget @ 2022-07-20  6:34 UTC (permalink / raw)
  To: git; +Cc: Luke Diamand, Moritz Baumann, Moritz Baumann

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.

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

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/2] git-p4: fix typo in P4Submit.applyCommit()
  2022-07-20  6:34 ` [PATCH 1/2] git-p4: fix typo in P4Submit.applyCommit() Moritz Baumann via GitGitGadget
@ 2022-07-20 16:09   ` Junio C Hamano
  0 siblings, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2022-07-20 16:09 UTC (permalink / raw)
  To: Moritz Baumann via GitGitGadget; +Cc: git, Luke Diamand, Moritz Baumann

"Moritz Baumann via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Moritz Baumann <moritz.baumann@sap.com>
>
> Signed-off-by: Moritz Baumann <moritz.baumann@sap.com>
> ---
>  git-p4.py | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/git-p4.py b/git-p4.py
> index 8fbf6eb1fe3..1de3d6f1cd4 100755
> --- a/git-p4.py
> +++ b/git-p4.py
> @@ -2226,7 +2226,7 @@ class P4Submit(Command, P4UserMap):
>                                  raw=True):
>                              if regexp.search(line):
>                                  if verbose:
> -                                    print("got keyword match on %s in %s in %s" % (regex.pattern, line, file))
> +                                    print("got keyword match on %s in %s in %s" % (regexp.pattern, line, file))

OK.  That's an obvious fix.

>                                  kwfiles[file] = regexp
>                                  break

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] git-p4: fix error handling in P4Unshelve.renameBranch()
  2022-07-20  6:34 ` [PATCH 2/2] git-p4: fix error handling in P4Unshelve.renameBranch() Moritz Baumann via GitGitGadget
@ 2022-07-20 16:18   ` Junio C Hamano
  2022-07-20 16:21     ` Junio C Hamano
  0 siblings, 1 reply; 11+ messages in thread
From: Junio C Hamano @ 2022-07-20 16:18 UTC (permalink / raw)
  To: Moritz Baumann via GitGitGadget; +Cc: git, Luke Diamand, Moritz Baumann

"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.



>
> 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 ...

>          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.

> +        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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] git-p4: fix error handling in P4Unshelve.renameBranch()
  2022-07-20 16:18   ` Junio C Hamano
@ 2022-07-20 16:21     ` Junio C Hamano
  2022-07-20 17:01       ` Baumann, Moritz
  0 siblings, 1 reply; 11+ messages in thread
From: Junio C Hamano @ 2022-07-20 16:21 UTC (permalink / raw)
  To: Moritz Baumann via GitGitGadget; +Cc: git, Luke Diamand, Moritz Baumann

Junio C Hamano <gitster@pobox.com> writes:

>>  
>> -        found = True
>
> This has to be initialized to False, because ...
>
>>          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.

By the way, isn't this a typical time-of-check to time-of-use bug?
Not the problem with the fix in the patch but in the original, but
regardless of whose fault it is, it may be good to fix it (outside
the topic of this patch).

Thanks.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [PATCH 2/2] git-p4: fix error handling in P4Unshelve.renameBranch()
  2022-07-20 16:21     ` Junio C Hamano
@ 2022-07-20 17:01       ` Baumann, Moritz
  0 siblings, 0 replies; 11+ messages in thread
From: Baumann, Moritz @ 2022-07-20 17:01 UTC (permalink / raw)
  To: Junio C Hamano, Moritz Baumann via GitGitGadget; +Cc: git, Luke Diamand

Thank you for your criticism, I will keep it in mind and submit more detailed descriptions in the future.

> By the way, isn't this a typical time-of-check to time-of-use bug?
> Not the problem with the fix in the patch but in the original, but regardless of
> whose fault it is, it may be good to fix it (outside the topic of this patch).

Is concurrent use even meant to be supported in general? I have not done a thorough review, but judging from what I have seen so far, I highly doubt that the majority of git-p4.py was written with potential concurrency problems in mind.

Best regards,
Moritz

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 0/2] git-p4: fix two undefined variables
  2022-07-20  6:34 [PATCH 0/2] git-p4: fix two undefined variables Moritz Baumann via GitGitGadget
  2022-07-20  6:34 ` [PATCH 1/2] git-p4: fix typo in P4Submit.applyCommit() Moritz Baumann via GitGitGadget
  2022-07-20  6:34 ` [PATCH 2/2] git-p4: fix error handling in P4Unshelve.renameBranch() Moritz Baumann via GitGitGadget
@ 2022-07-20 18:55 ` Moritz Baumann via GitGitGadget
  2022-07-20 18:55   ` [PATCH v2 1/2] git-p4: fix typo in P4Submit.applyCommit() Moritz Baumann via GitGitGadget
                     ` (2 more replies)
  2 siblings, 3 replies; 11+ messages in thread
From: Moritz Baumann via GitGitGadget @ 2022-07-20 18:55 UTC (permalink / raw)
  To: git; +Cc: Luke Diamand, Moritz Baumann

Moritz Baumann (2):
  git-p4: fix typo in P4Submit.applyCommit()
  git-p4: fix error handling in P4Unshelve.renameBranch()

 git-p4.py | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)


base-commit: bbea4dcf42b28eb7ce64a6306cdde875ae5d09ca
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1297%2Fmbs-c%2Ffix-undefined-variables-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1297/mbs-c/fix-undefined-variables-v2
Pull-Request: https://github.com/git/git/pull/1297

Range-diff vs v1:

 1:  1484eee8961 = 1:  1484eee8961 git-p4: fix typo in P4Submit.applyCommit()
 2:  69c9fd5fbec ! 2:  f7566dd5fc3 git-p4: fix error handling in P4Unshelve.renameBranch()
     @@ Metadata
       ## Commit message ##
          git-p4: fix error handling in P4Unshelve.renameBranch()
      
     -    The error handling code referenced a variable that does not exist.
     -    Also, the condition could never evaluate to True.
     +    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>
      

-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 1/2] git-p4: fix typo in P4Submit.applyCommit()
  2022-07-20 18:55 ` [PATCH v2 0/2] git-p4: fix two undefined variables Moritz Baumann via GitGitGadget
@ 2022-07-20 18:55   ` Moritz Baumann via GitGitGadget
  2022-07-20 18:55   ` [PATCH v2 2/2] git-p4: fix error handling in P4Unshelve.renameBranch() Moritz Baumann via GitGitGadget
  2022-07-20 20:54   ` [PATCH v2 0/2] git-p4: fix two undefined variables Junio C Hamano
  2 siblings, 0 replies; 11+ messages in thread
From: Moritz Baumann via GitGitGadget @ 2022-07-20 18:55 UTC (permalink / raw)
  To: git; +Cc: Luke Diamand, Moritz Baumann, Moritz Baumann

From: Moritz Baumann <moritz.baumann@sap.com>

Signed-off-by: Moritz Baumann <moritz.baumann@sap.com>
---
 git-p4.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/git-p4.py b/git-p4.py
index 8fbf6eb1fe3..1de3d6f1cd4 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -2226,7 +2226,7 @@ class P4Submit(Command, P4UserMap):
                                 raw=True):
                             if regexp.search(line):
                                 if verbose:
-                                    print("got keyword match on %s in %s in %s" % (regex.pattern, line, file))
+                                    print("got keyword match on %s in %s in %s" % (regexp.pattern, line, file))
                                 kwfiles[file] = regexp
                                 break
 
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 2/2] git-p4: fix error handling in P4Unshelve.renameBranch()
  2022-07-20 18:55 ` [PATCH v2 0/2] git-p4: fix two undefined variables Moritz Baumann via GitGitGadget
  2022-07-20 18:55   ` [PATCH v2 1/2] git-p4: fix typo in P4Submit.applyCommit() Moritz Baumann via GitGitGadget
@ 2022-07-20 18:55   ` Moritz Baumann via GitGitGadget
  2022-07-20 20:54   ` [PATCH v2 0/2] git-p4: fix two undefined variables Junio C Hamano
  2 siblings, 0 replies; 11+ messages in thread
From: Moritz Baumann via GitGitGadget @ 2022-07-20 18:55 UTC (permalink / raw)
  To: git; +Cc: Luke Diamand, Moritz Baumann, Moritz Baumann

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

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 0/2] git-p4: fix two undefined variables
  2022-07-20 18:55 ` [PATCH v2 0/2] git-p4: fix two undefined variables Moritz Baumann via GitGitGadget
  2022-07-20 18:55   ` [PATCH v2 1/2] git-p4: fix typo in P4Submit.applyCommit() Moritz Baumann via GitGitGadget
  2022-07-20 18:55   ` [PATCH v2 2/2] git-p4: fix error handling in P4Unshelve.renameBranch() Moritz Baumann via GitGitGadget
@ 2022-07-20 20:54   ` Junio C Hamano
  2 siblings, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2022-07-20 20:54 UTC (permalink / raw)
  To: Moritz Baumann via GitGitGadget; +Cc: git, Luke Diamand, Moritz Baumann

"Moritz Baumann via GitGitGadget" <gitgitgadget@gmail.com> writes:

> Moritz Baumann (2):
>   git-p4: fix typo in P4Submit.applyCommit()
>   git-p4: fix error handling in P4Unshelve.renameBranch()
>
>  git-p4.py | 9 +++------
>  1 file changed, 3 insertions(+), 6 deletions(-)

Perfect.  Thanks.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2022-07-20 20:54 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-20  6:34 [PATCH 0/2] git-p4: fix two undefined variables Moritz Baumann via GitGitGadget
2022-07-20  6:34 ` [PATCH 1/2] git-p4: fix typo in P4Submit.applyCommit() Moritz Baumann via GitGitGadget
2022-07-20 16:09   ` Junio C Hamano
2022-07-20  6:34 ` [PATCH 2/2] git-p4: fix error handling in P4Unshelve.renameBranch() Moritz Baumann via GitGitGadget
2022-07-20 16:18   ` Junio C Hamano
2022-07-20 16:21     ` Junio C Hamano
2022-07-20 17:01       ` Baumann, Moritz
2022-07-20 18:55 ` [PATCH v2 0/2] git-p4: fix two undefined variables Moritz Baumann via GitGitGadget
2022-07-20 18:55   ` [PATCH v2 1/2] git-p4: fix typo in P4Submit.applyCommit() Moritz Baumann via GitGitGadget
2022-07-20 18:55   ` [PATCH v2 2/2] git-p4: fix error handling in P4Unshelve.renameBranch() Moritz Baumann via GitGitGadget
2022-07-20 20:54   ` [PATCH v2 0/2] git-p4: fix two undefined variables Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).