Merge branch 'maint-1.6.2' into maint-1.6.3
authorJunio C Hamano <gitster@pobox.com>
Tue, 19 Jan 2010 05:29:47 +0000 (21:29 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 19 Jan 2010 05:29:47 +0000 (21:29 -0800)
* maint-1.6.2:
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".
textconv: stop leaking file descriptors
commit: --cleanup is a message option
git count-objects: handle packs bigger than 4G
t7102: make the test fail if one of its check fails

Conflicts:
diff.c

1  2 
base85.c
builtin-branch.c
builtin-checkout.c
builtin-commit.c
diff.c
t/t5403-post-checkout-hook.sh
diff --combined base85.c
index b417a15bbc83fff7180078a4cf9f73603477a295,24ddf60eb0a20f35efcdc2d37f99e46d94e8f5c9..e459feebbf90c6557dbf3ff913f83a57a8afb210
+++ b/base85.c
@@@ -57,14 -57,8 +57,8 @@@ int decode_85(char *dst, const char *bu
                de = de85[ch];
                if (--de < 0)
                        return error("invalid base85 alphabet %c", ch);
-               /*
-                * Detect overflow.  The largest
-                * 5-letter possible is "|NsC0" to
-                * encode 0xffffffff, and "|NsC" gives
-                * 0x03030303 at this point (i.e.
-                * 0xffffffff = 0x03030303 * 85).
-                */
-               if (0x03030303 < acc ||
+               /* Detect overflow. */
+               if (0xffffffff / 85 < acc ||
                    0xffffffff - de < (acc *= 85))
                        return error("invalid base85 sequence %.5s", buffer-5);
                acc += de;
  
  void encode_85(char *buf, const unsigned char *data, int bytes)
  {
-       prep_base85();
        say("encode 85");
        while (bytes) {
                unsigned acc = 0;
                int cnt;
                for (cnt = 24; cnt >= 0; cnt -= 8) {
 -                      int ch = *data++;
 +                      unsigned ch = *data++;
                        acc |= ch << cnt;
                        if (--bytes == 0)
                                break;
@@@ -118,7 -110,7 +110,7 @@@ int main(int ac, char **av
                int len = strlen(av[2]);
                encode_85(buf, av[2], len);
                if (len <= 26) len = len + 'A' - 1;
-               else len = len + 'a' - 26 + 1;
+               else len = len + 'a' - 26 - 1;
                printf("encoded: %c%s\n", len, buf);
                return 0;
        }
diff --combined builtin-branch.c
index 887fa60fa54139eb5e25f801e5b964978db846ab,74c404bd3f72d8931d42c3ecd841e25a5b2edec1..73101a183eda3d33412fc7951b006425d5d1a10a
@@@ -32,18 -32,18 +32,18 @@@ static unsigned char head_sha1[20]
  
  static int branch_use_color = -1;
  static char branch_colors[][COLOR_MAXLEN] = {
 -      "\033[m",       /* reset */
 -      "",             /* PLAIN (normal) */
 -      "\033[31m",     /* REMOTE (red) */
 -      "",             /* LOCAL (normal) */
 -      "\033[32m",     /* CURRENT (green) */
 +      GIT_COLOR_RESET,
 +      GIT_COLOR_NORMAL,       /* PLAIN */
 +      GIT_COLOR_RED,          /* REMOTE */
 +      GIT_COLOR_NORMAL,       /* LOCAL */
 +      GIT_COLOR_GREEN,        /* CURRENT */
  };
  enum color_branch {
 -      COLOR_BRANCH_RESET = 0,
 -      COLOR_BRANCH_PLAIN = 1,
 -      COLOR_BRANCH_REMOTE = 2,
 -      COLOR_BRANCH_LOCAL = 3,
 -      COLOR_BRANCH_CURRENT = 4,
 +      BRANCH_COLOR_RESET = 0,
 +      BRANCH_COLOR_PLAIN = 1,
 +      BRANCH_COLOR_REMOTE = 2,
 +      BRANCH_COLOR_LOCAL = 3,
 +      BRANCH_COLOR_CURRENT = 4,
  };
  
  static enum merge_filter {
@@@ -56,15 -56,15 +56,15 @@@ static unsigned char merge_filter_ref[2
  static int parse_branch_color_slot(const char *var, int ofs)
  {
        if (!strcasecmp(var+ofs, "plain"))
 -              return COLOR_BRANCH_PLAIN;
 +              return BRANCH_COLOR_PLAIN;
        if (!strcasecmp(var+ofs, "reset"))
 -              return COLOR_BRANCH_RESET;
 +              return BRANCH_COLOR_RESET;
        if (!strcasecmp(var+ofs, "remote"))
 -              return COLOR_BRANCH_REMOTE;
 +              return BRANCH_COLOR_REMOTE;
        if (!strcasecmp(var+ofs, "local"))
 -              return COLOR_BRANCH_LOCAL;
 +              return BRANCH_COLOR_LOCAL;
        if (!strcasecmp(var+ofs, "current"))
 -              return COLOR_BRANCH_CURRENT;
 +              return BRANCH_COLOR_CURRENT;
        die("bad config variable '%s'", var);
  }
  
@@@ -121,7 -121,11 +121,7 @@@ static int delete_branches(int argc, co
                        die("Couldn't look up commit object for HEAD");
        }
        for (i = 0; i < argc; i++, strbuf_release(&bname)) {
 -              int len = strlen(argv[i]);
 -
 -              if (interpret_nth_last_branch(argv[i], &bname) != len)
 -                      strbuf_add(&bname, argv[i], len);
 -
 +              strbuf_branchname(&bname, argv[i]);
                if (kinds == REF_LOCAL_BRANCH && !strcmp(head, bname.buf)) {
                        error("Cannot delete the branch '%s' "
                              "which you are currently on.", bname.buf);
  
  struct ref_item {
        char *name;
 -      unsigned int kind;
 +      char *dest;
 +      unsigned int kind, len;
        struct commit *commit;
  };
  
  struct ref_list {
        struct rev_info revs;
 -      int index, alloc, maxwidth;
 +      int index, alloc, maxwidth, verbose, abbrev;
        struct ref_item *list;
        struct commit_list *with_commit;
        int kinds;
  };
  
 +static char *resolve_symref(const char *src, const char *prefix)
 +{
 +      unsigned char sha1[20];
 +      int flag;
 +      const char *dst, *cp;
 +
 +      dst = resolve_ref(src, sha1, 0, &flag);
 +      if (!(dst && (flag & REF_ISSYMREF)))
 +              return NULL;
 +      if (prefix && (cp = skip_prefix(dst, prefix)))
 +              dst = cp;
 +      return xstrdup(dst);
 +}
 +
  static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
  {
        struct ref_list *ref_list = (struct ref_list*)(cb_data);
        struct ref_item *newitem;
        struct commit *commit;
 -      int kind;
 -      int len;
 +      int kind, i;
 +      const char *prefix, *orig_refname = refname;
 +
 +      static struct {
 +              int kind;
 +              const char *prefix;
 +              int pfxlen;
 +      } ref_kind[] = {
 +              { REF_LOCAL_BRANCH, "refs/heads/", 11 },
 +              { REF_REMOTE_BRANCH, "refs/remotes/", 13 },
 +      };
  
        /* Detect kind */
 -      if (!prefixcmp(refname, "refs/heads/")) {
 -              kind = REF_LOCAL_BRANCH;
 -              refname += 11;
 -      } else if (!prefixcmp(refname, "refs/remotes/")) {
 -              kind = REF_REMOTE_BRANCH;
 -              refname += 13;
 -      } else
 -              return 0;
 -
 -      commit = lookup_commit_reference_gently(sha1, 1);
 -      if (!commit)
 -              return error("branch '%s' does not point at a commit", refname);
 -
 -      /* Filter with with_commit if specified */
 -      if (!is_descendant_of(commit, ref_list->with_commit))
 +      for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
 +              prefix = ref_kind[i].prefix;
 +              if (strncmp(refname, prefix, ref_kind[i].pfxlen))
 +                      continue;
 +              kind = ref_kind[i].kind;
 +              refname += ref_kind[i].pfxlen;
 +              break;
 +      }
 +      if (ARRAY_SIZE(ref_kind) <= i)
                return 0;
  
        /* Don't add types the caller doesn't want */
        if ((kind & ref_list->kinds) == 0)
                return 0;
  
 -      if (merge_filter != NO_FILTER)
 -              add_pending_object(&ref_list->revs,
 -                                 (struct object *)commit, refname);
 +      commit = NULL;
 +      if (ref_list->verbose || ref_list->with_commit || merge_filter != NO_FILTER) {
 +              commit = lookup_commit_reference_gently(sha1, 1);
 +              if (!commit)
 +                      return error("branch '%s' does not point at a commit", refname);
 +
 +              /* Filter with with_commit if specified */
 +              if (!is_descendant_of(commit, ref_list->with_commit))
 +                      return 0;
 +
 +              if (merge_filter != NO_FILTER)
 +                      add_pending_object(&ref_list->revs,
 +                                         (struct object *)commit, refname);
 +      }
  
        /* Resize buffer */
        if (ref_list->index >= ref_list->alloc) {
        newitem->name = xstrdup(refname);
        newitem->kind = kind;
        newitem->commit = commit;
 -      len = strlen(newitem->name);
 -      if (len > ref_list->maxwidth)
 -              ref_list->maxwidth = len;
 +      newitem->len = strlen(refname);
 +      newitem->dest = resolve_symref(orig_refname, prefix);
 +      /* adjust for "remotes/" */
 +      if (newitem->kind == REF_REMOTE_BRANCH &&
 +          ref_list->kinds != REF_REMOTE_BRANCH)
 +              newitem->len += 8;
 +      if (newitem->len > ref_list->maxwidth)
 +              ref_list->maxwidth = newitem->len;
  
        return 0;
  }
@@@ -287,10 -257,8 +287,10 @@@ static void free_ref_list(struct ref_li
  {
        int i;
  
 -      for (i = 0; i < ref_list->index; i++)
 +      for (i = 0; i < ref_list->index; i++) {
                free(ref_list->list[i].name);
 +              free(ref_list->list[i].dest);
 +      }
        free(ref_list->list);
  }
  
@@@ -304,30 -272,19 +304,30 @@@ static int ref_cmp(const void *r1, cons
        return strcmp(c1->name, c2->name);
  }
  
 -static void fill_tracking_info(struct strbuf *stat, const char *branch_name)
 +static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
 +              int show_upstream_ref)
  {
        int ours, theirs;
        struct branch *branch = branch_get(branch_name);
  
 -      if (!stat_tracking_info(branch, &ours, &theirs) || (!ours && !theirs))
 +      if (!stat_tracking_info(branch, &ours, &theirs)) {
 +              if (branch && branch->merge && branch->merge[0]->dst &&
 +                  show_upstream_ref)
 +                      strbuf_addf(stat, "[%s] ",
 +                          shorten_unambiguous_ref(branch->merge[0]->dst, 0));
                return;
 +      }
 +
 +      strbuf_addch(stat, '[');
 +      if (show_upstream_ref)
 +              strbuf_addf(stat, "%s: ",
 +                      shorten_unambiguous_ref(branch->merge[0]->dst, 0));
        if (!ours)
 -              strbuf_addf(stat, "[behind %d] ", theirs);
 +              strbuf_addf(stat, "behind %d] ", theirs);
        else if (!theirs)
 -              strbuf_addf(stat, "[ahead %d] ", ours);
 +              strbuf_addf(stat, "ahead %d] ", ours);
        else
 -              strbuf_addf(stat, "[ahead %d, behind %d] ", ours, theirs);
 +              strbuf_addf(stat, "ahead %d, behind %d] ", ours, theirs);
  }
  
  static int matches_merge_filter(struct commit *commit)
  }
  
  static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
 -                         int abbrev, int current)
 +                         int abbrev, int current, char *prefix)
  {
        char c;
        int color;
        struct commit *commit = item->commit;
 +      struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
  
        if (!matches_merge_filter(commit))
                return;
  
        switch (item->kind) {
        case REF_LOCAL_BRANCH:
 -              color = COLOR_BRANCH_LOCAL;
 +              color = BRANCH_COLOR_LOCAL;
                break;
        case REF_REMOTE_BRANCH:
 -              color = COLOR_BRANCH_REMOTE;
 +              color = BRANCH_COLOR_REMOTE;
                break;
        default:
 -              color = COLOR_BRANCH_PLAIN;
 +              color = BRANCH_COLOR_PLAIN;
                break;
        }
  
        c = ' ';
        if (current) {
                c = '*';
 -              color = COLOR_BRANCH_CURRENT;
 +              color = BRANCH_COLOR_CURRENT;
        }
  
 -      if (verbose) {
 +      strbuf_addf(&name, "%s%s", prefix, item->name);
 +      if (verbose)
 +              strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
 +                          maxwidth, name.buf,
 +                          branch_get_color(BRANCH_COLOR_RESET));
 +      else
 +              strbuf_addf(&out, "%c %s%s%s", c, branch_get_color(color),
 +                          name.buf, branch_get_color(BRANCH_COLOR_RESET));
 +
 +      if (item->dest)
 +              strbuf_addf(&out, " -> %s", item->dest);
 +      else if (verbose) {
                struct strbuf subject = STRBUF_INIT, stat = STRBUF_INIT;
                const char *sub = " **** invalid ref ****";
  
                }
  
                if (item->kind == REF_LOCAL_BRANCH)
 -                      fill_tracking_info(&stat, item->name);
 +                      fill_tracking_info(&stat, item->name, verbose > 1);
  
 -              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.buf, sub);
 +              strbuf_addf(&out, " %s %s%s",
 +                      find_unique_abbrev(item->commit->object.sha1, abbrev),
 +                      stat.buf, sub);
                strbuf_release(&stat);
                strbuf_release(&subject);
 -      } else {
 -              printf("%c %s%s%s\n", c, branch_get_color(color), item->name,
 -                     branch_get_color(COLOR_BRANCH_RESET));
        }
 +      printf("%s\n", out.buf);
 +      strbuf_release(&name);
 +      strbuf_release(&out);
  }
  
  static int calc_maxwidth(struct ref_list *refs)
  {
 -      int i, l, w = 0;
 +      int i, w = 0;
        for (i = 0; i < refs->index; i++) {
                if (!matches_merge_filter(refs->list[i].commit))
                        continue;
 -              l = strlen(refs->list[i].name);
 -              if (l > w)
 -                      w = l;
 +              if (refs->list[i].len > w)
 +                      w = refs->list[i].len;
        }
        return w;
  }
  
 +
 +static void show_detached(struct ref_list *ref_list)
 +{
 +      struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1);
 +
 +      if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) {
 +              struct ref_item item;
 +              item.name = xstrdup("(no branch)");
 +              item.len = strlen(item.name);
 +              item.kind = REF_LOCAL_BRANCH;
 +              item.dest = NULL;
 +              item.commit = head_commit;
 +              if (item.len > ref_list->maxwidth)
 +                      ref_list->maxwidth = item.len;
 +              print_ref_item(&item, ref_list->maxwidth, ref_list->verbose, ref_list->abbrev, 1, "");
 +              free(item.name);
 +      }
 +}
 +
  static void print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit)
  {
        int i;
        struct ref_list ref_list;
 -      struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1);
  
        memset(&ref_list, 0, sizeof(ref_list));
        ref_list.kinds = kinds;
 +      ref_list.verbose = verbose;
 +      ref_list.abbrev = abbrev;
        ref_list.with_commit = with_commit;
        if (merge_filter != NO_FILTER)
                init_revisions(&ref_list.revs, NULL);
 -      for_each_ref(append_ref, &ref_list);
 +      for_each_rawref(append_ref, &ref_list);
        if (merge_filter != NO_FILTER) {
                struct commit *filter;
                filter = lookup_commit_reference_gently(merge_filter_ref, 0);
        qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
  
        detached = (detached && (kinds & REF_LOCAL_BRANCH));
 -      if (detached && head_commit &&
 -          is_descendant_of(head_commit, with_commit)) {
 -              struct ref_item item;
 -              item.name = xstrdup("(no branch)");
 -              item.kind = REF_LOCAL_BRANCH;
 -              item.commit = head_commit;
 -              if (strlen(item.name) > ref_list.maxwidth)
 -                      ref_list.maxwidth = strlen(item.name);
 -              print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1);
 -              free(item.name);
 -      }
 +      if (detached)
 +              show_detached(&ref_list);
  
        for (i = 0; i < ref_list.index; i++) {
                int current = !detached &&
                        (ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
                        !strcmp(ref_list.list[i].name, head);
 +              char *prefix = (kinds != REF_REMOTE_BRANCH &&
 +                              ref_list.list[i].kind == REF_REMOTE_BRANCH)
 +                              ? "remotes/" : "";
                print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
 -                             abbrev, current);
 +                             abbrev, current, prefix);
        }
  
        free_ref_list(&ref_list);
@@@ -487,27 -421,22 +487,27 @@@ static void rename_branch(const char *o
        struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
        unsigned char sha1[20];
        struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
 +      int recovery = 0;
  
        if (!oldname)
                die("cannot rename the current branch while not on any.");
  
 -      strbuf_addf(&oldref, "refs/heads/%s", oldname);
 -
 -      if (check_ref_format(oldref.buf))
 -              die("Invalid branch name: %s", oldref.buf);
 -
 -      strbuf_addf(&newref, "refs/heads/%s", newname);
 +      if (strbuf_check_branch_ref(&oldref, oldname)) {
 +              /*
 +               * Bad name --- this could be an attempt to rename a
 +               * ref that we used to allow to be created by accident.
 +               */
 +              if (resolve_ref(oldref.buf, sha1, 1, NULL))
 +                      recovery = 1;
 +              else
 +                      die("Invalid branch name: '%s'", oldname);
 +      }
  
 -      if (check_ref_format(newref.buf))
 -              die("Invalid branch name: %s", newref.buf);
 +      if (strbuf_check_branch_ref(&newref, newname))
 +              die("Invalid branch name: '%s'", newname);
  
        if (resolve_ref(newref.buf, sha1, 1, NULL) && !force)
 -              die("A branch named '%s' already exists.", newname);
 +              die("A branch named '%s' already exists.", newref.buf + 11);
  
        strbuf_addf(&logmsg, "Branch: renamed %s to %s",
                 oldref.buf, newref.buf);
                die("Branch rename failed");
        strbuf_release(&logmsg);
  
 +      if (recovery)
 +              warning("Renamed a misnamed branch '%s' away", oldref.buf + 11);
 +
        /* no need to pass logmsg here as HEAD didn't really move */
        if (!strcmp(oldname, head) && create_symref("HEAD", newref.buf, NULL))
                die("Branch renamed to %s, but HEAD is not updated!", newname);
@@@ -634,10 -560,12 +634,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 b8a4b0139b3b9c2f731315413a73db30edaf9231,e0bbea58ffe16a0eb0edab8e4c260e23e2ef1b80..edefb703fa8a62d96ba7c3d64966ee9ddafda16b
@@@ -179,7 -179,7 +179,7 @@@ static int checkout_merged(int pos, str
        /*
         * 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,7 -216,7 +216,7 @@@ static int checkout_paths(struct tree *
        struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
  
        newfd = hold_locked_index(lock_file, 1);
 -      if (read_cache() < 0)
 +      if (read_cache_preload(pathspec) < 0)
                return error("corrupt index file");
  
        if (source_tree)
@@@ -293,8 -293,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);
  }
@@@ -351,11 -349,16 +351,11 @@@ struct branch_info 
  static void setup_branch_path(struct branch_info *branch)
  {
        struct strbuf buf = STRBUF_INIT;
 -      int ret;
  
 -      if ((ret = interpret_nth_last_branch(branch->name, &buf))
 -          && ret == strlen(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);
 -      } else {
 -              strbuf_addstr(&buf, "refs/heads/");
 -              strbuf_addstr(&buf, branch->name);
 -      }
 +      strbuf_splice(&buf, 0, 0, "refs/heads/", 11);
        branch->path = strbuf_detach(&buf, NULL);
  }
  
@@@ -366,7 -369,7 +366,7 @@@ static int merge_working_tree(struct ch
        struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
        int newfd = hold_locked_index(lock_file, 1);
  
 -      if (read_cache() < 0)
 +      if (read_cache_preload(NULL) < 0)
                return error("corrupt index file");
  
        if (opts->force) {
                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);
                init_tree_desc(&trees[0], 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 */
  
@@@ -541,10 -550,18 +547,10 @@@ static int switch_branches(struct check
                parse_commit(new->commit);
        }
  
 -      /*
 -       * If we were on a detached HEAD, but we are now moving 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);
 +                      warning("You appear to be on a branch yet to be born.");
 +                      warning("Forcing checkout of %s.", new->name);
                }
                opts->force = 1;
        }
        if (ret)
                return ret;
  
 +      /*
 +       * 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);
 +
        update_refs_for_switch(opts, &old, new);
  
        ret = post_checkout_hook(old.commit, new->commit, 1);
@@@ -731,11 -740,12 +737,11 @@@ no_reference
  
        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 builtin-commit.c
index baaa75cf908d57e6d8540b6483684400c0b2494b,0871ad5d66ea07361e263a1c2c38554d72e5af1a..0e72a5657a52d3e3be2aae10993a30784607a896
@@@ -86,8 -86,8 +86,8 @@@ static int opt_parse_m(const struct opt
  static struct option builtin_commit_options[] = {
        OPT__QUIET(&quiet),
        OPT__VERBOSE(&verbose),
-       OPT_GROUP("Commit message options"),
  
+       OPT_GROUP("Commit message options"),
        OPT_STRING('F', "file", &logfile, "FILE", "read log from file"),
        OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
        OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m),
@@@ -96,6 -96,8 +96,8 @@@
        OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
        OPT_STRING('t', "template", &template_file, "FILE", "use specified template file"),
        OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"),
+       OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
+       /* end commit message options */
  
        OPT_GROUP("Commit contents options"),
        OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
        OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
        { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
        OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
-       OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
+       /* end commit contents options */
  
        OPT_END()
  };
@@@ -699,11 -701,7 +701,11 @@@ static int parse_and_validate_options(i
  
        argc = parse_options(argc, argv, builtin_commit_options, usage, 0);
        logfile = parse_options_fix_filename(prefix, logfile);
 +      if (logfile)
 +              logfile = xstrdup(logfile);
        template_file = parse_options_fix_filename(prefix, template_file);
 +      if (template_file)
 +              template_file = xstrdup(template_file);
  
        if (force_author && !strchr(force_author, '>'))
                force_author = find_author_by_nickname(force_author);
diff --combined diff.c
index f0b580c1503147d093d928915ead2c5f88a0042b,9ec5767709898af88d83256c6ea98d74863042d8..ad5c0a20ee013a7f13ec9b0da1525bd5fdeafa70
--- 1/diff.c
--- 2/diff.c
+++ b/diff.c
@@@ -30,14 -30,14 +30,14 @@@ int diff_auto_refresh_index = 1
  static int diff_mnemonic_prefix;
  
  static char diff_colors[][COLOR_MAXLEN] = {
 -      "\033[m",       /* reset */
 -      "",             /* PLAIN (normal) */
 -      "\033[1m",      /* METAINFO (bold) */
 -      "\033[36m",     /* FRAGINFO (cyan) */
 -      "\033[31m",     /* OLD (red) */
 -      "\033[32m",     /* NEW (green) */
 -      "\033[33m",     /* COMMIT (yellow) */
 -      "\033[41m",     /* WHITESPACE (red background) */
 +      GIT_COLOR_RESET,
 +      GIT_COLOR_NORMAL,       /* PLAIN */
 +      GIT_COLOR_BOLD,         /* METAINFO */
 +      GIT_COLOR_CYAN,         /* FRAGINFO */
 +      GIT_COLOR_RED,          /* OLD */
 +      GIT_COLOR_GREEN,        /* NEW */
 +      GIT_COLOR_YELLOW,       /* COMMIT */
 +      GIT_COLOR_BG_RED,       /* WHITESPACE */
  };
  
  static void diff_filespec_load_driver(struct diff_filespec *one);
@@@ -62,15 -62,6 +62,15 @@@ static int parse_diff_color_slot(const 
        die("bad config variable '%s'", var);
  }
  
 +static int git_config_rename(const char *var, const char *value)
 +{
 +      if (!value)
 +              return DIFF_DETECT_RENAME;
 +      if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy"))
 +              return  DIFF_DETECT_COPY;
 +      return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0;
 +}
 +
  /*
   * These are to give UI layer defaults.
   * The core-level commands such as git-diff-files should
@@@ -84,7 -75,13 +84,7 @@@ int git_diff_ui_config(const char *var
                return 0;
        }
        if (!strcmp(var, "diff.renames")) {
 -              if (!value)
 -                      diff_detect_rename_default = DIFF_DETECT_RENAME;
 -              else if (!strcasecmp(value, "copies") ||
 -                       !strcasecmp(value, "copy"))
 -                      diff_detect_rename_default = DIFF_DETECT_COPY;
 -              else if (git_config_bool(var,value))
 -                      diff_detect_rename_default = DIFF_DETECT_RENAME;
 +              diff_detect_rename_default = git_config_rename(var, value);
                return 0;
        }
        if (!strcmp(var, "diff.autorefreshindex")) {
@@@ -189,7 -186,7 +189,7 @@@ static void remove_tempfile(void
        int i;
        for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
                if (diff_temp[i].name == diff_temp[i].tmp_path)
 -                      unlink(diff_temp[i].name);
 +                      unlink_or_warn(diff_temp[i].name);
                diff_temp[i].name = NULL;
        }
  }
@@@ -876,9 -873,9 +876,9 @@@ static void fill_print_name(struct diff
        file->print_name = pname;
  }
  
 -static void show_stats(struct diffstat_tdata, struct diff_options *options)
 +static void show_stats(struct diffstat_t *data, struct diff_options *options)
  {
 -      int i, len, add, del, total, adds = 0, dels = 0;
 +      int i, len, add, del, adds = 0, dels = 0;
        int max_change = 0, max_len = 0;
        int total_files = data->nr;
        int width, name_width;
                 */
                add = added;
                del = deleted;
 -              total = add + del;
                adds += add;
                dels += del;
  
                if (width <= max_change) {
                        add = scale_linear(add, width, max_change);
                        del = scale_linear(del, width, max_change);
 -                      total = add + del;
                }
                show_name(options->file, prefix, name, len, reset, set);
                fprintf(options->file, "%5d%s", added + deleted,
@@@ -1025,7 -1024,7 +1025,7 @@@ static void show_shortstats(struct diff
               total_files, adds, dels);
  }
  
 -static void show_numstat(struct diffstat_tdata, struct diff_options *options)
 +static void show_numstat(struct diffstat_t *data, struct diff_options *options)
  {
        int i;
  
@@@ -2583,13 -2582,13 +2583,13 @@@ int diff_opt_parse(struct diff_options 
  
        /* xdiff options */
        else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
 -              options->xdl_opts |= XDF_IGNORE_WHITESPACE;
 +              DIFF_XDL_SET(options, IGNORE_WHITESPACE);
        else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
 -              options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
 +              DIFF_XDL_SET(options, IGNORE_WHITESPACE_CHANGE);
        else if (!strcmp(arg, "--ignore-space-at-eol"))
 -              options->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
 +              DIFF_XDL_SET(options, IGNORE_WHITESPACE_AT_EOL);
        else if (!strcmp(arg, "--patience"))
 -              options->xdl_opts |= XDF_PATIENCE_DIFF;
 +              DIFF_XDL_SET(options, PATIENCE_DIFF);
  
        /* flags options */
        else if (!strcmp(arg, "--binary")) {
                DIFF_OPT_SET(options, COLOR_DIFF);
        else if (!strcmp(arg, "--no-color"))
                DIFF_OPT_CLR(options, COLOR_DIFF);
 -      else if (!strcmp(arg, "--color-words"))
 -              options->flags |= DIFF_OPT_COLOR_DIFF | DIFF_OPT_COLOR_DIFF_WORDS;
 +      else if (!strcmp(arg, "--color-words")) {
 +              DIFF_OPT_SET(options, COLOR_DIFF);
 +              DIFF_OPT_SET(options, COLOR_DIFF_WORDS);
 +      }
        else if (!prefixcmp(arg, "--color-words=")) {
 -              options->flags |= DIFF_OPT_COLOR_DIFF | DIFF_OPT_COLOR_DIFF_WORDS;
 +              DIFF_OPT_SET(options, COLOR_DIFF);
 +              DIFF_OPT_SET(options, COLOR_DIFF_WORDS);
                options->word_regex = arg + 14;
        }
        else if (!strcmp(arg, "--exit-code"))
@@@ -3590,11 -3586,12 +3590,13 @@@ static char *run_textconv(const char *p
        if (start_command(&child) != 0 ||
            strbuf_read(&buf, child.out, 0) < 0 ||
            finish_command(&child) != 0) {
+               close(child.out);
 +              strbuf_release(&buf);
                remove_tempfile();
                error("error running textconv command '%s'", pgm);
                return NULL;
        }
+       close(child.out);
        remove_tempfile();
  
        return strbuf_detach(&buf, outsize);
index 5858b868ed6ff05d458996cf8c359a7148591582,a7bef9371fc190d799e1e3741605fe8b626029c3..d05a9138b46eec072750142f912a41caa3e23d83
@@@ -7,19 -7,19 +7,19 @@@ test_description='Test the post-checkou
  . ./test-lib.sh
  
  test_expect_success setup '
-        echo Data for commit0. >a &&
-        echo Data for commit0. >b &&
-        git update-index --add a &&
-        git update-index --add b &&
-        tree0=$(git write-tree) &&
-        commit0=$(echo setup | git commit-tree $tree0) &&
-         git update-ref refs/heads/master $commit0 &&
-        git clone ./. clone1 &&
-        git clone ./. clone2 &&
-         GIT_DIR=clone2/.git git branch -a new2 &&
-         echo Data for commit1. >clone2/b &&
-        GIT_DIR=clone2/.git git add clone2/b &&
-        GIT_DIR=clone2/.git git commit -m new2
+       echo Data for commit0. >a &&
+       echo Data for commit0. >b &&
+       git update-index --add a &&
+       git update-index --add b &&
+       tree0=$(git write-tree) &&
+       commit0=$(echo setup | git commit-tree $tree0) &&
+       git update-ref refs/heads/master $commit0 &&
+       git clone ./. clone1 &&
+       git clone ./. clone2 &&
+       GIT_DIR=clone2/.git git branch new2 &&
+       echo Data for commit1. >clone2/b &&
+       GIT_DIR=clone2/.git git add clone2/b &&
+       GIT_DIR=clone2/.git git commit -m new2
  '
  
  for clone in 1 2; do
@@@ -71,18 -71,4 +71,18 @@@ test_expect_success 'post-checkout rece
          test $old = $new -a $flag = 0
  '
  
 +if test "$(git config --bool core.filemode)" = true; then
 +mkdir -p templates/hooks
 +cat >templates/hooks/post-checkout <<'EOF'
 +#!/bin/sh
 +echo $@ > $GIT_DIR/post-checkout.args
 +EOF
 +chmod +x templates/hooks/post-checkout
 +
 +test_expect_success 'post-checkout hook is triggered by clone' '
 +      git clone --template=templates . clone3 &&
 +      test -f clone3/.git/post-checkout.args
 +'
 +fi
 +
  test_done