Merge branch 'jk/reset-reflog-message-fix' into maint
authorJunio C Hamano <gitster@pobox.com>
Mon, 12 Sep 2011 05:33:20 +0000 (22:33 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 12 Sep 2011 05:33:20 +0000 (22:33 -0700)
* jk/reset-reflog-message-fix:
reset: give better reflog messages

1  2 
builtin/reset.c
diff --combined builtin/reset.c
index 777e7c612900f867c5a52723ebdd56c9db793489,27b342661dc62dda32fca57fb73114ea03195610..811e8e252c1c6a54e65179557203daf2bc42bdb9
@@@ -33,25 -33,6 +33,6 @@@ static const char *reset_type_names[] 
        N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
  };
  
- static char *args_to_str(const char **argv)
- {
-       char *buf = NULL;
-       unsigned long len, space = 0, nr = 0;
-       for (; *argv; argv++) {
-               len = strlen(*argv);
-               ALLOC_GROW(buf, nr + 1 + len, space);
-               if (nr)
-                       buf[nr++] = ' ';
-               memcpy(buf + nr, *argv, len);
-               nr += len;
-       }
-       ALLOC_GROW(buf, nr + 1, space);
-       buf[nr] = '\0';
-       return buf;
- }
  static inline int is_merge(void)
  {
        return !access(git_path("MERGE_HEAD"), F_OK);
@@@ -162,7 -143,7 +143,7 @@@ static void update_index_from_diff(stru
  
        for (i = 0; i < q->nr; i++) {
                struct diff_filespec *one = q->queue[i]->one;
 -              if (one->mode) {
 +              if (one->mode && !is_null_sha1(one->sha1)) {
                        struct cache_entry *ce;
                        ce = make_cache_entry(one->mode, one->sha1, one->path,
                                0, 0);
@@@ -215,14 -196,18 +196,18 @@@ static int read_from_tree(const char *p
        return update_index_refresh(index_fd, lock, refresh_flags);
  }
  
- static void prepend_reflog_action(const char *action, char *buf, size_t size)
+ static void set_reflog_message(struct strbuf *sb, const char *action,
+                              const char *rev)
  {
-       const char *sep = ": ";
        const char *rla = getenv("GIT_REFLOG_ACTION");
-       if (!rla)
-               rla = sep = "";
-       if (snprintf(buf, size, "%s%s%s", rla, sep, action) >= size)
-               warning(_("Reflog action message too long: %.*s..."), 50, buf);
+       strbuf_reset(sb);
+       if (rla)
+               strbuf_addf(sb, "%s: %s", rla, action);
+       else if (rev)
+               strbuf_addf(sb, "reset: moving to %s", rev);
+       else
+               strbuf_addf(sb, "reset: %s", action);
  }
  
  static void die_if_unmerged_cache(int reset_type)
@@@ -241,7 -226,7 +226,7 @@@ int cmd_reset(int argc, const char **ar
        unsigned char sha1[20], *orig = NULL, sha1_orig[20],
                                *old_orig = NULL, sha1_old_orig[20];
        struct commit *commit;
-       char *reflog_action, msg[1024];
+       struct strbuf msg = STRBUF_INIT;
        const struct option options[] = {
                OPT__QUIET(&quiet, "be quiet, only report errors"),
                OPT_SET_INT(0, "mixed", &reset_type,
  
        argc = parse_options(argc, argv, prefix, options, git_reset_usage,
                                                PARSE_OPT_KEEP_DASHDASH);
-       reflog_action = args_to_str(argv);
-       setenv("GIT_REFLOG_ACTION", reflog_action, 0);
  
        /*
         * Possible arguments are:
                old_orig = sha1_old_orig;
        if (!get_sha1("HEAD", sha1_orig)) {
                orig = sha1_orig;
-               prepend_reflog_action("updating ORIG_HEAD", msg, sizeof(msg));
-               update_ref(msg, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
+               set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
+               update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
        }
        else if (old_orig)
                delete_ref("ORIG_HEAD", old_orig, 0);
-       prepend_reflog_action("updating HEAD", msg, sizeof(msg));
-       update_ref_status = update_ref(msg, "HEAD", sha1, orig, 0, MSG_ON_ERR);
+       set_reflog_message(&msg, "updating HEAD", rev);
+       update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0, MSG_ON_ERR);
  
        switch (reset_type) {
        case HARD:
  
        remove_branch_state();
  
-       free(reflog_action);
+       strbuf_release(&msg);
  
        return update_ref_status;
  }