Code

Merge branch 'maint-1.6.0' into maint-1.6.1
authorJunio C Hamano <gitster@pobox.com>
Sun, 10 Jan 2010 08:48:47 +0000 (00:48 -0800)
committerJunio C Hamano <gitster@pobox.com>
Sun, 10 Jan 2010 08:48:47 +0000 (00:48 -0800)
* maint-1.6.0:
  base85: Make the code more obvious instead of explaining the non-obvious
  base85: encode_85() does not use the decode table
  base85 debug code: Fix length byte calculation
  checkout -m: do not try to fall back to --merge from an unborn branch
  branch: die explicitly why when calling "git branch [-a|-r] branchname".

1  2 
builtin-branch.c
builtin-checkout.c

diff --combined builtin-branch.c
index 23b6949fec30e8276d0ef3d34e0aaee10eb49165,7dd51d1fed23dad3edfeedff9659618e9f276ea6..7c615871100aa8c9e637bde41197419bfb04d68a
@@@ -97,6 -97,7 +97,6 @@@ static int delete_branches(int argc, co
        unsigned char sha1[20];
        char *name = NULL;
        const char *fmt, *remote;
 -      char section[PATH_MAX];
        int i;
        int ret = 0;
  
                               argv[i]);
                        ret = 1;
                } else {
 -                      printf("Deleted %sbranch %s.\n", remote, argv[i]);
 -                      snprintf(section, sizeof(section), "branch.%s",
 -                               argv[i]);
 -                      if (git_config_rename_section(section, NULL) < 0)
 +                      struct strbuf buf = STRBUF_INIT;
 +                      printf("Deleted %sbranch %s (was %s).\n", remote, argv[i],
 +                              find_unique_abbrev(sha1, DEFAULT_ABBREV));
 +                      strbuf_addf(&buf, "branch.%s", argv[i]);
 +                      if (git_config_rename_section(buf.buf, NULL) < 0)
                                warning("Update of config-file failed");
 +                      strbuf_release(&buf);
                }
        }
  
@@@ -280,7 -279,7 +280,7 @@@ static int ref_cmp(const void *r1, cons
        return strcmp(c1->name, c2->name);
  }
  
 -static void fill_tracking_info(char *stat, const char *branch_name)
 +static void fill_tracking_info(struct strbuf *stat, const char *branch_name)
  {
        int ours, theirs;
        struct branch *branch = branch_get(branch_name);
        if (!stat_tracking_info(branch, &ours, &theirs) || (!ours && !theirs))
                return;
        if (!ours)
 -              sprintf(stat, "[behind %d] ", theirs);
 +              strbuf_addf(stat, "[behind %d] ", theirs);
        else if (!theirs)
 -              sprintf(stat, "[ahead %d] ", ours);
 +              strbuf_addf(stat, "[ahead %d] ", ours);
        else
 -              sprintf(stat, "[ahead %d, behind %d] ", ours, theirs);
 +              strbuf_addf(stat, "[ahead %d, behind %d] ", ours, theirs);
  }
  
  static int matches_merge_filter(struct commit *commit)
