rev-list: check reflog_info before showing usage
[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
97struct commit_info_lifo {
98 struct commit_info {
99 struct commit *commit;
100 void *util;
101 } *items;
102 int nr, alloc;
103};
104
105static struct commit_info *get_commit_info(struct commit *commit,
106 struct commit_info_lifo *lifo, int pop)
107{
108 int i;
109 for (i = 0; i < lifo->nr; i++)
110 if (lifo->items[i].commit == commit) {
111 struct commit_info *result = &lifo->items[i];
112 if (pop) {
113 if (i + 1 < lifo->nr)
114 memmove(lifo->items + i,
115 lifo->items + i + 1,
116 (lifo->nr - i) *
117 sizeof(struct commit_info));
118 lifo->nr--;
119 }
120 return result;
121 }
122 return NULL;
123}
124
125static void add_commit_info(struct commit *commit, void *util,
126 struct commit_info_lifo *lifo)
127{
128 struct commit_info *info;
6647cc26 129 ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
8860fd42
JS
130 info = lifo->items + lifo->nr;
131 info->commit = commit;
132 info->util = util;
133 lifo->nr++;
134}
135
136struct commit_reflog {
a763126b
JK
137 int recno;
138 enum selector_type {
139 SELECTOR_NONE,
140 SELECTOR_INDEX,
141 SELECTOR_DATE
142 } selector;
8860fd42
JS
143 struct complete_reflogs *reflogs;
144};
145
146struct reflog_walk_info {
147 struct commit_info_lifo reflogs;
c455c87c 148 struct string_list complete_reflogs;
8860fd42
JS
149 struct commit_reflog *last_commit_reflog;
150};
151
24d36f14 152void init_reflog_walk(struct reflog_walk_info **info)
8860fd42 153{
8e1aa2f7 154 *info = xcalloc(1, sizeof(struct reflog_walk_info));
75afe7ac 155 (*info)->complete_reflogs.strdup_strings = 1;
8860fd42
JS
156}
157
7b69b873 158int add_reflog_for_walk(struct reflog_walk_info *info,
8860fd42
JS
159 struct commit *commit, const char *name)
160{
dddbad72 161 timestamp_t timestamp = 0;
8860fd42 162 int recno = -1;
c455c87c 163 struct string_list_item *item;
8860fd42
JS
164 struct complete_reflogs *reflogs;
165 char *branch, *at = strchr(name, '@');
166 struct commit_reflog *commit_reflog;
a763126b 167 enum selector_type selector = SELECTOR_NONE;
8860fd42 168
db055e65
JS
169 if (commit->object.flags & UNINTERESTING)
170 die ("Cannot walk reflogs for %s", name);
171
8860fd42
JS
172 branch = xstrdup(name);
173 if (at && at[1] == '{') {
174 char *ep;
175 branch[at - name] = '\0';
176 recno = strtoul(at + 2, &ep, 10);
177 if (*ep != '}') {
178 recno = -1;
179 timestamp = approxidate(at + 2);
a763126b 180 selector = SELECTOR_DATE;
8860fd42 181 }
a763126b
JK
182 else
183 selector = SELECTOR_INDEX;
8860fd42
JS
184 } else
185 recno = 0;
186
e8c8b713 187 item = string_list_lookup(&info->complete_reflogs, branch);
8860fd42
JS
188 if (item)
189 reflogs = item->util;
190 else {
d271fd53 191 if (*branch == '\0') {
2928325f 192 struct object_id oid;
d271fd53 193 free(branch);
2928325f 194 branch = resolve_refdup("HEAD", 0, oid.hash, NULL);
96ec7b1e
NTND
195 if (!branch)
196 die ("No current branch");
197
d271fd53 198 }
8860fd42 199 reflogs = read_complete_reflog(branch);
eb3a4822 200 if (!reflogs || reflogs->nr == 0) {
2928325f 201 struct object_id oid;
eb3a4822 202 char *b;
5026b471
JS
203 int ret = dwim_log(branch, strlen(branch),
204 oid.hash, &b);
205 if (ret > 1)
206 free(b);
207 else if (ret == 1) {
e30d463d 208 free_complete_reflog(reflogs);
eb3a4822
JS
209 free(branch);
210 branch = b;
211 reflogs = read_complete_reflog(branch);
212 }
213 }
5026b471 214 if (!reflogs || reflogs->nr == 0) {
e30d463d 215 free_complete_reflog(reflogs);
5026b471 216 free(branch);
7b69b873 217 return -1;
5026b471 218 }
78a395d3 219 string_list_insert(&info->complete_reflogs, branch)->util
8860fd42
JS
220 = reflogs;
221 }
5026b471 222 free(branch);
8860fd42 223
8e1aa2f7 224 commit_reflog = xcalloc(1, sizeof(struct commit_reflog));
8860fd42 225 if (recno < 0) {
8860fd42
JS
226 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
227 if (commit_reflog->recno < 0) {
8860fd42 228 free(commit_reflog);
7b69b873 229 return -1;
8860fd42
JS
230 }
231 } else
232 commit_reflog->recno = reflogs->nr - recno - 1;
a763126b 233 commit_reflog->selector = selector;
8860fd42
JS
234 commit_reflog->reflogs = reflogs;
235
236 add_commit_info(commit, commit_reflog, &info->reflogs);
7b69b873 237 return 0;
8860fd42
JS
238}
239
240void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
241{
242 struct commit_info *commit_info =
243 get_commit_info(commit, &info->reflogs, 0);
244 struct commit_reflog *commit_reflog;
aecad374 245 struct object *logobj;
8860fd42
JS
246 struct reflog_info *reflog;
247
248 info->last_commit_reflog = NULL;
249 if (!commit_info)
250 return;
251
252 commit_reflog = commit_info->util;
253 if (commit_reflog->recno < 0) {
254 commit->parents = NULL;
255 return;
256 }
8860fd42 257 info->last_commit_reflog = commit_reflog;
aecad374
DK
258
259 do {
260 reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
261 commit_reflog->recno--;
c251c83d 262 logobj = parse_object(&reflog->ooid);
aecad374
DK
263 } while (commit_reflog->recno && (logobj && logobj->type != OBJ_COMMIT));
264
fb4e352b 265 if (!logobj && commit_reflog->recno >= 0 && is_null_oid(&reflog->ooid)) {
71abeb75
SG
266 /* a root commit, but there are still more entries to show */
267 reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
c251c83d 268 logobj = parse_object(&reflog->noid);
2272d3e5 269 if (!logobj)
be5982a7 270 logobj = parse_object(&reflog->ooid);
71abeb75
SG
271 }
272
aecad374
DK
273 if (!logobj || logobj->type != OBJ_COMMIT) {
274 commit_info->commit = NULL;
8860fd42
JS
275 commit->parents = NULL;
276 return;
277 }
aecad374 278 commit_info->commit = (struct commit *)logobj;
8860fd42 279
8e1aa2f7 280 commit->parents = xcalloc(1, sizeof(struct commit_list));
8860fd42 281 commit->parents->item = commit_info->commit;
8860fd42
JS
282}
283
72b103fe
TR
284void get_reflog_selector(struct strbuf *sb,
285 struct reflog_walk_info *reflog_info,
a5481a6c 286 const struct date_mode *dmode, int force_date,
8f8f5476 287 int shorten)
72b103fe
TR
288{
289 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
290 struct reflog_info *info;
8f8f5476 291 const char *printed_ref;
72b103fe
TR
292
293 if (!commit_reflog)
294 return;
295
8f8f5476
TR
296 if (shorten) {
297 if (!commit_reflog->reflogs->short_ref)
298 commit_reflog->reflogs->short_ref
299 = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
300 printed_ref = commit_reflog->reflogs->short_ref;
301 } else {
302 printed_ref = commit_reflog->reflogs->ref;
303 }
304
305 strbuf_addf(sb, "%s@{", printed_ref);
794151e9 306 if (commit_reflog->selector == SELECTOR_DATE ||
55ccf85a 307 (commit_reflog->selector == SELECTOR_NONE && force_date)) {
72b103fe
TR
308 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
309 strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
310 } else {
311 strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
312 - 2 - commit_reflog->recno);
313 }
314
315 strbuf_addch(sb, '}');
316}
317
8f8f5476
TR
318void get_reflog_message(struct strbuf *sb,
319 struct reflog_walk_info *reflog_info)
320{
321 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
322 struct reflog_info *info;
323 size_t len;
324
325 if (!commit_reflog)
326 return;
327
328 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
329 len = strlen(info->message);
330 if (len > 0)
331 len--; /* strip away trailing newline */
332 strbuf_add(sb, info->message, len);
333}
334
cd1957f5
JK
335const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
336{
337 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
338 struct reflog_info *info;
339
340 if (!commit_reflog)
341 return NULL;
342
343 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
344 return info->email;
345}
346
72b103fe 347void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
a5481a6c 348 const struct date_mode *dmode, int force_date)
8860fd42 349{
72b103fe
TR
350 if (reflog_info && reflog_info->last_commit_reflog) {
351 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
8860fd42 352 struct reflog_info *info;
72b103fe 353 struct strbuf selector = STRBUF_INIT;
8860fd42 354
4d12a471 355 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
55ccf85a 356 get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
4d12a471 357 if (oneline) {
72b103fe 358 printf("%s: %s", selector.buf, info->message);
4d12a471
JH
359 }
360 else {
72b103fe
TR
361 printf("Reflog: %s (%s)\nReflog message: %s",
362 selector.buf, info->email, info->message);
4d12a471 363 }
72b103fe
TR
364
365 strbuf_release(&selector);
8860fd42
JS
366 }
367}
7f97de5e
JK
368
369int reflog_walk_empty(struct reflog_walk_info *info)
370{
371 return !info || !info->reflogs.nr;
372}