Merge branch 'mg/timestamp-t-fix' into maint
[git/debian.git] / reflog-walk.c
blob74ebe5148f6623f0bffd612262519266a8ab2cc4
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 const 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);
57 static struct complete_reflogs *read_complete_reflog(const char *ref)
59 struct complete_reflogs *reflogs =
60 xcalloc(1, sizeof(struct complete_reflogs));
61 reflogs->ref = xstrdup(ref);
62 for_each_reflog_ent(ref, read_one_reflog, reflogs);
63 if (reflogs->nr == 0) {
64 struct object_id oid;
65 const char *name;
66 void *name_to_free;
67 name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
68 oid.hash, NULL);
69 if (name) {
70 for_each_reflog_ent(name, read_one_reflog, reflogs);
71 free(name_to_free);
74 if (reflogs->nr == 0) {
75 char *refname = xstrfmt("refs/%s", ref);
76 for_each_reflog_ent(refname, read_one_reflog, reflogs);
77 if (reflogs->nr == 0) {
78 free(refname);
79 refname = xstrfmt("refs/heads/%s", ref);
80 for_each_reflog_ent(refname, read_one_reflog, reflogs);
82 free(refname);
84 return reflogs;
87 static int get_reflog_recno_by_time(struct complete_reflogs *array,
88 timestamp_t timestamp)
90 int i;
91 for (i = array->nr - 1; i >= 0; i--)
92 if (timestamp >= array->items[i].timestamp)
93 return i;
94 return -1;
97 struct commit_reflog {
98 int recno;
99 enum selector_type {
100 SELECTOR_NONE,
101 SELECTOR_INDEX,
102 SELECTOR_DATE
103 } selector;
104 struct complete_reflogs *reflogs;
107 struct reflog_walk_info {
108 struct commit_reflog **logs;
109 size_t nr, alloc;
110 struct string_list complete_reflogs;
111 struct commit_reflog *last_commit_reflog;
114 void init_reflog_walk(struct reflog_walk_info **info)
116 *info = xcalloc(1, sizeof(struct reflog_walk_info));
117 (*info)->complete_reflogs.strdup_strings = 1;
120 int add_reflog_for_walk(struct reflog_walk_info *info,
121 struct commit *commit, const char *name)
123 timestamp_t timestamp = 0;
124 int recno = -1;
125 struct string_list_item *item;
126 struct complete_reflogs *reflogs;
127 char *branch, *at = strchr(name, '@');
128 struct commit_reflog *commit_reflog;
129 enum selector_type selector = SELECTOR_NONE;
131 if (commit->object.flags & UNINTERESTING)
132 die ("Cannot walk reflogs for %s", name);
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);
142 selector = SELECTOR_DATE;
144 else
145 selector = SELECTOR_INDEX;
146 } else
147 recno = 0;
149 item = string_list_lookup(&info->complete_reflogs, branch);
150 if (item)
151 reflogs = item->util;
152 else {
153 if (*branch == '\0') {
154 struct object_id oid;
155 free(branch);
156 branch = resolve_refdup("HEAD", 0, oid.hash, NULL);
157 if (!branch)
158 die ("No current branch");
161 reflogs = read_complete_reflog(branch);
162 if (!reflogs || reflogs->nr == 0) {
163 struct object_id oid;
164 char *b;
165 int ret = dwim_log(branch, strlen(branch),
166 oid.hash, &b);
167 if (ret > 1)
168 free(b);
169 else if (ret == 1) {
170 free_complete_reflog(reflogs);
171 free(branch);
172 branch = b;
173 reflogs = read_complete_reflog(branch);
176 if (!reflogs || reflogs->nr == 0) {
177 free_complete_reflog(reflogs);
178 free(branch);
179 return -1;
181 string_list_insert(&info->complete_reflogs, branch)->util
182 = reflogs;
184 free(branch);
186 commit_reflog = xcalloc(1, sizeof(struct commit_reflog));
187 if (recno < 0) {
188 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
189 if (commit_reflog->recno < 0) {
190 free(commit_reflog);
191 return -1;
193 } else
194 commit_reflog->recno = reflogs->nr - recno - 1;
195 commit_reflog->selector = selector;
196 commit_reflog->reflogs = reflogs;
198 ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
199 info->logs[info->nr++] = commit_reflog;
201 return 0;
204 void get_reflog_selector(struct strbuf *sb,
205 struct reflog_walk_info *reflog_info,
206 const struct date_mode *dmode, int force_date,
207 int shorten)
209 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
210 struct reflog_info *info;
211 const char *printed_ref;
213 if (!commit_reflog)
214 return;
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;
225 strbuf_addf(sb, "%s@{", printed_ref);
226 if (commit_reflog->selector == SELECTOR_DATE ||
227 (commit_reflog->selector == SELECTOR_NONE && force_date)) {
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);
235 strbuf_addch(sb, '}');
238 void get_reflog_message(struct strbuf *sb,
239 struct reflog_walk_info *reflog_info)
241 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
242 struct reflog_info *info;
243 size_t len;
245 if (!commit_reflog)
246 return;
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);
255 const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
257 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
258 struct reflog_info *info;
260 if (!commit_reflog)
261 return NULL;
263 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
264 return info->email;
267 timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
269 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
270 struct reflog_info *info;
272 if (!commit_reflog)
273 return 0;
275 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
276 return info->timestamp;
279 void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
280 const struct date_mode *dmode, int force_date)
282 if (reflog_info && reflog_info->last_commit_reflog) {
283 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
284 struct reflog_info *info;
285 struct strbuf selector = STRBUF_INIT;
287 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
288 get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
289 if (oneline) {
290 printf("%s: %s", selector.buf, info->message);
292 else {
293 printf("Reflog: %s (%s)\nReflog message: %s",
294 selector.buf, info->email, info->message);
297 strbuf_release(&selector);
301 int reflog_walk_empty(struct reflog_walk_info *info)
303 return !info || !info->nr;
306 static struct commit *next_reflog_commit(struct commit_reflog *log)
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);
312 if (obj && obj->type == OBJ_COMMIT)
313 return (struct commit *)obj;
315 return NULL;
318 static timestamp_t log_timestamp(struct commit_reflog *log)
320 return log->reflogs->items[log->recno].timestamp;
323 struct commit *next_reflog_entry(struct reflog_walk_info *walk)
325 struct commit_reflog *best = NULL;
326 struct commit *best_commit = NULL;
327 size_t i;
329 for (i = 0; i < walk->nr; i++) {
330 struct commit_reflog *log = walk->logs[i];
331 struct commit *commit = next_reflog_commit(log);
333 if (!commit)
334 continue;
336 if (!best || log_timestamp(log) > log_timestamp(best)) {
337 best = log;
338 best_commit = commit;
342 if (best) {
343 best->recno--;
344 walk->last_commit_reflog = best;
345 return best_commit;
348 return NULL;