ee5d6518c8f8ad7e713ab05fea52f6b79fa493fa
   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 */
  35static const char * const builtin_clone_usage[] = {
  36        "git clone [options] [--] <repo> [<dir>]",
  37        NULL
  38};
  39
  40static int option_no_checkout, option_bare, option_mirror;
  41static int option_local, option_no_hardlinks, option_shared, option_recursive;
  42static char *option_template, *option_depth;
  43static char *option_origin = NULL;
  44static char *option_branch = NULL;
  45static const char *real_git_dir;
  46static char *option_upload_pack = "git-upload-pack";
  47static int option_verbosity;
  48static int option_progress;
  49static struct string_list option_reference;
  50
  51static 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
  60static 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
 100static const char *argv_submodule[] = {
 101        "submodule", "update", "--init", "--recursive", NULL
 102};
 103
 104static 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
 132static 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
 201static 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
 210static int add_one_reference(struct string_list_item *item, void *cb_data)
 211{
 212        const char *ref_git;
 213        char *ref_git_copy;
 214
 215        struct remote *remote;
 216        struct transport *transport;
 217        const struct ref *extra;
 218
 219        ref_git = real_path(item->string);
 220
 221        if (is_directory(mkpath("%s/.git/objects", ref_git)))
 222                ref_git = mkpath("%s/.git", ref_git);
 223        else if (!is_directory(mkpath("%s/objects", ref_git)))
 224                die(_("reference repository '%s' is not a local directory."),
 225                    item->string);
 226
 227        ref_git_copy = xstrdup(ref_git);
 228
 229        add_to_alternates_file(ref_git_copy);
 230
 231        remote = remote_get(ref_git_copy);
 232        transport = transport_get(remote, ref_git_copy);
 233        for (extra = transport_get_remote_refs(transport); extra;
 234             extra = extra->next)
 235                add_extra_ref(extra->name, extra->old_sha1, 0);
 236
 237        transport_disconnect(transport);
 238
 239        free(ref_git_copy);
 240        return 0;
 241}
 242
 243static void setup_reference(void)
 244{
 245        for_each_string_list(&option_reference, add_one_reference, NULL);
 246}
 247
 248static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)
 249{
 250        struct dirent *de;
 251        struct stat buf;
 252        int src_len, dest_len;
 253        DIR *dir;
 254
 255        dir = opendir(src->buf);
 256        if (!dir)
 257                die_errno(_("failed to open '%s'"), src->buf);
 258
 259        if (mkdir(dest->buf, 0777)) {
 260                if (errno != EEXIST)
 261                        die_errno(_("failed to create directory '%s'"), dest->buf);
 262                else if (stat(dest->buf, &buf))
 263                        die_errno(_("failed to stat '%s'"), dest->buf);
 264                else if (!S_ISDIR(buf.st_mode))
 265                        die(_("%s exists and is not a directory"), dest->buf);
 266        }
 267
 268        strbuf_addch(src, '/');
 269        src_len = src->len;
 270        strbuf_addch(dest, '/');
 271        dest_len = dest->len;
 272
 273        while ((de = readdir(dir)) != NULL) {
 274                strbuf_setlen(src, src_len);
 275                strbuf_addstr(src, de->d_name);
 276                strbuf_setlen(dest, dest_len);
 277                strbuf_addstr(dest, de->d_name);
 278                if (stat(src->buf, &buf)) {
 279                        warning (_("failed to stat %s\n"), src->buf);
 280                        continue;
 281                }
 282                if (S_ISDIR(buf.st_mode)) {
 283                        if (de->d_name[0] != '.')
 284                                copy_or_link_directory(src, dest);
 285                        continue;
 286                }
 287
 288                if (unlink(dest->buf) && errno != ENOENT)
 289                        die_errno(_("failed to unlink '%s'"), dest->buf);
 290                if (!option_no_hardlinks) {
 291                        if (!link(src->buf, dest->buf))
 292                                continue;
 293                        if (option_local)
 294                                die_errno(_("failed to create link '%s'"), dest->buf);
 295                        option_no_hardlinks = 1;
 296                }
 297                if (copy_file_with_time(dest->buf, src->buf, 0666))
 298                        die_errno(_("failed to copy file to '%s'"), dest->buf);
 299        }
 300        closedir(dir);
 301}
 302
 303static const struct ref *clone_local(const char *src_repo,
 304                                     const char *dest_repo)
 305{
 306        const struct ref *ret;
 307        struct strbuf src = STRBUF_INIT;
 308        struct strbuf dest = STRBUF_INIT;
 309        struct remote *remote;
 310        struct transport *transport;
 311
 312        if (option_shared)
 313                add_to_alternates_file(src_repo);
 314        else {
 315                strbuf_addf(&src, "%s/objects", src_repo);
 316                strbuf_addf(&dest, "%s/objects", dest_repo);
 317                copy_or_link_directory(&src, &dest);
 318                strbuf_release(&src);
 319                strbuf_release(&dest);
 320        }
 321
 322        remote = remote_get(src_repo);
 323        transport = transport_get(remote, src_repo);
 324        ret = transport_get_remote_refs(transport);
 325        transport_disconnect(transport);
 326        if (0 <= option_verbosity)
 327                printf(_("done.\n"));
 328        return ret;
 329}
 330
 331static const char *junk_work_tree;
 332static const char *junk_git_dir;
 333static pid_t junk_pid;
 334
 335static void remove_junk(void)
 336{
 337        struct strbuf sb = STRBUF_INIT;
 338        if (getpid() != junk_pid)
 339                return;
 340        if (junk_git_dir) {
 341                strbuf_addstr(&sb, junk_git_dir);
 342                remove_dir_recursively(&sb, 0);
 343                strbuf_reset(&sb);
 344        }
 345        if (junk_work_tree) {
 346                strbuf_addstr(&sb, junk_work_tree);
 347                remove_dir_recursively(&sb, 0);
 348                strbuf_reset(&sb);
 349        }
 350}
 351
 352static void remove_junk_on_signal(int signo)
 353{
 354        remove_junk();
 355        sigchain_pop(signo);
 356        raise(signo);
 357}
 358
 359static struct ref *wanted_peer_refs(const struct ref *refs,
 360                struct refspec *refspec)
 361{
 362        struct ref *local_refs = NULL;
 363        struct ref **tail = &local_refs;
 364
 365        get_fetch_map(refs, refspec, &tail, 0);
 366        if (!option_mirror)
 367                get_fetch_map(refs, tag_refspec, &tail, 0);
 368
 369        return local_refs;
 370}
 371
 372static void write_remote_refs(const struct ref *local_refs)
 373{
 374        const struct ref *r;
 375
 376        for (r = local_refs; r; r = r->next)
 377                add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
 378
 379        pack_refs(PACK_REFS_ALL);
 380        clear_extra_refs();
 381}
 382
 383int cmd_clone(int argc, const char **argv, const char *prefix)
 384{
 385        int is_bundle = 0, is_local;
 386        struct stat buf;
 387        const char *repo_name, *repo, *work_tree, *git_dir;
 388        char *path, *dir;
 389        int dest_exists;
 390        const struct ref *refs, *remote_head;
 391        const struct ref *remote_head_points_at;
 392        const struct ref *our_head_points_at;
 393        struct ref *mapped_refs;
 394        struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
 395        struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
 396        struct transport *transport = NULL;
 397        char *src_ref_prefix = "refs/heads/";
 398        int err = 0;
 399
 400        struct refspec *refspec;
 401        const char *fetch_pattern;
 402
 403        junk_pid = getpid();
 404
 405        packet_trace_identity("clone");
 406        argc = parse_options(argc, argv, prefix, builtin_clone_options,
 407                             builtin_clone_usage, 0);
 408
 409        if (argc > 2)
 410                usage_msg_opt(_("Too many arguments."),
 411                        builtin_clone_usage, builtin_clone_options);
 412
 413        if (argc == 0)
 414                usage_msg_opt(_("You must specify a repository to clone."),
 415                        builtin_clone_usage, builtin_clone_options);
 416
 417        if (option_mirror)
 418                option_bare = 1;
 419
 420        if (option_bare) {
 421                if (option_origin)
 422                        die(_("--bare and --origin %s options are incompatible."),
 423                            option_origin);
 424                option_no_checkout = 1;
 425        }
 426
 427        if (!option_origin)
 428                option_origin = "origin";
 429
 430        repo_name = argv[0];
 431
 432        path = get_repo_path(repo_name, &is_bundle);
 433        if (path)
 434                repo = xstrdup(absolute_path(repo_name));
 435        else if (!strchr(repo_name, ':'))
 436                die(_("repository '%s' does not exist"), repo_name);
 437        else
 438                repo = repo_name;
 439        is_local = path && !is_bundle;
 440        if (is_local && option_depth)
 441                warning(_("--depth is ignored in local clones; use file:// instead."));
 442
 443        if (argc == 2)
 444                dir = xstrdup(argv[1]);
 445        else
 446                dir = guess_dir_name(repo_name, is_bundle, option_bare);
 447        strip_trailing_slashes(dir);
 448
 449        dest_exists = !stat(dir, &buf);
 450        if (dest_exists && !is_empty_dir(dir))
 451                die(_("destination path '%s' already exists and is not "
 452                        "an empty directory."), dir);
 453
 454        strbuf_addf(&reflog_msg, "clone: from %s", repo);
 455
 456        if (option_bare)
 457                work_tree = NULL;
 458        else {
 459                work_tree = getenv("GIT_WORK_TREE");
 460                if (work_tree && !stat(work_tree, &buf))
 461                        die(_("working tree '%s' already exists."), work_tree);
 462        }
 463
 464        if (option_bare || work_tree)
 465                git_dir = xstrdup(dir);
 466        else {
 467                work_tree = dir;
 468                git_dir = xstrdup(mkpath("%s/.git", dir));
 469        }
 470
 471        if (!option_bare) {
 472                junk_work_tree = work_tree;
 473                if (safe_create_leading_directories_const(work_tree) < 0)
 474                        die_errno(_("could not create leading directories of '%s'"),
 475                                  work_tree);
 476                if (!dest_exists && mkdir(work_tree, 0755))
 477                        die_errno(_("could not create work tree dir '%s'."),
 478                                  work_tree);
 479                set_git_work_tree(work_tree);
 480        }
 481        junk_git_dir = git_dir;
 482        atexit(remove_junk);
 483        sigchain_push_common(remove_junk_on_signal);
 484
 485        setenv(CONFIG_ENVIRONMENT, mkpath("%s/config", git_dir), 1);
 486
 487        if (safe_create_leading_directories_const(git_dir) < 0)
 488                die(_("could not create leading directories of '%s'"), git_dir);
 489
 490        set_git_dir_init(git_dir, real_git_dir, 0);
 491        if (real_git_dir)
 492                git_dir = real_git_dir;
 493
 494        if (0 <= option_verbosity) {
 495                if (option_bare)
 496                        printf(_("Cloning into bare repository %s...\n"), dir);
 497                else
 498                        printf(_("Cloning into %s...\n"), dir);
 499        }
 500        init_db(option_template, INIT_DB_QUIET);
 501
 502        /*
 503         * At this point, the config exists, so we do not need the
 504         * environment variable.  We actually need to unset it, too, to
 505         * re-enable parsing of the global configs.
 506         */
 507        unsetenv(CONFIG_ENVIRONMENT);
 508
 509        git_config(git_default_config, NULL);
 510
 511        if (option_bare) {
 512                if (option_mirror)
 513                        src_ref_prefix = "refs/";
 514                strbuf_addstr(&branch_top, src_ref_prefix);
 515
 516                git_config_set("core.bare", "true");
 517        } else {
 518                strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
 519        }
 520
 521        strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
 522
 523        if (option_mirror || !option_bare) {
 524                /* Configure the remote */
 525                strbuf_addf(&key, "remote.%s.fetch", option_origin);
 526                git_config_set_multivar(key.buf, value.buf, "^$", 0);
 527                strbuf_reset(&key);
 528
 529                if (option_mirror) {
 530                        strbuf_addf(&key, "remote.%s.mirror", option_origin);
 531                        git_config_set(key.buf, "true");
 532                        strbuf_reset(&key);
 533                }
 534        }
 535
 536        strbuf_addf(&key, "remote.%s.url", option_origin);
 537        git_config_set(key.buf, repo);
 538        strbuf_reset(&key);
 539
 540        if (option_reference.nr)
 541                setup_reference();
 542
 543        fetch_pattern = value.buf;
 544        refspec = parse_fetch_refspec(1, &fetch_pattern);
 545
 546        strbuf_reset(&value);
 547
 548        if (is_local) {
 549                refs = clone_local(path, git_dir);
 550                mapped_refs = wanted_peer_refs(refs, refspec);
 551        } else {
 552                struct remote *remote = remote_get(option_origin);
 553                transport = transport_get(remote, remote->url[0]);
 554
 555                if (!transport->get_refs_list || !transport->fetch)
 556                        die(_("Don't know how to clone %s"), transport->url);
 557
 558                transport_set_option(transport, TRANS_OPT_KEEP, "yes");
 559
 560                if (option_depth)
 561                        transport_set_option(transport, TRANS_OPT_DEPTH,
 562                                             option_depth);
 563
 564                transport_set_verbosity(transport, option_verbosity, option_progress);
 565
 566                if (option_upload_pack)
 567                        transport_set_option(transport, TRANS_OPT_UPLOADPACK,
 568                                             option_upload_pack);
 569
 570                refs = transport_get_remote_refs(transport);
 571                if (refs) {
 572                        mapped_refs = wanted_peer_refs(refs, refspec);
 573                        transport_fetch_refs(transport, mapped_refs);
 574                }
 575        }
 576
 577        if (refs) {
 578                clear_extra_refs();
 579
 580                write_remote_refs(mapped_refs);
 581
 582                remote_head = find_ref_by_name(refs, "HEAD");
 583                remote_head_points_at =
 584                        guess_remote_head(remote_head, mapped_refs, 0);
 585
 586                if (option_branch) {
 587                        struct strbuf head = STRBUF_INIT;
 588                        strbuf_addstr(&head, src_ref_prefix);
 589                        strbuf_addstr(&head, option_branch);
 590                        our_head_points_at =
 591                                find_ref_by_name(mapped_refs, head.buf);
 592                        strbuf_release(&head);
 593
 594                        if (!our_head_points_at) {
 595                                warning(_("Remote branch %s not found in "
 596                                        "upstream %s, using HEAD instead"),
 597                                        option_branch, option_origin);
 598                                our_head_points_at = remote_head_points_at;
 599                        }
 600                }
 601                else
 602                        our_head_points_at = remote_head_points_at;
 603        }
 604        else {
 605                warning(_("You appear to have cloned an empty repository."));
 606                our_head_points_at = NULL;
 607                remote_head_points_at = NULL;
 608                remote_head = NULL;
 609                option_no_checkout = 1;
 610                if (!option_bare)
 611                        install_branch_config(0, "master", option_origin,
 612                                              "refs/heads/master");
 613        }
 614
 615        if (remote_head_points_at && !option_bare) {
 616                struct strbuf head_ref = STRBUF_INIT;
 617                strbuf_addstr(&head_ref, branch_top.buf);
 618                strbuf_addstr(&head_ref, "HEAD");
 619                create_symref(head_ref.buf,
 620                              remote_head_points_at->peer_ref->name,
 621                              reflog_msg.buf);
 622        }
 623
 624        if (our_head_points_at) {
 625                /* Local default branch link */
 626                create_symref("HEAD", our_head_points_at->name, NULL);
 627                if (!option_bare) {
 628                        const char *head = skip_prefix(our_head_points_at->name,
 629                                                       "refs/heads/");
 630                        update_ref(reflog_msg.buf, "HEAD",
 631                                   our_head_points_at->old_sha1,
 632                                   NULL, 0, DIE_ON_ERR);
 633                        install_branch_config(0, head, option_origin,
 634                                              our_head_points_at->name);
 635                }
 636        } else if (remote_head) {
 637                /* Source had detached HEAD pointing somewhere. */
 638                if (!option_bare) {
 639                        update_ref(reflog_msg.buf, "HEAD",
 640                                   remote_head->old_sha1,
 641                                   NULL, REF_NODEREF, DIE_ON_ERR);
 642                        our_head_points_at = remote_head;
 643                }
 644        } else {
 645                /* Nothing to checkout out */
 646                if (!option_no_checkout)
 647                        warning(_("remote HEAD refers to nonexistent ref, "
 648                                "unable to checkout.\n"));
 649                option_no_checkout = 1;
 650        }
 651
 652        if (transport) {
 653                transport_unlock_pack(transport);
 654                transport_disconnect(transport);
 655        }
 656
 657        if (!option_no_checkout) {
 658                struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
 659                struct unpack_trees_options opts;
 660                struct tree *tree;
 661                struct tree_desc t;
 662                int fd;
 663
 664                /* We need to be in the new work tree for the checkout */
 665                setup_work_tree();
 666
 667                fd = hold_locked_index(lock_file, 1);
 668
 669                memset(&opts, 0, sizeof opts);
 670                opts.update = 1;
 671                opts.merge = 1;
 672                opts.fn = oneway_merge;
 673                opts.verbose_update = (option_verbosity > 0);
 674                opts.src_index = &the_index;
 675                opts.dst_index = &the_index;
 676
 677                tree = parse_tree_indirect(our_head_points_at->old_sha1);
 678                parse_tree(tree);
 679                init_tree_desc(&t, tree->buffer, tree->size);
 680                unpack_trees(1, &t, &opts);
 681
 682                if (write_cache(fd, active_cache, active_nr) ||
 683                    commit_locked_index(lock_file))
 684                        die(_("unable to write new index file"));
 685
 686                err |= run_hook(NULL, "post-checkout", sha1_to_hex(null_sha1),
 687                                sha1_to_hex(our_head_points_at->old_sha1), "1",
 688                                NULL);
 689
 690                if (!err && option_recursive)
 691                        err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
 692        }
 693
 694        strbuf_release(&reflog_msg);
 695        strbuf_release(&branch_top);
 696        strbuf_release(&key);
 697        strbuf_release(&value);
 698        junk_pid = 0;
 699        return err;
 700}