diff options
Diffstat (limited to 'reflog-walk.c')
-rw-r--r-- | reflog-walk.c | 82 |
1 files changed, 50 insertions, 32 deletions
diff --git a/reflog-walk.c b/reflog-walk.c index caba4f743f..222de762eb 100644 --- a/reflog-walk.c +++ b/reflog-walk.c @@ -26,14 +26,10 @@ static int read_one_reflog(unsigned char *osha1, unsigned char *nsha1, struct complete_reflogs *array = cb_data; struct reflog_info *item; - if (array->nr >= array->alloc) { - array->alloc = alloc_nr(array->nr + 1); - array->items = xrealloc(array->items, array->alloc * - sizeof(struct reflog_info)); - } + ALLOC_GROW(array->items, array->nr + 1, array->alloc); item = array->items + array->nr; - memcpy(item->osha1, osha1, 20); - memcpy(item->nsha1, nsha1, 20); + hashcpy(item->osha1, osha1); + hashcpy(item->nsha1, nsha1); item->email = xstrdup(email); item->timestamp = timestamp; item->tz = tz; @@ -45,14 +41,19 @@ static int read_one_reflog(unsigned char *osha1, unsigned char *nsha1, static struct complete_reflogs *read_complete_reflog(const char *ref) { struct complete_reflogs *reflogs = - xcalloc(sizeof(struct complete_reflogs), 1); + xcalloc(1, sizeof(struct complete_reflogs)); reflogs->ref = xstrdup(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, RESOLVE_REF_READING, + sha1, NULL); + if (name) { for_each_reflog_ent(name, read_one_reflog, reflogs); + free(name_to_free); + } } if (reflogs->nr == 0) { int len = strlen(ref); @@ -110,11 +111,7 @@ static void add_commit_info(struct commit *commit, void *util, struct commit_info_lifo *lifo) { struct commit_info *info; - if (lifo->nr >= lifo->alloc) { - lifo->alloc = alloc_nr(lifo->nr + 1); - lifo->items = xrealloc(lifo->items, - lifo->alloc * sizeof(struct commit_info)); - } + ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc); info = lifo->items + lifo->nr; info->commit = commit; info->util = util; @@ -122,7 +119,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; }; @@ -132,9 +134,9 @@ struct reflog_walk_info { struct commit_reflog *last_commit_reflog; }; -void init_reflog_walk(struct reflog_walk_info** info) +void init_reflog_walk(struct reflog_walk_info **info) { - *info = xcalloc(sizeof(struct reflog_walk_info), 1); + *info = xcalloc(1, sizeof(struct reflog_walk_info)); } int add_reflog_for_walk(struct reflog_walk_info *info, @@ -146,6 +148,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); @@ -158,21 +161,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", 0, sha1, NULL); + if (!branch) + die ("No current branch"); + } reflogs = read_complete_reflog(branch); if (!reflogs || reflogs->nr == 0) { @@ -190,13 +196,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); + commit_reflog = xcalloc(1, sizeof(struct commit_reflog)); if (recno < 0) { - commit_reflog->flag = 1; commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp); if (commit_reflog->recno < 0) { free(branch); @@ -205,6 +210,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); @@ -237,14 +243,13 @@ void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit) return; } - commit->parents = xcalloc(sizeof(struct commit_list), 1); + commit->parents = xcalloc(1, sizeof(struct commit_list)); commit->parents->item = commit_info->commit; - commit->object.flags &= ~(ADDED | SEEN | SHOWN); } void get_reflog_selector(struct strbuf *sb, struct reflog_walk_info *reflog_info, - enum date_mode dmode, + enum date_mode dmode, int force_date, int shorten) { struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; @@ -264,7 +269,8 @@ void get_reflog_selector(struct strbuf *sb, } strbuf_addf(sb, "%s@{", printed_ref); - if (commit_reflog->flag || dmode) { + 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 { @@ -292,8 +298,20 @@ void get_reflog_message(struct strbuf *sb, 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) + 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; @@ -301,7 +319,7 @@ void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline, struct strbuf selector = STRBUF_INIT; info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; - get_reflog_selector(&selector, reflog_info, dmode, 0); + get_reflog_selector(&selector, reflog_info, dmode, force_date, 0); if (oneline) { printf("%s: %s", selector.buf, info->message); } |