refs: pass NULL to refs_resolve_refdup() if hash is not needed
[git/git.git] / reflog-walk.c
CommitLineData
8860fd42
JS
1#include "cache.h"
2#include "commit.h"
3#include "refs.h"
4#include "diff.h"
5#include "revision.h"
c455c87c 6#include "string-list.h"
53645a3a 7#include "reflog-walk.h"
8860fd42
JS
8
9struct complete_reflogs {
10 char *ref;
8f8f5476 11 const char *short_ref;
8860fd42 12 struct reflog_info {
8ebc3fd0 13 struct object_id ooid, noid;
8860fd42 14 char *email;
dddbad72 15 timestamp_t timestamp;
8860fd42
JS
16 int tz;
17 char *message;
18 } *items;
19 int nr, alloc;
20};
21
9461d272 22static int read_one_reflog(struct object_id *ooid, struct object_id *noid,
dddbad72 23 const char *email, timestamp_t timestamp, int tz,
8860fd42
JS
24 const char *message, void *cb_data)
25{
26 struct complete_reflogs *array = cb_data;
27 struct reflog_info *item;
28
6647cc26 29 ALLOC_GROW(array->items, array->nr + 1, array->alloc);
8860fd42 30 item = array->items + array->nr;
9461d272 31 oidcpy(&item->ooid, ooid);
32 oidcpy(&item->noid, noid);
8860fd42
JS
33 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
e30d463d
JK
41static 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);
54 free(array);
55}
56
8860fd42
JS
57static struct complete_reflogs *read_complete_reflog(const char *ref)
58{
59 struct complete_reflogs *reflogs =
8e1aa2f7 60 xcalloc(1, sizeof(struct complete_reflogs));
8860fd42
JS
61 reflogs->ref = xstrdup(ref);
62 for_each_reflog_ent(ref, read_one_reflog, reflogs);
63 if (reflogs->nr == 0) {
2928325f 64 struct object_id oid;
96ec7b1e
NTND
65 const char *name;
66 void *name_to_free;
7695d118 67 name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
2928325f 68 oid.hash, NULL);
d5a35c11 69 if (name) {
8860fd42 70 for_each_reflog_ent(name, read_one_reflog, reflogs);
96ec7b1e 71 free(name_to_free);
d5a35c11 72 }
8860fd42
JS
73 }
74 if (reflogs->nr == 0) {
75faa45a 75 char *refname = xstrfmt("refs/%s", ref);
8860fd42
JS
76 for_each_reflog_ent(refname, read_one_reflog, reflogs);
77 if (reflogs->nr == 0) {
75faa45a
JK
78 free(refname);
79 refname = xstrfmt("refs/heads/%s", ref);
8860fd42
JS
80 for_each_reflog_ent(refname, read_one_reflog, reflogs);
81 }
82 free(refname);
83 }
84 return reflogs;
85}
86
87static int get_reflog_recno_by_time(struct complete_reflogs *array,
dddbad72 88 timestamp_t timestamp)
8860fd42
JS
89{
90 int i;
40ab7c33 91 for (i = array->nr - 1; i >= 0; i--)
8860fd42
JS
92 if (timestamp >= array->items[i].timestamp)
93 return i;
94 return -1;
95}
96
8860fd42 97struct commit_reflog {
a763126b
JK
98 int recno;
99 enum selector_type {
100 SELECTOR_NONE,
101 SELECTOR_INDEX,
102 SELECTOR_DATE
103 } selector;
8860fd42
JS
104 struct complete_reflogs *reflogs;
105};
106
107struct reflog_walk_info {
d08565bf
JK
108 struct commit_reflog **logs;
109 size_t nr, alloc;
c455c87c 110 struct string_list complete_reflogs;
8860fd42
JS
111 struct commit_reflog *last_commit_reflog;
112};
113
24d36f14 114void init_reflog_walk(struct reflog_walk_info **info)
8860fd42 115{
8e1aa2f7 116 *info = xcalloc(1, sizeof(struct reflog_walk_info));
75afe7ac 117 (*info)->complete_reflogs.strdup_strings = 1;
8860fd42
JS
118}
119
7b69b873 120int add_reflog_for_walk(struct reflog_walk_info *info,
8860fd42
JS
121 struct commit *commit, const char *name)
122{
dddbad72 123 timestamp_t timestamp = 0;
8860fd42 124 int recno = -1;
c455c87c 125 struct string_list_item *item;
8860fd42
JS
126 struct complete_reflogs *reflogs;
127 char *branch, *at = strchr(name, '@');
128 struct commit_reflog *commit_reflog;
a763126b 129 enum selector_type selector = SELECTOR_NONE;
8860fd42 130
db055e65
JS
131 if (commit->object.flags & UNINTERESTING)
132 die ("Cannot walk reflogs for %s", name);
133
8860fd42
JS
134 branch = xstrdup(name);
135 if (at && at[1] == '{') {
136 char *ep;
137 branch[at - name] = '\0';
138 recno = strtoul(at + 2, &ep, 10);
139 if (*ep != '}') {
140 recno = -1;
141 timestamp = approxidate(at + 2);
a763126b 142 selector = SELECTOR_DATE;
8860fd42 143 }
a763126b
JK
144 else
145 selector = SELECTOR_INDEX;
8860fd42
JS
146 } else
147 recno = 0;
148
e8c8b713 149 item = string_list_lookup(&info->complete_reflogs, branch);
8860fd42
JS
150 if (item)
151 reflogs = item->util;
152 else {
d271fd53 153 if (*branch == '\0') {
2928325f 154 struct object_id oid;
d271fd53 155 free(branch);
2928325f 156 branch = resolve_refdup("HEAD", 0, oid.hash, NULL);
96ec7b1e
NTND
157 if (!branch)
158 die ("No current branch");
159
d271fd53 160 }
8860fd42 161 reflogs = read_complete_reflog(branch);
eb3a4822 162 if (!reflogs || reflogs->nr == 0) {
2928325f 163 struct object_id oid;
eb3a4822 164 char *b;
5026b471
JS
165 int ret = dwim_log(branch, strlen(branch),
166 oid.hash, &b);
167 if (ret > 1)
168 free(b);
169 else if (ret == 1) {
e30d463d 170 free_complete_reflog(reflogs);
eb3a4822
JS
171 free(branch);
172 branch = b;
173 reflogs = read_complete_reflog(branch);
174 }
175 }
5026b471 176 if (!reflogs || reflogs->nr == 0) {
e30d463d 177 free_complete_reflog(reflogs);
5026b471 178 free(branch);
7b69b873 179 return -1;
5026b471 180 }
78a395d3 181 string_list_insert(&info->complete_reflogs, branch)->util
8860fd42
JS
182 = reflogs;
183 }
5026b471 184 free(branch);
8860fd42 185
8e1aa2f7 186 commit_reflog = xcalloc(1, sizeof(struct commit_reflog));
8860fd42 187 if (recno < 0) {
8860fd42
JS
188 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
189 if (commit_reflog->recno < 0) {
8860fd42 190 free(commit_reflog);
7b69b873 191 return -1;
8860fd42
JS
192 }
193 } else
194 commit_reflog->recno = reflogs->nr - recno - 1;
a763126b 195 commit_reflog->selector = selector;
8860fd42
JS
196 commit_reflog->reflogs = reflogs;
197
d08565bf
JK
198 ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
199 info->logs[info->nr++] = commit_reflog;
8860fd42 200
d08565bf 201 return 0;
8860fd42
JS
202}
203
72b103fe
TR
204void get_reflog_selector(struct strbuf *sb,
205 struct reflog_walk_info *reflog_info,
a5481a6c 206 const struct date_mode *dmode, int force_date,
8f8f5476 207 int shorten)
72b103fe
TR
208{
209 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
210 struct reflog_info *info;
8f8f5476 211 const char *printed_ref;
72b103fe
TR
212
213 if (!commit_reflog)
214 return;
215
8f8f5476
TR
216 if (shorten) {
217 if (!commit_reflog->reflogs->short_ref)
218 commit_reflog->reflogs->short_ref
219 = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
220 printed_ref = commit_reflog->reflogs->short_ref;
221 } else {
222 printed_ref = commit_reflog->reflogs->ref;
223 }
224
225 strbuf_addf(sb, "%s@{", printed_ref);
794151e9 226 if (commit_reflog->selector == SELECTOR_DATE ||
55ccf85a 227 (commit_reflog->selector == SELECTOR_NONE && force_date)) {
72b103fe
TR
228 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
229 strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
230 } else {
231 strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
232 - 2 - commit_reflog->recno);
233 }
234
235 strbuf_addch(sb, '}');
236}
237
8f8f5476
TR
238void get_reflog_message(struct strbuf *sb,
239 struct reflog_walk_info *reflog_info)
240{
241 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
242 struct reflog_info *info;
243 size_t len;
244
245 if (!commit_reflog)
246 return;
247
248 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
249 len = strlen(info->message);
250 if (len > 0)
251 len--; /* strip away trailing newline */
252 strbuf_add(sb, info->message, len);
253}
254
cd1957f5
JK
255const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
256{
257 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
258 struct reflog_info *info;
259
260 if (!commit_reflog)
261 return NULL;
262
263 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
264 return info->email;
265}
266
de239446
JK
267timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
268{
269 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
270 struct reflog_info *info;
271
272 if (!commit_reflog)
273 return 0;
274
275 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
276 return info->timestamp;
277}
278
72b103fe 279void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
a5481a6c 280 const struct date_mode *dmode, int force_date)
8860fd42 281{
72b103fe
TR
282 if (reflog_info && reflog_info->last_commit_reflog) {
283 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
8860fd42 284 struct reflog_info *info;
72b103fe 285 struct strbuf selector = STRBUF_INIT;
8860fd42 286
4d12a471 287 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
55ccf85a 288 get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
4d12a471 289 if (oneline) {
72b103fe 290 printf("%s: %s", selector.buf, info->message);
4d12a471
JH
291 }
292 else {
72b103fe
TR
293 printf("Reflog: %s (%s)\nReflog message: %s",
294 selector.buf, info->email, info->message);
4d12a471 295 }
72b103fe
TR
296
297 strbuf_release(&selector);
8860fd42
JS
298 }
299}
7f97de5e
JK
300
301int reflog_walk_empty(struct reflog_walk_info *info)
302{
d08565bf
JK
303 return !info || !info->nr;
304}
305
306static struct commit *next_reflog_commit(struct commit_reflog *log)
307{
308 for (; log->recno >= 0; log->recno--) {
309 struct reflog_info *entry = &log->reflogs->items[log->recno];
310 struct object *obj = parse_object(&entry->noid);
311
312 if (obj && obj->type == OBJ_COMMIT)
313 return (struct commit *)obj;
314 }
315 return NULL;
316}
317
318static timestamp_t log_timestamp(struct commit_reflog *log)
319{
320 return log->reflogs->items[log->recno].timestamp;
321}
322
323struct commit *next_reflog_entry(struct reflog_walk_info *walk)
324{
325 struct commit_reflog *best = NULL;
326 struct commit *best_commit = NULL;
327 size_t i;
328
329 for (i = 0; i < walk->nr; i++) {
330 struct commit_reflog *log = walk->logs[i];
331 struct commit *commit = next_reflog_commit(log);
332
333 if (!commit)
334 continue;
335
336 if (!best || log_timestamp(log) > log_timestamp(best)) {
337 best = log;
338 best_commit = commit;
339 }
340 }
341
342 if (best) {
343 best->recno--;
344 walk->last_commit_reflog = best;
345 return best_commit;
346 }
347
348 return NULL;
7f97de5e 349}