git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH v5 03/10] clone: factor out checkout code
Date: Mon, 16 Jan 2012 16:46:09 +0700	[thread overview]
Message-ID: <1326707176-8045-4-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1326439322-15648-1-git-send-email-pclouds@gmail.com>

Read HEAD from disk instead of relying on local variable
our_head_points_at, so that if earlier code fails to make HEAD
properly, it'll be detected.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/clone.c |  101 +++++++++++++++++++++++++++++++-----------------------
 1 files changed, 58 insertions(+), 43 deletions(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index 91862f7..98e3542 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -486,6 +486,63 @@ static void write_followtags(const struct ref *refs, const char *msg)
 	}
 }
 
+static int checkout(void)
+{
+	unsigned char sha1[20];
+	char *head;
+	struct lock_file *lock_file;
+	struct unpack_trees_options opts;
+	struct tree *tree;
+	struct tree_desc t;
+	int err = 0, fd;
+
+	if (option_no_checkout)
+		return 0;
+
+	head = resolve_refdup("HEAD", sha1, 1, NULL);
+	if (!head) {
+		warning(_("remote HEAD refers to nonexistent ref, "
+			  "unable to checkout.\n"));
+		return 0;
+	}
+	if (strcmp(head, "HEAD")) {
+		if (prefixcmp(head, "refs/heads/"))
+			die(_("HEAD not found below refs/heads!"));
+	}
+	free(head);
+
+	/* We need to be in the new work tree for the checkout */
+	setup_work_tree();
+
+	lock_file = xcalloc(1, sizeof(struct lock_file));
+	fd = hold_locked_index(lock_file, 1);
+
+	memset(&opts, 0, sizeof opts);
+	opts.update = 1;
+	opts.merge = 1;
+	opts.fn = oneway_merge;
+	opts.verbose_update = (option_verbosity > 0);
+	opts.src_index = &the_index;
+	opts.dst_index = &the_index;
+
+	tree = parse_tree_indirect(sha1);
+	parse_tree(tree);
+	init_tree_desc(&t, tree->buffer, tree->size);
+	unpack_trees(1, &t, &opts);
+
+	if (write_cache(fd, active_cache, active_nr) ||
+	    commit_locked_index(lock_file))
+		die(_("unable to write new index file"));
+
+	err |= run_hook(NULL, "post-checkout", sha1_to_hex(null_sha1),
+			sha1_to_hex(sha1), "1", NULL);
+
+	if (!err && option_recursive)
+		err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
+
+	return err;
+}
+
 static int write_one_config(const char *key, const char *value, void *data)
 {
 	return git_config_set_multivar(key, value ? value : "true", "^$", 0);
@@ -766,13 +823,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 		/* Source had detached HEAD pointing somewhere. */
 		update_ref(reflog_msg.buf, "HEAD", remote_head->old_sha1,
 			   NULL, REF_NODEREF, DIE_ON_ERR);
-		our_head_points_at = remote_head;
-	} else {
-		/* Nothing to checkout out */
-		if (!option_no_checkout)
-			warning(_("remote HEAD refers to nonexistent ref, "
-				"unable to checkout.\n"));
-		option_no_checkout = 1;
 	}
 
 	if (transport) {
@@ -780,42 +830,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 		transport_disconnect(transport);
 	}
 
-	if (!option_no_checkout) {
-		struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
-		struct unpack_trees_options opts;
-		struct tree *tree;
-		struct tree_desc t;
-		int fd;
-
-		/* We need to be in the new work tree for the checkout */
-		setup_work_tree();
-
-		fd = hold_locked_index(lock_file, 1);
-
-		memset(&opts, 0, sizeof opts);
-		opts.update = 1;
-		opts.merge = 1;
-		opts.fn = oneway_merge;
-		opts.verbose_update = (option_verbosity > 0);
-		opts.src_index = &the_index;
-		opts.dst_index = &the_index;
-
-		tree = parse_tree_indirect(our_head_points_at->old_sha1);
-		parse_tree(tree);
-		init_tree_desc(&t, tree->buffer, tree->size);
-		unpack_trees(1, &t, &opts);
-
-		if (write_cache(fd, active_cache, active_nr) ||
-		    commit_locked_index(lock_file))
-			die(_("unable to write new index file"));
-
-		err |= run_hook(NULL, "post-checkout", sha1_to_hex(null_sha1),
-				sha1_to_hex(our_head_points_at->old_sha1), "1",
-				NULL);
-
-		if (!err && option_recursive)
-			err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
-	}
+	err = checkout();
 
 	strbuf_release(&reflog_msg);
 	strbuf_release(&branch_top);
