@matheusbsbs / matheus-bagliano / commits / ec8460bd91
Fork 0

push: better error messages when push.default = tracking

A common scenario is to create a new branch and push it (checkout -b && push [--set-upstream]). In this case, the user was getting "The current branch %s has no upstream branch.", which doesn't help much. Provide the user a command to push the current branch. To avoid the situation in the future, suggest --set-upstream. While we're there, also improve the error message in the "detached HEAD" case. We mention explicitly "detached HEAD" since this is the keyword to look for in documentations. Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthieu Moy committed Mar 2, 2011 at 21:12 UTC ec8460bd91007f0bd7851cd07494d1019a39937f
1 file changed +16 -6
builtin/push.c
+16 -6
@@ -64,14 +64,24 @@ static void set_refspecs(const char **refs, int nr)
64 }
65 }
66
67 -static void setup_push_upstream(void)
67 +static void setup_push_upstream(struct remote *remote)
68 {
69 struct strbuf refspec = STRBUF_INIT;
70 struct branch *branch = branch_get(NULL);
71 if (!branch)
72 - die("You are not currently on a branch.");
72 + die("You are not currently on a branch.\n"
73 + "To push the history leading to the current (detached HEAD)\n"
74 + "state now, use\n"
75 + "\n"
76 + " git push %s HEAD:<name-of-remote-branch>\n",
77 + remote->name);
78 if (!branch->merge_nr || !branch->merge)
74 - die("The current branch %s has no upstream branch.",
79 + die("The current branch %s has no upstream branch.\n"
80 + "To push the current branch and set the remote as upstream, use\n"
81 + "\n"
82 + " git push --set-upstream %s %s\n",
83 + branch->name,
84 + remote->name,
85 branch->name);
86 if (branch->merge_nr != 1)
87 die("The current branch %s has multiple upstream branches, "
@@ -80,7 +90,7 @@ static void setup_push_upstream(void)
90 add_refspec(refspec.buf);
91 }
92
83 -static void setup_default_push_refspecs(void)
93 +static void setup_default_push_refspecs(struct remote *remote)
94 {
95 switch (push_default) {
96 default:
@@ -89,7 +99,7 @@ static void setup_default_push_refspecs(void)
99 break;
100
101 case PUSH_DEFAULT_UPSTREAM:
92 - setup_push_upstream();
102 + setup_push_upstream(remote);
103 break;
104
105 case PUSH_DEFAULT_CURRENT:
@@ -175,7 +185,7 @@ static int do_push(const char *repo, int flags)
185 refspec = remote->push_refspec;
186 refspec_nr = remote->push_refspec_nr;
187 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
178 - setup_default_push_refspecs();
188 + setup_default_push_refspecs(remote);
189 }
190 errs = 0;
191 if (remote->pushurl_nr) {