Skip to content

Commit 757c6ee

Browse files
KarthikNayakgitster
authored andcommitted
builtin/push: call set_refspecs after validating remote
When an end-user runs "git push" with an empty string for the remote repository name, e.g. $ git push '' main "git push" fails with a BUG(). Even though this is a nonsense request that we want to fail, we shouldn't hit a BUG(). Instead we want to give a sensible error message, e.g., 'bad repository'". This is because since 9badf97 (remote: allow resetting url list, 2024-06-14), we reset the remote URL if the provided URL is empty. When a user of 'remotes_remote_get' tries to fetch a remote with an empty repo name, the function initializes the remote via 'make_remote'. But the remote is still not a valid remote, since the URL is empty, so it tries to add the URL alias using 'add_url_alias'. This in-turn will call 'add_url', but since the URL is empty we call 'strvec_clear' on the `remote->url`. Back in 'remotes_remote_get', we again check if the remote is valid, which fails, so we return 'NULL' for the 'struct remote *' value. The 'builtin/push.c' code, calls 'set_refspecs' before validating the remote. This worked with empty repo names earlier since we would get a remote, albeit with an empty URL. With the new changes, we get a 'NULL' remote value, this causes the check for remote to fail and raises the BUG in 'set_refspecs'. Do a simple fix by doing remote validation first. Also add a test to validate the bug fix. With this, we can also now directly pass remote to 'set_refspecs' instead of it trying to lazily obtain it. Helped-by: Jeff King <[email protected]> Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aecd794 commit 757c6ee

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

builtin/push.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
9696
refspec_append(refspec, ref);
9797
}
9898

99-
static void set_refspecs(const char **refs, int nr, const char *repo)
99+
static void set_refspecs(const char **refs, int nr, struct remote *remote)
100100
{
101-
struct remote *remote = NULL;
102101
struct ref *local_refs = NULL;
103102
int i;
104103

@@ -124,17 +123,10 @@ static void set_refspecs(const char **refs, int nr, const char *repo)
124123
local_refs = get_local_heads();
125124

126125
/* Does "ref" uniquely name our ref? */
127-
if (count_refspec_match(ref, local_refs, &matched) != 1) {
126+
if (count_refspec_match(ref, local_refs, &matched) != 1)
128127
refspec_append(&rs, ref);
129-
} else {
130-
/* lazily grab remote */
131-
if (!remote)
132-
remote = remote_get(repo);
133-
if (!remote)
134-
BUG("must get a remote for repo '%s'", repo);
135-
128+
else
136129
refspec_append_mapped(&rs, ref, remote, matched);
137-
}
138130
} else
139131
refspec_append(&rs, ref);
140132
}
@@ -630,10 +622,8 @@ int cmd_push(int argc, const char **argv, const char *prefix)
630622
if (tags)
631623
refspec_append(&rs, "refs/tags/*");
632624

633-
if (argc > 0) {
625+
if (argc > 0)
634626
repo = argv[0];
635-
set_refspecs(argv + 1, argc - 1, repo);
636-
}
637627

638628
remote = pushremote_get(repo);
639629
if (!remote) {
@@ -649,6 +639,9 @@ int cmd_push(int argc, const char **argv, const char *prefix)
649639
" git push <name>\n"));
650640
}
651641

642+
if (argc > 0)
643+
set_refspecs(argv + 1, argc - 1, remote);
644+
652645
if (remote->mirror)
653646
flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
654647

t/t5529-push-errors.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
test_description='detect some push errors early (before contacting remote)'
44

5+
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6+
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7+
58
TEST_PASSES_SANITIZE_LEAK=true
69
. ./test-lib.sh
710

@@ -38,6 +41,20 @@ test_expect_success 'detect missing sha1 expressions early' '
3841
test_cmp expect rp-ran
3942
'
4043

44+
# We use an existing local_ref, since it follows a different flow in
45+
# 'builtin/push.c:set_refspecs()' and we want to test that regression.
46+
test_expect_success 'detect empty remote with existing local ref' '
47+
test_must_fail git push "" main 2> stderr &&
48+
grep "fatal: bad repository ${SQ}${SQ}" stderr
49+
'
50+
51+
# While similar to the previous test, here we want to ensure that
52+
# even targeted refspecs are handled.
53+
test_expect_success 'detect empty remote with targeted refspec' '
54+
test_must_fail git push "" HEAD:refs/heads/main 2> stderr &&
55+
grep "fatal: bad repository ${SQ}${SQ}" stderr
56+
'
57+
4158
test_expect_success 'detect ambiguous refs early' '
4259
git branch foo &&
4360
git tag foo &&

0 commit comments

Comments
 (0)