Merge branch 'jc/maint-1.6.1-checkout-m-custom-merge' into maint
authorJunio C Hamano <gitster@pobox.com>
Wed, 10 Feb 2010 20:54:15 +0000 (12:54 -0800)
committerJunio C Hamano <gitster@pobox.com>
Wed, 10 Feb 2010 20:54:15 +0000 (12:54 -0800)
* jc/maint-1.6.1-checkout-m-custom-merge:
checkout -m path: fix recreating conflicts

Conflicts:
t/t7201-co.sh

1  2 
builtin-checkout.c
t/t7201-co.sh
diff --combined builtin-checkout.c
index 2708669383e21f038a1404e061b34bc4302e8a93,e41e73b2701624901e78dd165f8cecd7ae157a60..67229fc21607ceb51c34d77c4ef5682d96f1e909
@@@ -5,7 -5,6 +5,7 @@@
  #include "commit.h"
  #include "tree.h"
  #include "tree-walk.h"
 +#include "cache-tree.h"
  #include "unpack-trees.h"
  #include "dir.h"
  #include "run-command.h"
@@@ -39,13 -38,23 +39,13 @@@ struct checkout_opts 
  static int post_checkout_hook(struct commit *old, struct commit *new,
                              int changed)
  {
 -      struct child_process proc;
 -      const char *name = git_path("hooks/post-checkout");
 -      const char *argv[5];
 +      return run_hook(NULL, "post-checkout",
 +                      sha1_to_hex(old ? old->object.sha1 : null_sha1),
 +                      sha1_to_hex(new ? new->object.sha1 : null_sha1),
 +                      changed ? "1" : "0", NULL);
 +      /* "new" can be NULL when checking out from the index before
 +         a commit exists. */
  
 -      if (access(name, X_OK) < 0)
 -              return 0;
 -
 -      memset(&proc, 0, sizeof(proc));
 -      argv[0] = name;
 -      argv[1] = xstrdup(sha1_to_hex(old ? old->object.sha1 : null_sha1));
 -      argv[2] = xstrdup(sha1_to_hex(new->object.sha1));
 -      argv[3] = changed ? "1" : "0";
 -      argv[4] = NULL;
 -      proc.argv = argv;
 -      proc.no_stdin = 1;
 -      proc.stdout_to_stderr = 1;
 -      return run_command(&proc);
  }
  
  static int update_some(const unsigned char *sha1, const char *base, int baselen,
@@@ -54,6 -63,9 +54,6 @@@
        int len;
        struct cache_entry *ce;
  
 -      if (S_ISGITLINK(mode))
 -              return 0;
 -
        if (S_ISDIR(mode))
                return READ_TREE_RECURSIVE;
  
@@@ -167,7 -179,7 +167,7 @@@ static int checkout_merged(int pos, str
        fill_mm(active_cache[pos+2]->sha1, &theirs);
  
        status = ll_merge(&result_buf, path, &ancestor,
-                         &ours, "ours", &theirs, "theirs", 1);
+                         &ours, "ours", &theirs, "theirs", 0);
        free(ancestor.ptr);
        free(ours.ptr);
        free(theirs.ptr);
        /*
         * NEEDSWORK:
         * There is absolutely no reason to write this as a blob object
 -       * and create a phoney cache entry just to leak.  This hack is
 +       * and create a phony cache entry just to leak.  This hack is
         * primarily to get to the write_entry() machinery that massages
         * the contents to work-tree format and writes out which only
         * allows it for a cache entry.  The code in write_entry() needs
@@@ -216,8 -228,7 +216,8 @@@ static int checkout_paths(struct tree *
        struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
  
        newfd = hold_locked_index(lock_file, 1);
 -      read_cache();
 +      if (read_cache_preload(pathspec) < 0)
 +              return error("corrupt index file");
  
        if (source_tree)
                read_tree_some(source_tree, pathspec);
  
        for (pos = 0; pos < active_nr; pos++) {
                struct cache_entry *ce = active_cache[pos];
 -              pathspec_match(pathspec, ps_matched, ce->name, 0);
 +              match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, ps_matched);
        }
  
        if (report_path_error(ps_matched, pathspec, 0))
        /* Any unmerged paths? */
        for (pos = 0; pos < active_nr; pos++) {
                struct cache_entry *ce = active_cache[pos];
 -              if (pathspec_match(pathspec, NULL, ce->name, 0)) {
 +              if (match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, NULL)) {
                        if (!ce_stage(ce))
                                continue;
                        if (opts->force) {
        state.refresh_cache = 1;
        for (pos = 0; pos < active_nr; pos++) {
                struct cache_entry *ce = active_cache[pos];
 -              if (pathspec_match(pathspec, NULL, ce->name, 0)) {
 +              if (match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, NULL)) {
                        if (!ce_stage(ce)) {
                                errs |= checkout_entry(ce, &state, NULL);
                                continue;
@@@ -293,8 -304,6 +293,8 @@@ static void show_local_changes(struct o
        init_revisions(&rev, NULL);
        rev.abbrev = 0;
        rev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS;
 +      if (diff_setup_done(&rev.diffopt) < 0)
 +              die("diff_setup_done failed");
        add_pending_object(&rev, head, NULL);
        run_diff_index(&rev, 0);
  }
  static void describe_detached_head(char *msg, struct commit *commit)
  {
        struct strbuf sb = STRBUF_INIT;
 +      struct pretty_print_context ctx = {0};
        parse_commit(commit);
 -      pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb, 0, NULL, NULL, 0, 0);
 +      pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb, &ctx);
        fprintf(stderr, "%s %s... %s\n", msg,
                find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf);
        strbuf_release(&sb);
@@@ -352,11 -360,8 +352,11 @@@ struct branch_info 
  static void setup_branch_path(struct branch_info *branch)
  {
        struct strbuf buf = STRBUF_INIT;
 -      strbuf_addstr(&buf, "refs/heads/");
 -      strbuf_addstr(&buf, branch->name);
 +
 +      strbuf_branchname(&buf, branch->name);
 +      if (strcmp(buf.buf, branch->name))
 +              branch->name = xstrdup(buf.buf);
 +      strbuf_splice(&buf, 0, 0, "refs/heads/", 11);
        branch->path = strbuf_detach(&buf, NULL);
  }
  
@@@ -366,9 -371,7 +366,9 @@@ static int merge_working_tree(struct ch
        int ret;
        struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
        int newfd = hold_locked_index(lock_file, 1);
 -      read_cache();
 +
 +      if (read_cache_preload(NULL) < 0)
 +              return error("corrupt index file");
  
        if (opts->force) {
                ret = reset_tree(new->commit->tree, opts, 1);
                topts.initial_checkout = is_cache_unborn();
                topts.update = 1;
                topts.merge = 1;
 -              topts.gently = opts->merge;
 +              topts.gently = opts->merge && old->commit;
                topts.verbose_update = !opts->quiet;
                topts.fn = twoway_merge;
                topts.dir = xcalloc(1, sizeof(*topts.dir));
 -              topts.dir->show_ignored = 1;
 +              topts.dir->flags |= DIR_SHOW_IGNORED;
                topts.dir->exclude_per_dir = ".gitignore";
 -              tree = parse_tree_indirect(old->commit->object.sha1);
 +              tree = parse_tree_indirect(old->commit ?
 +                                         old->commit->object.sha1 :
 +                                         (unsigned char *)EMPTY_TREE_SHA1_BIN);
                init_tree_desc(&trees[0], tree->buffer, tree->size);
                tree = parse_tree_indirect(new->commit->object.sha1);
                init_tree_desc(&trees[1], tree->buffer, tree->size);
                        struct merge_options o;
                        if (!opts->merge)
                                return 1;
 -                      parse_commit(old->commit);
 +
 +                      /*
 +                       * Without old->commit, the below is the same as
 +                       * the two-tree unpack we already tried and failed.
 +                       */
 +                      if (!old->commit)
 +                              return 1;
  
                        /* Do more real merge */
  
@@@ -505,10 -500,10 +505,10 @@@ static void update_refs_for_switch(stru
                create_symref("HEAD", new->path, msg.buf);
                if (!opts->quiet) {
                        if (old->path && !strcmp(new->path, old->path))
 -                              fprintf(stderr, "Already on \"%s\"\n",
 +                              fprintf(stderr, "Already on '%s'\n",
                                        new->name);
                        else
 -                              fprintf(stderr, "Switched to%s branch \"%s\"\n",
 +                              fprintf(stderr, "Switched to%s branch '%s'\n",
                                        opts->new_branch ? " a new" : "",
                                        new->name);
                }
                           REF_NODEREF, DIE_ON_ERR);
                if (!opts->quiet) {
                        if (old->path)
 -                              fprintf(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);
 +                              fprintf(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);
                        describe_detached_head("HEAD is now at", new->commit);
                }
        }
@@@ -550,18 -545,26 +550,18 @@@ static int switch_branches(struct check
                parse_commit(new->commit);
        }
  
 +      ret = merge_working_tree(opts, &old, new);
 +      if (ret)
 +              return ret;
 +
        /*
 -       * If we were on a detached HEAD, but we are now moving to
 +       * If we were on a detached HEAD, but have now moved to
         * a new commit, we want to mention the old commit once more
         * to remind the user that it might be lost.
         */
        if (!opts->quiet && !old.path && old.commit && new->commit != old.commit)
                describe_detached_head("Previous HEAD position was", old.commit);
  
 -      if (!old.commit && !opts->force) {
 -              if (!opts->quiet) {
 -                      fprintf(stderr, "warning: You appear to be on a branch yet to be born.\n");
 -                      fprintf(stderr, "warning: Forcing checkout of %s.\n", new->name);
 -              }
 -              opts->force = 1;
 -      }
 -
 -      ret = merge_working_tree(opts, &old, new);
 -      if (ret)
 -              return ret;
 -
        update_refs_for_switch(opts, &old, new);
  
        ret = post_checkout_hook(old.commit, new->commit, 1);
@@@ -573,47 -576,6 +573,47 @@@ static int git_checkout_config(const ch
        return git_xmerge_config(var, value, cb);
  }
  
 +static int interactive_checkout(const char *revision, const char **pathspec,
 +                              struct checkout_opts *opts)
 +{
 +      return run_add_interactive(revision, "--patch=checkout", pathspec);
 +}
 +
 +struct tracking_name_data {
 +      const char *name;
 +      char *remote;
 +      int unique;
 +};
 +
 +static int check_tracking_name(const char *refname, const unsigned char *sha1,
 +                             int flags, void *cb_data)
 +{
 +      struct tracking_name_data *cb = cb_data;
 +      const char *slash;
 +
 +      if (prefixcmp(refname, "refs/remotes/"))
 +              return 0;
 +      slash = strchr(refname + 13, '/');
 +      if (!slash || strcmp(slash + 1, cb->name))
 +              return 0;
 +      if (cb->remote) {
 +              cb->unique = 0;
 +              return 0;
 +      }
 +      cb->remote = xstrdup(refname);
 +      return 0;
 +}
 +
 +static const char *unique_tracking_name(const char *name)
 +{
 +      struct tracking_name_data cb_data = { name, NULL, 1 };
 +      for_each_ref(check_tracking_name, &cb_data);
 +      if (cb_data.unique)
 +              return cb_data.remote;
 +      free(cb_data.remote);
 +      return NULL;
 +}
 +
  int cmd_checkout(int argc, const char **argv, const char *prefix)
  {
        struct checkout_opts opts;
        struct branch_info new;
        struct tree *source_tree = NULL;
        char *conflict_style = NULL;
 +      int patch_mode = 0;
 +      int dwim_new_local_branch = 1;
        struct option options[] = {
                OPT__QUIET(&opts.quiet),
                OPT_STRING('b', NULL, &opts.new_branch, "new branch", "branch"),
                            2),
                OPT_SET_INT('3', "theirs", &opts.writeout_stage, "stage",
                            3),
 -              OPT_BOOLEAN('f', NULL, &opts.force, "force"),
 +              OPT_BOOLEAN('f', "force", &opts.force, "force"),
                OPT_BOOLEAN('m', "merge", &opts.merge, "merge"),
                OPT_STRING(0, "conflict", &conflict_style, "style",
                           "conflict style (merge or diff3)"),
 +              OPT_BOOLEAN('p', "patch", &patch_mode, "select hunks interactively"),
 +              { OPTION_BOOLEAN, 0, "guess", &dwim_new_local_branch, NULL,
 +                "second guess 'git checkout no-such-branch'",
 +                PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
                OPT_END(),
        };
        int has_dash_dash;
  
        opts.track = BRANCH_TRACK_UNSPECIFIED;
  
 -      argc = parse_options(argc, argv, options, checkout_usage,
 +      argc = parse_options(argc, argv, prefix, options, checkout_usage,
                             PARSE_OPT_KEEP_DASHDASH);
  
 +      if (patch_mode && (opts.track > 0 || opts.new_branch
 +                         || opts.new_branch_log || opts.merge || opts.force))
 +              die ("--patch is incompatible with all other options");
 +
        /* --track without -b should DWIM */
        if (0 < opts.track && !opts.new_branch) {
                const char *argv0 = argv[0];
                opts.new_branch = argv0 + 1;
        }
  
 -      if (opts.track == BRANCH_TRACK_UNSPECIFIED)
 -              opts.track = git_branch_track;
        if (conflict_style) {
                opts.merge = 1; /* implied */
                git_xmerge_config("merge.conflictstyle", conflict_style, NULL);
         *   With no paths, if <something> is a commit, that is to
         *   switch to the branch or detach HEAD at it.
         *
 +       *   With no paths, if <something> is _not_ a commit, no -t nor -b
 +       *   was given, and there is a tracking branch whose name is
 +       *   <something> in one and only one remote, then this is a short-hand
 +       *   to fork local <something> from that remote tracking branch.
 +       *
         *   Otherwise <something> shall not be ambiguous.
         *   - If it's *only* a reference, treat it like case (1).
         *   - If it's only a path, treat it like case (2).
                arg = argv[0];
                has_dash_dash = (argc > 1) && !strcmp(argv[1], "--");
  
 +              if (!strcmp(arg, "-"))
 +                      arg = "@{-1}";
 +
                if (get_sha1(arg, rev)) {
                        if (has_dash_dash)          /* case (1) */
                                die("invalid reference: %s", arg);
 -                      goto no_reference;          /* case (3 -> 2) */
 +                      if (!patch_mode &&
 +                          dwim_new_local_branch &&
 +                          opts.track == BRANCH_TRACK_UNSPECIFIED &&
 +                          !opts.new_branch &&
 +                          !check_filename(NULL, arg) &&
 +                          argc == 1) {
 +                              const char *remote = unique_tracking_name(arg);
 +                              if (!remote || get_sha1(remote, rev))
 +                                      goto no_reference;
 +                              opts.new_branch = arg;
 +                              arg = remote;
 +                              /* DWIMmed to create local branch */
 +                      }
 +                      else
 +                              goto no_reference;
                }
  
                /* we can't end up being in (2) anymore, eat the argument */
                argv++;
                argc--;
  
 +              new.name = arg;
                if ((new.commit = lookup_commit_reference_gently(rev, 1))) {
 -                      new.name = arg;
                        setup_branch_path(&new);
                        if (resolve_ref(new.path, rev, 1, NULL))
                                new.commit = lookup_commit_reference(rev);
        }
  
  no_reference:
 +
 +      if (opts.track == BRANCH_TRACK_UNSPECIFIED)
 +              opts.track = git_branch_track;
 +
        if (argc) {
                const char **pathspec = get_pathspec(prefix, argv);
  
                if (!pathspec)
                        die("invalid path specification");
  
 +              if (patch_mode)
 +                      return interactive_checkout(new.name, pathspec, &opts);
 +
                /* Checkout paths */
                if (opts.new_branch) {
                        if (argc == 1) {
                return checkout_paths(source_tree, pathspec, &opts);
        }
  
 +      if (patch_mode)
 +              return interactive_checkout(new.name, NULL, &opts);
 +
        if (opts.new_branch) {
                struct strbuf buf = STRBUF_INIT;
 -              strbuf_addstr(&buf, "refs/heads/");
 -              strbuf_addstr(&buf, opts.new_branch);
 +              if (strbuf_check_branch_ref(&buf, opts.new_branch))
 +                      die("git checkout: we do not like '%s' as a branch name.",
 +                          opts.new_branch);
                if (!get_sha1(buf.buf, rev))
                        die("git checkout: branch %s already exists", opts.new_branch);
 -              if (check_ref_format(buf.buf))
 -                      die("git checkout: we do not like '%s' as a branch name.", opts.new_branch);
                strbuf_release(&buf);
        }
  
diff --combined t/t7201-co.sh
index ebfd34df36068f8808406a98d371731fb85012c4,3214ad29e146e252618f84b1845b77968ddb9f14..6442f710be8bcaea11931044d52deb5b75d8f7e0
@@@ -171,7 -171,7 +171,7 @@@ test_expect_success 'checkout to detac
        git checkout -f renamer && git clean -f &&
        git checkout renamer^ 2>messages &&
        (cat >messages.expect <<EOF
 -Note: moving to "renamer^" which isn'"'"'t a local branch
 +Note: moving to '\''renamer^'\'' which isn'\''t a local branch
  If you want to create a new branch from this checkout, you may do so
  (now or later) by using -b with the checkout command again. Example:
    git checkout -b <new_branch_name>
@@@ -534,12 -534,61 +534,69 @@@ test_expect_success 'failing checkout -
  
  '
  
 +test_expect_success 'switch out of non-branch' '
 +      git reset --hard master &&
 +      git checkout master^0 &&
 +      echo modified >one &&
 +      test_must_fail git checkout renamer 2>error.log &&
 +      ! grep "^Previous HEAD" error.log
 +'
 +
+ (
+  echo "#!$SHELL_PATH"
+  cat <<\EOF
+ O=$1 A=$2 B=$3
+ cat "$A" >.tmp
+ exec >"$A"
+ echo '<<<<<<< filfre-theirs'
+ cat "$B"
+ echo '||||||| filfre-common'
+ cat "$O"
+ echo '======='
+ cat ".tmp"
+ echo '>>>>>>> filfre-ours'
+ rm -f .tmp
+ exit 1
+ EOF
+ ) >filfre.sh
+ chmod +x filfre.sh
+ test_expect_success 'custom merge driver with checkout -m' '
+       git reset --hard &&
+       git config merge.filfre.driver "./filfre.sh %O %A %B" &&
+       git config merge.filfre.name "Feel-free merge driver" &&
+       git config merge.filfre.recursive binary &&
+       echo "arm merge=filfre" >.gitattributes &&
+       git checkout -b left &&
+       echo neutral >arm &&
+       git add arm .gitattributes &&
+       test_tick &&
+       git commit -m neutral &&
+       git branch right &&
+       echo left >arm &&
+       test_tick &&
+       git commit -a -m left &&
+       git checkout right &&
+       echo right >arm &&
+       test_tick &&
+       git commit -a -m right &&
+       test_must_fail git merge left &&
+       (
+               for t in filfre-common left right
+               do
+                       grep $t arm || exit 1
+               done
+               exit 0
+       ) &&
+       mv arm expect &&
+       git checkout -m arm &&
+       test_cmp expect arm
+ '
  test_done