Merge branch 'maint'
authorJunio C Hamano <gitster@pobox.com>
Sun, 9 Nov 2008 18:56:26 +0000 (10:56 -0800)
committerJunio C Hamano <gitster@pobox.com>
Sun, 9 Nov 2008 18:56:26 +0000 (10:56 -0800)
* maint:
Documentation: bisect: change a few instances of "git-cmd" to "git cmd"
Documentation: rev-list: change a few instances of "git-cmd" to "git cmd"
checkout: Don't crash when switching away from an invalid branch.

1  2 
Documentation/rev-list-options.txt
builtin-checkout.c
index 966276b169d6fb09c571a05257dfbc1fe3bd4f03,1023ac2b59c139cad2d9634a7a88b812d7761102..68a253f9778e50472fcfcf5e3a1690ccdd0492e7
@@@ -285,7 -285,7 +285,7 @@@ See also linkgit:git-reflog[1]
  History Simplification
  ~~~~~~~~~~~~~~~~~~~~~~
  
- When optional paths are given, 'git-rev-list' simplifies commits with
+ When optional paths are given, 'git rev-list' simplifies commits with
  various strategies, according to the options you have selected.
  
  Suppose you specified `foo` as the <paths>.  We shall call commits
@@@ -413,48 -413,6 +413,48 @@@ Note that without '\--full-history', th
  one of the parents is TREESAME, we follow only that one, so the other
  sides of the merge are never walked.
  
 +Finally, there is a fourth simplification mode available:
 +
 +--simplify-merges::
 +
 +      First, build a history graph in the same way that
 +      '\--full-history' with parent rewriting does (see above).
 ++
 +Then simplify each commit `C` to its replacement `C'` in the final
 +history according to the following rules:
 ++
 +--
 +* Set `C'` to `C`.
 ++
 +* Replace each parent `P` of `C'` with its simplification `P'`.  In
 +  the process, drop parents that are ancestors of other parents, and
 +  remove duplicates.
 ++
 +* If after this parent rewriting, `C'` is a root or merge commit (has
 +  zero or >1 parents), a boundary commit, or !TREESAME, it remains.
 +  Otherwise, it is replaced with its only parent.
 +--
 ++
 +The effect of this is best shown by way of comparing to
 +'\--full-history' with parent rewriting.  The example turns into:
 ++
 +-----------------------------------------------------------------------
 +        .-A---M---N---O
 +       /     /       /
 +      I     B       D
 +       \   /       /
 +        `---------'
 +-----------------------------------------------------------------------
 ++
 +Note the major differences in `N` and `P` over '\--full-history':
 ++
 +--
 +* `N`'s parent list had `I` removed, because it is an ancestor of the
 +  other parent `M`.  Still, `N` remained because it is !TREESAME.
 ++
 +* `P`'s parent list similarly had `I` removed.  `P` was then
 +  removed completely, because it had one parent and is TREESAME.
 +--
  
  ifdef::git-rev-list[]
  Bisection Helpers
@@@ -466,14 -424,14 +466,14 @@@ Limit output to the one commit object w
  the included and excluded commits. Thus, if
  
  -----------------------------------------------------------------------
-       $ git-rev-list --bisect foo ^bar ^baz
+       $ git rev-list --bisect foo ^bar ^baz
  -----------------------------------------------------------------------
  
  outputs 'midpoint', the output of the two commands
  
  -----------------------------------------------------------------------
-       $ git-rev-list foo ^midpoint
-       $ git-rev-list midpoint ^bar ^baz
+       $ git rev-list foo ^midpoint
+       $ git rev-list midpoint ^bar ^baz
  -----------------------------------------------------------------------
  
  would be of roughly the same length.  Finding the change which
diff --combined builtin-checkout.c
index 57b94d282931187a0cb6394d4df707237038a7eb,05eee4ecc71d7a20d9faf59c849d3c693ae4cc8d..06904c3bd3595b4c8cbc6c66d3a1b9c364381bd6
@@@ -13,9 -13,6 +13,9 @@@
  #include "diff.h"
  #include "revision.h"
  #include "remote.h"
 +#include "blob.h"
 +#include "xdiff-interface.h"
 +#include "ll-merge.h"
  
  static const char * const checkout_usage[] = {
        "git checkout [options] <branch>",
        NULL,
  };
  
 +struct checkout_opts {
 +      int quiet;
 +      int merge;
 +      int force;
 +      int writeout_stage;
 +      int writeout_error;
 +
 +      const char *new_branch;
 +      int new_branch_log;
 +      enum branch_track track;
 +};
 +
  static int post_checkout_hook(struct commit *old, struct commit *new,
                              int changed)
  {
@@@ -47,7 -32,7 +47,7 @@@
  
        memset(&proc, 0, sizeof(proc));
        argv[0] = name;
-       argv[1] = xstrdup(sha1_to_hex(old->object.sha1));
+       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;
@@@ -99,121 -84,8 +99,121 @@@ static int skip_same_name(struct cache_
        return pos;
  }
  
 +static int check_stage(int stage, struct cache_entry *ce, int pos)
 +{
 +      while (pos < active_nr &&
 +             !strcmp(active_cache[pos]->name, ce->name)) {
 +              if (ce_stage(active_cache[pos]) == stage)
 +                      return 0;
 +              pos++;
 +      }
 +      return error("path '%s' does not have %s version",
 +                   ce->name,
 +                   (stage == 2) ? "our" : "their");
 +}
 +
 +static int check_all_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);
 +      return 0;
 +}
 +
 +static int checkout_stage(int stage, struct cache_entry *ce, int pos,
 +                        struct checkout *state)
 +{
 +      while (pos < active_nr &&
 +             !strcmp(active_cache[pos]->name, ce->name)) {
 +              if (ce_stage(active_cache[pos]) == stage)
 +                      return checkout_entry(active_cache[pos], state, NULL);
 +              pos++;
 +      }
 +      return error("path '%s' does not have %s version",
 +                   ce->name,
 +                   (stage == 2) ? "our" : "their");
 +}
 +
 +/* NEEDSWORK: share with merge-recursive */
 +static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
 +{
 +      unsigned long size;
 +      enum object_type type;
 +
 +      if (!hashcmp(sha1, null_sha1)) {
 +              mm->ptr = xstrdup("");
 +              mm->size = 0;
 +              return;
 +      }
 +
 +      mm->ptr = read_sha1_file(sha1, &type, &size);
 +      if (!mm->ptr || type != OBJ_BLOB)
 +              die("unable to read blob object %s", sha1_to_hex(sha1));
 +      mm->size = size;
 +}
 +
 +static int checkout_merged(int pos, struct checkout *state)
 +{
 +      struct cache_entry *ce = active_cache[pos];
 +      const char *path = ce->name;
 +      mmfile_t ancestor, ours, theirs;
 +      int status;
 +      unsigned char sha1[20];
 +      mmbuffer_t result_buf;
 +
 +      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);
 +
 +      fill_mm(active_cache[pos]->sha1, &ancestor);
 +      fill_mm(active_cache[pos+1]->sha1, &ours);
 +      fill_mm(active_cache[pos+2]->sha1, &theirs);
 +
 +      status = ll_merge(&result_buf, path, &ancestor,
 +                        &ours, "ours", &theirs, "theirs", 1);
 +      free(ancestor.ptr);
 +      free(ours.ptr);
 +      free(theirs.ptr);
 +      if (status < 0 || !result_buf.ptr) {
 +              free(result_buf.ptr);
 +              return error("path '%s': cannot merge", path);
 +      }
 +
 +      /*
 +       * 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
 +       * 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
 +       * to be refactored to allow us to feed a <buffer, size, mode>
 +       * instead of a cache entry.  Such a refactoring would help
 +       * merge_recursive as well (it also writes the merge result to the
 +       * object database even when it may contain conflicts).
 +       */
 +      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);
 +      if (!ce)
 +              die("make_cache_entry failed for path '%s'", path);
 +      status = checkout_entry(ce, state, NULL);
 +      return status;
 +}
  
 -static int checkout_paths(struct tree *source_tree, const char **pathspec)
 +static int checkout_paths(struct tree *source_tree, const char **pathspec,
 +                        struct checkout_opts *opts)
  {
        int pos;
        struct checkout state;
        int flag;
        struct commit *head;
        int errs = 0;
 -
 +      int stage = opts->writeout_stage;
 +      int merge = opts->merge;
        int newfd;
        struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
  
                if (pathspec_match(pathspec, NULL, ce->name, 0)) {
                        if (!ce_stage(ce))
                                continue;
 -                      errs = 1;
 -                      error("path '%s' is unmerged", ce->name);
 +                      if (opts->force) {
 +                              warning("path '%s' is unmerged", ce->name);
 +                      } else if (stage) {
 +                              errs |= check_stage(stage, ce, pos);
 +                      } else if (opts->merge) {
 +                              errs |= check_all_stages(ce, pos);
 +                      } else {
 +                              errs = 1;
 +                              error("path '%s' is unmerged", ce->name);
 +                      }
                        pos = skip_same_name(ce, pos) - 1;
                }
        }
                                errs |= checkout_entry(ce, &state, NULL);
                                continue;
                        }
 +                      if (stage)
 +                              errs |= checkout_stage(stage, ce, pos, &state);
 +                      else if (merge)
 +                              errs |= checkout_merged(pos, &state);
                        pos = skip_same_name(ce, pos) - 1;
                }
        }
@@@ -310,7 -169,8 +310,7 @@@ static void show_local_changes(struct o
  
  static void describe_detached_head(char *msg, struct commit *commit)
  {
 -      struct strbuf sb;
 -      strbuf_init(&sb, 0);
 +      struct strbuf sb = STRBUF_INIT;
        parse_commit(commit);
        pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb, 0, NULL, NULL, 0, 0);
        fprintf(stderr, "%s %s... %s\n", msg,
        strbuf_release(&sb);
  }
  
 -struct checkout_opts {
 -      int quiet;
 -      int merge;
 -      int force;
 -      int writeout_error;
 -
 -      char *new_branch;
 -      int new_branch_log;
 -      enum branch_track track;
 -};
 -
  static int reset_tree(struct tree *tree, struct checkout_opts *o, int worktree)
  {
        struct unpack_trees_options opts;
@@@ -359,7 -230,8 +359,7 @@@ struct branch_info 
  
  static void setup_branch_path(struct branch_info *branch)
  {
 -      struct strbuf buf;
 -      strbuf_init(&buf, 0);
 +      struct strbuf buf = STRBUF_INIT;
        strbuf_addstr(&buf, "refs/heads/");
        strbuf_addstr(&buf, branch->name);
        branch->path = strbuf_detach(&buf, NULL);
@@@ -421,7 -293,6 +421,7 @@@ static int merge_working_tree(struct ch
                         */
                        struct tree *result;
                        struct tree *work;
 +                      struct merge_options o;
                        if (!opts->merge)
                                return 1;
                        parse_commit(old->commit);
                         */
  
                        add_files_to_cache(NULL, NULL, 0);
 -                      work = write_tree_from_memory();
 +                      init_merge_options(&o);
 +                      o.verbosity = 0;
 +                      work = write_tree_from_memory(&o);
  
                        ret = reset_tree(new->commit->tree, opts, 1);
                        if (ret)
                                return ret;
 -                      merge_trees(new->commit->tree, work, old->commit->tree,
 -                                  new->name, "local", &result);
 +                      o.branch1 = new->name;
 +                      o.branch2 = "local";
 +                      merge_trees(&o, new->commit->tree, work,
 +                              old->commit->tree, &result);
                        ret = reset_tree(new->commit->tree, opts, 0);
                        if (ret)
                                return ret;
@@@ -482,7 -349,7 +482,7 @@@ static void update_refs_for_switch(stru
                                   struct branch_info *old,
                                   struct branch_info *new)
  {
 -      struct strbuf msg;
 +      struct strbuf msg = STRBUF_INIT;
        const char *old_desc;
        if (opts->new_branch) {
                create_branch(old->name, opts->new_branch, new->name, 0,
                setup_branch_path(new);
        }
  
 -      strbuf_init(&msg, 0);
        old_desc = old->name;
-       if (!old_desc)
+       if (!old_desc && old->commit)
                old_desc = sha1_to_hex(old->commit->object.sha1);
        strbuf_addf(&msg, "checkout: moving from %s to %s",
-                   old_desc, new->name);
+                   old_desc ? old_desc : "(invalid)", new->name);
  
        if (new->path) {
                create_symref("HEAD", new->path, msg.buf);
@@@ -551,7 -419,7 +551,7 @@@ static int switch_branches(struct check
         * 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 && new->commit != old.commit)
+       if (!opts->quiet && !old.path && old.commit && new->commit != old.commit)
                describe_detached_head("Previous HEAD position was", old.commit);
  
        if (!old.commit) {
        return ret || opts->writeout_error;
  }
  
 +static int git_checkout_config(const char *var, const char *value, void *cb)
 +{
 +      return git_xmerge_config(var, value, cb);
 +}
 +
  int cmd_checkout(int argc, const char **argv, const char *prefix)
  {
        struct checkout_opts opts;
        const char *arg;
        struct branch_info new;
        struct tree *source_tree = NULL;
 +      char *conflict_style = NULL;
        struct option options[] = {
                OPT__QUIET(&opts.quiet),
                OPT_STRING('b', NULL, &opts.new_branch, "new branch", "branch"),
                OPT_BOOLEAN('l', NULL, &opts.new_branch_log, "log for new branch"),
                OPT_SET_INT('t', "track",  &opts.track, "track",
                        BRANCH_TRACK_EXPLICIT),
 +              OPT_SET_INT('2', "ours", &opts.writeout_stage, "stage",
 +                          2),
 +              OPT_SET_INT('3', "theirs", &opts.writeout_stage, "stage",
 +                          3),
                OPT_BOOLEAN('f', NULL, &opts.force, "force"),
 -              OPT_BOOLEAN('m', NULL, &opts.merge, "merge"),
 +              OPT_BOOLEAN('m', "merge", &opts.merge, "merge"),
 +              OPT_STRING(0, "conflict", &conflict_style, "style",
 +                         "conflict style (merge or diff3)"),
                OPT_END(),
        };
        int has_dash_dash;
        memset(&opts, 0, sizeof(opts));
        memset(&new, 0, sizeof(new));
  
 -      git_config(git_default_config, NULL);
 +      git_config(git_checkout_config, NULL);
  
 -      opts.track = git_branch_track;
 +      opts.track = BRANCH_TRACK_UNSPECIFIED;
  
        argc = parse_options(argc, argv, options, checkout_usage,
                             PARSE_OPT_KEEP_DASHDASH);
  
 -      if (!opts.new_branch && (opts.track != git_branch_track))
 -              die("git checkout: --track and --no-track require -b");
 +      /* --track without -b should DWIM */
 +      if (0 < opts.track && !opts.new_branch) {
 +              const char *argv0 = argv[0];
 +              if (!argc || !strcmp(argv0, "--"))
 +                      die ("--track needs a branch name");
 +              if (!prefixcmp(argv0, "refs/"))
 +                      argv0 += 5;
 +              if (!prefixcmp(argv0, "remotes/"))
 +                      argv0 += 8;
 +              argv0 = strchr(argv0, '/');
 +              if (!argv0 || !argv0[1])
 +                      die ("Missing branch name; try -b");
 +              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);
 +      }
  
        if (opts.force && opts.merge)
                die("git checkout: -f and -m are incompatible");
@@@ -717,22 -554,20 +717,22 @@@ no_reference
                        die("invalid path specification");
  
                /* Checkout paths */
 -              if (opts.new_branch || opts.force || opts.merge) {
 +              if (opts.new_branch) {
                        if (argc == 1) {
 -                              die("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]);
 +                              die("git checkout: updating paths is incompatible with switching branches.\nDid you intend to checkout '%s' which can not be resolved as commit?", argv[0]);
                        } else {
 -                              die("git checkout: updating paths is incompatible with switching branches/forcing");
 +                              die("git checkout: updating paths is incompatible with switching branches.");
                        }
                }
  
 -              return checkout_paths(source_tree, pathspec);
 +              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);
        }
  
        if (opts.new_branch) {
 -              struct strbuf buf;
 -              strbuf_init(&buf, 0);
 +              struct strbuf buf = STRBUF_INIT;
                strbuf_addstr(&buf, "refs/heads/");
                strbuf_addstr(&buf, opts.new_branch);
                if (!get_sha1(buf.buf, rev))
        if (new.name && !new.commit) {
                die("Cannot switch branch to a non-commit.");
        }
 +      if (opts.writeout_stage)
 +              die("--ours/--theirs is incompatible with switching branches.");
  
        return switch_branches(&opts, &new);
  }