Sync with 2.38.5
[git.git] / reflog-walk.c
blob8a4d8fa3bd5589fca43e97b05982a830af8bd27a
1 #include "cache.h"
2 #include "commit.h"
3 #include "refs.h"
4 #include "diff.h"
5 #include "revision.h"
6 #include "string-list.h"
7 #include "reflog-walk.h"
9 struct complete_reflogs {
10 char *ref;
11 char *short_ref;
12 struct reflog_info {
13 struct object_id ooid, noid;
14 char *email;
15 timestamp_t timestamp;
16 int tz;
17 char *message;
18 } *items;
19 int nr, alloc;
22 static int read_one_reflog(struct object_id *ooid, struct object_id *noid,
23 const char *email, timestamp_t timestamp, int tz,
24 const char *message, void *cb_data)
26 struct complete_reflogs *array = cb_data;
27 struct reflog_info *item;
29 ALLOC_GROW(array->items, array->nr + 1, array->alloc);
30 item = array->items + array->nr;
31 oidcpy(&item->ooid, ooid);
32 oidcpy(&item->noid, noid);
33 item->email = xstrdup(email);
34 item->timestamp = timestamp;
35 item->tz = tz;
36 item->message = xstrdup(message);
37 array->nr++;
38 return 0;
41 static void free_complete_reflog(struct complete_reflogs *array)
43 int i;
45 if (!array)
46 return;
48 for (i = 0; i < array->nr; i++) {
49 free(array->items[i].email);
50 free(array->items[i].message);
52 free(array->items);
53 free(array->ref);
54 free(array->short_ref);
55 free(array);
58 static void complete_reflogs_clear(void *util, const char *str UNUSED)
60 struct complete_reflogs *array = util;
61 free_complete_reflog(array);
64 static struct complete_reflogs *read_complete_reflog(const char *ref)
66 struct complete_reflogs *reflogs =
67 xcalloc(1, sizeof(struct complete_reflogs));
68 reflogs->ref = xstrdup(ref);
69 for_each_reflog_ent(ref, read_one_reflog, reflogs);
70 if (reflogs->nr == 0) {
71 const char *name;
72 void *name_to_free;
73 name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
74 NULL, NULL);
75 if (name) {
76 for_each_reflog_ent(name, read_one_reflog, reflogs);
77 free(name_to_free);
80 if (reflogs->nr == 0) {
81 char *refname = xstrfmt("refs/%s", ref);
82 for_each_reflog_ent(refname, read_one_reflog, reflogs);
83 if (reflogs->nr == 0) {
84 free(refname);
85 refname = xstrfmt("refs/heads/%s", ref);
86 for_each_reflog_ent(refname, read_one_reflog, reflogs);
88 free(refname);
90 return reflogs;
93 static int get_reflog_recno_by_time(struct complete_reflogs *array,
94 timestamp_t timestamp)
96 int i;
97 for (i = array->nr - 1; i >= 0; i--)
98 if (timestamp >= array->items[i].timestamp)
99 return i;
100 return -1;
103 struct commit_reflog {
104 int recno;
105 enum selector_type {
106 SELECTOR_NONE,
107 SELECTOR_INDEX,
108 SELECTOR_DATE
109 } selector;
110 struct complete_reflogs *reflogs;
113 struct reflog_walk_info {
114 struct commit_reflog **logs;
115 size_t nr, alloc;
116 struct string_list complete_reflogs;
117 struct commit_reflog *last_commit_reflog;
120 void init_reflog_walk(struct reflog_walk_info **info)
122 CALLOC_ARRAY(*info, 1);
123 (*info)->complete_reflogs.strdup_strings = 1;
126 void reflog_walk_info_release(struct reflog_walk_info *info)
128 size_t i;
130 if (!info)
131 return;
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);
141 int add_reflog_for_walk(struct reflog_walk_info *info,
142 struct commit *commit, const char *name)
144 timestamp_t timestamp = 0;
145 int recno = -1;
146 struct string_list_item *item;
147 struct complete_reflogs *reflogs;
148 char *branch, *at = strchr(name, '@');
149 struct commit_reflog *commit_reflog;
150 enum selector_type selector = SELECTOR_NONE;
152 if (commit->object.flags & UNINTERESTING)
153 die("cannot walk reflogs for %s", name);
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);
163 selector = SELECTOR_DATE;
165 else
166 selector = SELECTOR_INDEX;
167 } else
168 recno = 0;
170 item = string_list_lookup(&info->complete_reflogs, branch);
171 if (item)
172 reflogs = item->util;
173 else {
174 if (*branch == '\0') {
175 free(branch);
176 branch = resolve_refdup("HEAD", 0, NULL, NULL);
177 if (!branch)
178 die("no current branch");
181 reflogs = read_complete_reflog(branch);
182 if (!reflogs || reflogs->nr == 0) {
183 char *b;
184 int ret = dwim_log(branch, strlen(branch),
185 NULL, &b);
186 if (ret > 1)
187 free(b);
188 else if (ret == 1) {
189 free_complete_reflog(reflogs);
190 free(branch);
191 branch = b;
192 reflogs = read_complete_reflog(branch);
195 if (!reflogs || reflogs->nr == 0) {
196 free_complete_reflog(reflogs);
197 free(branch);
198 return -1;
200 string_list_insert(&info->complete_reflogs, branch)->util
201 = reflogs;
203 free(branch);
205 CALLOC_ARRAY(commit_reflog, 1);
206 if (recno < 0) {
207 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
208 if (commit_reflog->recno < 0) {
209 free(commit_reflog);
210 return -1;
212 } else
213 commit_reflog->recno = reflogs->nr - recno - 1;
214 commit_reflog->selector = selector;
215 commit_reflog->reflogs = reflogs;
217 ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
218 info->logs[info->nr++] = commit_reflog;
220 return 0;
223 void get_reflog_selector(struct strbuf *sb,
224 struct reflog_walk_info *reflog_info,
225 const struct date_mode *dmode, int force_date,
226 int shorten)
228 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
229 struct reflog_info *info;
230 const char *printed_ref;
232 if (!commit_reflog)
233 return;
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;
244 strbuf_addf(sb, "%s@{", printed_ref);
245 if (commit_reflog->selector == SELECTOR_DATE ||
246 (commit_reflog->selector == SELECTOR_NONE && force_date)) {
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);
254 strbuf_addch(sb, '}');
257 void get_reflog_message(struct strbuf *sb,
258 struct reflog_walk_info *reflog_info)
260 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
261 struct reflog_info *info;
262 size_t len;
264 if (!commit_reflog)
265 return;
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);
274 const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
276 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
277 struct reflog_info *info;
279 if (!commit_reflog)
280 return NULL;
282 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
283 return info->email;
286 timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
288 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
289 struct reflog_info *info;
291 if (!commit_reflog)
292 return 0;
294 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
295 return info->timestamp;
298 void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
299 const struct date_mode *dmode, int force_date)
301 if (reflog_info && reflog_info->last_commit_reflog) {
302 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
303 struct reflog_info *info;
304 struct strbuf selector = STRBUF_INIT;
306 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
307 get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
308 if (oneline) {
309 printf("%s: %s", selector.buf, info->message);
311 else {
312 printf("Reflog: %s (%s)\nReflog message: %s",
313 selector.buf, info->email, info->message);
316 strbuf_release(&selector);
320 int reflog_walk_empty(struct reflog_walk_info *info)
322 return !info || !info->nr;
325 static struct commit *next_reflog_commit(struct commit_reflog *log)
327 for (; log->recno >= 0; log->recno--) {
328 struct reflog_info *entry = &log->reflogs->items[log->recno];
329 struct object *obj = parse_object(the_repository,
330 &entry->noid);
332 if (obj && obj->type == OBJ_COMMIT)
333 return (struct commit *)obj;
335 return NULL;
338 static timestamp_t log_timestamp(struct commit_reflog *log)
340 return log->reflogs->items[log->recno].timestamp;
343 struct commit *next_reflog_entry(struct reflog_walk_info *walk)
345 struct commit_reflog *best = NULL;
346 struct commit *best_commit = NULL;
347 size_t i;
349 for (i = 0; i < walk->nr; i++) {
350 struct commit_reflog *log = walk->logs[i];
351 struct commit *commit = next_reflog_commit(log);
353 if (!commit)
354 continue;
356 if (!best || log_timestamp(log) > log_timestamp(best)) {
357 best = log;
358 best_commit = commit;
362 if (best) {
363 best->recno--;
364 walk->last_commit_reflog = best;
365 return best_commit;
368 return NULL;