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