6 #include "string-list.h"
7 #include "reflog-walk.h"
9 struct complete_reflogs {
11 const char *short_ref;
13 unsigned char osha1[20], nsha1[20];
15 unsigned long timestamp;
22 static int read_one_reflog(unsigned char *osha1, unsigned char *nsha1,
23 const char *email, unsigned long timestamp, int tz,
24 const char *message, void *cb_data)
26 struct complete_reflogs *array = cb_data;
27 struct reflog_info *item;
29 ALLOC_GROW(array->items, array->nr + 1, array->alloc);
30 item = array->items + array->nr;
31 hashcpy(item->osha1, osha1);
32 hashcpy(item->nsha1, nsha1);
33 item->email = xstrdup(email);
34 item->timestamp = timestamp;
36 item->message = xstrdup(message);
41 static struct complete_reflogs *read_complete_reflog(const char *ref)
43 struct complete_reflogs *reflogs =
44 xcalloc(1, sizeof(struct complete_reflogs));
45 reflogs->ref = xstrdup(ref);
46 for_each_reflog_ent(ref, read_one_reflog, reflogs);
47 if (reflogs->nr == 0) {
48 unsigned char sha1[20];
51 name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
54 for_each_reflog_ent(name, read_one_reflog, reflogs);
58 if (reflogs->nr == 0) {
59 int len = strlen(ref);
60 char *refname = xmalloc(len + 12);
61 sprintf(refname, "refs/%s", ref);
62 for_each_reflog_ent(refname, read_one_reflog, reflogs);
63 if (reflogs->nr == 0) {
64 sprintf(refname, "refs/heads/%s", ref);
65 for_each_reflog_ent(refname, read_one_reflog, reflogs);
72 static int get_reflog_recno_by_time(struct complete_reflogs *array,
73 unsigned long timestamp)
76 for (i = array->nr - 1; i >= 0; i--)
77 if (timestamp >= array->items[i].timestamp)
82 struct commit_info_lifo {
84 struct commit *commit;
90 static struct commit_info *get_commit_info(struct commit *commit,
91 struct commit_info_lifo *lifo, int pop)
94 for (i = 0; i < lifo->nr; i++)
95 if (lifo->items[i].commit == commit) {
96 struct commit_info *result = &lifo->items[i];
99 memmove(lifo->items + i,
102 sizeof(struct commit_info));
110 static void add_commit_info(struct commit *commit, void *util,
111 struct commit_info_lifo *lifo)
113 struct commit_info *info;
114 ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
115 info = lifo->items + lifo->nr;
116 info->commit = commit;
121 struct commit_reflog {
128 struct complete_reflogs *reflogs;
131 struct reflog_walk_info {
132 struct commit_info_lifo reflogs;
133 struct string_list complete_reflogs;
134 struct commit_reflog *last_commit_reflog;
137 void init_reflog_walk(struct reflog_walk_info **info)
139 *info = xcalloc(1, sizeof(struct reflog_walk_info));
142 int add_reflog_for_walk(struct reflog_walk_info *info,
143 struct commit *commit, const char *name)
145 unsigned long timestamp = 0;
147 struct string_list_item *item;
148 struct complete_reflogs *reflogs;
149 char *branch, *at = strchr(name, '@');
150 struct commit_reflog *commit_reflog;
151 enum selector_type selector = SELECTOR_NONE;
153 if (commit->object.flags & UNINTERESTING)
154 die ("Cannot walk reflogs for %s", name);
156 branch = xstrdup(name);
157 if (at && at[1] == '{') {
159 branch[at - name] = '\0';
160 recno = strtoul(at + 2, &ep, 10);
163 timestamp = approxidate(at + 2);
164 selector = SELECTOR_DATE;
167 selector = SELECTOR_INDEX;
171 item = string_list_lookup(&info->complete_reflogs, branch);
173 reflogs = item->util;
175 if (*branch == '\0') {
176 unsigned char sha1[20];
178 branch = resolve_refdup("HEAD", 0, sha1, NULL);
180 die ("No current branch");
183 reflogs = read_complete_reflog(branch);
184 if (!reflogs || reflogs->nr == 0) {
185 unsigned char sha1[20];
187 if (dwim_log(branch, strlen(branch), sha1, &b) == 1) {
194 reflogs = read_complete_reflog(branch);
197 if (!reflogs || reflogs->nr == 0)
199 string_list_insert(&info->complete_reflogs, branch)->util
203 commit_reflog = xcalloc(1, sizeof(struct commit_reflog));
205 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
206 if (commit_reflog->recno < 0) {
212 commit_reflog->recno = reflogs->nr - recno - 1;
213 commit_reflog->selector = selector;
214 commit_reflog->reflogs = reflogs;
216 add_commit_info(commit, commit_reflog, &info->reflogs);
220 void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
222 struct commit_info *commit_info =
223 get_commit_info(commit, &info->reflogs, 0);
224 struct commit_reflog *commit_reflog;
225 struct reflog_info *reflog;
227 info->last_commit_reflog = NULL;
231 commit_reflog = commit_info->util;
232 if (commit_reflog->recno < 0) {
233 commit->parents = NULL;
237 reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
238 info->last_commit_reflog = commit_reflog;
239 commit_reflog->recno--;
240 commit_info->commit = (struct commit *)parse_object(reflog->osha1);
241 if (!commit_info->commit) {
242 commit->parents = NULL;
246 commit->parents = xcalloc(1, sizeof(struct commit_list));
247 commit->parents->item = commit_info->commit;
250 void get_reflog_selector(struct strbuf *sb,
251 struct reflog_walk_info *reflog_info,
252 const struct date_mode *dmode, int force_date,
255 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
256 struct reflog_info *info;
257 const char *printed_ref;
263 if (!commit_reflog->reflogs->short_ref)
264 commit_reflog->reflogs->short_ref
265 = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
266 printed_ref = commit_reflog->reflogs->short_ref;
268 printed_ref = commit_reflog->reflogs->ref;
271 strbuf_addf(sb, "%s@{", printed_ref);
272 if (commit_reflog->selector == SELECTOR_DATE ||
273 (commit_reflog->selector == SELECTOR_NONE && force_date)) {
274 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
275 strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
277 strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
278 - 2 - commit_reflog->recno);
281 strbuf_addch(sb, '}');
284 void get_reflog_message(struct strbuf *sb,
285 struct reflog_walk_info *reflog_info)
287 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
288 struct reflog_info *info;
294 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
295 len = strlen(info->message);
297 len--; /* strip away trailing newline */
298 strbuf_add(sb, info->message, len);
301 const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
303 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
304 struct reflog_info *info;
309 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
313 void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
314 const struct date_mode *dmode, int force_date)
316 if (reflog_info && reflog_info->last_commit_reflog) {
317 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
318 struct reflog_info *info;
319 struct strbuf selector = STRBUF_INIT;
321 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
322 get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
324 printf("%s: %s", selector.buf, info->message);
327 printf("Reflog: %s (%s)\nReflog message: %s",
328 selector.buf, info->email, info->message);
331 strbuf_release(&selector);