{"thread":{"id":"11948","subject":"[PATCH v2 11/11] Build in checkout","startedAt":"2008-02-07T16:40:23Z","lastAt":"2008-02-14T18:55:16Z","messageCount":4,"participants":["Daniel Barkalow","Johannes Schindelin"],"isPatch":true,"patchVersion":2,"patchTotal":11},"messages":[{"id":"67828","messageId":"alpine.LNX.1.00.0802071137490.13593@iabervon.org","threadId":"11948","inReplyTo":null,"subject":"[PATCH v2 11/11] Build in checkout","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2008-02-07T16:40:23Z","receivedAt":"2008-02-07T16:40:23Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"The only differences in behavior should be:\n\n - git checkout -m with non-trivial merging won't print out\n   merge-recursive messages (see the change in t7201-co.sh)\n\n - git checkout -- paths... will give a sensible error message if\n   HEAD is invalid as a commit.\n\n - some intermediate states which were written to disk in the shell\n   version (in particular, index states) are only kept in memory in\n   this version, and therefore these can no longer be revealed by\n   later write operations becoming impossible.\n\n - when we change branches, we discard MERGE_MSG, SQUASH_MSG, and\n   rr-cache/MERGE_RR, like reset always has.\n\nI'm not 100% sure I got the merge recursive setup exactly right; the\nbase for a non-trivial merge in the shell code doesn't seem\ntheoretically justified to me, but I tried to match it anyway, and the\ntests all pass this way.\n\nOther than these items, the results should be identical to the shell\nversion, so far as I can tell.\n\nSigned-off-by: Daniel Barkalow <barkalow@iabervon.org>\n---\n Makefile                                           |    3 +-\n builtin-checkout.c                                 |  479 ++++++++++++++++++++\n builtin.h                                          |    1 +\n .../examples/git-checkout.sh                       |    0 \n git.c                                              |    1 +\n t/t7201-co.sh                                      |    7 -\n 6 files changed, 483 insertions(+), 8 deletions(-)\n create mode 100644 builtin-checkout.c\n rename git-checkout.sh => contrib/examples/git-checkout.sh (100%)\n\ndiff --git a/Makefile b/Makefile\nindex cc0643e..2b5093e 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -223,7 +223,7 @@ BASIC_CFLAGS =\n BASIC_LDFLAGS =\n \n SCRIPT_SH = \\\n-\tgit-bisect.sh git-checkout.sh \\\n+\tgit-bisect.sh \\\n \tgit-clone.sh \\\n \tgit-merge-one-file.sh git-mergetool.sh git-parse-remote.sh \\\n \tgit-pull.sh git-rebase.sh git-rebase--interactive.sh \\\n@@ -335,6 +335,7 @@ BUILTIN_OBJS = \\\n \tbuiltin-bundle.o \\\n \tbuiltin-cat-file.o \\\n \tbuiltin-check-attr.o \\\n+\tbuiltin-checkout.o \\\n \tbuiltin-checkout-index.o \\\n \tbuiltin-check-ref-format.o \\\n \tbuiltin-clean.o \\\ndiff --git a/builtin-checkout.c b/builtin-checkout.c\nnew file mode 100644\nindex 0000000..0894eae\n--- /dev/null\n+++ b/builtin-checkout.c\n@@ -0,0 +1,479 @@\n+#include \"cache.h\"\n+#include \"builtin.h\"\n+#include \"parse-options.h\"\n+#include \"refs.h\"\n+#include \"commit.h\"\n+#include \"tree.h\"\n+#include \"tree-walk.h\"\n+#include \"unpack-trees.h\"\n+#include \"dir.h\"\n+#include \"run-command.h\"\n+#include \"merge-recursive.h\"\n+#include \"branch.h\"\n+#include \"diff.h\"\n+#include \"revision.h\"\n+\n+static const char * const checkout_usage[] = {\n+\t\"git checkout [options] <branch>\",\n+\t\"git checkout [options] [<branch>] -- <file>...\",\n+\tNULL,\n+};\n+\n+static int post_checkout_hook(struct commit *old, struct commit *new,\n+\t\t\t      int changed)\n+{\n+\tstruct child_process proc;\n+\tconst char *name = git_path(\"hooks/post-checkout\");\n+\tconst char *argv[5];\n+\n+\tif (access(name, X_OK) < 0)\n+\t\treturn 0;\n+\n+\tmemset(&proc, 0, sizeof(proc));\n+\targv[0] = name;\n+\targv[1] = xstrdup(sha1_to_hex(old->object.sha1));\n+\targv[2] = xstrdup(sha1_to_hex(new->object.sha1));\n+\targv[3] = changed ? \"1\" : \"0\";\n+\targv[4] = NULL;\n+\tproc.argv = argv;\n+\tproc.no_stdin = 1;\n+\tproc.stdout_to_stderr = 1;\n+\treturn run_command(&proc);\n+}\n+\n+static int update_some(const unsigned char *sha1, const char *base, int baselen,\n+\t\t       const char *pathname, unsigned mode, int stage)\n+{\n+\tint len;\n+\tstruct cache_entry *ce;\n+\n+\tif (S_ISGITLINK(mode))\n+\t\treturn 0;\n+\n+\tif (S_ISDIR(mode))\n+\t\treturn READ_TREE_RECURSIVE;\n+\n+\tlen = baselen + strlen(pathname);\n+\tce = xcalloc(1, cache_entry_size(len));\n+\thashcpy(ce->sha1, sha1);\n+\tmemcpy(ce->name, base, baselen);\n+\tmemcpy(ce->name + baselen, pathname, len - baselen);\n+\tce->ce_flags = create_ce_flags(len, 0);\n+\tce->ce_mode = create_ce_mode(mode);\n+\tadd_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);\n+\treturn 0;\n+}\n+\n+static int read_tree_some(struct tree *tree, const char **pathspec)\n+{\n+\tint newfd;\n+\tstruct lock_file lock_file;\n+\tnewfd = hold_locked_index(&lock_file, 1);\n+\tread_cache();\n+\n+\tread_tree_recursive(tree, \"\", 0, 0, pathspec, update_some);\n+\n+\tif (write_cache(newfd, active_cache, active_nr) ||\n+\t    commit_locked_index(&lock_file))\n+\t\tdie(\"unable to write new index file\");\n+\n+\t/* update the index with the given tree's info\n+\t * for all args, expanding wildcards, and exit\n+\t * with any non-zero return code.\n+\t */\n+\treturn 0;\n+}\n+\n+static int checkout_paths(const char **pathspec)\n+{\n+\tint pos;\n+\tstruct checkout state;\n+\tstatic char *ps_matched;\n+\tunsigned char rev[20];\n+\tint flag;\n+\tstruct commit *head;\n+\n+\tfor (pos = 0; pathspec[pos]; pos++)\n+\t\t;\n+\tps_matched = xcalloc(1, pos);\n+\n+\tfor (pos = 0; pos < active_nr; pos++) {\n+\t\tstruct cache_entry *ce = active_cache[pos];\n+\t\tpathspec_match(pathspec, ps_matched, ce->name, 0);\n+\t}\n+\n+\tif (report_path_error(ps_matched, pathspec, 0))\n+\t\treturn 1;\n+\n+\tmemset(&state, 0, sizeof(state));\n+\tstate.force = 1;\n+\tstate.refresh_cache = 1;\n+\tfor (pos = 0; pos < active_nr; pos++) {\n+\t\tstruct cache_entry *ce = active_cache[pos];\n+\t\tif (pathspec_match(pathspec, NULL, ce->name, 0)) {\n+\t\t\tcheckout_entry(ce, &state, NULL);\n+\t\t}\n+\t}\n+\n+\tresolve_ref(\"HEAD\", rev, 0, &flag);\n+\thead = lookup_commit_reference_gently(rev, 1);\n+\n+\treturn post_checkout_hook(head, head, 0);\n+}\n+\n+static void show_local_changes(struct object *head)\n+{\n+\tstruct rev_info rev;\n+\t/* I think we want full paths, even if we're in a subdirectory. */\n+\tinit_revisions(&rev, NULL);\n+\trev.abbrev = 0;\n+\trev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS;\n+\tadd_pending_object(&rev, head, NULL);\n+\trun_diff_index(&rev, 0);\n+}\n+\n+static void describe_detached_head(char *msg, struct commit *commit)\n+{\n+\tstruct strbuf sb;\n+\tstrbuf_init(&sb, 0);\n+\tparse_commit(commit);\n+\tpretty_print_commit(CMIT_FMT_ONELINE, commit, &sb, 0, \"\", \"\", 0, 0);\n+\tfprintf(stderr, \"%s %s... %s\\n\", msg,\n+\t\tfind_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf);\n+\tstrbuf_release(&sb);\n+}\n+\n+static int reset_to_new(struct tree *tree, int quiet)\n+{\n+\tstruct unpack_trees_options opts;\n+\tstruct tree_desc tree_desc;\n+\tmemset(&opts, 0, sizeof(opts));\n+\topts.head_idx = -1;\n+\topts.update = 1;\n+\topts.reset = 1;\n+\topts.merge = 1;\n+\topts.fn = oneway_merge;\n+\topts.verbose_update = !quiet;\n+\tparse_tree(tree);\n+\tinit_tree_desc(&tree_desc, tree->buffer, tree->size);\n+\tif (unpack_trees(1, &tree_desc, &opts))\n+\t\treturn 128;\n+\treturn 0;\n+}\n+\n+static void reset_clean_to_new(struct tree *tree, int quiet)\n+{\n+\tstruct unpack_trees_options opts;\n+\tstruct tree_desc tree_desc;\n+\tmemset(&opts, 0, sizeof(opts));\n+\topts.head_idx = -1;\n+\topts.skip_unmerged = 1;\n+\topts.reset = 1;\n+\topts.merge = 1;\n+\topts.fn = oneway_merge;\n+\topts.verbose_update = !quiet;\n+\tparse_tree(tree);\n+\tinit_tree_desc(&tree_desc, tree->buffer, tree->size);\n+\tif (unpack_trees(1, &tree_desc, &opts))\n+\t\texit(128);\n+}\n+\n+struct checkout_opts {\n+\tint quiet;\n+\tint merge;\n+\tint force;\n+\n+\tchar *new_branch;\n+\tint new_branch_log;\n+\tint track;\n+};\n+\n+struct branch_info {\n+\tconst char *name; /* The short name used */\n+\tconst char *path; /* The full name of a real branch */\n+\tstruct commit *commit; /* The named commit */\n+};\n+\n+static void setup_branch_path(struct branch_info *branch)\n+{\n+\tstruct strbuf buf;\n+\tstrbuf_init(&buf, 0);\n+\tstrbuf_addstr(&buf, \"refs/heads/\");\n+\tstrbuf_addstr(&buf, branch->name);\n+\tbranch->path = strbuf_detach(&buf, NULL);\n+}\n+\n+static int merge_working_tree(struct checkout_opts *opts,\n+\t\t\t      struct branch_info *old, struct branch_info *new,\n+\t\t\t      const char *prefix)\n+{\n+\tint ret;\n+\tstruct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));\n+\tint newfd = hold_locked_index(lock_file, 1);\n+\tread_cache();\n+\n+\tif (opts->force) {\n+\t\tret = reset_to_new(new->commit->tree, opts->quiet);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t} else {\n+\t\tstruct tree_desc trees[2];\n+\t\tstruct tree *tree;\n+\t\tstruct unpack_trees_options topts;\n+\t\tmemset(&topts, 0, sizeof(topts));\n+\t\ttopts.head_idx = -1;\n+\n+\t\trefresh_cache(REFRESH_QUIET);\n+\n+\t\tif (unmerged_cache()) {\n+\t\t\tret = opts->merge ? -1 :\n+\t\t\t\terror(\"you need to resolve your current index first\");\n+\t\t} else {\n+\t\t\ttopts.update = 1;\n+\t\t\ttopts.merge = 1;\n+\t\t\ttopts.gently = opts->merge;\n+\t\t\ttopts.fn = twoway_merge;\n+\t\t\ttopts.dir = xcalloc(1, sizeof(*topts.dir));\n+\t\t\ttopts.dir->show_ignored = 1;\n+\t\t\ttopts.dir->exclude_per_dir = \".gitignore\";\n+\t\t\ttopts.prefix = prefix;\n+\t\t\ttree = parse_tree_indirect(old->commit->object.sha1);\n+\t\t\tinit_tree_desc(&trees[0], tree->buffer, tree->size);\n+\t\t\ttree = parse_tree_indirect(new->commit->object.sha1);\n+\t\t\tinit_tree_desc(&trees[1], tree->buffer, tree->size);\n+\t\t\tret = unpack_trees(2, trees, &topts);\n+\t\t}\n+\t\tif (ret) {\n+\t\t\t/*\n+\t\t\t * Unpack couldn't do a trivial merge; either\n+\t\t\t * give up or do a real merge, depending on\n+\t\t\t * whether the merge flag was used.\n+\t\t\t */\n+\t\t\tstruct tree *result;\n+\t\t\tstruct tree *work;\n+\t\t\tif (!opts->merge)\n+\t\t\t\treturn 1;\n+\t\t\tparse_commit(old->commit);\n+\n+\t\t\t/* Do more real merge */\n+\n+\t\t\t/*\n+\t\t\t * We update the index fully, then write the\n+\t\t\t * tree from the index, then merge the new\n+\t\t\t * branch with the current tree, with the old\n+\t\t\t * branch as the base. Then we reset the index\n+\t\t\t * (but not the working tree) to the new\n+\t\t\t * branch, leaving the working tree as the\n+\t\t\t * merged version, but skipping unmerged\n+\t\t\t * entries in the index.\n+\t\t\t */\n+\n+\t\t\tadd_files_to_cache(0, NULL, NULL);\n+\t\t\twork = write_tree_from_memory();\n+\n+\t\t\tret = reset_to_new(new->commit->tree, opts->quiet);\n+\t\t\tif (ret)\n+\t\t\t\treturn ret;\n+\t\t\tmerge_trees(new->commit->tree, work, old->commit->tree,\n+\t\t\t\t    new->name, \"local\", &result);\n+\t\t\treset_clean_to_new(new->commit->tree, opts->quiet);\n+\t\t}\n+\t}\n+\n+\tif (write_cache(newfd, active_cache, active_nr) ||\n+\t    commit_locked_index(lock_file))\n+\t\tdie(\"unable to write new index file\");\n+\n+\tif (!opts->force)\n+\t\tshow_local_changes(&new->commit->object);\n+\n+\treturn 0;\n+}\n+\n+static void update_refs_for_switch(struct checkout_opts *opts,\n+\t\t\t\t   struct branch_info *old,\n+\t\t\t\t   struct branch_info *new)\n+{\n+\tstruct strbuf msg;\n+\tconst char *old_desc;\n+\tif (opts->new_branch) {\n+\t\tcreate_branch(old->name, opts->new_branch, new->name, 0,\n+\t\t\t      opts->new_branch_log, opts->track);\n+\t\tnew->name = opts->new_branch;\n+\t\tsetup_branch_path(new);\n+\t}\n+\n+\tstrbuf_init(&msg, 0);\n+\told_desc = old->name;\n+\tif (!old_desc)\n+\t\told_desc = sha1_to_hex(old->commit->object.sha1);\n+\tstrbuf_addf(&msg, \"checkout: moving from %s to %s\",\n+\t\t    old_desc, new->name);\n+\n+\tif (new->path) {\n+\t\tcreate_symref(\"HEAD\", new->path, msg.buf);\n+\t\tif (!opts->quiet) {\n+\t\t\tif (old->path && !strcmp(new->path, old->path))\n+\t\t\t\tfprintf(stderr, \"Already on \\\"%s\\\"\\n\",\n+\t\t\t\t\tnew->name);\n+\t\t\telse\n+\t\t\t\tfprintf(stderr, \"Switched to%s branch \\\"%s\\\"\\n\",\n+\t\t\t\t\topts->new_branch ? \" a new\" : \"\",\n+\t\t\t\t\tnew->name);\n+\t\t}\n+\t} else if (strcmp(new->name, \"HEAD\")) {\n+\t\tupdate_ref(msg.buf, \"HEAD\", new->commit->object.sha1, NULL,\n+\t\t\t   REF_NODEREF, DIE_ON_ERR);\n+\t\tif (!opts->quiet) {\n+\t\t\tif (old->path)\n+\t\t\t\tfprintf(stderr, \"Note: moving to \\\"%s\\\" which isn't a local branch\\nIf you want to create a new branch from this checkout, you may do so\\n(now or later) by using -b with the checkout command again. Example:\\n  git checkout -b <new_branch_name>\\n\", new->name);\n+\t\t\tdescribe_detached_head(\"HEAD is now at\", new->commit);\n+\t\t}\n+\t}\n+\tremove_branch_state();\n+\tstrbuf_release(&msg);\n+}\n+\n+static int switch_branches(struct checkout_opts *opts,\n+\t\t\t   struct branch_info *new, const char *prefix)\n+{\n+\tint ret = 0;\n+\tstruct branch_info old;\n+\tunsigned char rev[20];\n+\tint flag;\n+\tmemset(&old, 0, sizeof(old));\n+\told.path = resolve_ref(\"HEAD\", rev, 0, &flag);\n+\told.commit = lookup_commit_reference_gently(rev, 1);\n+\tif (!(flag & REF_ISSYMREF))\n+\t\told.path = NULL;\n+\n+\tif (old.path && !prefixcmp(old.path, \"refs/heads/\"))\n+\t\told.name = old.path + strlen(\"refs/heads/\");\n+\n+\tif (!new->name) {\n+\t\tnew->name = \"HEAD\";\n+\t\tnew->commit = old.commit;\n+\t\tif (!new->commit)\n+\t\t\tdie(\"You are on a branch yet to be born\");\n+\t\tparse_commit(new->commit);\n+\t}\n+\n+\t/*\n+\t * If the new thing isn't a branch and isn't HEAD and we're\n+\t * not starting a new branch, and we want messages, and we\n+\t * weren't on a branch, and we're moving to a new commit,\n+\t * describe the old commit.\n+\t */\n+\tif (!new->path && strcmp(new->name, \"HEAD\") && !opts->new_branch &&\n+\t    !opts->quiet && !old.path && new->commit != old.commit)\n+\t\tdescribe_detached_head(\"Previous HEAD position was\", old.commit);\n+\n+\tif (!old.commit) {\n+\t\tif (!opts->quiet) {\n+\t\t\tfprintf(stderr, \"warning: You appear to be on a branch yet to be born.\\n\");\n+\t\t\tfprintf(stderr, \"warning: Forcing checkout of %s.\\n\", new->name);\n+\t\t}\n+\t\topts->force = 1;\n+\t}\n+\n+\tret = merge_working_tree(opts, &old, new, prefix);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tupdate_refs_for_switch(opts, &old, new);\n+\n+\treturn post_checkout_hook(old.commit, new->commit, 1);\n+}\n+\n+static int branch_track = 0;\n+\n+static int git_checkout_config(const char *var, const char *value)\n+{\n+\tif (!strcmp(var, \"branch.autosetupmerge\"))\n+\t\tbranch_track = git_config_bool(var, value);\n+\n+\treturn git_default_config(var, value);\n+}\n+\n+int cmd_checkout(int argc, const char **argv, const char *prefix)\n+{\n+\tstruct checkout_opts opts;\n+\tunsigned char rev[20];\n+\tconst char *arg;\n+\tstruct branch_info new;\n+\tstruct tree *source_tree = NULL;\n+\n+\tmemset(&opts, 0, sizeof(opts));\n+\n+\tmemset(&new, 0, sizeof(new));\n+\n+\tstruct option options[] = {\n+\t\tOPT__QUIET(&opts.quiet),\n+\t\tOPT_STRING('b', NULL, &opts.new_branch, \"new branch\", \"branch\"),\n+\t\tOPT_BOOLEAN('l', NULL, &opts.new_branch_log, \"log for new branch\"),\n+\t\tOPT_BOOLEAN( 0 , \"track\", &opts.track, \"track\"),\n+\t\tOPT_BOOLEAN('f', NULL, &opts.force, \"force\"),\n+\t\tOPT_BOOLEAN('m', NULL, &opts.merge, \"merge\"),\n+\t};\n+\n+\tgit_config(git_checkout_config);\n+\n+\topts.track = branch_track;\n+\n+\targc = parse_options(argc, argv, options, checkout_usage, 0);\n+\tif (argc) {\n+\t\targ = argv[0];\n+\t\tif (get_sha1(arg, rev))\n+\t\t\t;\n+\t\telse if ((new.commit = lookup_commit_reference_gently(rev, 1))) {\n+\t\t\tnew.name = arg;\n+\t\t\tsetup_branch_path(&new);\n+\t\t\tif (resolve_ref(new.path, rev, 1, NULL))\n+\t\t\t\tnew.commit = lookup_commit_reference(rev);\n+\t\t\telse\n+\t\t\t\tnew.path = NULL;\n+\t\t\tparse_commit(new.commit);\n+\t\t\tsource_tree = new.commit->tree;\n+\t\t\targv++;\n+\t\t\targc--;\n+\t\t} else if ((source_tree = parse_tree_indirect(rev))) {\n+\t\t\targv++;\n+\t\t\targc--;\n+\t\t}\n+\t}\n+\n+\tif (argc && !strcmp(argv[0], \"--\")) {\n+\t\targv++;\n+\t\targc--;\n+\t}\n+\n+\tif (!opts.new_branch && (opts.track != branch_track))\n+\t\tdie(\"git checkout: --track and --no-track require -b\");\n+\n+\tif (opts.force && opts.merge)\n+\t\tdie(\"git checkout: -f and -m are incompatible\");\n+\n+\tif (argc) {\n+\t\tconst char **pathspec = get_pathspec(prefix, argv);\n+\t\t/* Checkout paths */\n+\t\tif (opts.new_branch || opts.force || opts.merge) {\n+\t\t\tif (argc == 1) {\n+\t\t\t\tdie(\"git checkout: updating paths is incompatible with switching branches/forcing\\nDid you intend to checkout '%s' which can not be resolved as commit?\", argv[0]);\n+\t\t\t} else {\n+\t\t\t\tdie(\"git checkout: updating paths is incompatible with switching branches/forcing\");\n+\t\t\t}\n+\t\t}\n+\n+\t\tif (source_tree)\n+\t\t\tread_tree_some(source_tree, pathspec);\n+\t\telse\n+\t\t\tread_cache();\n+\t\treturn checkout_paths(pathspec);\n+\t}\n+\n+\tif (new.name && !new.commit) {\n+\t\tdie(\"Cannot switch branch to a non-commit.\");\n+\t}\n+\n+\treturn switch_branches(&opts, &new, prefix);\n+}\ndiff --git a/builtin.h b/builtin.h\nindex 16b85c6..674c8a1 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -18,6 +18,7 @@ extern int cmd_blame(int argc, const char **argv, const char *prefix);\n extern int cmd_branch(int argc, const char **argv, const char *prefix);\n extern int cmd_bundle(int argc, const char **argv, const char *prefix);\n extern int cmd_cat_file(int argc, const char **argv, const char *prefix);\n+extern int cmd_checkout(int argc, const char **argv, const char *prefix);\n extern int cmd_checkout_index(int argc, const char **argv, const char *prefix);\n extern int cmd_check_attr(int argc, const char **argv, const char *prefix);\n extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);\ndiff --git a/git-checkout.sh b/contrib/examples/git-checkout.sh\nsimilarity index 100%\nrename from git-checkout.sh\nrename to contrib/examples/git-checkout.sh\ndiff --git a/git.c b/git.c\nindex 114ea75..fc15686 100644\n--- a/git.c\n+++ b/git.c\n@@ -287,6 +287,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"branch\", cmd_branch, RUN_SETUP },\n \t\t{ \"bundle\", cmd_bundle },\n \t\t{ \"cat-file\", cmd_cat_file, RUN_SETUP },\n+\t\t{ \"checkout\", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"checkout-index\", cmd_checkout_index,\n \t\t\tRUN_SETUP | NEED_WORK_TREE},\n \t\t{ \"check-ref-format\", cmd_check_ref_format },\ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 42cf0ab..06959d9 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -103,13 +103,6 @@ test_expect_success \"checkout -m with dirty tree\" '\n \ttest \"$(git symbolic-ref HEAD)\" = \"refs/heads/side\" &&\n \n \t(cat >expect.messages <<EOF\n-Merging side with local\n-Merging:\n-ab76817 Side M one, D two, A three\n-virtual local\n-found 1 common ancestor(s):\n-7329388 Initial A one, A two\n-Auto-merged one\n M\tone\n EOF\n ) &&\n-- \n1.5.4\n"},{"id":"68758","messageId":"alpine.LSU.1.00.0802141816120.30505@racer.site","threadId":"11948","inReplyTo":"alpine.LNX.1.00.0802071137490.13593@iabervon.org","subject":"[PATCH] builtin-checkout: use struct lock_file correctly","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2008-02-14T18:17:05Z","receivedAt":"2008-02-14T18:17:05Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"\nA lock_file instance must not be cleaned up, since an atexit() handler\nwill try to access even correctly committed lock_files, since it has\nto make sure that they were correctly committed.\n\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n---\n\n\tI am using builtin checkout since quite some time, but this is the \n\tfirst time it crashed... That fix helps; you might want to amend \n\tyour patch 11/11.\n\n builtin-checkout.c |    6 +++---\n 1 files changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin-checkout.c b/builtin-checkout.c\nindex 0894eae..12000b1 100644\n--- a/builtin-checkout.c\n+++ b/builtin-checkout.c\n@@ -67,14 +67,14 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,\n static int read_tree_some(struct tree *tree, const char **pathspec)\n {\n \tint newfd;\n-\tstruct lock_file lock_file;\n-\tnewfd = hold_locked_index(&lock_file, 1);\n+\tstruct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));\n+\tnewfd = hold_locked_index(lock_file, 1);\n \tread_cache();\n \n \tread_tree_recursive(tree, \"\", 0, 0, pathspec, update_some);\n \n \tif (write_cache(newfd, active_cache, active_nr) ||\n-\t    commit_locked_index(&lock_file))\n+\t    commit_locked_index(lock_file))\n \t\tdie(\"unable to write new index file\");\n \n \t/* update the index with the given tree's info\n-- \n1.5.4.1.1353.g0d5dd\n"},{"id":"68759","messageId":"alpine.LNX.1.00.0802141319380.13593@iabervon.org","threadId":"11948","inReplyTo":"alpine.LSU.1.00.0802141816120.30505@racer.site","subject":"Re: [PATCH] builtin-checkout: use struct lock_file correctly","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2008-02-14T18:22:39Z","receivedAt":"2008-02-14T18:22:39Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"On Thu, 14 Feb 2008, Johannes Schindelin wrote:\n\n> A lock_file instance must not be cleaned up, since an atexit() handler\n> will try to access even correctly committed lock_files, since it has\n> to make sure that they were correctly committed.\n\nI think the lock_file API changed between when I wrote this code (using \nother code as reference) and now; could you check that there aren't other \nthings I have to change as well, since I think you were following the \nlock_file changes at the time, and I wasn't? I got the most obvious one \n(calling close() on the fd yourself is no longer right, but I didn't know \nabout this one, and I don't know about any further ones, if there are \nany.)\n\n\t-Daniel\n*This .sig left intentionally blank*\n"},{"id":"68761","messageId":"alpine.LSU.1.00.0802141854280.30505@racer.site","threadId":"11948","inReplyTo":"alpine.LNX.1.00.0802141319380.13593@iabervon.org","subject":"Re: [PATCH] builtin-checkout: use struct lock_file correctly","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2008-02-14T18:55:16Z","receivedAt":"2008-02-14T18:55:16Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Thu, 14 Feb 2008, Daniel Barkalow wrote:\n\n> On Thu, 14 Feb 2008, Johannes Schindelin wrote:\n> \n> > A lock_file instance must not be cleaned up, since an atexit() handler \n> > will try to access even correctly committed lock_files, since it has \n> > to make sure that they were correctly committed.\n> \n> I think the lock_file API changed between when I wrote this code (using \n> other code as reference) and now; could you check that there aren't other \n> things I have to change as well, since I think you were following the \n> lock_file changes at the time, and I wasn't?\n\nI think the rest is safe.  (Of course, I grepped for \"struct lock_file\" in \nthe _whole_ working directory).\n\nCiao,\nDscho\n"}]}