git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/3] push: fail early with detached HEAD and current

From
Junio C Hamano <gitster@pobox.com>
Date
May 29, 2013, 19:34 UTC
Message-ID
<7vppw9shq2.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1369855311-23367-3-git-send-email-artagnon@gmail.com>
Ramkumar Ramachandra <artagnon@gmail.com> writes:
Show 48 quoted lines
> Setting push.default to current adds the refspec "HEAD" for the
> transport layer to handle.  If "HEAD" doesn't resolve to a branch (and
> since no refspec rhs is specified), the push fails after some time with
> a cryptic error message:
>
>   $ git push
>   error: unable to push to unqualified destination: HEAD
>   The destination refspec neither matches an existing ref on the remote nor
>   begins with refs/, and we are unable to guess a prefix based on the source ref.
>   error: failed to push some refs to 'git@github.com:artagnon/git'
>
> Fail early with a nicer error message:
>
>   $ git push
>   fatal: You are not currently on a branch.
>   To push the history leading to the current (detached HEAD)
>   state now, use
>
>     git push ram HEAD:<name-of-remote-branch>
>
> Just like in the upstream and simple cases.
>
> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
> ---
>  builtin/push.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/builtin/push.c b/builtin/push.c
> index ef3aa97..a79038c 100644
> --- a/builtin/push.c
> +++ b/builtin/push.c
> @@ -175,6 +175,8 @@ static void warn_unspecified_push_default_configuration(void)
>  
>  static void setup_default_push_refspecs(struct remote *remote)
>  {
> +	struct branch *branch = branch_get(NULL);
> +
>  	switch (push_default) {
>  	default:
>  	case PUSH_DEFAULT_UNSPECIFIED:
> @@ -194,6 +196,8 @@ static void setup_default_push_refspecs(struct remote *remote)
>  		break;
>  
>  	case PUSH_DEFAULT_CURRENT:
> +		if (!branch)
> +			die(_(message_detached_head_die), remote->name);
>  		add_refspec("HEAD");
>  		break;

This means well, but I am not sure calling branch_get() for all other cases that do not care about the current branch is a right thing to do. It's not like you need branch variable to free some resource after done, or pass it around to callees from this function.

Would it hurt to do
	if (!branch_get(NULL))
		die(...);
here, without the first hunk?  
Previous: Ramkumar RamachandraNext: Ramkumar Ramachandra
Message 4 of 8 in “Early HEAD resolution in push.default = current”
  1. 0/3 Early HEAD resolution in push.default = currentRamkumar Ramachandra, May 29, 2013
  2. 1/3 push: factor out the detached HEAD error messageRamkumar Ramachandra, May 29, 2013
  3. 2/3 push: fail early with detached HEAD and currentRamkumar Ramachandra, May 29, 2013
  4. Junio C HamanoMay 29, 2013
  5. Ramkumar RamachandraMay 29, 2013
  6. Junio C HamanoMay 29, 2013
  7. Ramkumar RamachandraMay 29, 2013
  8. 3/3 push: make push.default = current use resolved HEADRamkumar Ramachandra, May 29, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.