git
/
reflog-walk.c
383 строки · 9.0 Кб
1#define USE_THE_REPOSITORY_VARIABLE2
3#include "git-compat-util.h"4#include "commit.h"5#include "refs.h"6#include "diff.h"7#include "repository.h"8#include "revision.h"9#include "string-list.h"10#include "reflog-walk.h"11
12struct complete_reflogs {13char *ref;14char *short_ref;15struct reflog_info {16struct object_id ooid, noid;17char *email;18timestamp_t timestamp;19int tz;20char *message;21} *items;22int nr, alloc;23};24
25static int read_one_reflog(struct object_id *ooid, struct object_id *noid,26const char *email, timestamp_t timestamp, int tz,27const char *message, void *cb_data)28{
29struct complete_reflogs *array = cb_data;30struct reflog_info *item;31
32ALLOC_GROW(array->items, array->nr + 1, array->alloc);33item = array->items + array->nr;34oidcpy(&item->ooid, ooid);35oidcpy(&item->noid, noid);36item->email = xstrdup(email);37item->timestamp = timestamp;38item->tz = tz;39item->message = xstrdup(message);40array->nr++;41return 0;42}
43
44static void free_complete_reflog(struct complete_reflogs *array)45{
46int i;47
48if (!array)49return;50
51for (i = 0; i < array->nr; i++) {52free(array->items[i].email);53free(array->items[i].message);54}55free(array->items);56free(array->ref);57free(array->short_ref);58free(array);59}
60
61static void complete_reflogs_clear(void *util, const char *str UNUSED)62{
63struct complete_reflogs *array = util;64free_complete_reflog(array);65}
66
67static struct complete_reflogs *read_complete_reflog(const char *ref)68{
69struct complete_reflogs *reflogs =70xcalloc(1, sizeof(struct complete_reflogs));71reflogs->ref = xstrdup(ref);72refs_for_each_reflog_ent(get_main_ref_store(the_repository), ref,73read_one_reflog, reflogs);74if (reflogs->nr == 0) {75const char *name;76void *name_to_free;77name = name_to_free = refs_resolve_refdup(get_main_ref_store(the_repository),78ref,79RESOLVE_REF_READING,80NULL, NULL);81if (name) {82refs_for_each_reflog_ent(get_main_ref_store(the_repository),83name, read_one_reflog,84reflogs);85free(name_to_free);86}87}88if (reflogs->nr == 0) {89char *refname = xstrfmt("refs/%s", ref);90refs_for_each_reflog_ent(get_main_ref_store(the_repository),91refname, read_one_reflog, reflogs);92if (reflogs->nr == 0) {93free(refname);94refname = xstrfmt("refs/heads/%s", ref);95refs_for_each_reflog_ent(get_main_ref_store(the_repository),96refname, read_one_reflog,97reflogs);98}99free(refname);100}101return reflogs;102}
103
104static int get_reflog_recno_by_time(struct complete_reflogs *array,105timestamp_t timestamp)106{
107int i;108for (i = array->nr - 1; i >= 0; i--)109if (timestamp >= array->items[i].timestamp)110return i;111return -1;112}
113
114struct commit_reflog {115int recno;116enum selector_type {117SELECTOR_NONE,118SELECTOR_INDEX,119SELECTOR_DATE
120} selector;121struct complete_reflogs *reflogs;122};123
124struct reflog_walk_info {125struct commit_reflog **logs;126size_t nr, alloc;127struct string_list complete_reflogs;128struct commit_reflog *last_commit_reflog;129};130
131void init_reflog_walk(struct reflog_walk_info **info)132{
133CALLOC_ARRAY(*info, 1);134(*info)->complete_reflogs.strdup_strings = 1;135}
136
137void reflog_walk_info_release(struct reflog_walk_info *info)138{
139size_t i;140
141if (!info)142return;143
144for (i = 0; i < info->nr; i++)145free(info->logs[i]);146string_list_clear_func(&info->complete_reflogs,147complete_reflogs_clear);148free(info->logs);149free(info);150}
151
152int add_reflog_for_walk(struct reflog_walk_info *info,153struct commit *commit, const char *name)154{
155timestamp_t timestamp = 0;156int recno = -1;157struct string_list_item *item;158struct complete_reflogs *reflogs;159char *branch, *at = strchr(name, '@');160struct commit_reflog *commit_reflog;161enum selector_type selector = SELECTOR_NONE;162
163if (commit->object.flags & UNINTERESTING)164die("cannot walk reflogs for %s", name);165
166branch = xstrdup(name);167if (at && at[1] == '{') {168char *ep;169branch[at - name] = '\0';170recno = strtoul(at + 2, &ep, 10);171if (*ep != '}') {172recno = -1;173timestamp = approxidate(at + 2);174selector = SELECTOR_DATE;175}176else177selector = SELECTOR_INDEX;178} else179recno = 0;180
181item = string_list_lookup(&info->complete_reflogs, branch);182if (item)183reflogs = item->util;184else {185if (*branch == '\0') {186free(branch);187branch = refs_resolve_refdup(get_main_ref_store(the_repository),188"HEAD", 0, NULL, NULL);189if (!branch)190die("no current branch");191
192}193reflogs = read_complete_reflog(branch);194if (!reflogs || reflogs->nr == 0) {195char *b;196int ret = repo_dwim_log(the_repository, branch, strlen(branch),197NULL, &b);198if (ret > 1)199free(b);200else if (ret == 1) {201free_complete_reflog(reflogs);202free(branch);203branch = b;204reflogs = read_complete_reflog(branch);205}206}207if (!reflogs || reflogs->nr == 0) {208free_complete_reflog(reflogs);209free(branch);210return -1;211}212string_list_insert(&info->complete_reflogs, branch)->util213= reflogs;214}215free(branch);216
217CALLOC_ARRAY(commit_reflog, 1);218if (recno < 0) {219commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);220if (commit_reflog->recno < 0) {221free(commit_reflog);222return -1;223}224} else225commit_reflog->recno = reflogs->nr - recno - 1;226commit_reflog->selector = selector;227commit_reflog->reflogs = reflogs;228
229ALLOC_GROW(info->logs, info->nr + 1, info->alloc);230info->logs[info->nr++] = commit_reflog;231
232return 0;233}
234
235void get_reflog_selector(struct strbuf *sb,236struct reflog_walk_info *reflog_info,237struct date_mode dmode, int force_date,238int shorten)239{
240struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;241struct reflog_info *info;242const char *printed_ref;243
244if (!commit_reflog)245return;246
247if (shorten) {248if (!commit_reflog->reflogs->short_ref)249commit_reflog->reflogs->short_ref250= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),251commit_reflog->reflogs->ref,2520);253printed_ref = commit_reflog->reflogs->short_ref;254} else {255printed_ref = commit_reflog->reflogs->ref;256}257
258strbuf_addf(sb, "%s@{", printed_ref);259if (commit_reflog->selector == SELECTOR_DATE ||260(commit_reflog->selector == SELECTOR_NONE && force_date)) {261info = &commit_reflog->reflogs->items[commit_reflog->recno+1];262strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));263} else {264strbuf_addf(sb, "%d", commit_reflog->reflogs->nr265- 2 - commit_reflog->recno);266}267
268strbuf_addch(sb, '}');269}
270
271void get_reflog_message(struct strbuf *sb,272struct reflog_walk_info *reflog_info)273{
274struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;275struct reflog_info *info;276size_t len;277
278if (!commit_reflog)279return;280
281info = &commit_reflog->reflogs->items[commit_reflog->recno+1];282len = strlen(info->message);283if (len > 0)284len--; /* strip away trailing newline */285strbuf_add(sb, info->message, len);286}
287
288const char *get_reflog_ident(struct reflog_walk_info *reflog_info)289{
290struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;291struct reflog_info *info;292
293if (!commit_reflog)294return NULL;295
296info = &commit_reflog->reflogs->items[commit_reflog->recno+1];297return info->email;298}
299
300timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)301{
302struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;303struct reflog_info *info;304
305if (!commit_reflog)306return 0;307
308info = &commit_reflog->reflogs->items[commit_reflog->recno+1];309return info->timestamp;310}
311
312void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,313struct date_mode dmode, int force_date)314{
315if (reflog_info && reflog_info->last_commit_reflog) {316struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;317struct reflog_info *info;318struct strbuf selector = STRBUF_INIT;319
320info = &commit_reflog->reflogs->items[commit_reflog->recno+1];321get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);322if (oneline) {323printf("%s: %s", selector.buf, info->message);324}325else {326printf("Reflog: %s (%s)\nReflog message: %s",327selector.buf, info->email, info->message);328}329
330strbuf_release(&selector);331}332}
333
334int reflog_walk_empty(struct reflog_walk_info *info)335{
336return !info || !info->nr;337}
338
339static struct commit *next_reflog_commit(struct commit_reflog *log)340{
341for (; log->recno >= 0; log->recno--) {342struct reflog_info *entry = &log->reflogs->items[log->recno];343struct object *obj = parse_object(the_repository,344&entry->noid);345
346if (obj && obj->type == OBJ_COMMIT)347return (struct commit *)obj;348}349return NULL;350}
351
352static timestamp_t log_timestamp(struct commit_reflog *log)353{
354return log->reflogs->items[log->recno].timestamp;355}
356
357struct commit *next_reflog_entry(struct reflog_walk_info *walk)358{
359struct commit_reflog *best = NULL;360struct commit *best_commit = NULL;361size_t i;362
363for (i = 0; i < walk->nr; i++) {364struct commit_reflog *log = walk->logs[i];365struct commit *commit = next_reflog_commit(log);366
367if (!commit)368continue;369
370if (!best || log_timestamp(log) > log_timestamp(best)) {371best = log;372best_commit = commit;373}374}375
376if (best) {377best->recno--;378walk->last_commit_reflog = best;379return best_commit;380}381
382return NULL;383}
384