{"thread":{"id":"11881","subject":"[PATCH 9/9] Build in checkout","startedAt":"2008-02-04T18:36:07Z","lastAt":"2008-02-06T01:04:11Z","messageCount":9,"participants":["Daniel Barkalow","Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":9},"messages":[{"id":"67406","messageId":"alpine.LNX.1.00.0802041336060.13593@iabervon.org","threadId":"11881","inReplyTo":null,"subject":"[PATCH 9/9] Build in checkout","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2008-02-04T18:36:07Z","receivedAt":"2008-02-04T18:36:07Z","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\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                                 |  478 ++++++++++++++++++++\n builtin.h                                          |    1 +\n .../examples/git-checkout.sh                       |    0 \n git.c                                              |    1 +\n t/t7201-co.sh                                      |    7 -\n 6 files changed, 482 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 85ec1c6..1bfcbdc 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -219,7 +219,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@@ -331,6 +331,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..2950d5c\n--- /dev/null\n+++ b/builtin-checkout.c\n@@ -0,0 +1,478 @@\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 \"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 cache_unmerged(void)\n+{\n+\tint i;\n+\tfor (i = 0; i < active_nr; i++) {\n+\t\tif (ce_stage(active_cache[i]))\n+\t\t\treturn 1;\n+\t}\n+\treturn 0;\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+\tif (S_ISGITLINK(mode)) {\n+\t\treturn 0;\n+\t} else if (S_ISDIR(mode)) {\n+\t\treturn READ_TREE_RECURSIVE;\n+\t}\n+\tint len = baselen + strlen(pathname);\n+\tstruct cache_entry *ce = 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 int remove_merge_head(void)\n+{\n+\tunlink(git_path(\"MERGE_HEAD\"));\n+\treturn 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+\tunpack_trees(1, &tree_desc, &opts);\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+\tint no_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+\tchar *path = xmalloc(strlen(branch->name) +\n+\t\t\t     strlen(\"refs/heads/\") + 1);\n+\tsprintf(path, \"refs/heads/%s\", branch->name);\n+\tbranch->path = path;\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 (cache_unmerged()) {\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+\tchar *msg;\n+\tconst char *old_desc;\n+\tif (opts->new_branch) {\n+\t\tcreate_branch(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+\tmsg = malloc(1024);\n+\told_desc = old->name;\n+\tif (!old_desc)\n+\t\told_desc = sha1_to_hex(old->commit->object.sha1);\n+\tsnprintf(msg, 1024, \"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);\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, \"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_merge_head();\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+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 , \"no-track\", &opts.no_track, \"don't track\"),\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_default_config);\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 || opts.no_track)) {\n+\t\tdie(\"git checkout: --track and --no-track require -b\");\n+\t}\n+\tif (opts.force && opts.merge) {\n+\t\tdie(\"git checkout: -f and -m are incompatible\");\n+\t}\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 428160d..25d91bb 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -19,6 +19,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 57c9712..e06a866 100644\n--- a/git.c\n+++ b/git.c\n@@ -296,6 +296,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 c6f93a9..92c9ba9 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":"67470","messageId":"alpine.LSU.1.00.0802050143050.8543@racer.site","threadId":"11881","inReplyTo":"alpine.LNX.1.00.0802041336060.13593@iabervon.org","subject":"Re: [PATCH 9/9] Build in checkout","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2008-02-05T02:14:35Z","receivedAt":"2008-02-05T02:14:35Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Mon, 4 Feb 2008, Daniel Barkalow wrote:\n\n> diff --git a/builtin-checkout.c b/builtin-checkout.c\n> new file mode 100644\n> index 0000000..2950d5c\n> --- /dev/null\n> +++ b/builtin-checkout.c\n> @@ -0,0 +1,478 @@\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 \"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\nWould this not be helped by the patch of Paolo in \nhttp://article.gmane.org/gmane.comp.version-control.git/72495?\n\nWe would only need to move the function into run-command.[ch]...\n\n> +static int cache_unmerged(void)\n> +{\n> +\tint i;\n> +\tfor (i = 0; i < active_nr; i++) {\n> +\t\tif (ce_stage(active_cache[i]))\n> +\t\t\treturn 1;\n> +\t}\n> +\treturn 0;\n> +}\n\nAFAICT we have this function in merge-recursive.c (unmerged_index()) and \nbuiltin-reset.c (unmerged_files()) already.  Maybe it is time to \nconsolidate them into one function in read-cache.c?\n\n> +\n> +static int update_some(const unsigned char *sha1, const char *base, int baselen,\n\nHeh, nice function name ;-)\n\n> +\t\t       const char *pathname, unsigned mode, int stage)\n> +{\n> +\tif (S_ISGITLINK(mode)) {\n> +\t\treturn 0;\n> +\t} else if (S_ISDIR(mode)) {\n> +\t\treturn READ_TREE_RECURSIVE;\n> +\t}\n\nYou could lose the curly brackets and the \"else\", for readability.\n\n> +\tint len = baselen + strlen(pathname);\n> +\tstruct cache_entry *ce = xcalloc(1, cache_entry_size(len));\n\nThese variables need to be declared before the code (the if()s) starts.\n\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\nI have a hunch that you could share code with builtin-reset.c, namely \ncall function read_from_tree() here.\n\n> +static int remove_merge_head(void)\n> +{\n> +\tunlink(git_path(\"MERGE_HEAD\"));\n> +\treturn 0;\n> +}\n\nPlease make this either void, or return what the unlink() returns.\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\nPlease avoid C++-style comments.\n\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\nI am tempted to suggest\n\n\tpretty_print_commit(get_commit_format(\"format:%h... %s\"), <...>);\n\nhere...\n\n> +static int reset_to_new(struct tree *tree, int quiet)\n\nAgain, I think that this would benefit from code sharing with \nbuiltin-reset.  It can be a bit tricky to avoid writing files when they \nare already up-to-date...\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> +\tint no_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> +\tchar *path = xmalloc(strlen(branch->name) +\n> +\t\t\t     strlen(\"refs/heads/\") + 1);\n> +\tsprintf(path, \"refs/heads/%s\", branch->name);\n> +\tbranch->path = path;\n\nLooks like a perfect candidate for strbuf to me.\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> +\tchar *msg;\n> +\tconst char *old_desc;\n> +\tif (opts->new_branch) {\n> +\t\tcreate_branch(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> +\tmsg = malloc(1024);\n> +\told_desc = old->name;\n> +\tif (!old_desc)\n> +\t\told_desc = sha1_to_hex(old->commit->object.sha1);\n> +\tsnprintf(msg, 1024, \"checkout: moving from %s to %s\",\n> +\t\t old_desc, new->name);\n\nAgain, a perfect candidate for strbuf.\n\nUnfortunately, I am a bit too tired to go on (especially for finding \ncommon ground between builtin-reset and read_tree_some(), \ncheckout_paths(), reset_to_new(), and reset_clean_to_new()).  Hopefully \nI'll find time to look at it tomorrow...\n\nCiao,\nDscho\n"},{"id":"67476","messageId":"alpine.LNX.1.00.0802042243150.13593@iabervon.org","threadId":"11881","inReplyTo":"alpine.LSU.1.00.0802050143050.8543@racer.site","subject":"Re: [PATCH 9/9] Build in checkout","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2008-02-05T03:45:39Z","receivedAt":"2008-02-05T03:45:39Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"On Tue, 5 Feb 2008, Johannes Schindelin wrote:\n\n> Unfortunately, I am a bit too tired to go on (especially for finding \n> common ground between builtin-reset and read_tree_some(), \n> checkout_paths(), reset_to_new(), and reset_clean_to_new()).  Hopefully \n> I'll find time to look at it tomorrow...\n\nI'm too tired to implement any non-trivial suggestions tonight, anyway, so \nit doesn't matter too much. Thanks for the comments so far, and I'm \nlooking forward to tomorrow's.\n\n\t-Daniel\n*This .sig left intentionally blank*\n"},{"id":"67554","messageId":"alpine.LNX.1.00.0802042201060.13593@iabervon.org","threadId":"11881","inReplyTo":"alpine.LSU.1.00.0802050143050.8543@racer.site","subject":"Re: [PATCH 9/9] Build in checkout","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2008-02-05T20:39:44Z","receivedAt":"2008-02-05T20:39:44Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"On Tue, 5 Feb 2008, Johannes Schindelin wrote:\n\n> Hi,\n\nSo, as an introductory comment, I was working quite strictly from \ngit-checkout.sh, using the implementations of the operations as given \nthere, and not looking at what other code has the same goal and may be \ndoing it differently, so I've almost certainly missed a number of chances \nfor sharing code (beyond calling functions directly instead of putting \ntogether command lines and having them parsed). In particular, I hadn't \nlooked at all at builtin-reset, which obviously does a bunch of the same \nthings.\n\n> On Mon, 4 Feb 2008, Daniel Barkalow wrote:\n> \n> > diff --git a/builtin-checkout.c b/builtin-checkout.c\n> > new file mode 100644\n> > index 0000000..2950d5c\n> > --- /dev/null\n> > +++ b/builtin-checkout.c\n> > @@ -0,0 +1,478 @@\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 \"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> Would this not be helped by the patch of Paolo in \n> http://article.gmane.org/gmane.comp.version-control.git/72495?\n> \n> We would only need to move the function into run-command.[ch]...\n\nProbably, but that hasn't been merged yet as of origin/next, which is what \nI'm working from. Good candidate for a follow-up when both series are in, \nor if I do a revision of this series after that one is in.\n\n> > +static int cache_unmerged(void)\n> > +{\n> > +\tint i;\n> > +\tfor (i = 0; i < active_nr; i++) {\n> > +\t\tif (ce_stage(active_cache[i]))\n> > +\t\t\treturn 1;\n> > +\t}\n> > +\treturn 0;\n> > +}\n> \n> AFAICT we have this function in merge-recursive.c (unmerged_index()) and \n> builtin-reset.c (unmerged_files()) already.  Maybe it is time to \n> consolidate them into one function in read-cache.c?\n\nunmerged_files() is a little different, in that it starts with a \nread_cache(), but yes. It'll be \"unmerged_cache()\" aka \n\"unmerged_index(&the_index)\".\n\n> > +\n> > +static int update_some(const unsigned char *sha1, const char *base, int baselen,\n> \n> Heh, nice function name ;-)\n> \n> > +\t\t       const char *pathname, unsigned mode, int stage)\n> > +{\n> > +\tif (S_ISGITLINK(mode)) {\n> > +\t\treturn 0;\n> > +\t} else if (S_ISDIR(mode)) {\n> > +\t\treturn READ_TREE_RECURSIVE;\n> > +\t}\n> \n> You could lose the curly brackets and the \"else\", for readability.\n\nTrue.\n\n> > +\tint len = baselen + strlen(pathname);\n> > +\tstruct cache_entry *ce = xcalloc(1, cache_entry_size(len));\n> \n> These variables need to be declared before the code (the if()s) starts.\n\nYup.\n\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> I have a hunch that you could share code with builtin-reset.c, namely \n> call function read_from_tree() here.\n\nThis version matches the \"ls-tree | update-index\" implementation in \ngit-checkout.sh; I'm not terribly fond of the diff-index-based version, \nsince it messed up the index, rereads it from disk, and then applies \nchanges. On the other hand, it would probably be good to update the cache \nentries with a function that would keep the stat info if nothing else \nchanged.\n\n> > +static int remove_merge_head(void)\n> > +{\n> > +\tunlink(git_path(\"MERGE_HEAD\"));\n> > +\treturn 0;\n> > +}\n> \n> Please make this either void, or return what the unlink() returns.\n\nI think, actually, that I should steal the end of \"builtin-reset\", which \ndiscards a few other files as well that won't be appropriate after the \nswitch.\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> \n> Please avoid C++-style comments.\n\nYup. Can we make git build by default with --std=c89, so I get errors for \nthis sort of thing? *sigh*\n\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> \n> I am tempted to suggest\n> \n> \tpretty_print_commit(get_commit_format(\"format:%h... %s\"), <...>);\n> \n> here...\n\nI'm not familiar for all the pretty_print_commit features, so you're \nprobably right. I'll look into that now.\n\n> > +static int reset_to_new(struct tree *tree, int quiet)\n> \n> Again, I think that this would benefit from code sharing with \n> builtin-reset.  It can be a bit tricky to avoid writing files when they \n> are already up-to-date...\n\nReset actually execs \"read-tree\", which does a bunch of parsing to set up \nthe arguments like this function does (except that it doesn't keep the \nstat info, I think), and then uses the same actual code that this function \ncalls directly (which has done the only-write-changes thing correctly \nsince June 2005 or before).\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> > +\tint no_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> > +\tchar *path = xmalloc(strlen(branch->name) +\n> > +\t\t\t     strlen(\"refs/heads/\") + 1);\n> > +\tsprintf(path, \"refs/heads/%s\", branch->name);\n> > +\tbranch->path = path;\n> \n> Looks like a perfect candidate for strbuf to me.\n\nYup.\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> > +\tchar *msg;\n> > +\tconst char *old_desc;\n> > +\tif (opts->new_branch) {\n> > +\t\tcreate_branch(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> > +\tmsg = malloc(1024);\n> > +\told_desc = old->name;\n> > +\tif (!old_desc)\n> > +\t\told_desc = sha1_to_hex(old->commit->object.sha1);\n> > +\tsnprintf(msg, 1024, \"checkout: moving from %s to %s\",\n> > +\t\t old_desc, new->name);\n> \n> Again, a perfect candidate for strbuf.\n\nYup.\n\n\t-Daniel\n*This .sig left intentionally blank*\n"},{"id":"67565","messageId":"alpine.LSU.1.00.0802052205210.8543@racer.site","threadId":"11881","inReplyTo":"alpine.LNX.1.00.0802042201060.13593@iabervon.org","subject":"Re: [PATCH 9/9] Build in checkout","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2008-02-05T22:11:43Z","receivedAt":"2008-02-05T22:11:43Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Tue, 5 Feb 2008, Daniel Barkalow wrote:\n\n> On Tue, 5 Feb 2008, Johannes Schindelin wrote:\n> \n> So, as an introductory comment, I was working quite strictly from \n> git-checkout.sh, using the implementations of the operations as given \n> there, and not looking at what other code has the same goal and may be \n> doing it differently, so I've almost certainly missed a number of \n> chances for sharing code (beyond calling functions directly instead of \n> putting together command lines and having them parsed). In particular, I \n> hadn't looked at all at builtin-reset, which obviously does a bunch of \n> the same things.\n\nWell, it seems like I won't be able to review any more til this weekend.  \nSorry.\n\n> > On Mon, 4 Feb 2008, Daniel Barkalow wrote:\n> > \n> > > diff --git a/builtin-checkout.c b/builtin-checkout.c\n> > > new file mode 100644\n> > > index 0000000..2950d5c\n> > > --- /dev/null\n> > > +++ b/builtin-checkout.c\n> > > @@ -0,0 +1,478 @@\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 \"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> > Would this not be helped by the patch of Paolo in \n> > http://article.gmane.org/gmane.comp.version-control.git/72495?\n> > \n> > We would only need to move the function into run-command.[ch]...\n> \n> Probably, but that hasn't been merged yet as of origin/next, which is \n> what I'm working from. Good candidate for a follow-up when both series \n> are in, or if I do a revision of this series after that one is in.\n\nConcur.  I briefly looked at moving run_hook(), and it is not as nice as I \nthought: builtin-commit's version of run_hook() wants to set the \nenvironment variable GIT_INDEX_FILE, and therefore it has a parameter \nindex_file which most other hooks probably do not need.\n\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> > I have a hunch that you could share code with builtin-reset.c, namely \n> > call function read_from_tree() here.\n> \n> This version matches the \"ls-tree | update-index\" implementation in \n> git-checkout.sh; I'm not terribly fond of the diff-index-based version, \n> since it messed up the index, rereads it from disk, and then applies \n> changes. On the other hand, it would probably be good to update the \n> cache entries with a function that would keep the stat info if nothing \n> else changed.\n\nExactly.  In fact, that is the only reason I suggested that: to prevent \nthe index from needing an update.\n\nThis is quite important if you have a large working tree, and switch \nbranches from A to B.  For example, \"make\" will punish you where it hurts.\n\n> > > +static int remove_merge_head(void)\n> > > +{\n> > > +\tunlink(git_path(\"MERGE_HEAD\"));\n> > > +\treturn 0;\n> > > +}\n> > \n> > Please make this either void, or return what the unlink() returns.\n> \n> I think, actually, that I should steal the end of \"builtin-reset\", which \n> discards a few other files as well that won't be appropriate after the \n> switch.\n\nGood.\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> > \n> > Please avoid C++-style comments.\n> \n> Yup. Can we make git build by default with --std=c89, so I get errors for \n> this sort of thing? *sigh*\n\nHeh.  You can do that in config.mak.  For the Makefile, I am afraid this \nis no option, as we try to support other compilers than gcc, too.\n\n> > > +static int reset_to_new(struct tree *tree, int quiet)\n> > \n> > Again, I think that this would benefit from code sharing with \n> > builtin-reset.  It can be a bit tricky to avoid writing files when they \n> > are already up-to-date...\n> \n> Reset actually execs \"read-tree\", which does a bunch of parsing to set up \n> the arguments like this function does (except that it doesn't keep the \n> stat info, I think), and then uses the same actual code that this function \n> calls directly (which has done the only-write-changes thing correctly \n> since June 2005 or before).\n\nOkay, I'll just wait and see (as I do not have the time to dive into the \ncode).\n\nThanks,\nDscho\n"},{"id":"67568","messageId":"alpine.LNX.1.00.0802051716220.13593@iabervon.org","threadId":"11881","inReplyTo":"alpine.LSU.1.00.0802052205210.8543@racer.site","subject":"Re: [PATCH 9/9] Build in checkout","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2008-02-05T22:28:58Z","receivedAt":"2008-02-05T22:28:58Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"On Tue, 5 Feb 2008, Johannes Schindelin wrote:\n\n> Hi,\n> \n> On Tue, 5 Feb 2008, Daniel Barkalow wrote:\n> \n> > On Tue, 5 Feb 2008, Johannes Schindelin wrote:\n> > \n> > So, as an introductory comment, I was working quite strictly from \n> > git-checkout.sh, using the implementations of the operations as given \n> > there, and not looking at what other code has the same goal and may be \n> > doing it differently, so I've almost certainly missed a number of \n> > chances for sharing code (beyond calling functions directly instead of \n> > putting together command lines and having them parsed). In particular, I \n> > hadn't looked at all at builtin-reset, which obviously does a bunch of \n> > the same things.\n> \n> Well, it seems like I won't be able to review any more til this weekend.  \n> Sorry.\n\nIt's okay, I've got other things to work on. I'll probably send out a new \nversion of the series now, in any case, so that you can see if I handled \nyour comments correctly thus far.\n\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> > > I have a hunch that you could share code with builtin-reset.c, namely \n> > > call function read_from_tree() here.\n> > \n> > This version matches the \"ls-tree | update-index\" implementation in \n> > git-checkout.sh; I'm not terribly fond of the diff-index-based version, \n> > since it messed up the index, rereads it from disk, and then applies \n> > changes. On the other hand, it would probably be good to update the \n> > cache entries with a function that would keep the stat info if nothing \n> > else changed.\n> \n> Exactly.  In fact, that is the only reason I suggested that: to prevent \n> the index from needing an update.\n> \n> This is quite important if you have a large working tree, and switch \n> branches from A to B.  For example, \"make\" will punish you where it hurts.\n\nThis isn't used for switching branches; this is used for checking out \npaths. If you do \"git checkout <not-head> -- <every single path>\", make \nwill punish you, but why would you do that? I'd guess that people are \nunlikely to have a significant number of non-changes in this piece of \ncode, just because they wouldn't list things that they didn't think had \nchanges.\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> > > \n> > > Please avoid C++-style comments.\n> > \n> > Yup. Can we make git build by default with --std=c89, so I get errors for \n> > this sort of thing? *sigh*\n> \n> Heh.  You can do that in config.mak.  For the Makefile, I am afraid this \n> is no option, as we try to support other compilers than gcc, too.\n\nI'd say only for the case where CC is initially unset, at which point the \ncompiler is going to be \"gcc\" because we set it to that. And people who \nsymlink their non-gcc-like system compiler to gcc have other problems, \nmost likely.\n\n> > > > +static int reset_to_new(struct tree *tree, int quiet)\n> > > \n> > > Again, I think that this would benefit from code sharing with \n> > > builtin-reset.  It can be a bit tricky to avoid writing files when they \n> > > are already up-to-date...\n> > \n> > Reset actually execs \"read-tree\", which does a bunch of parsing to set up \n> > the arguments like this function does (except that it doesn't keep the \n> > stat info, I think), and then uses the same actual code that this function \n> > calls directly (which has done the only-write-changes thing correctly \n> > since June 2005 or before).\n> \n> Okay, I'll just wait and see (as I do not have the time to dive into the \n> code).\n\nSure. I'll probably actually change builtin-reset to do the non-fork \nthing (in a separate series; it's really unrelated). I doubt there's any \npoint to sharing the code for setting all of the options, though, which is \nbest to have inlined by the compiler and be easy to find.\n\n\t-Daniel\n*This .sig left intentionally blank*\n"},{"id":"67571","messageId":"alpine.LSU.1.00.0802052244160.8543@racer.site","threadId":"11881","inReplyTo":"alpine.LNX.1.00.0802051716220.13593@iabervon.org","subject":"Re: [PATCH 9/9] Build in checkout","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2008-02-05T22:45:53Z","receivedAt":"2008-02-05T22:45:53Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Tue, 5 Feb 2008, Daniel Barkalow wrote:\n\n> On Tue, 5 Feb 2008, Johannes Schindelin wrote:\n> \n> > I suggested that: to prevent the index from needing an update.\n> > \n> > This is quite important if you have a large working tree, and switch \n> > branches from A to B.  For example, \"make\" will punish you where it \n> > hurts.\n> \n> This isn't used for switching branches; this is used for checking out \n> paths. If you do \"git checkout <not-head> -- <every single path>\", make \n> will punish you, but why would you do that? I'd guess that people are \n> unlikely to have a significant number of non-changes in this piece of \n> code, just because they wouldn't list things that they didn't think had \n> changes.\n\nFor convenience, you can also say\n\n\t$ git checkout -- <path>\n\nand expect checkout to not really touch the unchanged files.\n\nCiao,\nDscho\n"},{"id":"67574","messageId":"alpine.LSU.1.00.0802052251420.8543@racer.site","threadId":"11881","inReplyTo":"alpine.LNX.1.00.0802051716220.13593@iabervon.org","subject":"Re: [PATCH 9/9] Build in checkout","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2008-02-05T22:53:29Z","receivedAt":"2008-02-05T22:53:29Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Tue, 5 Feb 2008, Daniel Barkalow wrote:\n\n> On Tue, 5 Feb 2008, Johannes Schindelin wrote:\n> >\n> > > Well, it seems like I won't be able to review any more til this \n> > > weekend. Sorry.\n> \n> It's okay, I've got other things to work on. I'll probably send out a \n> new version of the series now, in any case, so that you can see if I \n> handled your comments correctly thus far.\n\nIt is a real pleasure to see you addressing my concerns so quickly and to \nmy satisfaction.  And by that, I mean also the cases where you explained \nwhy my objections were wrong.\n\nThanks,\nDscho \"who will hopefully have some time on the weekend\"\n"},{"id":"67596","messageId":"alpine.LNX.1.00.0802051958440.13593@iabervon.org","threadId":"11881","inReplyTo":"alpine.LSU.1.00.0802052244160.8543@racer.site","subject":"Re: [PATCH 9/9] Build in checkout","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2008-02-06T01:04:11Z","receivedAt":"2008-02-06T01:04:11Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"On Tue, 5 Feb 2008, Johannes Schindelin wrote:\n\n> Hi,\n> \n> On Tue, 5 Feb 2008, Daniel Barkalow wrote:\n> \n> > On Tue, 5 Feb 2008, Johannes Schindelin wrote:\n> > \n> > > I suggested that: to prevent the index from needing an update.\n> > > \n> > > This is quite important if you have a large working tree, and switch \n> > > branches from A to B.  For example, \"make\" will punish you where it \n> > > hurts.\n> > \n> > This isn't used for switching branches; this is used for checking out \n> > paths. If you do \"git checkout <not-head> -- <every single path>\", make \n> > will punish you, but why would you do that? I'd guess that people are \n> > unlikely to have a significant number of non-changes in this piece of \n> > code, just because they wouldn't list things that they didn't think had \n> > changes.\n> \n> For convenience, you can also say\n> \n> \t$ git checkout -- <path>\n> \n> and expect checkout to not really touch the unchanged files.\n\nThat also doesn't use this function, which is only for reading the tree \ninto the index if a tree is given. On the other hand:\n\n$ git checkout HEAD -- .\n\nWith either git-checkout.sh or my code, touches all of your files.\n\n\t-Daniel\n*This .sig left intentionally blank*\n"}]}