blob: 8a4d8fa3bd5589fca43e97b05982a830af8bd27a [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;
Ævar Arnfjörð Bjarmason81ffbf82022-04-13 22:01:52 +020011 char *short_ref;
Johannes Schindelin8860fd42007-01-11 11:47:48 +010012 struct reflog_info {
brian m. carlson8ebc3fd2017-02-21 23:47:31 +000013 struct object_id ooid, noid;
Johannes Schindelin8860fd42007-01-11 11:47:48 +010014 char *email;
Johannes Schindelindddbad72017-04-26 21:29:31 +020015 timestamp_t timestamp;
Johannes Schindelin8860fd42007-01-11 11:47:48 +010016 int tz;
17 char *message;
18 } *items;
19 int nr, alloc;
20};
21
brian m. carlson9461d272017-02-21 23:47:32 +000022static int read_one_reflog(struct object_id *ooid, struct object_id *noid,
Johannes Schindelindddbad72017-04-26 21:29:31 +020023 const char *email, timestamp_t timestamp, int tz,
Johannes Schindelin8860fd42007-01-11 11:47:48 +010024 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;
brian m. carlson9461d272017-02-21 23:47:32 +000031 oidcpy(&item->ooid, ooid);
32 oidcpy(&item->noid, noid);
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
Jeff Kinge30d4632017-07-07 04:43:16 -040041static void free_complete_reflog(struct complete_reflogs *array)
42{
43 int i;
44
45 if (!array)
46 return;
47
48 for (i = 0; i < array->nr; i++) {
49 free(array->items[i].email);
50 free(array->items[i].message);
51 }
52 free(array->items);
53 free(array->ref);
Ævar Arnfjörð Bjarmason81ffbf82022-04-13 22:01:52 +020054 free(array->short_ref);
Jeff Kinge30d4632017-07-07 04:43:16 -040055 free(array);
56}
57
Jeff King1ee34712022-10-17 21:05:32 -040058static void complete_reflogs_clear(void *util, const char *str UNUSED)
Ævar Arnfjörð Bjarmason81ffbf82022-04-13 22:01:52 +020059{
60 struct complete_reflogs *array = util;
61 free_complete_reflog(array);
62}
63
Johannes Schindelin8860fd42007-01-11 11:47:48 +010064static struct complete_reflogs *read_complete_reflog(const char *ref)
65{
66 struct complete_reflogs *reflogs =
Brian Gesiak8e1aa2f2014-05-27 00:33:54 +090067 xcalloc(1, sizeof(struct complete_reflogs));
Johannes Schindelin8860fd42007-01-11 11:47:48 +010068 reflogs->ref = xstrdup(ref);
69 for_each_reflog_ent(ref, read_one_reflog, reflogs);
70 if (reflogs->nr == 0) {
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +070071 const char *name;
72 void *name_to_free;
Ronnie Sahlberg7695d112014-07-15 12:59:36 -070073 name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
René Scharfeefbd4fd2017-10-01 09:29:03 +020074 NULL, NULL);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +070075 if (name) {
Johannes Schindelin8860fd42007-01-11 11:47:48 +010076 for_each_reflog_ent(name, read_one_reflog, reflogs);
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +070077 free(name_to_free);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +070078 }
Johannes Schindelin8860fd42007-01-11 11:47:48 +010079 }
80 if (reflogs->nr == 0) {
Jeff King75faa452015-09-24 17:07:03 -040081 char *refname = xstrfmt("refs/%s", ref);
Johannes Schindelin8860fd42007-01-11 11:47:48 +010082 for_each_reflog_ent(refname, read_one_reflog, reflogs);
83 if (reflogs->nr == 0) {
Jeff King75faa452015-09-24 17:07:03 -040084 free(refname);
85 refname = xstrfmt("refs/heads/%s", ref);
Johannes Schindelin8860fd42007-01-11 11:47:48 +010086 for_each_reflog_ent(refname, read_one_reflog, reflogs);
87 }
88 free(refname);
89 }
90 return reflogs;
91}
92
93static int get_reflog_recno_by_time(struct complete_reflogs *array,
Johannes Schindelindddbad72017-04-26 21:29:31 +020094 timestamp_t timestamp)
Johannes Schindelin8860fd42007-01-11 11:47:48 +010095{
96 int i;
Johannes Schindelin40ab7c32007-01-20 10:49:15 +010097 for (i = array->nr - 1; i >= 0; i--)
Johannes Schindelin8860fd42007-01-11 11:47:48 +010098 if (timestamp >= array->items[i].timestamp)
99 return i;
100 return -1;
101}
102
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100103struct commit_reflog {
Jeff Kinga7631262012-05-04 01:26:26 -0400104 int recno;
105 enum selector_type {
106 SELECTOR_NONE,
107 SELECTOR_INDEX,
108 SELECTOR_DATE
109 } selector;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100110 struct complete_reflogs *reflogs;
111};
112
113struct reflog_walk_info {
Jeff Kingd08565b2017-07-07 05:14:07 -0400114 struct commit_reflog **logs;
115 size_t nr, alloc;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100116 struct string_list complete_reflogs;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100117 struct commit_reflog *last_commit_reflog;
118};
119
David Aguilar24d36f12014-08-31 13:11:31 -0700120void init_reflog_walk(struct reflog_walk_info **info)
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100121{
René Scharfeca56dad2021-03-13 17:17:22 +0100122 CALLOC_ARRAY(*info, 1);
Jeff King75afe7a2017-07-07 04:39:50 -0400123 (*info)->complete_reflogs.strdup_strings = 1;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100124}
125
Ævar Arnfjörð Bjarmason81ffbf82022-04-13 22:01:52 +0200126void reflog_walk_info_release(struct reflog_walk_info *info)
127{
128 size_t i;
129
130 if (!info)
131 return;
132
133 for (i = 0; i < info->nr; i++)
134 free(info->logs[i]);
135 string_list_clear_func(&info->complete_reflogs,
136 complete_reflogs_clear);
137 free(info->logs);
138 free(info);
139}
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{
Johannes Schindelindddbad72017-04-26 21:29:31 +0200144 timestamp_t timestamp = 0;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100145 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)
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200153 die("cannot walk reflogs for %s", name);
Johannes Schindelindb055e62007-01-20 03:28:19 +0100154
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') {
Johannes Schindelind271fd52007-02-02 00:07:24 +0100175 free(branch);
René Scharfeefbd4fd2017-10-01 09:29:03 +0200176 branch = resolve_refdup("HEAD", 0, NULL, NULL);
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700177 if (!branch)
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200178 die("no current branch");
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700179
Johannes Schindelind271fd52007-02-02 00:07:24 +0100180 }
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100181 reflogs = read_complete_reflog(branch);
Johannes Schindelineb3a4822007-02-09 01:28:23 +0100182 if (!reflogs || reflogs->nr == 0) {
Johannes Schindelineb3a4822007-02-09 01:28:23 +0100183 char *b;
Johannes Schindelin5026b472017-05-04 15:58:42 +0200184 int ret = dwim_log(branch, strlen(branch),
Ævar Arnfjörð Bjarmason6f45ec82021-08-23 13:36:08 +0200185 NULL, &b);
Johannes Schindelin5026b472017-05-04 15:58:42 +0200186 if (ret > 1)
187 free(b);
188 else if (ret == 1) {
Jeff Kinge30d4632017-07-07 04:43:16 -0400189 free_complete_reflog(reflogs);
Johannes Schindelineb3a4822007-02-09 01:28:23 +0100190 free(branch);
191 branch = b;
192 reflogs = read_complete_reflog(branch);
193 }
194 }
Johannes Schindelin5026b472017-05-04 15:58:42 +0200195 if (!reflogs || reflogs->nr == 0) {
Jeff Kinge30d4632017-07-07 04:43:16 -0400196 free_complete_reflog(reflogs);
Johannes Schindelin5026b472017-05-04 15:58:42 +0200197 free(branch);
Johannes Schindelin7b69b872007-07-24 00:39:50 +0100198 return -1;
Johannes Schindelin5026b472017-05-04 15:58:42 +0200199 }
Julian Phillips78a395d2010-06-26 00:41:35 +0100200 string_list_insert(&info->complete_reflogs, branch)->util
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100201 = reflogs;
202 }
Johannes Schindelin5026b472017-05-04 15:58:42 +0200203 free(branch);
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100204
René Scharfeca56dad2021-03-13 17:17:22 +0100205 CALLOC_ARRAY(commit_reflog, 1);
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100206 if (recno < 0) {
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100207 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
208 if (commit_reflog->recno < 0) {
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100209 free(commit_reflog);
Johannes Schindelin7b69b872007-07-24 00:39:50 +0100210 return -1;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100211 }
212 } else
213 commit_reflog->recno = reflogs->nr - recno - 1;
Jeff Kinga7631262012-05-04 01:26:26 -0400214 commit_reflog->selector = selector;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100215 commit_reflog->reflogs = reflogs;
216
Jeff Kingd08565b2017-07-07 05:14:07 -0400217 ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
218 info->logs[info->nr++] = commit_reflog;
219
Johannes Schindelin7b69b872007-07-24 00:39:50 +0100220 return 0;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100221}
222
Thomas Rast72b103f2009-10-19 17:48:09 +0200223void get_reflog_selector(struct strbuf *sb,
224 struct reflog_walk_info *reflog_info,
Jeff Kinga5481a62015-06-25 12:55:02 -0400225 const struct date_mode *dmode, int force_date,
Thomas Rast8f8f5472009-10-19 17:48:10 +0200226 int shorten)
Thomas Rast72b103f2009-10-19 17:48:09 +0200227{
228 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
229 struct reflog_info *info;
Thomas Rast8f8f5472009-10-19 17:48:10 +0200230 const char *printed_ref;
Thomas Rast72b103f2009-10-19 17:48:09 +0200231
232 if (!commit_reflog)
233 return;
234
Thomas Rast8f8f5472009-10-19 17:48:10 +0200235 if (shorten) {
236 if (!commit_reflog->reflogs->short_ref)
237 commit_reflog->reflogs->short_ref
238 = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
239 printed_ref = commit_reflog->reflogs->short_ref;
240 } else {
241 printed_ref = commit_reflog->reflogs->ref;
242 }
243
244 strbuf_addf(sb, "%s@{", printed_ref);
Jeff King794151e2012-05-04 01:27:25 -0400245 if (commit_reflog->selector == SELECTOR_DATE ||
Junio C Hamano55ccf852012-05-07 14:11:32 -0700246 (commit_reflog->selector == SELECTOR_NONE && force_date)) {
Thomas Rast72b103f2009-10-19 17:48:09 +0200247 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
248 strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
249 } else {
250 strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
251 - 2 - commit_reflog->recno);
252 }
253
254 strbuf_addch(sb, '}');
255}
256
Thomas Rast8f8f5472009-10-19 17:48:10 +0200257void get_reflog_message(struct strbuf *sb,
258 struct reflog_walk_info *reflog_info)
259{
260 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
261 struct reflog_info *info;
262 size_t len;
263
264 if (!commit_reflog)
265 return;
266
267 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
268 len = strlen(info->message);
269 if (len > 0)
270 len--; /* strip away trailing newline */
271 strbuf_add(sb, info->message, len);
272}
273
Jeff Kingcd1957f2011-12-16 06:40:24 -0500274const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
275{
276 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
277 struct reflog_info *info;
278
279 if (!commit_reflog)
280 return NULL;
281
282 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
283 return info->email;
284}
285
Jeff Kingde239442017-07-07 05:16:21 -0400286timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
287{
288 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
289 struct reflog_info *info;
290
291 if (!commit_reflog)
292 return 0;
293
294 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
295 return info->timestamp;
296}
297
Thomas Rast72b103f2009-10-19 17:48:09 +0200298void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
Jeff Kinga5481a62015-06-25 12:55:02 -0400299 const struct date_mode *dmode, int force_date)
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100300{
Thomas Rast72b103f2009-10-19 17:48:09 +0200301 if (reflog_info && reflog_info->last_commit_reflog) {
302 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100303 struct reflog_info *info;
Thomas Rast72b103f2009-10-19 17:48:09 +0200304 struct strbuf selector = STRBUF_INIT;
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100305
Junio C Hamano4d12a472007-01-20 00:51:41 -0800306 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
Junio C Hamano55ccf852012-05-07 14:11:32 -0700307 get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
Junio C Hamano4d12a472007-01-20 00:51:41 -0800308 if (oneline) {
Thomas Rast72b103f2009-10-19 17:48:09 +0200309 printf("%s: %s", selector.buf, info->message);
Junio C Hamano4d12a472007-01-20 00:51:41 -0800310 }
311 else {
Thomas Rast72b103f2009-10-19 17:48:09 +0200312 printf("Reflog: %s (%s)\nReflog message: %s",
313 selector.buf, info->email, info->message);
Junio C Hamano4d12a472007-01-20 00:51:41 -0800314 }
Thomas Rast72b103f2009-10-19 17:48:09 +0200315
316 strbuf_release(&selector);
Johannes Schindelin8860fd42007-01-11 11:47:48 +0100317 }
318}
Jeff King7f97de52017-07-07 05:08:30 -0400319
320int reflog_walk_empty(struct reflog_walk_info *info)
321{
Jeff Kingd08565b2017-07-07 05:14:07 -0400322 return !info || !info->nr;
323}
324
325static struct commit *next_reflog_commit(struct commit_reflog *log)
326{
327 for (; log->recno >= 0; log->recno--) {
328 struct reflog_info *entry = &log->reflogs->items[log->recno];
Stefan Beller109cd762018-06-28 18:21:51 -0700329 struct object *obj = parse_object(the_repository,
330 &entry->noid);
Jeff Kingd08565b2017-07-07 05:14:07 -0400331
332 if (obj && obj->type == OBJ_COMMIT)
333 return (struct commit *)obj;
334 }
335 return NULL;
336}
337
338static timestamp_t log_timestamp(struct commit_reflog *log)
339{
340 return log->reflogs->items[log->recno].timestamp;
341}
342
343struct commit *next_reflog_entry(struct reflog_walk_info *walk)
344{
345 struct commit_reflog *best = NULL;
346 struct commit *best_commit = NULL;
347 size_t i;
348
349 for (i = 0; i < walk->nr; i++) {
350 struct commit_reflog *log = walk->logs[i];
351 struct commit *commit = next_reflog_commit(log);
352
353 if (!commit)
354 continue;
355
356 if (!best || log_timestamp(log) > log_timestamp(best)) {
357 best = log;
358 best_commit = commit;
359 }
360 }
361
362 if (best) {
363 best->recno--;
364 walk->last_commit_reflog = best;
365 return best_commit;
366 }
367
368 return NULL;
Jeff King7f97de52017-07-07 05:08:30 -0400369}