blob: a246af27678a76d37c87bb56959ac050dff1f97f [file] [log] [blame]
Johannes Schindelin8860fd42007-01-11 11:47:48 +01001#include "cache.h"
2#include "commit.h"
3#include "refs.h"
4#include "diff.h"
5#include "revision.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01006#include "string-list.h"
Junio C Hamano53645a32007-01-20 00:47:34 -08007#include "reflog-walk.h"
Johannes Schindelin8860fd42007-01-11 11:47:48 +01008
9struct complete_reflogs {
10 char *ref;
Thomas Rast8f8f5472009-10-19 17:48:10 +020011 const char *short_ref;
Johannes Schindelin8860fd42007-01-11 11:47:48 +010012 struct reflog_info {
13 unsigned char osha1[20], nsha1[20];
14 char *email;
15 unsigned long timestamp;
16 int tz;
17 char *message;
18 } *items;
19 int nr, alloc;
20};
21
22static 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)
25{
26 struct complete_reflogs *array = cb_data;
27 struct reflog_info *item;
28
Dmitry S. Dolzhenko6647cc22014-03-04 02:31:57 +040029 ALLOC_GROW(array->items, array->nr + 1, array->alloc);
Johannes Schindelin8860fd42007-01-11 11:47:48 +010030 item = array->items + array->nr;
Sun He50546b12014-03-03 17:39:59 +080031 hashcpy(item->osha1, osha1);
32 hashcpy(item->nsha1, nsha1);
Johannes Schindelin8860fd42007-01-11 11:47:48 +010033 item->email = xstrdup(email);
34 item->timestamp = timestamp;
35 item->tz = tz;
36 item->message = xstrdup(message);
37 array->nr++;
38 return 0;
39}
40
41static struct complete_reflogs *read_complete_reflog(const char *ref)
42{
43 struct complete_reflogs *reflogs =
Brian Gesiak8e1aa2f2014-05-27 00:33:54 +090044 xcalloc(1, sizeof(struct complete_reflogs));
Johannes Schindelin8860fd42007-01-11 11:47:48 +010045 reflogs->ref = xstrdup(ref);
46 for_each_reflog_ent(ref, read_one_reflog, reflogs);
47 if (reflogs->nr == 0) {
48 unsigned char sha1[20];
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +070049 const char *name;
50 void *name_to_free;
Ronnie Sahlberg7695d112014-07-15 12:59:36 -070051 name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
52 sha1, NULL);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +070053 if (name) {
Johannes Schindelin8860fd42007-01-11 11:47:48 +010054 for_each_reflog_ent(name, read_one_reflog, reflogs);
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +070055 free(name_to_free);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +070056 }
Johannes Schindelin8860fd42007-01-11 11:47:48 +010057 }
58 if (reflogs->nr == 0) {
Jeff King75faa452015-09-24 17:07:03 -040059 char *refname = xstrfmt("refs/%s", ref);
Johannes Schindelin8860fd42007-01-11 11:47:48 +010060 for_each_reflog_ent(refname, read_one_reflog, reflogs);
61 if (reflogs->nr == 0) {
Jeff King75faa452015-09-24 17:07:03 -040062 free(refname);
63 refname = xstrfmt("refs/heads/%s", ref);
Johannes Schindelin8860fd42007-01-11 11:47:48 +010064 for_each_reflog_ent(refname, read_one_reflog, reflogs);
65 }
66 free(refname);
67 }
68 return reflogs;
69}
70
71static int get_reflog_recno_by_time(struct complete_reflogs *array,
72 unsigned long timestamp)
73{
74 int i;
Johannes Schindelin40ab7c32007-01-20 10:49:15 +010075 for (i = array->nr - 1; i >= 0; i--)
Johannes Schindelin8860fd42007-01-11 11:47:48 +010076 if (timestamp >= array->items[i].timestamp)
77 return i;
78 return -1;
79}
80
81struct commit_info_lifo {
82 struct commit_info {
83 struct commit *commit;
84 void *util;
85 } *items;
86 int nr, alloc;
87};
88
89static struct commit_info *get_commit_info(struct commit *commit,
90 struct commit_info_lifo *lifo, int pop)
91{
92 int i;
93 for (i = 0; i < lifo->nr; i++)
94 if (lifo->items[i].commit == commit) {
95 struct commit_info *result = &lifo->items[i];
96 if (pop) {
97 if (i + 1 < lifo->nr)
98 memmove(lifo->items + i,
99 lifo->items + i + 1,
100 (lifo->nr - i) *
101 sizeof(struct commit_info));
102 lifo->nr--;
103 }
104 return result;
105 }
106 return NULL;
107}
108
109static void add_commit_info(struct commit *commit, void *util,
110 struct commit_info_lifo *lifo)
111{
112 struct commit_info *info;
Dmitry S. Dolzhenko6647cc22014-03-04 02:31:57 +0400113 ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100114 info = lifo->items + lifo->nr;
115 info->commit = commit;
116 info->util = util;
117 lifo->nr++;
118}
119
120struct commit_reflog {
Jeff Kinga7631262012-05-04 01:26:26 -0400121 int recno;
122 enum selector_type {
123 SELECTOR_NONE,
124 SELECTOR_INDEX,
125 SELECTOR_DATE
126 } selector;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100127 struct complete_reflogs *reflogs;
128};
129
130struct reflog_walk_info {
131 struct commit_info_lifo reflogs;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100132 struct string_list complete_reflogs;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100133 struct commit_reflog *last_commit_reflog;
134};
135
David Aguilar24d36f12014-08-31 13:11:31 -0700136void init_reflog_walk(struct reflog_walk_info **info)
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100137{
Brian Gesiak8e1aa2f2014-05-27 00:33:54 +0900138 *info = xcalloc(1, sizeof(struct reflog_walk_info));
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100139}
140
Johannes Schindelin7b69b872007-07-24 00:39:50 +0100141int add_reflog_for_walk(struct reflog_walk_info *info,
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100142 struct commit *commit, const char *name)
143{
144 unsigned long timestamp = 0;
145 int recno = -1;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100146 struct string_list_item *item;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100147 struct complete_reflogs *reflogs;
148 char *branch, *at = strchr(name, '@');
149 struct commit_reflog *commit_reflog;
Jeff Kinga7631262012-05-04 01:26:26 -0400150 enum selector_type selector = SELECTOR_NONE;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100151
Johannes Schindelindb055e62007-01-20 03:28:19 +0100152 if (commit->object.flags & UNINTERESTING)
153 die ("Cannot walk reflogs for %s", name);
154
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100155 branch = xstrdup(name);
156 if (at && at[1] == '{') {
157 char *ep;
158 branch[at - name] = '\0';
159 recno = strtoul(at + 2, &ep, 10);
160 if (*ep != '}') {
161 recno = -1;
162 timestamp = approxidate(at + 2);
Jeff Kinga7631262012-05-04 01:26:26 -0400163 selector = SELECTOR_DATE;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100164 }
Jeff Kinga7631262012-05-04 01:26:26 -0400165 else
166 selector = SELECTOR_INDEX;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100167 } else
168 recno = 0;
169
Julian Phillipse8c8b712010-06-26 00:41:37 +0100170 item = string_list_lookup(&info->complete_reflogs, branch);
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100171 if (item)
172 reflogs = item->util;
173 else {
Johannes Schindelind271fd52007-02-02 00:07:24 +0100174 if (*branch == '\0') {
175 unsigned char sha1[20];
Johannes Schindelind271fd52007-02-02 00:07:24 +0100176 free(branch);
Ronnie Sahlberg7695d112014-07-15 12:59:36 -0700177 branch = resolve_refdup("HEAD", 0, sha1, NULL);
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700178 if (!branch)
179 die ("No current branch");
180
Johannes Schindelind271fd52007-02-02 00:07:24 +0100181 }
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100182 reflogs = read_complete_reflog(branch);
Johannes Schindelineb3a4822007-02-09 01:28:23 +0100183 if (!reflogs || reflogs->nr == 0) {
184 unsigned char sha1[20];
185 char *b;
186 if (dwim_log(branch, strlen(branch), sha1, &b) == 1) {
187 if (reflogs) {
188 free(reflogs->ref);
189 free(reflogs);
190 }
191 free(branch);
192 branch = b;
193 reflogs = read_complete_reflog(branch);
194 }
195 }
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100196 if (!reflogs || reflogs->nr == 0)
Johannes Schindelin7b69b872007-07-24 00:39:50 +0100197 return -1;
Julian Phillips78a395d2010-06-26 00:41:35 +0100198 string_list_insert(&info->complete_reflogs, branch)->util
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100199 = reflogs;
200 }
201
Brian Gesiak8e1aa2f2014-05-27 00:33:54 +0900202 commit_reflog = xcalloc(1, sizeof(struct commit_reflog));
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100203 if (recno < 0) {
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100204 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
205 if (commit_reflog->recno < 0) {
206 free(branch);
207 free(commit_reflog);
Johannes Schindelin7b69b872007-07-24 00:39:50 +0100208 return -1;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100209 }
210 } else
211 commit_reflog->recno = reflogs->nr - recno - 1;
Jeff Kinga7631262012-05-04 01:26:26 -0400212 commit_reflog->selector = selector;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100213 commit_reflog->reflogs = reflogs;
214
215 add_commit_info(commit, commit_reflog, &info->reflogs);
Johannes Schindelin7b69b872007-07-24 00:39:50 +0100216 return 0;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100217}
218
219void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
220{
221 struct commit_info *commit_info =
222 get_commit_info(commit, &info->reflogs, 0);
223 struct commit_reflog *commit_reflog;
Dennis Kaarsemakeraecad372016-01-05 22:12:10 +0100224 struct object *logobj;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100225 struct reflog_info *reflog;
226
227 info->last_commit_reflog = NULL;
228 if (!commit_info)
229 return;
230
231 commit_reflog = commit_info->util;
232 if (commit_reflog->recno < 0) {
233 commit->parents = NULL;
234 return;
235 }
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100236 info->last_commit_reflog = commit_reflog;
Dennis Kaarsemakeraecad372016-01-05 22:12:10 +0100237
238 do {
239 reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
240 commit_reflog->recno--;
241 logobj = parse_object(reflog->osha1);
242 } while (commit_reflog->recno && (logobj && logobj->type != OBJ_COMMIT));
243
SZEDER Gábor71abeb72016-06-03 22:42:35 +0200244 if (!logobj && commit_reflog->recno >= 0 && is_null_sha1(reflog->osha1)) {
245 /* a root commit, but there are still more entries to show */
246 reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
247 logobj = parse_object(reflog->nsha1);
248 }
249
Dennis Kaarsemakeraecad372016-01-05 22:12:10 +0100250 if (!logobj || logobj->type != OBJ_COMMIT) {
251 commit_info->commit = NULL;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100252 commit->parents = NULL;
253 return;
254 }
Dennis Kaarsemakeraecad372016-01-05 22:12:10 +0100255 commit_info->commit = (struct commit *)logobj;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100256
Brian Gesiak8e1aa2f2014-05-27 00:33:54 +0900257 commit->parents = xcalloc(1, sizeof(struct commit_list));
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100258 commit->parents->item = commit_info->commit;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100259}
260
Thomas Rast72b103f2009-10-19 17:48:09 +0200261void get_reflog_selector(struct strbuf *sb,
262 struct reflog_walk_info *reflog_info,
Jeff Kinga5481a62015-06-25 12:55:02 -0400263 const struct date_mode *dmode, int force_date,
Thomas Rast8f8f5472009-10-19 17:48:10 +0200264 int shorten)
Thomas Rast72b103f2009-10-19 17:48:09 +0200265{
266 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
267 struct reflog_info *info;
Thomas Rast8f8f5472009-10-19 17:48:10 +0200268 const char *printed_ref;
Thomas Rast72b103f2009-10-19 17:48:09 +0200269
270 if (!commit_reflog)
271 return;
272
Thomas Rast8f8f5472009-10-19 17:48:10 +0200273 if (shorten) {
274 if (!commit_reflog->reflogs->short_ref)
275 commit_reflog->reflogs->short_ref
276 = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
277 printed_ref = commit_reflog->reflogs->short_ref;
278 } else {
279 printed_ref = commit_reflog->reflogs->ref;
280 }
281
282 strbuf_addf(sb, "%s@{", printed_ref);
Jeff King794151e2012-05-04 01:27:25 -0400283 if (commit_reflog->selector == SELECTOR_DATE ||
Junio C Hamano55ccf852012-05-07 14:11:32 -0700284 (commit_reflog->selector == SELECTOR_NONE && force_date)) {
Thomas Rast72b103f2009-10-19 17:48:09 +0200285 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
286 strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
287 } else {
288 strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
289 - 2 - commit_reflog->recno);
290 }
291
292 strbuf_addch(sb, '}');
293}
294
Thomas Rast8f8f5472009-10-19 17:48:10 +0200295void get_reflog_message(struct strbuf *sb,
296 struct reflog_walk_info *reflog_info)
297{
298 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
299 struct reflog_info *info;
300 size_t len;
301
302 if (!commit_reflog)
303 return;
304
305 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
306 len = strlen(info->message);
307 if (len > 0)
308 len--; /* strip away trailing newline */
309 strbuf_add(sb, info->message, len);
310}
311
Jeff Kingcd1957f2011-12-16 06:40:24 -0500312const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
313{
314 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
315 struct reflog_info *info;
316
317 if (!commit_reflog)
318 return NULL;
319
320 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
321 return info->email;
322}
323
Thomas Rast72b103f2009-10-19 17:48:09 +0200324void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
Jeff Kinga5481a62015-06-25 12:55:02 -0400325 const struct date_mode *dmode, int force_date)
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100326{
Thomas Rast72b103f2009-10-19 17:48:09 +0200327 if (reflog_info && reflog_info->last_commit_reflog) {
328 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100329 struct reflog_info *info;
Thomas Rast72b103f2009-10-19 17:48:09 +0200330 struct strbuf selector = STRBUF_INIT;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100331
Junio C Hamano4d12a472007-01-20 00:51:41 -0800332 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
Junio C Hamano55ccf852012-05-07 14:11:32 -0700333 get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
Junio C Hamano4d12a472007-01-20 00:51:41 -0800334 if (oneline) {
Thomas Rast72b103f2009-10-19 17:48:09 +0200335 printf("%s: %s", selector.buf, info->message);
Junio C Hamano4d12a472007-01-20 00:51:41 -0800336 }
337 else {
Thomas Rast72b103f2009-10-19 17:48:09 +0200338 printf("Reflog: %s (%s)\nReflog message: %s",
339 selector.buf, info->email, info->message);
Junio C Hamano4d12a472007-01-20 00:51:41 -0800340 }
Thomas Rast72b103f2009-10-19 17:48:09 +0200341
342 strbuf_release(&selector);
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100343 }
344}