]> git.ipfire.org Git - thirdparty/git.git/blame - reflog-walk.c
mingw: remove unneeded `NO_CURL` directive
[thirdparty/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;
81ffbf83 11 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);
81ffbf83 54 free(array->short_ref);
e30d463d
JK
55 free(array);
56}
57
81ffbf83
ÆAB
58static void complete_reflogs_clear(void *util, const char *str)
59{
60 struct complete_reflogs *array = util;
61 free_complete_reflog(array);
62}
63
8860fd42
JS
64static struct complete_reflogs *read_complete_reflog(const char *ref)
65{
66 struct complete_reflogs *reflogs =
8e1aa2f7 67 xcalloc(1, sizeof(struct complete_reflogs));
8860fd42
JS
68 reflogs->ref = xstrdup(ref);
69 for_each_reflog_ent(ref, read_one_reflog, reflogs);
70 if (reflogs->nr == 0) {
96ec7b1e
NTND
71 const char *name;
72 void *name_to_free;
7695d118 73 name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
efbd4fdf 74 NULL, NULL);
d5a35c11 75 if (name) {
8860fd42 76 for_each_reflog_ent(name, read_one_reflog, reflogs);
96ec7b1e 77 free(name_to_free);
d5a35c11 78 }
8860fd42
JS
79 }
80 if (reflogs->nr == 0) {
75faa45a 81 char *refname = xstrfmt("refs/%s", ref);
8860fd42
JS
82 for_each_reflog_ent(refname, read_one_reflog, reflogs);
83 if (reflogs->nr == 0) {
75faa45a
JK
84 free(refname);
85 refname = xstrfmt("refs/heads/%s", ref);
8860fd42
JS
86 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,
dddbad72 94 timestamp_t timestamp)
8860fd42
JS
95{
96 int i;
40ab7c33 97 for (i = array->nr - 1; i >= 0; i--)
8860fd42
JS
98 if (timestamp >= array->items[i].timestamp)
99 return i;
100 return -1;
101}
102
8860fd42 103struct commit_reflog {
a763126b
JK
104 int recno;
105 enum selector_type {
106 SELECTOR_NONE,
107 SELECTOR_INDEX,
108 SELECTOR_DATE
109 } selector;
8860fd42
JS
110 struct complete_reflogs *reflogs;
111};
112
113struct reflog_walk_info {
d08565bf
JK
114 struct commit_reflog **logs;
115 size_t nr, alloc;
c455c87c 116 struct string_list complete_reflogs;
8860fd42
JS
117 struct commit_reflog *last_commit_reflog;
118};
119
24d36f14 120void init_reflog_walk(struct reflog_walk_info **info)
8860fd42 121{
ca56dadb 122 CALLOC_ARRAY(*info, 1);
75afe7ac 123 (*info)->complete_reflogs.strdup_strings = 1;
8860fd42
JS
124}
125
81ffbf83
ÆAB
126void 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
7b69b873 141int add_reflog_for_walk(struct reflog_walk_info *info,
8860fd42
JS
142 struct commit *commit, const char *name)
143{
dddbad72 144 timestamp_t timestamp = 0;
8860fd42 145 int recno = -1;
c455c87c 146 struct string_list_item *item;
8860fd42
JS
147 struct complete_reflogs *reflogs;
148 char *branch, *at = strchr(name, '@');
149 struct commit_reflog *commit_reflog;
a763126b 150 enum selector_type selector = SELECTOR_NONE;
8860fd42 151
db055e65 152 if (commit->object.flags & UNINTERESTING)
1a07e59c 153 die("cannot walk reflogs for %s", name);
db055e65 154
8860fd42
JS
155 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);
a763126b 163 selector = SELECTOR_DATE;
8860fd42 164 }
a763126b
JK
165 else
166 selector = SELECTOR_INDEX;
8860fd42
JS
167 } else
168 recno = 0;
169
e8c8b713 170 item = string_list_lookup(&info->complete_reflogs, branch);
8860fd42
JS
171 if (item)
172 reflogs = item->util;
173 else {
d271fd53 174 if (*branch == '\0') {
d271fd53 175 free(branch);
efbd4fdf 176 branch = resolve_refdup("HEAD", 0, NULL, NULL);
96ec7b1e 177 if (!branch)
1a07e59c 178 die("no current branch");
96ec7b1e 179
d271fd53 180 }
8860fd42 181 reflogs = read_complete_reflog(branch);
eb3a4822 182 if (!reflogs || reflogs->nr == 0) {
eb3a4822 183 char *b;
5026b471 184 int ret = dwim_log(branch, strlen(branch),
6f45ec88 185 NULL, &b);
5026b471
JS
186 if (ret > 1)
187 free(b);
188 else if (ret == 1) {
e30d463d 189 free_complete_reflog(reflogs);
eb3a4822
JS
190 free(branch);
191 branch = b;
192 reflogs = read_complete_reflog(branch);
193 }
194 }
5026b471 195 if (!reflogs || reflogs->nr == 0) {
e30d463d 196 free_complete_reflog(reflogs);
5026b471 197 free(branch);
7b69b873 198 return -1;
5026b471 199 }
78a395d3 200 string_list_insert(&info->complete_reflogs, branch)->util
8860fd42
JS
201 = reflogs;
202 }
5026b471 203 free(branch);
8860fd42 204
ca56dadb 205 CALLOC_ARRAY(commit_reflog, 1);
8860fd42 206 if (recno < 0) {
8860fd42
JS
207 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
208 if (commit_reflog->recno < 0) {
8860fd42 209 free(commit_reflog);
7b69b873 210 return -1;
8860fd42
JS
211 }
212 } else
213 commit_reflog->recno = reflogs->nr - recno - 1;
a763126b 214 commit_reflog->selector = selector;
8860fd42
JS
215 commit_reflog->reflogs = reflogs;
216
d08565bf
JK
217 ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
218 info->logs[info->nr++] = commit_reflog;
8860fd42 219
d08565bf 220 return 0;
8860fd42
JS
221}
222
72b103fe
TR
223void get_reflog_selector(struct strbuf *sb,
224 struct reflog_walk_info *reflog_info,
a5481a6c 225 const struct date_mode *dmode, int force_date,
8f8f5476 226 int shorten)
72b103fe
TR
227{
228 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
229 struct reflog_info *info;
8f8f5476 230 const char *printed_ref;
72b103fe
TR
231
232 if (!commit_reflog)
233 return;
234
8f8f5476
TR
235 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);
794151e9 245 if (commit_reflog->selector == SELECTOR_DATE ||
55ccf85a 246 (commit_reflog->selector == SELECTOR_NONE && force_date)) {
72b103fe
TR
247 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
8f8f5476
TR
257void 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
cd1957f5
JK
274const 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
de239446
JK
286timestamp_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
72b103fe 298void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
a5481a6c 299 const struct date_mode *dmode, int force_date)
8860fd42 300{
72b103fe
TR
301 if (reflog_info && reflog_info->last_commit_reflog) {
302 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
8860fd42 303 struct reflog_info *info;
72b103fe 304 struct strbuf selector = STRBUF_INIT;
8860fd42 305
4d12a471 306 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
55ccf85a 307 get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
4d12a471 308 if (oneline) {
72b103fe 309 printf("%s: %s", selector.buf, info->message);
4d12a471
JH
310 }
311 else {
72b103fe
TR
312 printf("Reflog: %s (%s)\nReflog message: %s",
313 selector.buf, info->email, info->message);
4d12a471 314 }
72b103fe
TR
315
316 strbuf_release(&selector);
8860fd42
JS
317 }
318}
7f97de5e
JK
319
320int reflog_walk_empty(struct reflog_walk_info *info)
321{
d08565bf
JK
322 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];
109cd76d
SB
329 struct object *obj = parse_object(the_repository,
330 &entry->noid);
d08565bf
JK
331
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;
7f97de5e 369}