clone: clone from a repository with relative alternates
[git/git.git] / builtin / clone.c
1 /*
2 * Builtin "git clone"
3 *
4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>,
5 * 2008 Daniel Barkalow <barkalow@iabervon.org>
6 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
7 *
8 * Clone a repository into a different directory that does not yet exist.
9 */
10
11 #include "builtin.h"
12 #include "parse-options.h"
13 #include "fetch-pack.h"
14 #include "refs.h"
15 #include "tree.h"
16 #include "tree-walk.h"
17 #include "unpack-trees.h"
18 #include "transport.h"
19 #include "strbuf.h"
20 #include "dir.h"
21 #include "pack-refs.h"
22 #include "sigchain.h"
23 #include "branch.h"
24 #include "remote.h"
25 #include "run-command.h"
26
27 /*
28 * Overall FIXMEs:
29 * - respect DB_ENVIRONMENT for .git/objects.
30 *
31 * Implementation notes:
32 * - dropping use-separate-remote and no-separate-remote compatibility
33 *
34 */
35 static const char * const builtin_clone_usage[] = {
36 "git clone [options] [--] <repo> [<dir>]",
37 NULL
38 };
39
40 static int option_no_checkout, option_bare, option_mirror;
41 static int option_local, option_no_hardlinks, option_shared, option_recursive;
42 static char *option_template, *option_depth;
43 static char *option_origin = NULL;
44 static char *option_branch = NULL;
45 static const char *real_git_dir;
46 static char *option_upload_pack = "git-upload-pack";
47 static int option_verbosity;
48 static int option_progress;
49 static struct string_list option_reference;
50
51 static int opt_parse_reference(const struct option *opt, const char *arg, int unset)
52 {
53 struct string_list *option_reference = opt->value;
54 if (!arg)
55 return -1;
56 string_list_append(option_reference, arg);
57 return 0;
58 }
59
60 static struct option builtin_clone_options[] = {
61 OPT__VERBOSITY(&option_verbosity),
62 OPT_BOOLEAN(0, "progress", &option_progress,
63 "force progress reporting"),
64 OPT_BOOLEAN('n', "no-checkout", &option_no_checkout,
65 "don't create a checkout"),
66 OPT_BOOLEAN(0, "bare", &option_bare, "create a bare repository"),
67 { OPTION_BOOLEAN, 0, "naked", &option_bare, NULL,
68 "create a bare repository",
69 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
70 OPT_BOOLEAN(0, "mirror", &option_mirror,
71 "create a mirror repository (implies bare)"),
72 OPT_BOOLEAN('l', "local", &option_local,
73 "to clone from a local repository"),
74 OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks,
75 "don't use local hardlinks, always copy"),
76 OPT_BOOLEAN('s', "shared", &option_shared,
77 "setup as shared repository"),
78 OPT_BOOLEAN(0, "recursive", &option_recursive,
79 "initialize submodules in the clone"),
80 OPT_BOOLEAN(0, "recurse-submodules", &option_recursive,
81 "initialize submodules in the clone"),
82 OPT_STRING(0, "template", &option_template, "template-directory",
83 "directory from which templates will be used"),
84 OPT_CALLBACK(0 , "reference", &option_reference, "repo",
85 "reference repository", &opt_parse_reference),
86 OPT_STRING('o', "origin", &option_origin, "branch",
87 "use <branch> instead of 'origin' to track upstream"),
88 OPT_STRING('b', "branch", &option_branch, "branch",
89 "checkout <branch> instead of the remote's HEAD"),
90 OPT_STRING('u', "upload-pack", &option_upload_pack, "path",
91 "path to git-upload-pack on the remote"),
92 OPT_STRING(0, "depth", &option_depth, "depth",
93 "create a shallow clone of that depth"),
94 OPT_STRING(0, "separate-git-dir", &real_git_dir, "gitdir",
95 "separate git dir from working tree"),
96
97 OPT_END()
98 };
99
100 static const char *argv_submodule[] = {
101 "submodule", "update", "--init", "--recursive", NULL
102 };
103
104 static char *get_repo_path(const char *repo, int *is_bundle)
105 {
106 static char *suffix[] = { "/.git", ".git", "" };
107 static char *bundle_suffix[] = { ".bundle", "" };
108 struct stat st;
109 int i;
110
111 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
112 const char *path;
113 path = mkpath("%s%s", repo, suffix[i]);
114 if (is_directory(path)) {
115 *is_bundle = 0;
116 return xstrdup(absolute_path(path));
117 }
118 }
119
120 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
121 const char *path;
122 path = mkpath("%s%s", repo, bundle_suffix[i]);
123 if (!stat(path, &st) && S_ISREG(st.st_mode)) {
124 *is_bundle = 1;
125 return xstrdup(absolute_path(path));
126 }
127 }
128
129 return NULL;
130 }
131
132 static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
133 {
134 const char *end = repo + strlen(repo), *start;
135 char *dir;
136
137 /*
138 * Strip trailing spaces, slashes and /.git
139 */
140 while (repo < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
141 end--;
142 if (end - repo > 5 && is_dir_sep(end[-5]) &&
143 !strncmp(end - 4, ".git", 4)) {
144 end -= 5;
145 while (repo < end && is_dir_sep(end[-1]))
146 end--;
147 }
148
149 /*
150 * Find last component, but be prepared that repo could have
151 * the form "remote.example.com:foo.git", i.e. no slash
152 * in the directory part.
153 */
154 start = end;
155 while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
156 start--;
157
158 /*
159 * Strip .{bundle,git}.
160 */
161 if (is_bundle) {
162 if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
163 end -= 7;
164 } else {
165 if (end - start > 4 && !strncmp(end - 4, ".git", 4))
166 end -= 4;
167 }
168
169 if (is_bare) {
170 struct strbuf result = STRBUF_INIT;
171 strbuf_addf(&result, "%.*s.git", (int)(end - start), start);
172 dir = strbuf_detach(&result, NULL);
173 } else
174 dir = xstrndup(start, end - start);
175 /*
176 * Replace sequences of 'control' characters and whitespace
177 * with one ascii space, remove leading and trailing spaces.
178 */
179 if (*dir) {
180 char *out = dir;
181 int prev_space = 1 /* strip leading whitespace */;
182 for (end = dir; *end; ++end) {
183 char ch = *end;
184 if ((unsigned char)ch < '\x20')
185 ch = '\x20';
186 if (isspace(ch)) {
187 if (prev_space)
188 continue;
189 prev_space = 1;
190 } else
191 prev_space = 0;
192 *out++ = ch;
193 }
194 *out = '\0';
195 if (out > dir && prev_space)
196 out[-1] = '\0';
197 }
198 return dir;
199 }
200
201 static void strip_trailing_slashes(char *dir)
202 {
203 char *end = dir + strlen(dir);
204
205 while (dir < end - 1 && is_dir_sep(end[-1]))
206 end--;
207 *end = '\0';
208 }
209
210 static int add_one_reference(struct string_list_item *item, void *cb_data)
211 {
212 char *ref_git;
213 struct strbuf alternate = STRBUF_INIT;
214 struct remote *remote;
215 struct transport *transport;
216 const struct ref *extra;
217
218 /* Beware: real_path() and mkpath() return static buffer */
219 ref_git = xstrdup(real_path(item->string));
220 if (is_directory(mkpath("%s/.git/objects", ref_git))) {
221 char *ref_git_git = xstrdup(mkpath("%s/.git", ref_git));
222 free(ref_git);
223 ref_git = ref_git_git;
224 } else if (!is_directory(mkpath("%s/objects", ref_git)))
225 die(_("reference repository '%s' is not a local directory."),
226 item->string);
227
228 strbuf_addf(&alternate, "%s/objects", ref_git);
229 add_to_alternates_file(alternate.buf);
230 strbuf_release(&alternate);
231
232 remote = remote_get(ref_git);
233 transport = transport_get(remote, ref_git);
234 for (extra = transport_get_remote_refs(transport); extra;
235 extra = extra->next)
236 add_extra_ref(extra->name, extra->old_sha1, 0);
237
238 transport_disconnect(transport);
239 free(ref_git);
240 return 0;
241 }
242
243 static void setup_reference(void)
244 {
245 for_each_string_list(&option_reference, add_one_reference, NULL);
246 }
247
248 static void copy_alternates(struct strbuf *src, struct strbuf *dst,
249 const char *src_repo)
250 {
251 /*
252 * Read from the source objects/info/alternates file
253 * and copy the entries to corresponding file in the
254 * destination repository with add_to_alternates_file().
255 * Both src and dst have "$path/objects/info/alternates".
256 *
257 * Instead of copying bit-for-bit from the original,
258 * we need to append to existing one so that the already
259 * created entry via "clone -s" is not lost, and also
260 * to turn entries with paths relative to the original
261 * absolute, so that they can be used in the new repository.
262 */
263 FILE *in = fopen(src->buf, "r");
264 struct strbuf line = STRBUF_INIT;
265
266 while (strbuf_getline(&line, in, '\n') != EOF) {
267 char *abs_path, abs_buf[PATH_MAX];
268 if (!line.len || line.buf[0] == '#')
269 continue;
270 if (is_absolute_path(line.buf)) {
271 add_to_alternates_file(line.buf);
272 continue;
273 }
274 abs_path = mkpath("%s/objects/%s", src_repo, line.buf);
275 normalize_path_copy(abs_buf, abs_path);
276 add_to_alternates_file(abs_buf);
277 }
278 strbuf_release(&line);
279 fclose(in);
280 }
281
282 static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
283 const char *src_repo, int src_baselen)
284 {
285 struct dirent *de;
286 struct stat buf;
287 int src_len, dest_len;
288 DIR *dir;
289
290 dir = opendir(src->buf);
291 if (!dir)
292 die_errno(_("failed to open '%s'"), src->buf);
293
294 if (mkdir(dest->buf, 0777)) {
295 if (errno != EEXIST)
296 die_errno(_("failed to create directory '%s'"), dest->buf);
297 else if (stat(dest->buf, &buf))
298 die_errno(_("failed to stat '%s'"), dest->buf);
299 else if (!S_ISDIR(buf.st_mode))
300 die(_("%s exists and is not a directory"), dest->buf);
301 }
302
303 strbuf_addch(src, '/');
304 src_len = src->len;
305 strbuf_addch(dest, '/');
306 dest_len = dest->len;
307
308 while ((de = readdir(dir)) != NULL) {
309 strbuf_setlen(src, src_len);
310 strbuf_addstr(src, de->d_name);
311 strbuf_setlen(dest, dest_len);
312 strbuf_addstr(dest, de->d_name);
313 if (stat(src->buf, &buf)) {
314 warning (_("failed to stat %s\n"), src->buf);
315 continue;
316 }
317 if (S_ISDIR(buf.st_mode)) {
318 if (de->d_name[0] != '.')
319 copy_or_link_directory(src, dest,
320 src_repo, src_baselen);
321 continue;
322 }
323
324 /* Files that cannot be copied bit-for-bit... */
325 if (!strcmp(src->buf + src_baselen, "/info/alternates")) {
326 copy_alternates(src, dest, src_repo);
327 continue;
328 }
329
330 if (unlink(dest->buf) && errno != ENOENT)
331 die_errno(_("failed to unlink '%s'"), dest->buf);
332 if (!option_no_hardlinks) {
333 if (!link(src->buf, dest->buf))
334 continue;
335 if (option_local)
336 die_errno(_("failed to create link '%s'"), dest->buf);
337 option_no_hardlinks = 1;
338 }
339 if (copy_file_with_time(dest->buf, src->buf, 0666))
340 die_errno(_("failed to copy file to '%s'"), dest->buf);
341 }
342 closedir(dir);
343 }
344
345 static const struct ref *clone_local(const char *src_repo,
346 const char *dest_repo)
347 {
348 const struct ref *ret;
349 struct remote *remote;
350 struct transport *transport;
351
352 if (option_shared) {
353 struct strbuf alt = STRBUF_INIT;
354 strbuf_addf(&alt, "%s/objects", src_repo);
355 add_to_alternates_file(alt.buf);
356 strbuf_release(&alt);
357 } else {
358 struct strbuf src = STRBUF_INIT;
359 struct strbuf dest = STRBUF_INIT;
360 strbuf_addf(&src, "%s/objects", src_repo);
361 strbuf_addf(&dest, "%s/objects", dest_repo);
362 copy_or_link_directory(&src, &dest, src_repo, src.len);
363 strbuf_release(&src);
364 strbuf_release(&dest);
365 }
366
367 remote = remote_get(src_repo);
368 transport = transport_get(remote, src_repo);
369 ret = transport_get_remote_refs(transport);
370 transport_disconnect(transport);
371 if (0 <= option_verbosity)
372 printf(_("done.\n"));
373 return ret;
374 }
375
376 static const char *junk_work_tree;
377 static const char *junk_git_dir;
378 static pid_t junk_pid;
379
380 static void remove_junk(void)
381 {
382 struct strbuf sb = STRBUF_INIT;
383 if (getpid() != junk_pid)
384 return;
385 if (junk_git_dir) {
386 strbuf_addstr(&sb, junk_git_dir);
387 remove_dir_recursively(&sb, 0);
388 strbuf_reset(&sb);
389 }
390 if (junk_work_tree) {
391 strbuf_addstr(&sb, junk_work_tree);
392 remove_dir_recursively(&sb, 0);
393 strbuf_reset(&sb);
394 }
395 }
396
397 static void remove_junk_on_signal(int signo)
398 {
399 remove_junk();
400 sigchain_pop(signo);
401 raise(signo);
402 }
403
404 static struct ref *wanted_peer_refs(const struct ref *refs,
405 struct refspec *refspec)
406 {
407 struct ref *local_refs = NULL;
408 struct ref **tail = &local_refs;
409
410 get_fetch_map(refs, refspec, &tail, 0);
411 if (!option_mirror)
412 get_fetch_map(refs, tag_refspec, &tail, 0);
413
414 return local_refs;
415 }
416
417 static void write_remote_refs(const struct ref *local_refs)
418 {
419 const struct ref *r;
420
421 for (r = local_refs; r; r = r->next)
422 add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
423
424 pack_refs(PACK_REFS_ALL);
425 clear_extra_refs();
426 }
427
428 int cmd_clone(int argc, const char **argv, const char *prefix)
429 {
430 int is_bundle = 0, is_local;
431 struct stat buf;
432 const char *repo_name, *repo, *work_tree, *git_dir;
433 char *path, *dir;
434 int dest_exists;
435 const struct ref *refs, *remote_head;
436 const struct ref *remote_head_points_at;
437 const struct ref *our_head_points_at;
438 struct ref *mapped_refs;
439 struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
440 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
441 struct transport *transport = NULL;
442 char *src_ref_prefix = "refs/heads/";
443 int err = 0;
444
445 struct refspec *refspec;
446 const char *fetch_pattern;
447
448 junk_pid = getpid();
449
450 packet_trace_identity("clone");
451 argc = parse_options(argc, argv, prefix, builtin_clone_options,
452 builtin_clone_usage, 0);
453
454 if (argc > 2)
455 usage_msg_opt(_("Too many arguments."),
456 builtin_clone_usage, builtin_clone_options);
457
458 if (argc == 0)
459 usage_msg_opt(_("You must specify a repository to clone."),
460 builtin_clone_usage, builtin_clone_options);
461
462 if (option_mirror)
463 option_bare = 1;
464
465 if (option_bare) {
466 if (option_origin)
467 die(_("--bare and --origin %s options are incompatible."),
468 option_origin);
469 option_no_checkout = 1;
470 }
471
472 if (!option_origin)
473 option_origin = "origin";
474
475 repo_name = argv[0];
476
477 path = get_repo_path(repo_name, &is_bundle);
478 if (path)
479 repo = xstrdup(absolute_path(repo_name));
480 else if (!strchr(repo_name, ':'))
481 die(_("repository '%s' does not exist"), repo_name);
482 else
483 repo = repo_name;
484 is_local = path && !is_bundle;
485 if (is_local && option_depth)
486 warning(_("--depth is ignored in local clones; use file:// instead."));
487
488 if (argc == 2)
489 dir = xstrdup(argv[1]);
490 else
491 dir = guess_dir_name(repo_name, is_bundle, option_bare);
492 strip_trailing_slashes(dir);
493
494 dest_exists = !stat(dir, &buf);
495 if (dest_exists && !is_empty_dir(dir))
496 die(_("destination path '%s' already exists and is not "
497 "an empty directory."), dir);
498
499 strbuf_addf(&reflog_msg, "clone: from %s", repo);
500
501 if (option_bare)
502 work_tree = NULL;
503 else {
504 work_tree = getenv("GIT_WORK_TREE");
505 if (work_tree && !stat(work_tree, &buf))
506 die(_("working tree '%s' already exists."), work_tree);
507 }
508
509 if (option_bare || work_tree)
510 git_dir = xstrdup(dir);
511 else {
512 work_tree = dir;
513 git_dir = xstrdup(mkpath("%s/.git", dir));
514 }
515
516 if (!option_bare) {
517 junk_work_tree = work_tree;
518 if (safe_create_leading_directories_const(work_tree) < 0)
519 die_errno(_("could not create leading directories of '%s'"),
520 work_tree);
521 if (!dest_exists && mkdir(work_tree, 0755))
522 die_errno(_("could not create work tree dir '%s'."),
523 work_tree);
524 set_git_work_tree(work_tree);
525 }
526 junk_git_dir = git_dir;
527 atexit(remove_junk);
528 sigchain_push_common(remove_junk_on_signal);
529
530 setenv(CONFIG_ENVIRONMENT, mkpath("%s/config", git_dir), 1);
531
532 if (safe_create_leading_directories_const(git_dir) < 0)
533 die(_("could not create leading directories of '%s'"), git_dir);
534
535 set_git_dir_init(git_dir, real_git_dir, 0);
536 if (real_git_dir)
537 git_dir = real_git_dir;
538
539 if (0 <= option_verbosity) {
540 if (option_bare)
541 printf(_("Cloning into bare repository %s...\n"), dir);
542 else
543 printf(_("Cloning into %s...\n"), dir);
544 }
545 init_db(option_template, INIT_DB_QUIET);
546
547 /*
548 * At this point, the config exists, so we do not need the
549 * environment variable. We actually need to unset it, too, to
550 * re-enable parsing of the global configs.
551 */
552 unsetenv(CONFIG_ENVIRONMENT);
553
554 git_config(git_default_config, NULL);
555
556 if (option_bare) {
557 if (option_mirror)
558 src_ref_prefix = "refs/";
559 strbuf_addstr(&branch_top, src_ref_prefix);
560
561 git_config_set("core.bare", "true");
562 } else {
563 strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
564 }
565
566 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
567
568 if (option_mirror || !option_bare) {
569 /* Configure the remote */
570 strbuf_addf(&key, "remote.%s.fetch", option_origin);
571 git_config_set_multivar(key.buf, value.buf, "^$", 0);
572 strbuf_reset(&key);
573
574 if (option_mirror) {
575 strbuf_addf(&key, "remote.%s.mirror", option_origin);
576 git_config_set(key.buf, "true");
577 strbuf_reset(&key);
578 }
579 }
580
581 strbuf_addf(&key, "remote.%s.url", option_origin);
582 git_config_set(key.buf, repo);
583 strbuf_reset(&key);
584
585 if (option_reference.nr)
586 setup_reference();
587
588 fetch_pattern = value.buf;
589 refspec = parse_fetch_refspec(1, &fetch_pattern);
590
591 strbuf_reset(&value);
592
593 if (is_local) {
594 refs = clone_local(path, git_dir);
595 mapped_refs = wanted_peer_refs(refs, refspec);
596 } else {
597 struct remote *remote = remote_get(option_origin);
598 transport = transport_get(remote, remote->url[0]);
599
600 if (!transport->get_refs_list || !transport->fetch)
601 die(_("Don't know how to clone %s"), transport->url);
602
603 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
604
605 if (option_depth)
606 transport_set_option(transport, TRANS_OPT_DEPTH,
607 option_depth);
608
609 transport_set_verbosity(transport, option_verbosity, option_progress);
610
611 if (option_upload_pack)
612 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
613 option_upload_pack);
614
615 refs = transport_get_remote_refs(transport);
616 if (refs) {
617 mapped_refs = wanted_peer_refs(refs, refspec);
618 transport_fetch_refs(transport, mapped_refs);
619 }
620 }
621
622 if (refs) {
623 clear_extra_refs();
624
625 write_remote_refs(mapped_refs);
626
627 remote_head = find_ref_by_name(refs, "HEAD");
628 remote_head_points_at =
629 guess_remote_head(remote_head, mapped_refs, 0);
630
631 if (option_branch) {
632 struct strbuf head = STRBUF_INIT;
633 strbuf_addstr(&head, src_ref_prefix);
634 strbuf_addstr(&head, option_branch);
635 our_head_points_at =
636 find_ref_by_name(mapped_refs, head.buf);
637 strbuf_release(&head);
638
639 if (!our_head_points_at) {
640 warning(_("Remote branch %s not found in "
641 "upstream %s, using HEAD instead"),
642 option_branch, option_origin);
643 our_head_points_at = remote_head_points_at;
644 }
645 }
646 else
647 our_head_points_at = remote_head_points_at;
648 }
649 else {
650 warning(_("You appear to have cloned an empty repository."));
651 our_head_points_at = NULL;
652 remote_head_points_at = NULL;
653 remote_head = NULL;
654 option_no_checkout = 1;
655 if (!option_bare)
656 install_branch_config(0, "master", option_origin,
657 "refs/heads/master");
658 }
659
660 if (remote_head_points_at && !option_bare) {
661 struct strbuf head_ref = STRBUF_INIT;
662 strbuf_addstr(&head_ref, branch_top.buf);
663 strbuf_addstr(&head_ref, "HEAD");
664 create_symref(head_ref.buf,
665 remote_head_points_at->peer_ref->name,
666 reflog_msg.buf);
667 }
668
669 if (our_head_points_at) {
670 /* Local default branch link */
671 create_symref("HEAD", our_head_points_at->name, NULL);
672 if (!option_bare) {
673 const char *head = skip_prefix(our_head_points_at->name,
674 "refs/heads/");
675 update_ref(reflog_msg.buf, "HEAD",
676 our_head_points_at->old_sha1,
677 NULL, 0, DIE_ON_ERR);
678 install_branch_config(0, head, option_origin,
679 our_head_points_at->name);
680 }
681 } else if (remote_head) {
682 /* Source had detached HEAD pointing somewhere. */
683 if (!option_bare) {
684 update_ref(reflog_msg.buf, "HEAD",
685 remote_head->old_sha1,
686 NULL, REF_NODEREF, DIE_ON_ERR);
687 our_head_points_at = remote_head;
688 }
689 } else {
690 /* Nothing to checkout out */
691 if (!option_no_checkout)
692 warning(_("remote HEAD refers to nonexistent ref, "
693 "unable to checkout.\n"));
694 option_no_checkout = 1;
695 }
696
697 if (transport) {
698 transport_unlock_pack(transport);
699 transport_disconnect(transport);
700 }
701
702 if (!option_no_checkout) {
703 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
704 struct unpack_trees_options opts;
705 struct tree *tree;
706 struct tree_desc t;
707 int fd;
708
709 /* We need to be in the new work tree for the checkout */
710 setup_work_tree();
711
712 fd = hold_locked_index(lock_file, 1);
713
714 memset(&opts, 0, sizeof opts);
715 opts.update = 1;
716 opts.merge = 1;
717 opts.fn = oneway_merge;
718 opts.verbose_update = (option_verbosity > 0);
719 opts.src_index = &the_index;
720 opts.dst_index = &the_index;
721
722 tree = parse_tree_indirect(our_head_points_at->old_sha1);
723 parse_tree(tree);
724 init_tree_desc(&t, tree->buffer, tree->size);
725 unpack_trees(1, &t, &opts);
726
727 if (write_cache(fd, active_cache, active_nr) ||
728 commit_locked_index(lock_file))
729 die(_("unable to write new index file"));
730
731 err |= run_hook(NULL, "post-checkout", sha1_to_hex(null_sha1),
732 sha1_to_hex(our_head_points_at->old_sha1), "1",
733 NULL);
734
735 if (!err && option_recursive)
736 err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
737 }
738
739 strbuf_release(&reflog_msg);
740 strbuf_release(&branch_top);
741 strbuf_release(&key);
742 strbuf_release(&value);
743 junk_pid = 0;
744 return err;
745 }