Skip to content

Commit 48adf2f

Browse files
push: suggest <remote> <branch> when a slash slips into the repository
"git push origin/main" treats "origin/main" as a repository, builds an anonymous remote from it, and fails later with "'origin/main' does not appear to be a git repository", giving no hint that a space was meant instead of a slash. When the repository argument is not a configured remote and its part before the first slash names one, suggest the intended "git push <remote> <branch>" form. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
1 parent e45aa87 commit 48adf2f

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

builtin/push.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,24 @@ int cmd_push(int argc,
744744

745745
if (repo) {
746746
if (!add_remote_or_group(repo, &remote_group)) {
747+
const char *slash = strchr(repo, '/');
748+
749+
/*
750+
* A repository argument of the form
751+
* "<remote>/<branch>" is almost always a slip for
752+
* "<remote> <branch>" rather than a path or URL.
753+
*/
754+
if (slash) {
755+
struct strbuf name = STRBUF_INIT;
756+
757+
strbuf_add(&name, repo, slash - repo);
758+
if (remote_is_configured(remote_get(name.buf), 0))
759+
die(_("'%s' does not appear to be a git repository.\n"
760+
"Did you mean to use: git push %s %s?"),
761+
repo, name.buf, slash + 1);
762+
strbuf_release(&name);
763+
}
764+
747765
/*
748766
* Not a configured remote name or group name.
749767
* Try treating it as a direct URL or path, e.g.

t/t5529-push-errors.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ test_expect_success 'detect empty remote with targeted refspec' '
5454
grep "fatal: bad repository ${SQ}${SQ}" stderr
5555
'
5656

57+
test_expect_success 'suggest <remote> <branch> for a <remote>/<branch> slip' '
58+
test_must_fail git push origin/main 2>stderr &&
59+
grep "${SQ}origin/main${SQ} does not appear to be a git repository" stderr &&
60+
grep "Did you mean to use: git push origin main?" stderr
61+
'
62+
63+
test_expect_success 'no suggestion when prefix is not a configured remote' '
64+
test_must_fail git push not-a-remote/main 2>stderr &&
65+
! grep "Did you mean" stderr
66+
'
67+
5768
test_expect_success 'detect ambiguous refs early' '
5869
git branch foo &&
5970
git tag foo &&

0 commit comments

Comments
 (0)