Code

Merge branch 'jc/checkout-m-twoway'
authorJunio C Hamano <gitster@pobox.com>
Tue, 20 Dec 2011 00:06:22 +0000 (16:06 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 20 Dec 2011 00:06:22 +0000 (16:06 -0800)
* jc/checkout-m-twoway:
  checkout_merged(): squelch false warning from some gcc
  Test 'checkout -m -- path'
  checkout -m: no need to insist on having all 3 stages

1  2 
builtin/checkout.c

diff --combined builtin/checkout.c
index 011c0561d980064513379601ad6fe230f086d138,064e7a14c130419c355bb209ca4336130d24187f..f1984d9933c526bcd2af66fd745dc64a607ac19b
@@@ -19,7 -19,6 +19,7 @@@
  #include "ll-merge.h"
  #include "resolve-undo.h"
  #include "submodule.h"
 +#include "argv-array.h"
  
  static const char * const checkout_usage[] = {
        "git checkout [options] <branch>",
@@@ -34,7 -33,6 +34,7 @@@ struct checkout_opts 
        int force_detach;
        int writeout_stage;
        int writeout_error;
 +      int overwrite_ignore;
  
        /* not set by parse_options */
        int branch_exists;
@@@ -73,7 -71,7 +73,7 @@@ static int update_some(const unsigned c
        hashcpy(ce->sha1, sha1);
        memcpy(ce->name, base, baselen);
        memcpy(ce->name + baselen, pathname, len - baselen);
 -      ce->ce_flags = create_ce_flags(len, 0);
 +      ce->ce_flags = create_ce_flags(len, 0) | CE_UPDATE;
        ce->ce_mode = create_ce_mode(mode);
        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
        return 0;
  
  static int read_tree_some(struct tree *tree, const char **pathspec)
  {
 -      read_tree_recursive(tree, "", 0, 0, pathspec, update_some, NULL);
 +      struct pathspec ps;
 +      init_pathspec(&ps, pathspec);
 +      read_tree_recursive(tree, "", 0, 0, &ps, update_some, NULL);
 +      free_pathspec(&ps);
  
        /* update the index with the given tree's info
         * for all args, expanding wildcards, and exit
@@@ -115,16 -110,21 +115,21 @@@ static int check_stage(int stage, struc
                return error(_("path '%s' does not have their version"), ce->name);
  }
  
- static int check_all_stages(struct cache_entry *ce, int pos)
+ static int check_stages(unsigned stages, struct cache_entry *ce, int pos)
  {
-       if (ce_stage(ce) != 1 ||
-           active_nr <= pos + 2 ||
-           strcmp(active_cache[pos+1]->name, ce->name) ||
-           ce_stage(active_cache[pos+1]) != 2 ||
-           strcmp(active_cache[pos+2]->name, ce->name) ||
-           ce_stage(active_cache[pos+2]) != 3)
-               return error(_("path '%s' does not have all three versions"),
-                            ce->name);
+       unsigned seen = 0;
+       const char *name = ce->name;
+       while (pos < active_nr) {
+               ce = active_cache[pos];
+               if (strcmp(name, ce->name))
+                       break;
+               seen |= (1 << ce_stage(ce));
+               pos++;
+       }
+       if ((stages & seen) != stages)
+               return error(_("path '%s' does not have all necessary versions"),
+                            name);
        return 0;
  }
  
@@@ -151,18 -151,27 +156,27 @@@ static int checkout_merged(int pos, str
        int status;
        unsigned char sha1[20];
        mmbuffer_t result_buf;
+       unsigned char threeway[3][20];
+       unsigned mode = 0;
  
-       if (ce_stage(ce) != 1 ||
-           active_nr <= pos + 2 ||
-           strcmp(active_cache[pos+1]->name, path) ||
-           ce_stage(active_cache[pos+1]) != 2 ||
-           strcmp(active_cache[pos+2]->name, path) ||
-           ce_stage(active_cache[pos+2]) != 3)
-               return error(_("path '%s' does not have all 3 versions"), path);
+       memset(threeway, 0, sizeof(threeway));
+       while (pos < active_nr) {
+               int stage;
+               stage = ce_stage(ce);
+               if (!stage || strcmp(path, ce->name))
+                       break;
+               hashcpy(threeway[stage - 1], ce->sha1);
+               if (stage == 2)
+                       mode = create_ce_mode(ce->ce_mode);
+               pos++;
+               ce = active_cache[pos];
+       }
+       if (is_null_sha1(threeway[1]) || is_null_sha1(threeway[2]))
+               return error(_("path '%s' does not have necessary versions"), path);
  
-       read_mmblob(&ancestor, active_cache[pos]->sha1);
-       read_mmblob(&ours, active_cache[pos+1]->sha1);
-       read_mmblob(&theirs, active_cache[pos+2]->sha1);
+       read_mmblob(&ancestor, threeway[0]);
+       read_mmblob(&ours, threeway[1]);
+       read_mmblob(&theirs, threeway[2]);
  
        /*
         * NEEDSWORK: re-create conflicts from merges with
        if (write_sha1_file(result_buf.ptr, result_buf.size,
                            blob_type, sha1))
                die(_("Unable to add merge result for '%s'"), path);
-       ce = make_cache_entry(create_ce_mode(active_cache[pos+1]->ce_mode),
-                             sha1,
-                             path, 2, 0);
+       ce = make_cache_entry(mode, sha1, path, 2, 0);
        if (!ce)
                die(_("make_cache_entry failed for path '%s'"), path);
        status = checkout_entry(ce, state, NULL);
  }
  
  static int checkout_paths(struct tree *source_tree, const char **pathspec,
 -                        struct checkout_opts *opts)
 +                        const char *prefix, struct checkout_opts *opts)
  {
        int pos;
        struct checkout state;
  
        for (pos = 0; pos < active_nr; pos++) {
                struct cache_entry *ce = active_cache[pos];
 +              if (source_tree && !(ce->ce_flags & CE_UPDATE))
 +                      continue;
                match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, ps_matched);
        }
  
 -      if (report_path_error(ps_matched, pathspec, 0))
 +      if (report_path_error(ps_matched, pathspec, prefix))
                return 1;
  
        /* "checkout -m path" to recreate conflicted state */
                        } else if (stage) {
                                errs |= check_stage(stage, ce, pos);
                        } else if (opts->merge) {
-                               errs |= check_all_stages(ce, pos);
+                               errs |= check_stages((1<<2) | (1<<3), ce, pos);
                        } else {
                                errs = 1;
                                error(_("path '%s' is unmerged"), ce->name);
        state.refresh_cache = 1;
        for (pos = 0; pos < active_nr; pos++) {
                struct cache_entry *ce = active_cache[pos];
 +              if (source_tree && !(ce->ce_flags & CE_UPDATE))
 +                      continue;
                if (match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, NULL)) {
                        if (!ce_stage(ce)) {
                                errs |= checkout_entry(ce, &state, NULL);
            commit_locked_index(lock_file))
                die(_("unable to write new index file"));
  
 -      resolve_ref("HEAD", rev, 0, &flag);
 +      read_ref_full("HEAD", rev, 0, &flag);
        head = lookup_commit_reference_gently(rev, 1);
  
        errs |= post_checkout_hook(head, head, 0);
@@@ -312,8 -315,9 +324,8 @@@ static void show_local_changes(struct o
  static void describe_detached_head(const 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, &ctx);
 +      pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb);
        fprintf(stderr, "%s %s... %s\n", msg,
                find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf);
        strbuf_release(&sb);
@@@ -410,11 -414,9 +422,11 @@@ static int merge_working_tree(struct ch
                topts.gently = opts->merge && old->commit;
                topts.verbose_update = !opts->quiet;
                topts.fn = twoway_merge;
 -              topts.dir = xcalloc(1, sizeof(*topts.dir));
 -              topts.dir->flags |= DIR_SHOW_IGNORED;
 -              topts.dir->exclude_per_dir = ".gitignore";
 +              if (opts->overwrite_ignore) {
 +                      topts.dir = xcalloc(1, sizeof(*topts.dir));
 +                      topts.dir->flags |= DIR_SHOW_IGNORED;
 +                      setup_standard_excludes(topts.dir);
 +              }
                tree = parse_tree_indirect(old->commit ?
                                           old->commit->object.sha1 :
                                           EMPTY_TREE_SHA1_BIN);
@@@ -543,9 -545,7 +555,9 @@@ static void update_refs_for_switch(stru
                else
                        create_branch(old->name, opts->new_branch, new->name,
                                      opts->new_branch_force ? 1 : 0,
 -                                    opts->new_branch_log, opts->track);
 +                                    opts->new_branch_log,
 +                                    opts->new_branch_force ? 1 : 0,
 +                                    opts->track);
                new->name = opts->new_branch;
                setup_branch_path(new);
        }
                create_symref("HEAD", new->path, msg.buf);
                if (!opts->quiet) {
                        if (old->path && !strcmp(new->path, old->path)) {
 -                              fprintf(stderr, _("Already on '%s'\n"),
 -                                      new->name);
 +                              if (opts->new_branch_force)
 +                                      fprintf(stderr, _("Reset branch '%s'\n"),
 +                                              new->name);
 +                              else
 +                                      fprintf(stderr, _("Already on '%s'\n"),
 +                                              new->name);
                        } else if (opts->new_branch) {
                                if (opts->branch_exists)
                                        fprintf(stderr, _("Switched to and reset branch '%s'\n"), new->name);
                report_tracking(new);
  }
  
 -struct rev_list_args {
 -      int argc;
 -      int alloc;
 -      const char **argv;
 -};
 -
 -static void add_one_rev_list_arg(struct rev_list_args *args, const char *s)
 -{
 -      ALLOC_GROW(args->argv, args->argc + 1, args->alloc);
 -      args->argv[args->argc++] = s;
 -}
 -
 -static int add_one_ref_to_rev_list_arg(const char *refname,
 -                                     const unsigned char *sha1,
 -                                     int flags,
 -                                     void *cb_data)
 -{
 -      add_one_rev_list_arg(cb_data, refname);
 -      return 0;
 -}
 -
 -static int clear_commit_marks_from_one_ref(const char *refname,
 -                                    const unsigned char *sha1,
 -                                    int flags,
 -                                    void *cb_data)
 +static int add_pending_uninteresting_ref(const char *refname,
 +                                       const unsigned char *sha1,
 +                                       int flags, void *cb_data)
  {
 -      struct commit *commit = lookup_commit_reference_gently(sha1, 1);
 -      if (commit)
 -              clear_commit_marks(commit, -1);
 +      add_pending_sha1(cb_data, refname, sha1, flags | UNINTERESTING);
        return 0;
  }
  
  static void describe_one_orphan(struct strbuf *sb, struct commit *commit)
  {
 -      struct pretty_print_context ctx = { 0 };
 -
        parse_commit(commit);
        strbuf_addstr(sb, "  ");
        strbuf_addstr(sb,
                find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
        strbuf_addch(sb, ' ');
 -      pretty_print_commit(CMIT_FMT_ONELINE, commit, sb, &ctx);
 +      pp_commit_easy(CMIT_FMT_ONELINE, commit, sb);
        strbuf_addch(sb, '\n');
  }
  
@@@ -638,34 -660,21 +650,34 @@@ static void suggest_reattach(struct com
                if (more == 1)
                        describe_one_orphan(&sb, last);
                else
 -                      strbuf_addf(&sb, " ... and %d more.\n", more);
 +                      strbuf_addf(&sb, _(" ... and %d more.\n"), more);
        }
  
        fprintf(stderr,
 -              "Warning: you are leaving %d commit%s behind, "
 +              Q_(
 +              /* The singular version */
 +              "Warning: you are leaving %d commit behind, "
 +              "not connected to\n"
 +              "any of your branches:\n\n"
 +              "%s\n",
 +              /* The plural version */
 +              "Warning: you are leaving %d commits behind, "
                "not connected to\n"
                "any of your branches:\n\n"
 -              "%s\n"
 -              "If you want to keep them by creating a new branch, "
 -              "this may be a good time\nto do so with:\n\n"
 -              " git branch new_branch_name %s\n\n",
 -              lost, ((1 < lost) ? "s" : ""),
 -              sb.buf,
 -              sha1_to_hex(commit->object.sha1));
 +              "%s\n",
 +              /* Give ngettext() the count */
 +              lost),
 +              lost,
 +              sb.buf);
        strbuf_release(&sb);
 +
 +      if (advice_detached_head)
 +              fprintf(stderr,
 +                      _(
 +                      "If you want to keep them by creating a new branch, "
 +                      "this may be a good time\nto do so with:\n\n"
 +                      " git branch new_branch_name %s\n\n"),
 +                      sha1_to_hex(commit->object.sha1));
  }
  
  /*
   */
  static void orphaned_commit_warning(struct commit *commit)
  {
 -      struct rev_list_args args = { 0, 0, NULL };
        struct rev_info revs;
 -
 -      add_one_rev_list_arg(&args, "(internal)");
 -      add_one_rev_list_arg(&args, sha1_to_hex(commit->object.sha1));
 -      add_one_rev_list_arg(&args, "--not");
 -      for_each_ref(add_one_ref_to_rev_list_arg, &args);
 -      add_one_rev_list_arg(&args, "--");
 -      add_one_rev_list_arg(&args, NULL);
 +      struct object *object = &commit->object;
 +      struct object_array refs;
  
        init_revisions(&revs, NULL);
 -      if (setup_revisions(args.argc - 1, args.argv, &revs, NULL) != 1)
 -              die(_("internal error: only -- alone should have been left"));
 +      setup_revisions(0, NULL, &revs, NULL);
 +
 +      object->flags &= ~UNINTERESTING;
 +      add_pending_object(&revs, object, sha1_to_hex(object->sha1));
 +
 +      for_each_ref(add_pending_uninteresting_ref, &revs);
 +
 +      refs = revs.pending;
 +      revs.leak_pending = 1;
 +
        if (prepare_revision_walk(&revs))
                die(_("internal error in revision walk"));
        if (!(commit->object.flags & UNINTERESTING))
        else
                describe_detached_head(_("Previous HEAD position was"), commit);
  
 -      clear_commit_marks(commit, -1);
 -      for_each_ref(clear_commit_marks_from_one_ref, NULL);
 +      clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);
 +      free(refs.objects);
  }
  
  static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
  {
        int ret = 0;
        struct branch_info old;
 +      void *path_to_free;
        unsigned char rev[20];
        int flag;
        memset(&old, 0, sizeof(old));
 -      old.path = resolve_ref("HEAD", rev, 0, &flag);
 +      old.path = path_to_free = resolve_refdup("HEAD", rev, 0, &flag);
        old.commit = lookup_commit_reference_gently(rev, 1);
        if (!(flag & REF_ISSYMREF))
                old.path = NULL;
        }
  
        ret = merge_working_tree(opts, &old, new);
 -      if (ret)
 +      if (ret) {
 +              free(path_to_free);
                return ret;
 +      }
  
        if (!opts->quiet && !old.path && old.commit && new->commit != old.commit)
                orphaned_commit_warning(old.commit);
        update_refs_for_switch(opts, &old, new);
  
        ret = post_checkout_hook(old.commit, new->commit, 1);
 +      free(path_to_free);
        return ret || opts->writeout_error;
  }
  
@@@ -875,8 -878,8 +887,8 @@@ static int parse_branchname_arg(int arg
        new->name = arg;
        setup_branch_path(new);
  
 -      if (check_ref_format(new->path) == CHECK_REF_FORMAT_OK &&
 -          resolve_ref(new->path, branch_rev, 1, NULL))
 +      if (!check_refname_format(new->path, 0) &&
 +          !read_ref(new->path, branch_rev))
                hashcpy(rev, branch_rev);
        else
                new->path = NULL; /* not an existing branch */
@@@ -936,7 -939,6 +948,7 @@@ int cmd_checkout(int argc, const char *
                            3),
                OPT__FORCE(&opts.force, "force checkout (throw away local modifications)"),
                OPT_BOOLEAN('m', "merge", &opts.merge, "perform a 3-way merge with the new branch"),
 +              OPT_BOOLEAN(0, "overwrite-ignore", &opts.overwrite_ignore, "update ignored files (default)"),
                OPT_STRING(0, "conflict", &conflict_style, "style",
                           "conflict style (merge or diff3)"),
                OPT_BOOLEAN('p', "patch", &patch_mode, "select hunks interactively"),
  
        memset(&opts, 0, sizeof(opts));
        memset(&new, 0, sizeof(new));
 +      opts.overwrite_ignore = 1;
  
        gitmodules_config();
        git_config(git_checkout_config, &opts);
                die (_("--patch is incompatible with all other options"));
  
        if (opts.force_detach && (opts.new_branch || opts.new_orphan_branch))
 -              die("--detach cannot be used with -b/-B/--orphan");
 +              die(_("--detach cannot be used with -b/-B/--orphan"));
        if (opts.force_detach && 0 < opts.track)
 -              die("--detach cannot be used with -t");
 +              die(_("--detach cannot be used with -t"));
  
        /* --track without -b should DWIM */
        if (0 < opts.track && !opts.new_branch) {
                }
  
                if (opts.force_detach)
 -                      die("git checkout: --detach does not take a path argument");
 +                      die(_("git checkout: --detach does not take a path argument"));
  
                if (1 < !!opts.writeout_stage + !!opts.force + !!opts.merge)
                        die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\nchecking out of the index."));
  
 -              return checkout_paths(source_tree, pathspec, &opts);
 +              return checkout_paths(source_tree, pathspec, prefix, &opts);
        }
  
        if (patch_mode)
  
        if (opts.new_branch) {
                struct strbuf buf = STRBUF_INIT;
 -              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)) {
 -                      opts.branch_exists = 1;
 -                      if (!opts.new_branch_force)
 -                              die(_("git checkout: branch %s already exists"),
 -                                  opts.new_branch);
 -              }
 +
 +              opts.branch_exists = validate_new_branchname(opts.new_branch, &buf,
 +                                                           !!opts.new_branch_force,
 +                                                           !!opts.new_branch_force);
 +
                strbuf_release(&buf);
        }