Merge branch 'jk/t5500-typofix'
[git.git] / reflog-walk.c
blob5f09552c5ca5e75fdeb2746b2b3d0c1dd57ac9d3
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 refs_for_each_reflog_ent(get_main_ref_store(the_repository), ref,
71 read_one_reflog, reflogs);
72 if (reflogs->nr == 0) {
73 const char *name;
74 void *name_to_free;
75 name = name_to_free = refs_resolve_refdup(get_main_ref_store(the_repository),
76 ref,
77 RESOLVE_REF_READING,
78 NULL, NULL);
79 if (name) {
80 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
81 name, read_one_reflog,
82 reflogs);
83 free(name_to_free);
86 if (reflogs->nr == 0) {
87 char *refname = xstrfmt("refs/%s", ref);
88 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
89 refname, read_one_reflog, reflogs);
90 if (reflogs->nr == 0) {
91 free(refname);
92 refname = xstrfmt("refs/heads/%s", ref);
93 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
94 refname, read_one_reflog,
95 reflogs);
97 free(refname);
99 return reflogs;
102 static int get_reflog_recno_by_time(struct complete_reflogs *array,
103 timestamp_t timestamp)
105 int i;
106 for (i = array->nr - 1; i >= 0; i--)
107 if (timestamp >= array->items[i].timestamp)
108 return i;
109 return -1;
112 struct commit_reflog {
113 int recno;
114 enum selector_type {
115 SELECTOR_NONE,
116 SELECTOR_INDEX,
117 SELECTOR_DATE
118 } selector;
119 struct complete_reflogs *reflogs;
122 struct reflog_walk_info {
123 struct commit_reflog **logs;
124 size_t nr, alloc;
125 struct string_list complete_reflogs;
126 struct commit_reflog *last_commit_reflog;
129 void init_reflog_walk(struct reflog_walk_info **info)
131 CALLOC_ARRAY(*info, 1);
132 (*info)->complete_reflogs.strdup_strings = 1;
135 void reflog_walk_info_release(struct reflog_walk_info *info)
137 size_t i;
139 if (!info)
140 return;
142 for (i = 0; i < info->nr; i++)
143 free(info->logs[i]);
144 string_list_clear_func(&info->complete_reflogs,
145 complete_reflogs_clear);
146 free(info->logs);
147 free(info);
150 int add_reflog_for_walk(struct reflog_walk_info *info,
151 struct commit *commit, const char *name)
153 timestamp_t timestamp = 0;
154 int recno = -1;
155 struct string_list_item *item;
156 struct complete_reflogs *reflogs;
157 char *branch, *at = strchr(name, '@');
158 struct commit_reflog *commit_reflog;
159 enum selector_type selector = SELECTOR_NONE;
161 if (commit->object.flags & UNINTERESTING)
162 die("cannot walk reflogs for %s", name);
164 branch = xstrdup(name);
165 if (at && at[1] == '{') {
166 char *ep;
167 branch[at - name] = '\0';
168 recno = strtoul(at + 2, &ep, 10);
169 if (*ep != '}') {
170 recno = -1;
171 timestamp = approxidate(at + 2);
172 selector = SELECTOR_DATE;
174 else
175 selector = SELECTOR_INDEX;
176 } else
177 recno = 0;
179 item = string_list_lookup(&info->complete_reflogs, branch);
180 if (item)
181 reflogs = item->util;
182 else {
183 if (*branch == '\0') {
184 free(branch);
185 branch = refs_resolve_refdup(get_main_ref_store(the_repository),
186 "HEAD", 0, NULL, NULL);
187 if (!branch)
188 die("no current branch");
191 reflogs = read_complete_reflog(branch);
192 if (!reflogs || reflogs->nr == 0) {
193 char *b;
194 int ret = repo_dwim_log(the_repository, branch, strlen(branch),
195 NULL, &b);
196 if (ret > 1)
197 free(b);
198 else if (ret == 1) {
199 free_complete_reflog(reflogs);
200 free(branch);
201 branch = b;
202 reflogs = read_complete_reflog(branch);
205 if (!reflogs || reflogs->nr == 0) {
206 free_complete_reflog(reflogs);
207 free(branch);
208 return -1;
210 string_list_insert(&info->complete_reflogs, branch)->util
211 = reflogs;
213 free(branch);
215 CALLOC_ARRAY(commit_reflog, 1);
216 if (recno < 0) {
217 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
218 if (commit_reflog->recno < 0) {
219 free(commit_reflog);
220 return -1;
222 } else
223 commit_reflog->recno = reflogs->nr - recno - 1;
224 commit_reflog->selector = selector;
225 commit_reflog->reflogs = reflogs;
227 ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
228 info->logs[info->nr++] = commit_reflog;
230 return 0;
233 void get_reflog_selector(struct strbuf *sb,
234 struct reflog_walk_info *reflog_info,
235 struct date_mode dmode, int force_date,
236 int shorten)
238 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
239 struct reflog_info *info;
240 const char *printed_ref;
242 if (!commit_reflog)
243 return;
245 if (shorten) {
246 if (!commit_reflog->reflogs->short_ref)
247 commit_reflog->reflogs->short_ref
248 = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
249 commit_reflog->reflogs->ref,
251 printed_ref = commit_reflog->reflogs->short_ref;
252 } else {
253 printed_ref = commit_reflog->reflogs->ref;
256 strbuf_addf(sb, "%s@{", printed_ref);
257 if (commit_reflog->selector == SELECTOR_DATE ||
258 (commit_reflog->selector == SELECTOR_NONE && force_date)) {
259 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
260 strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
261 } else {
262 strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
263 - 2 - commit_reflog->recno);
266 strbuf_addch(sb, '}');
269 void get_reflog_message(struct strbuf *sb,
270 struct reflog_walk_info *reflog_info)
272 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
273 struct reflog_info *info;
274 size_t len;
276 if (!commit_reflog)
277 return;
279 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
280 len = strlen(info->message);
281 if (len > 0)
282 len--; /* strip away trailing newline */
283 strbuf_add(sb, info->message, len);
286 const char *get_reflog_ident(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 NULL;
294 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
295 return info->email;
298 timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
300 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
301 struct reflog_info *info;
303 if (!commit_reflog)
304 return 0;
306 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
307 return info->timestamp;
310 void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
311 struct date_mode dmode, int force_date)
313 if (reflog_info && reflog_info->last_commit_reflog) {
314 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
315 struct reflog_info *info;
316 struct strbuf selector = STRBUF_INIT;
318 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
319 get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
320 if (oneline) {
321 printf("%s: %s", selector.buf, info->message);
323 else {
324 printf("Reflog: %s (%s)\nReflog message: %s",
325 selector.buf, info->email, info->message);
328 strbuf_release(&selector);
332 int reflog_walk_empty(struct reflog_walk_info *info)
334 return !info || !info->nr;
337 static struct commit *next_reflog_commit(struct commit_reflog *log)
339 for (; log->recno >= 0; log->recno--) {
340 struct reflog_info *entry = &log->reflogs->items[log->recno];
341 struct object *obj = parse_object(the_repository,
342 &entry->noid);
344 if (obj && obj->type == OBJ_COMMIT)
345 return (struct commit *)obj;
347 return NULL;
350 static timestamp_t log_timestamp(struct commit_reflog *log)
352 return log->reflogs->items[log->recno].timestamp;
355 struct commit *next_reflog_entry(struct reflog_walk_info *walk)
357 struct commit_reflog *best = NULL;
358 struct commit *best_commit = NULL;
359 size_t i;
361 for (i = 0; i < walk->nr; i++) {
362 struct commit_reflog *log = walk->logs[i];
363 struct commit *commit = next_reflog_commit(log);
365 if (!commit)
366 continue;
368 if (!best || log_timestamp(log) > log_timestamp(best)) {
369 best = log;
370 best_commit = commit;
374 if (best) {
375 best->recno--;
376 walk->last_commit_reflog = best;
377 return best_commit;
380 return NULL;