Skip to content

Commit 6be17ec

Browse files
committed
Merge branch 'rr/push-head' into maint
* rr/push-head: push: make push.default = current use resolved HEAD push: fail early with detached HEAD and current push: factor out the detached HEAD error message
2 parents c9cae1e + 0f075b2 commit 6be17ec

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

builtin/push.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,19 @@ static NORETURN int die_push_simple(struct branch *branch, struct remote *remote
113113
remote->name, branch->name, advice_maybe);
114114
}
115115

116+
static const char message_detached_head_die[] =
117+
N_("You are not currently on a branch.\n"
118+
"To push the history leading to the current (detached HEAD)\n"
119+
"state now, use\n"
120+
"\n"
121+
" git push %s HEAD:<name-of-remote-branch>\n");
122+
116123
static void setup_push_upstream(struct remote *remote, int simple)
117124
{
118125
struct strbuf refspec = STRBUF_INIT;
119126
struct branch *branch = branch_get(NULL);
120127
if (!branch)
121-
die(_("You are not currently on a branch.\n"
122-
"To push the history leading to the current (detached HEAD)\n"
123-
"state now, use\n"
124-
"\n"
125-
" git push %s HEAD:<name-of-remote-branch>\n"),
126-
remote->name);
128+
die(_(message_detached_head_die), remote->name);
127129
if (!branch->merge_nr || !branch->merge || !branch->remote_name)
128130
die(_("The current branch %s has no upstream branch.\n"
129131
"To push the current branch and set the remote as upstream, use\n"
@@ -173,6 +175,8 @@ static void warn_unspecified_push_default_configuration(void)
173175

174176
static void setup_default_push_refspecs(struct remote *remote)
175177
{
178+
struct branch *branch;
179+
176180
switch (push_default) {
177181
default:
178182
case PUSH_DEFAULT_UNSPECIFIED:
@@ -192,7 +196,10 @@ static void setup_default_push_refspecs(struct remote *remote)
192196
break;
193197

194198
case PUSH_DEFAULT_CURRENT:
195-
add_refspec("HEAD");
199+
branch = branch_get(NULL);
200+
if (!branch)
201+
die(_(message_detached_head_die), remote->name);
202+
add_refspec(branch->name);
196203
break;
197204

198205
case PUSH_DEFAULT_NOTHING:

0 commit comments

Comments
 (0)