diff mbox series

ty: Fix git-config regexp in get_branch_info()

Message ID 20230310055753.1609529-1-mpe@ellerman.id.au (mailing list archive)
State Accepted
Headers show
Series ty: Fix git-config regexp in get_branch_info() | expand

Commit Message

Michael Ellerman March 10, 2023, 5:57 a.m. UTC
The regexp used to get the branch configuration in get_branch_info()
is subtly wrong. If there are multiple branches starting with the same
prefix, the configuration for all of them will be returned, which can
lead to the wrong branch info being used.

eg. Running b4 -d ty, with extra logging of every line in
get_config_from_git() shows:

  Running git --no-pager config -z --get-regexp branch\.foo\.*
  branch.foo.remote .
  branch.foo.merge refs/heads/foo-test
  branch.foo-test.remote .
  branch.foo-test.merge refs/heads/foo-base

Although the regexp has two escaped dots, the 2nd one is seemingly
consumed by the '*', leading to the regexp matching "foo" and
"foo-test".

This can be observed on the command line:
  $ git branch -t foo origin/master
  $ git branch -t foo-test origin/master
  $ git --no-pager config --get-regexp "branch\.foo\.*"
  branch.foo.remote .
  branch.foo.merge refs/heads/foo-test
  branch.foo-test.remote .
  branch.foo-test.merge refs/heads/foo-base

The real problem is that we meant to pass a regexp of
"branch\.foo\..*", so change the code to use that, which fixes the
problem:

  $ git --no-pager config --get-regexp "branch\.foo\..*"
  branch.foo.remote origin
  branch.foo.merge refs/heads/master

Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
---
 b4/ty.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Konstantin Ryabitsev March 10, 2023, 8:11 p.m. UTC | #1
On Fri, 10 Mar 2023 16:57:53 +1100, Michael Ellerman wrote:
> The regexp used to get the branch configuration in get_branch_info()
> is subtly wrong. If there are multiple branches starting with the same
> prefix, the configuration for all of them will be returned, which can
> lead to the wrong branch info being used.
> 
> eg. Running b4 -d ty, with extra logging of every line in
> get_config_from_git() shows:
> 
> [...]

Applied, thanks!

[1/1] ty: Fix git-config regexp in get_branch_info()
      commit: 4f90eab09937e78f27e63a01076af72f8d45e3f3

Best regards,
diff mbox series

Patch

diff --git a/b4/ty.py b/b4/ty.py
index 6da1a15..165afd9 100644
--- a/b4/ty.py
+++ b/b4/ty.py
@@ -665,7 +665,7 @@  BRANCH_INFO = None
 
     BRANCH_INFO = dict()
 
-    remotecfg = b4.get_config_from_git('branch\\.%s\\.*' % branch)
+    remotecfg = b4.get_config_from_git('branch\\.%s\\..*' % branch)
     if remotecfg is None or 'remote' not in remotecfg:
         # Did not find a matching branch entry, so look at remotes
         gitargs = ['remote', 'show']