clone: factor out checkout code
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
7f08c6858e
commit
c39852c18d
101
builtin/clone.c
101
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)
|
static int write_one_config(const char *key, const char *value, void *data)
|
||||||
{
|
{
|
||||||
return git_config_set_multivar(key, value ? value : "true", "^$", 0);
|
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. */
|
/* Source had detached HEAD pointing somewhere. */
|
||||||
update_ref(reflog_msg.buf, "HEAD", remote_head->old_sha1,
|
update_ref(reflog_msg.buf, "HEAD", remote_head->old_sha1,
|
||||||
NULL, REF_NODEREF, DIE_ON_ERR);
|
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) {
|
if (transport) {
|
||||||
@ -780,42 +830,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
|
|||||||
transport_disconnect(transport);
|
transport_disconnect(transport);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!option_no_checkout) {
|
err = 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
strbuf_release(&reflog_msg);
|
strbuf_release(&reflog_msg);
|
||||||
strbuf_release(&branch_top);
|
strbuf_release(&branch_top);
|
||||||
|
Reference in New Issue
Block a user