-- 
1.7.3.1.256.g2539c.dirty

  parent reply	other threads:[~2012-01-16  9:46 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-05 13:49 [PATCH] clone: allow detached checkout when --branch takes a tag Nguyễn Thái Ngọc Duy
2012-01-05 14:18 ` Jeff King
2012-01-06 11:09   ` Nguyen Thai Ngoc Duy
2012-01-06 14:42     ` Jeff King
2012-01-05 16:22 ` Junio C Hamano
2012-01-06  7:35   ` Nguyen Thai Ngoc Duy
2012-01-08 11:46 ` [PATCH v2 1/6] t5601: add && Nguyễn Thái Ngọc Duy
2012-01-08 11:46   ` [PATCH v2 2/6] clone: write detached HEAD in bare repositories Nguyễn Thái Ngọc Duy
2012-01-08 11:46   ` [PATCH v2 3/6] clone: factor out checkout code Nguyễn Thái Ngọc Duy
2012-01-10  0:32     ` Junio C Hamano
2012-01-10  2:01       ` Nguyen Thai Ngoc Duy
2012-01-10  4:59         ` Junio C Hamano
2012-01-10  5:57           ` Nguyen Thai Ngoc Duy
2012-01-08 11:46   ` [PATCH v2 4/6] clone: --branch=<branch> always means refs/heads/<branch> Nguyễn Thái Ngọc Duy
2012-01-10  0:33     ` Junio C Hamano
2012-01-08 11:46   ` [PATCH v2 5/6] clone: allow --branch to take a tag Nguyễn Thái Ngọc Duy
2012-01-08 11:46   ` [PATCH v2 6/6] clone: print advice on checking out detached HEAD Nguyễn Thái Ngọc Duy
2012-01-10  0:36     ` Junio C Hamano
2012-01-10  1:54       ` Nguyen Thai Ngoc Duy
2012-01-10  4:49         ` Junio C Hamano
2012-01-10  5:54           ` Nguyen Thai Ngoc Duy
2012-01-10  9:56   ` [PATCH v3 00/10] nd/clone-detached Nguyễn Thái Ngọc Duy
2012-01-13  7:21     ` [PATCH v4 " Nguyễn Thái Ngọc Duy
2012-01-13 19:52       ` Junio C Hamano
2012-01-14  4:48         ` Nguyen Thai Ngoc Duy
2012-01-14  6:53           ` Junio C Hamano
2012-01-14  7:40             ` Nguyen Thai Ngoc Duy
2012-01-15  2:34               ` Junio C Hamano
2012-01-16  9:46       ` [PATCH v5 00/10] nd/clone-detached rebase onto nd/clone-single-branch Nguyễn Thái Ngọc Duy
2012-01-16  9:46       ` [PATCH v5 01/10] t5601: add missing && cascade Nguyễn Thái Ngọc Duy
2012-01-16  9:46       ` [PATCH v5 02/10] clone: write detached HEAD in bare repositories Nguyễn Thái Ngọc Duy
2012-01-16  9:46       ` Nguyễn Thái Ngọc Duy [this message]
2012-01-16  9:46       ` [PATCH v5 04/10] clone: factor out HEAD update code Nguyễn Thái Ngọc Duy
2012-01-16  9:46       ` [PATCH v5 05/10] clone: factor out remote ref writing Nguyễn Thái Ngọc Duy
2012-01-16  9:46       ` [PATCH v5 06/10] clone: delay cloning until after remote HEAD checking Nguyễn Thái Ngọc Duy
2012-01-16  9:46       ` [PATCH v5 07/10] clone: --branch=<branch> always means refs/heads/<branch> Nguyễn Thái Ngọc Duy
2012-01-16  9:46       ` [PATCH v5 08/10] clone: refuse to clone if --branch points to bogus ref Nguyễn Thái Ngọc Duy
2012-01-16  9:46       ` [PATCH v5 09/10] clone: allow --branch to take a tag Nguyễn Thái Ngọc Duy
2012-01-16  9:46       ` [PATCH v5 10/10] clone: print advice on checking out detached HEAD Nguyễn Thái Ngọc Duy
2012-01-13  7:21     ` [PATCH v4 01/10] t5601: add missing && cascade Nguyễn Thái Ngọc Duy
2012-01-13  7:21     ` [PATCH v4 02/10] clone: write detached HEAD in bare repositories Nguyễn Thái Ngọc Duy
2012-01-13  7:21     ` [PATCH v4 03/10] clone: factor out checkout code Nguyễn Thái Ngọc Duy
2012-01-13  7:21     ` [PATCH v4 04/10] clone: factor out HEAD update code Nguyễn Thái Ngọc Duy
2012-01-13  7:21     ` [PATCH v4 05/10] clone: factor out remote ref writing Nguyễn Thái Ngọc Duy
2012-01-13  7:21     ` [PATCH v4 06/10] clone: delay cloning until after remote HEAD checking Nguyễn Thái Ngọc Duy
2012-01-13  7:21     ` [PATCH v4 07/10] clone: --branch=<branch> always means refs/heads/<branch> Nguyễn Thái Ngọc Duy
2012-01-13  7:22     ` [PATCH v4 08/10] clone: refuse to clone if --branch points to bogus ref Nguyễn Thái Ngọc Duy
2012-01-13  7:22     ` [PATCH v4 09/10] clone: allow --branch to take a tag Nguyễn Thái Ngọc Duy
2012-01-13  7:22     ` [PATCH v4 10/10] clone: print advice on checking out detached HEAD Nguyễn Thái Ngọc Duy
2012-01-10  9:56   ` [PATCH v3 01/10] t5601: add missing && cascade Nguyễn Thái Ngọc Duy
2012-01-10  9:56   ` [PATCH v3 02/10] clone: write detached HEAD in bare repositories Nguyễn Thái Ngọc Duy
2012-01-10  9:57   ` [PATCH v3 03/10] clone: factor out checkout code Nguyễn Thái Ngọc Duy
2012-01-10  9:57   ` [PATCH v3 04/10] clone: factor out HEAD update code Nguyễn Thái Ngọc Duy
2012-01-10  9:57   ` [PATCH v3 05/10] clone: factor out remote ref writing Nguyễn Thái Ngọc Duy
2012-01-10  9:57   ` [PATCH v3 06/10] clone: delay cloning until after remote HEAD checking Nguyễn Thái Ngọc Duy
2012-01-11 19:36     ` Junio C Hamano
2012-01-12  1:27       ` Nguyen Thai Ngoc Duy
2012-01-24  0:15     ` Junio C Hamano
2012-01-24  0:34       ` Junio C Hamano
2012-01-24  0:39         ` Junio C Hamano
2012-01-24  7:01           ` Nguyen Thai Ngoc Duy
2012-01-10  9:57   ` [PATCH v3 07/10] clone: --branch=<branch> always means refs/heads/<branch> Nguyễn Thái Ngọc Duy
2012-01-11 20:01     ` Junio C Hamano
2012-01-12  3:00       ` Nguyen Thai Ngoc Duy
2012-01-10  9:57   ` [PATCH v3 08/10] clone: refuse to clone if --branch points to bogus ref Nguyễn Thái Ngọc Duy
2012-01-10  9:57   ` [PATCH v3 09/10] clone: allow --branch to take a tag Nguyễn Thái Ngọc Duy
2012-01-11 23:57     ` Junio C Hamano
2012-01-10  9:57   ` [PATCH v3 10/10] clone: print advice on checking out detached HEAD Nguyễn Thái Ngọc Duy

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=1326707176-8045-4-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
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;
as well as URLs for NNTP newsgroup(s).