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 if (array->nr >= array->alloc) {
30 array->alloc = alloc_nr(array->nr + 1);
31 array->items = xrealloc(array->items, array->alloc *
32 sizeof(struct reflog_info));
34 item = array->items + array->nr;
35 memcpy(item->osha1, osha1, 20);
36 memcpy(item->nsha1, nsha1, 20);
37 item->email = xstrdup(email);
38 item->timestamp = timestamp;
40 item->message = xstrdup(message);
45 static struct complete_reflogs *read_complete_reflog(const char *ref)
47 struct complete_reflogs *reflogs =
48 xcalloc(sizeof(struct complete_reflogs), 1);
49 reflogs->ref = xstrdup(ref);
50 for_each_reflog_ent(ref, read_one_reflog, reflogs);
51 if (reflogs->nr == 0) {
52 unsigned char sha1[20];
53 const char *name = resolve_ref(ref, sha1, 1, NULL);
56 for_each_reflog_ent(name, read_one_reflog, reflogs);
60 if (reflogs->nr == 0) {
61 int len = strlen(ref);
62 char *refname = xmalloc(len + 12);
63 sprintf(refname, "refs/%s", ref);
64 for_each_reflog_ent(refname, read_one_reflog, reflogs);
65 if (reflogs->nr == 0) {
66 sprintf(refname, "refs/heads/%s", ref);
67 for_each_reflog_ent(refname, read_one_reflog, reflogs);
74 static int get_reflog_recno_by_time(struct complete_reflogs *array,
75 unsigned long timestamp)
78 for (i = array->nr - 1; i >= 0; i--)
79 if (timestamp >= array->items[i].timestamp)
84 struct commit_info_lifo {
86 struct commit *commit;
92 static struct commit_info *get_commit_info(struct commit *commit,
93 struct commit_info_lifo *lifo, int pop)
96 for (i = 0; i < lifo->nr; i++)
97 if (lifo->items[i].commit == commit) {
98 struct commit_info *result = &lifo->items[i];
100 if (i + 1 < lifo->nr)
101 memmove(lifo->items + i,
104 sizeof(struct commit_info));
112 static void add_commit_info(struct commit *commit, void *util,
113 struct commit_info_lifo *lifo)
115 struct commit_info *info;
116 if (lifo->nr >= lifo->alloc) {
117 lifo->alloc = alloc_nr(lifo->nr + 1);
118 lifo->items = xrealloc(lifo->items,
119 lifo->alloc * sizeof(struct commit_info));
121 info = lifo->items + lifo->nr;
122 info->commit = commit;
127 struct commit_reflog {
129 struct complete_reflogs *reflogs;
132 struct reflog_walk_info {
133 struct commit_info_lifo reflogs;
134 struct string_list complete_reflogs;
135 struct commit_reflog *last_commit_reflog;
138 void init_reflog_walk(struct reflog_walk_info** info)
140 *info = xcalloc(sizeof(struct reflog_walk_info), 1);
143 int add_reflog_for_walk(struct reflog_walk_info *info,
144 struct commit *commit, const char *name)
146 unsigned long timestamp = 0;
148 struct string_list_item *item;
149 struct complete_reflogs *reflogs;
150 char *branch, *at = strchr(name, '@');
151 struct commit_reflog *commit_reflog;
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);
168 item = string_list_lookup(&info->complete_reflogs, branch);
170 reflogs = item->util;
172 if (*branch == '\0') {
173 unsigned char sha1[20];
174 const char *head = resolve_ref("HEAD", sha1, 0, NULL);
176 die ("No current branch");
178 branch = xstrdup(head);
180 reflogs = read_complete_reflog(branch);
181 if (!reflogs || reflogs->nr == 0) {
182 unsigned char sha1[20];
184 if (dwim_log(branch, strlen(branch), sha1, &b) == 1) {
191 reflogs = read_complete_reflog(branch);
194 if (!reflogs || reflogs->nr == 0)
196 string_list_insert(&info->complete_reflogs, branch)->util
200 commit_reflog = xcalloc(sizeof(struct commit_reflog), 1);
202 commit_reflog->flag = 1;
203 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
204 if (commit_reflog->recno < 0) {
210 commit_reflog->recno = reflogs->nr - recno - 1;
211 commit_reflog->reflogs = reflogs;
213 add_commit_info(commit, commit_reflog, &info->reflogs);
217 void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
219 struct commit_info *commit_info =
220 get_commit_info(commit, &info->reflogs, 0);
221 struct commit_reflog *commit_reflog;
222 struct reflog_info *reflog;
224 info->last_commit_reflog = NULL;
228 commit_reflog = commit_info->util;
229 if (commit_reflog->recno < 0) {
230 commit->parents = NULL;
234 reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
235 info->last_commit_reflog = commit_reflog;
236 commit_reflog->recno--;
237 commit_info->commit = (struct commit *)parse_object(reflog->osha1);
238 if (!commit_info->commit) {
239 commit->parents = NULL;
243 commit->parents = xcalloc(sizeof(struct commit_list), 1);
244 commit->parents->item = commit_info->commit;
247 void get_reflog_selector(struct strbuf *sb,
248 struct reflog_walk_info *reflog_info,
249 enum date_mode dmode,
252 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
253 struct reflog_info *info;
254 const char *printed_ref;
260 if (!commit_reflog->reflogs->short_ref)
261 commit_reflog->reflogs->short_ref
262 = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
263 printed_ref = commit_reflog->reflogs->short_ref;
265 printed_ref = commit_reflog->reflogs->ref;
268 strbuf_addf(sb, "%s@{", printed_ref);
269 if (commit_reflog->flag || dmode) {
270 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
271 strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
273 strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
274 - 2 - commit_reflog->recno);
277 strbuf_addch(sb, '}');
280 void get_reflog_message(struct strbuf *sb,
281 struct reflog_walk_info *reflog_info)
283 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
284 struct reflog_info *info;
290 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
291 len = strlen(info->message);
293 len--; /* strip away trailing newline */
294 strbuf_add(sb, info->message, len);
297 void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
298 enum date_mode dmode)
300 if (reflog_info && reflog_info->last_commit_reflog) {
301 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
302 struct reflog_info *info;
303 struct strbuf selector = STRBUF_INIT;
305 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
306 get_reflog_selector(&selector, reflog_info, dmode, 0);
308 printf("%s: %s", selector.buf, info->message);
311 printf("Reflog: %s (%s)\nReflog message: %s",
312 selector.buf, info->email, info->message);
315 strbuf_release(&selector);