Code

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