date: make DATE_MODE thread-safe
[git.git] / reflog-walk.c
blob66484f4f32108a36923eb4c1e137b9395dfb7b40
1 #include "git-compat-util.h"
2 #include "commit.h"
3 #include "refs.h"
4 #include "diff.h"
5 #include "repository.h"
6 #include "revision.h"
7 #include "string-list.h"
8 #include "reflog-walk.h"
10 struct complete_reflogs {
11 char *ref;
12 char *short_ref;
13 struct reflog_info {
14 struct object_id ooid, noid;
15 char *email;
16 timestamp_t timestamp;
17 int tz;
18 char *message;
19 } *items;
20 int nr, alloc;
23 static int read_one_reflog(struct object_id *ooid, struct object_id *noid,
24 const char *email, timestamp_t timestamp, int tz,
25 const char *message, void *cb_data)
27 struct complete_reflogs *array = cb_data;
28 struct reflog_info *item;
30 ALLOC_GROW(array->items, array->nr + 1, array->alloc);
31 item = array->items + array->nr;
32 oidcpy(&item->ooid, ooid);
33 oidcpy(&item->noid, noid);
34 item->email = xstrdup(email);
35 item->timestamp = timestamp;
36 item->tz = tz;
37 item->message = xstrdup(message);
38 array->nr++;
39 return 0;
42 static void free_complete_reflog(struct complete_reflogs *array)
44 int i;
46 if (!array)
47 return;
49 for (i = 0; i < array->nr; i++) {
50 free(array->items[i].email);
51 free(array->items[i].message);
53 free(array->items);
54 free(array->ref);
55 free(array->short_ref);
56 free(array);
59 static void complete_reflogs_clear(void *util, const char *str UNUSED)
61 struct complete_reflogs *array = util;
62 free_complete_reflog(array);
65 static struct complete_reflogs *read_complete_reflog(const char *ref)
67 struct complete_reflogs *reflogs =
68 xcalloc(1, sizeof(struct complete_reflogs));
69 reflogs->ref = xstrdup(ref);
70 for_each_reflog_ent(ref, read_one_reflog, reflogs);
71 if (reflogs->nr == 0) {
72 const char *name;
73 void *name_to_free;
74 name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
75 NULL, NULL);
76 if (name) {
77 for_each_reflog_ent(name, read_one_reflog, reflogs);
78 free(name_to_free);
81 if (reflogs->nr == 0) {
82 char *refname = xstrfmt("refs/%s", ref);
83 for_each_reflog_ent(refname, read_one_reflog, reflogs);
84 if (reflogs->nr == 0) {
85 free(refname);
86 refname = xstrfmt("refs/heads/%s", ref);
87 for_each_reflog_ent(refname, read_one_reflog, reflogs);
89 free(refname);
91 return reflogs;
94 static int get_reflog_recno_by_time(struct complete_reflogs *array,
95 timestamp_t timestamp)
97 int i;
98 for (i = array->nr - 1; i >= 0; i--)
99 if (timestamp >= array->items[i].timestamp)
100 return i;
101 return -1;
104 struct commit_reflog {
105 int recno;
106 enum selector_type {
107 SELECTOR_NONE,
108 SELECTOR_INDEX,
109 SELECTOR_DATE
110 } selector;
111 struct complete_reflogs *reflogs;
114 struct reflog_walk_info {
115 struct commit_reflog **logs;
116 size_t nr, alloc;
117 struct string_list complete_reflogs;
118 struct commit_reflog *last_commit_reflog;
121 void init_reflog_walk(struct reflog_walk_info **info)
123 CALLOC_ARRAY(*info, 1);
124 (*info)->complete_reflogs.strdup_strings = 1;
127 void reflog_walk_info_release(struct reflog_walk_info *info)
129 size_t i;
131 if (!info)
132 return;
134 for (i = 0; i < info->nr; i++)
135 free(info->logs[i]);
136 string_list_clear_func(&info->complete_reflogs,
137 complete_reflogs_clear);
138 free(info->logs);
139 free(info);
142 int add_reflog_for_walk(struct reflog_walk_info *info,
143 struct commit *commit, const char *name)
145 timestamp_t timestamp = 0;
146 int recno = -1;
147 struct string_list_item *item;
148 struct complete_reflogs *reflogs;
149 char *branch, *at = strchr(name, '@');
150 struct commit_reflog *commit_reflog;
151 enum selector_type selector = SELECTOR_NONE;
153 if (commit->object.flags & UNINTERESTING)
154 die("cannot walk reflogs for %s", name);
156 branch = xstrdup(name);
157 if (at && at[1] == '{') {
158 char *ep;
159 branch[at - name] = '\0';
160 recno = strtoul(at + 2, &ep, 10);
161 if (*ep != '}') {
162 recno = -1;
163 timestamp = approxidate(at + 2);
164 selector = SELECTOR_DATE;
166 else
167 selector = SELECTOR_INDEX;
168 } else
169 recno = 0;
171 item = string_list_lookup(&info->complete_reflogs, branch);
172 if (item)
173 reflogs = item->util;
174 else {
175 if (*branch == '\0') {
176 free(branch);
177 branch = resolve_refdup("HEAD", 0, NULL, NULL);
178 if (!branch)
179 die("no current branch");
182 reflogs = read_complete_reflog(branch);
183 if (!reflogs || reflogs->nr == 0) {
184 char *b;
185 int ret = dwim_log(branch, strlen(branch),
186 NULL, &b);
187 if (ret > 1)
188 free(b);
189 else if (ret == 1) {
190 free_complete_reflog(reflogs);
191 free(branch);
192 branch = b;
193 reflogs = read_complete_reflog(branch);
196 if (!reflogs || reflogs->nr == 0) {
197 free_complete_reflog(reflogs);
198 free(branch);
199 return -1;
201 string_list_insert(&info->complete_reflogs, branch)->util
202 = reflogs;
204 free(branch);
206 CALLOC_ARRAY(commit_reflog, 1);
207 if (recno < 0) {
208 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
209 if (commit_reflog->recno < 0) {
210 free(commit_reflog);
211 return -1;
213 } else
214 commit_reflog->recno = reflogs->nr - recno - 1;
215 commit_reflog->selector = selector;
216 commit_reflog->reflogs = reflogs;
218 ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
219 info->logs[info->nr++] = commit_reflog;
221 return 0;
224 void get_reflog_selector(struct strbuf *sb,
225 struct reflog_walk_info *reflog_info,
226 struct date_mode dmode, int force_date,
227 int shorten)
229 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
230 struct reflog_info *info;
231 const char *printed_ref;
233 if (!commit_reflog)
234 return;
236 if (shorten) {
237 if (!commit_reflog->reflogs->short_ref)
238 commit_reflog->reflogs->short_ref
239 = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
240 printed_ref = commit_reflog->reflogs->short_ref;
241 } else {
242 printed_ref = commit_reflog->reflogs->ref;
245 strbuf_addf(sb, "%s@{", printed_ref);
246 if (commit_reflog->selector == SELECTOR_DATE ||
247 (commit_reflog->selector == SELECTOR_NONE && force_date)) {
248 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
249 strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
250 } else {
251 strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
252 - 2 - commit_reflog->recno);
255 strbuf_addch(sb, '}');
258 void get_reflog_message(struct strbuf *sb,
259 struct reflog_walk_info *reflog_info)
261 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
262 struct reflog_info *info;
263 size_t len;
265 if (!commit_reflog)
266 return;
268 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
269 len = strlen(info->message);
270 if (len > 0)
271 len--; /* strip away trailing newline */
272 strbuf_add(sb, info->message, len);
275 const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
277 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
278 struct reflog_info *info;
280 if (!commit_reflog)
281 return NULL;
283 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
284 return info->email;
287 timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
289 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
290 struct reflog_info *info;
292 if (!commit_reflog)
293 return 0;
295 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
296 return info->timestamp;
299 void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
300 struct date_mode dmode, int force_date)
302 if (reflog_info && reflog_info->last_commit_reflog) {
303 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
304 struct reflog_info *info;
305 struct strbuf selector = STRBUF_INIT;
307 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
308 get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
309 if (oneline) {
310 printf("%s: %s", selector.buf, info->message);
312 else {
313 printf("Reflog: %s (%s)\nReflog message: %s",
314 selector.buf, info->email, info->message);
317 strbuf_release(&selector);
321 int reflog_walk_empty(struct reflog_walk_info *info)
323 return !info || !info->nr;
326 static struct commit *next_reflog_commit(struct commit_reflog *log)
328 for (; log->recno >= 0; log->recno--) {
329 struct reflog_info *entry = &log->reflogs->items[log->recno];
330 struct object *obj = parse_object(the_repository,
331 &entry->noid);
333 if (obj && obj->type == OBJ_COMMIT)
334 return (struct commit *)obj;
336 return NULL;
339 static timestamp_t log_timestamp(struct commit_reflog *log)
341 return log->reflogs->items[log->recno].timestamp;
344 struct commit *next_reflog_entry(struct reflog_walk_info *walk)
346 struct commit_reflog *best = NULL;
347 struct commit *best_commit = NULL;
348 size_t i;
350 for (i = 0; i < walk->nr; i++) {
351 struct commit_reflog *log = walk->logs[i];
352 struct commit *commit = next_reflog_commit(log);
354 if (!commit)
355 continue;
357 if (!best || log_timestamp(log) > log_timestamp(best)) {
358 best = log;
359 best_commit = commit;
363 if (best) {
364 best->recno--;
365 walk->last_commit_reflog = best;
366 return best_commit;
369 return NULL;