@@@ -335,8 -334,12 +335,8 @@@ static void print_ref_item(struct ref_i
        }
  
        if (verbose) {
 -              struct strbuf subject;
 +              struct strbuf subject = STRBUF_INIT, stat = STRBUF_INIT;
                const char *sub = " **** invalid ref ****";
 -              char stat[128];
 -
 -              strbuf_init(&subject, 0);
 -              stat[0] = '\0';
  
                commit = item->commit;
                if (commit && !parse_commit(commit)) {
                }
  
                if (item->kind == REF_LOCAL_BRANCH)
 -                      fill_tracking_info(stat, item->name);
 +                      fill_tracking_info(&stat, item->name);
  
                printf("%c %s%-*s%s %s %s%s\n", c, branch_get_color(color),
                       maxwidth, item->name,
                       branch_get_color(COLOR_BRANCH_RESET),
                       find_unique_abbrev(item->commit->object.sha1, abbrev),
 -                     stat, sub);
 +                     stat.buf, sub);
 +              strbuf_release(&stat);
                strbuf_release(&subject);
        } else {
                printf("%c %s%s%s\n", c, branch_get_color(color), item->name,
@@@ -425,45 -427,42 +425,45 @@@ static void print_ref_list(int kinds, i
  
  static void rename_branch(const char *oldname, const char *newname, int force)
  {
 -      char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
 +      struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
        unsigned char sha1[20];
 -      char oldsection[PATH_MAX], newsection[PATH_MAX];
 +      struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
  
        if (!oldname)
                die("cannot rename the current branch while not on any.");
  
 -      if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
 -              die("Old branchname too long");
 +      strbuf_addf(&oldref, "refs/heads/%s", oldname);
  
 -      if (check_ref_format(oldref))
 -              die("Invalid branch name: %s", oldref);
 +      if (check_ref_format(oldref.buf))
 +              die("Invalid branch name: %s", oldref.buf);
  
 -      if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
 -              die("New branchname too long");
 +      strbuf_addf(&newref, "refs/heads/%s", newname);
  
 -      if (check_ref_format(newref))
 -              die("Invalid branch name: %s", newref);
 +      if (check_ref_format(newref.buf))
 +              die("Invalid branch name: %s", newref.buf);
  
 -      if (resolve_ref(newref, sha1, 1, NULL) && !force)
 +      if (resolve_ref(newref.buf, sha1, 1, NULL) && !force)
                die("A branch named '%s' already exists.", newname);
  
 -      snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s",
 -               oldref, newref);
 +      strbuf_addf(&logmsg, "Branch: renamed %s to %s",
 +               oldref.buf, newref.buf);
  
 -      if (rename_ref(oldref, newref, logmsg))
 +      if (rename_ref(oldref.buf, newref.buf, logmsg.buf))
                die("Branch rename failed");
 +      strbuf_release(&logmsg);
  
        /* no need to pass logmsg here as HEAD didn't really move */
 -      if (!strcmp(oldname, head) && create_symref("HEAD", newref, NULL))
 +      if (!strcmp(oldname, head) && create_symref("HEAD", newref.buf, NULL))
                die("Branch renamed to %s, but HEAD is not updated!", newname);
  
 -      snprintf(oldsection, sizeof(oldsection), "branch.%s", oldref + 11);
 -      snprintf(newsection, sizeof(newsection), "branch.%s", newref + 11);
 -      if (git_config_rename_section(oldsection, newsection) < 0)
 +      strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11);
 +      strbuf_release(&oldref);
 +      strbuf_addf(&newsection, "branch.%s", newref.buf + 11);
 +      strbuf_release(&newref);
 +      if (git_config_rename_section(oldsection.buf, newsection.buf) < 0)
                die("Branch is renamed, but update of config-file failed");
 +      strbuf_release(&oldsection);
 +      strbuf_release(&newsection);
  }
  
  static int opt_parse_with_commit(const struct option *opt, const char *arg, int unset)
@@@ -583,10 -582,12 +583,12 @@@ int cmd_branch(int argc, const char **a
                rename_branch(head, argv[0], rename > 1);
        else if (rename && (argc == 2))
                rename_branch(argv[0], argv[1], rename > 1);
-       else if (argc <= 2)
+       else if (argc <= 2) {
+               if (kinds != REF_LOCAL_BRANCH)
+                       die("-a and -r options to 'git branch' do not make sense with a branch name");
                create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
                              force_create, reflog, track);
-       else
+       else
                usage_with_options(builtin_branch_usage, options);
  
        return 0;
diff --combined builtin-checkout.c
index b5dd9c07b42e0130384259715730f52bc8c4e6c3,b76cd22776299955d1ff876b5668f2336b4321a2..f32b14838d718907b105bcefd4e015f31aed1403
@@@ -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)
  {
@@@ -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 checkout_paths(struct tree *source_tree, const char **pathspec)
 +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,
 +                        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));
  
        newfd = hold_locked_index(lock_file, 1);
 -      read_cache();
 +      if (read_cache() < 0)
 +              return error("corrupt index file");
  
        if (source_tree)
                read_tree_some(source_tree, pathspec);
                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;
                }
        }
@@@ -311,7 -169,8 +311,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;
@@@ -360,7 -230,8 +360,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);
@@@ -372,9 -243,7 +372,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() < 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));
                         */
                        struct tree *result;
                        struct tree *work;
 +                      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 */
  
                         */
  
                        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;
@@@ -484,7 -354,7 +490,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 && old->commit)
                old_desc = sha1_to_hex(old->commit->object.sha1);
@@@ -574,11 -445,6 +580,11 @@@ static int switch_branches(struct check
        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");
                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);
@@@ -719,22 -559,20 +725,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);
  }