Fourth batch for 1.7.12
[gitweb.git] / reflog-walk.c
index 5623ea6b48a2f355e8866eabb2805b51b4127a4a..b2fbdb2392f80a531d5f9a21630a036d45c0a827 100644 (file)
@@ -8,6 +8,7 @@
 
 struct complete_reflogs {
        char *ref;
+       const char *short_ref;
        struct reflog_info {
                unsigned char osha1[20], nsha1[20];
                char *email;
@@ -49,9 +50,13 @@ static struct complete_reflogs *read_complete_reflog(const char *ref)
        for_each_reflog_ent(ref, read_one_reflog, reflogs);
        if (reflogs->nr == 0) {
                unsigned char sha1[20];
-               const char *name = resolve_ref(ref, sha1, 1, NULL);
-               if (name)
+               const char *name;
+               void *name_to_free;
+               name = name_to_free = resolve_refdup(ref, sha1, 1, NULL);
+               if (name) {
                        for_each_reflog_ent(name, read_one_reflog, reflogs);
+                       free(name_to_free);
+               }
        }
        if (reflogs->nr == 0) {
                int len = strlen(ref);
@@ -121,7 +126,12 @@ static void add_commit_info(struct commit *commit, void *util,
 }
 
 struct commit_reflog {
-       int flag, recno;
+       int recno;
+       enum selector_type {
+               SELECTOR_NONE,
+               SELECTOR_INDEX,
+               SELECTOR_DATE
+       } selector;
        struct complete_reflogs *reflogs;
 };
 
@@ -145,6 +155,7 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
        struct complete_reflogs *reflogs;
        char *branch, *at = strchr(name, '@');
        struct commit_reflog *commit_reflog;
+       enum selector_type selector = SELECTOR_NONE;
 
        if (commit->object.flags & UNINTERESTING)
                die ("Cannot walk reflogs for %s", name);
@@ -157,21 +168,24 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
                if (*ep != '}') {
                        recno = -1;
                        timestamp = approxidate(at + 2);
+                       selector = SELECTOR_DATE;
                }
+               else
+                       selector = SELECTOR_INDEX;
        } else
                recno = 0;
 
-       item = string_list_lookup(branch, &info->complete_reflogs);
+       item = string_list_lookup(&info->complete_reflogs, branch);
        if (item)
                reflogs = item->util;
        else {
                if (*branch == '\0') {
                        unsigned char sha1[20];
-                       const char *head = resolve_ref("HEAD", sha1, 0, NULL);
-                       if (!head)
-                               die ("No current branch");
                        free(branch);
-                       branch = xstrdup(head);
+                       branch = resolve_refdup("HEAD", sha1, 0, NULL);
+                       if (!branch)
+                               die ("No current branch");
+
                }
                reflogs = read_complete_reflog(branch);
                if (!reflogs || reflogs->nr == 0) {
@@ -189,13 +203,12 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
                }
                if (!reflogs || reflogs->nr == 0)
                        return -1;
-               string_list_insert(branch, &info->complete_reflogs)->util
+               string_list_insert(&info->complete_reflogs, branch)->util
                        = reflogs;
        }
 
        commit_reflog = xcalloc(sizeof(struct commit_reflog), 1);
        if (recno < 0) {
-               commit_reflog->flag = 1;
                commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
                if (commit_reflog->recno < 0) {
                        free(branch);
@@ -204,6 +217,7 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
                }
        } else
                commit_reflog->recno = reflogs->nr - recno - 1;
+       commit_reflog->selector = selector;
        commit_reflog->reflogs = reflogs;
 
        add_commit_info(commit, commit_reflog, &info->reflogs);
@@ -238,39 +252,89 @@ void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
 
        commit->parents = xcalloc(sizeof(struct commit_list), 1);
        commit->parents->item = commit_info->commit;
-       commit->object.flags &= ~(ADDED | SEEN | SHOWN);
 }
 
-void show_reflog_message(struct reflog_walk_info *info, int oneline,
-       enum date_mode dmode)
+void get_reflog_selector(struct strbuf *sb,
+                        struct reflog_walk_info *reflog_info,
+                        enum date_mode dmode, int force_date,
+                        int shorten)
 {
-       if (info && info->last_commit_reflog) {
-               struct commit_reflog *commit_reflog = info->last_commit_reflog;
+       struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
+       struct reflog_info *info;
+       const char *printed_ref;
+
+       if (!commit_reflog)
+               return;
+
+       if (shorten) {
+               if (!commit_reflog->reflogs->short_ref)
+                       commit_reflog->reflogs->short_ref
+                               = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
+               printed_ref = commit_reflog->reflogs->short_ref;
+       } else {
+               printed_ref = commit_reflog->reflogs->ref;
+       }
+
+       strbuf_addf(sb, "%s@{", printed_ref);
+       if (commit_reflog->selector == SELECTOR_DATE ||
+           (commit_reflog->selector == SELECTOR_NONE && force_date)) {
+               info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
+               strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
+       } else {
+               strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
+                           - 2 - commit_reflog->recno);
+       }
+
+       strbuf_addch(sb, '}');
+}
+
+void get_reflog_message(struct strbuf *sb,
+                       struct reflog_walk_info *reflog_info)
+{
+       struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
+       struct reflog_info *info;
+       size_t len;
+
+       if (!commit_reflog)
+               return;
+
+       info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
+       len = strlen(info->message);
+       if (len > 0)
+               len--; /* strip away trailing newline */
+       strbuf_add(sb, info->message, len);
+}
+
+const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
+{
+       struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
+       struct reflog_info *info;
+
+       if (!commit_reflog)
+               return NULL;
+
+       info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
+       return info->email;
+}
+
+void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
+                        enum date_mode dmode, int force_date)
+{
+       if (reflog_info && reflog_info->last_commit_reflog) {
+               struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
                struct reflog_info *info;
+               struct strbuf selector = STRBUF_INIT;
 
                info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
+               get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
                if (oneline) {
-                       printf("%s@{", commit_reflog->reflogs->ref);
-                       if (commit_reflog->flag || dmode)
-                               printf("%s", show_date(info->timestamp,
-                                                      info->tz,
-                                                      dmode));
-                       else
-                               printf("%d", commit_reflog->reflogs->nr
-                                      - 2 - commit_reflog->recno);
-                       printf("}: %s", info->message);
+                       printf("%s: %s", selector.buf, info->message);
                }
                else {
-                       printf("Reflog: %s@{", commit_reflog->reflogs->ref);
-                       if (commit_reflog->flag || dmode)
-                               printf("%s", show_date(info->timestamp,
-                                                       info->tz,
-                                                       dmode));
-                       else
-                               printf("%d", commit_reflog->reflogs->nr
-                                      - 2 - commit_reflog->recno);
-                       printf("} (%s)\nReflog message: %s",
-                              info->email, info->message);
+                       printf("Reflog: %s (%s)\nReflog message: %s",
+                              selector.buf, info->email, info->message);
                }
+
+               strbuf_release(&selector);
        }
 }