public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox•com>
To: Johannes Schindelin <johannes.schindelin@gmx•de>
Cc: git@vger•kernel.org, Samuel Williams <space.ship.traveller@gmail•com>
Subject: Re: Trying to push into empty repo, get fatal: bad revision 'HEAD'
Date: Tue, 31 Mar 2015 18:36:03 -0700	[thread overview]
Message-ID: <xmqq8uecocjg.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <xmqqego4ogxw.fsf@gitster.dls.corp.google.com> (Junio C. Hamano's message of "Tue, 31 Mar 2015 17:00:59 -0700")

Junio C Hamano <gitster@pobox•com> writes:

> Samuel Williams <space.ship.traveller@gmail•com> writes:
>
>> I would expect if you push to an empty repo, it would update it
>> (because denyCurrentBranch = updateInstead).
>
> Good finding.
>
> I think the current implementation of updateInstead is set up to
> bootstrap from an empty repository but only supports incremental
> updates once the receiving repository and its working tree gets set
> up.  But I do not think it was a conscious design decison to forbid
> bootstrapping an empty repository, but was a mere gap in the
> implementation.  At least, I do not think of a reason why we should
> forbid it (and I am Cc'ing Dscho to confirm).
>
> Fixing it should not be too hard, but I am on a bus right now so...

A fix (or is it an enhancement) would probably look like this.

This is a tangent but I think we should unify the "do we already
have history behind HEAD, or is the current branch still unborn"
test done by various commands and tighten it.  As a quick and dirty
hack, I just mimicked what builtin/merge.c seems to do, but this
would tell a detached HEAD that points at a nonsense object name
(i.e. "abcde" not a full 40-hex) as "unborn", where we would be
better off stopping the operation instead of making the repository
breakage worse by doing further damage.

I originally suspected I'd need to fix the push_to_checkout()
codepath, too, but it turns out that the detection of unborn-ness
of the current branch is also outsourced to the push-to-checkout
hook, so I do not have to do anything special ;-)

 builtin/receive-pack.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index fc8ec9c..758b0b3 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -733,6 +733,13 @@ static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
 	return 0;
 }
 
+static int head_has_history(void)
+{
+	unsigned char sha1[20];
+
+	return !get_sha1("HEAD", sha1);
+}
+
 static const char *push_to_deploy(unsigned char *sha1,
 				  struct argv_array *env,
 				  const char *work_tree)
@@ -745,13 +752,15 @@ static const char *push_to_deploy(unsigned char *sha1,
 	};
 	const char *diff_index[] = {
 		"diff-index", "--quiet", "--cached", "--ignore-submodules",
-		"HEAD", "--", NULL
+		NULL, "--", NULL
 	};
 	const char *read_tree[] = {
 		"read-tree", "-u", "-m", NULL, NULL
 	};
 	struct child_process child = CHILD_PROCESS_INIT;
 
+	int empty = !head_has_history();
+
 	child.argv = update_refresh;
 	child.env = env->argv;
 	child.dir = work_tree;
@@ -772,6 +781,9 @@ static const char *push_to_deploy(unsigned char *sha1,
 	if (run_command(&child))
 		return "Working directory has unstaged changes";
 
+	/* diff-index with either HEAD or an empty tree */
+	diff_index[4] = empty ? EMPTY_TREE_SHA1_HEX : "HEAD";
+
 	child_process_init(&child);
 	child.argv = diff_index;
 	child.env = env->argv;

  reply	other threads:[~2015-04-01  1:36 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-31 23:29 Trying to push into empty repo, get fatal: bad revision 'HEAD' Samuel Williams
2015-04-01  0:00 ` Junio C Hamano
2015-04-01  1:36   ` Junio C Hamano [this message]
2015-04-01  6:15     ` Junio C Hamano
2015-04-01  8:51     ` Johannes Schindelin
2015-04-01 18:00       ` Junio C Hamano
2015-04-02  5:07         ` Johannes Schindelin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqq8uecocjg.fsf@gitster.dls.corp.google.com \
    --to=gitster@pobox$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    --cc=johannes.schindelin@gmx$(echo .)de \
    --cc=space.ship.traveller@gmail$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox