Merge branch 'jk/c99'
[git] / reflog-walk.c
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"
8
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;
20 };
21
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)
25 {
26         struct complete_reflogs *array = cb_data;
27         struct reflog_info *item;
28
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;
39 }
40
41 static 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);
54         free(array);
55 }
56
57 static struct complete_reflogs *read_complete_reflog(const char *ref)
58 {
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);
72                 }
73         }
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);
81                 }
82                 free(refname);
83         }
84         return reflogs;
85 }
86
87 static int get_reflog_recno_by_time(struct complete_reflogs *array,
88         timestamp_t timestamp)
89 {
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;
95 }
96
97 struct commit_info_lifo {
98         struct commit_info {
99                 struct commit *commit;
100                 void *util;
101         } *items;
102         int nr, alloc;
103 };
104
105 static struct commit_info *get_commit_info(struct commit *commit,
106                 struct commit_info_lifo *lifo, int pop)
107 {
108         int i;
109         for (i = 0; i < lifo->nr; i++)
110                 if (lifo->items[i].commit == commit) {
111                         struct commit_info *result = &lifo->items[i];
112                         if (pop) {
113                                 if (i + 1 < lifo->nr)
114                                         MOVE_ARRAY(lifo->items + i,
115                                                    lifo->items + i + 1,
116                                                    lifo->nr - i);
117                                 lifo->nr--;
118                         }
119                         return result;
120                 }
121         return NULL;
122 }
123
124 static void add_commit_info(struct commit *commit, void *util,
125                 struct commit_info_lifo *lifo)
126 {
127         struct commit_info *info;
128         ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
129         info = lifo->items + lifo->nr;
130         info->commit = commit;
131         info->util = util;
132         lifo->nr++;
133 }
134
135 struct commit_reflog {
136         int recno;
137         enum selector_type {
138                 SELECTOR_NONE,
139                 SELECTOR_INDEX,
140                 SELECTOR_DATE
141         } selector;
142         struct complete_reflogs *reflogs;
143 };
144
145 struct reflog_walk_info {
146         struct commit_info_lifo reflogs;
147         struct string_list complete_reflogs;
148         struct commit_reflog *last_commit_reflog;
149 };
150
151 void init_reflog_walk(struct reflog_walk_info **info)
152 {
153         *info = xcalloc(1, sizeof(struct reflog_walk_info));
154         (*info)->complete_reflogs.strdup_strings = 1;
155 }
156
157 int add_reflog_for_walk(struct reflog_walk_info *info,
158                 struct commit *commit, const char *name)
159 {
160         timestamp_t timestamp = 0;
161         int recno = -1;
162         struct string_list_item *item;
163         struct complete_reflogs *reflogs;
164         char *branch, *at = strchr(name, '@');
165         struct commit_reflog *commit_reflog;
166         enum selector_type selector = SELECTOR_NONE;
167
168         if (commit->object.flags & UNINTERESTING)
169                 die ("Cannot walk reflogs for %s", name);
170
171         branch = xstrdup(name);
172         if (at && at[1] == '{') {
173                 char *ep;
174                 branch[at - name] = '\0';
175                 recno = strtoul(at + 2, &ep, 10);
176                 if (*ep != '}') {
177                         recno = -1;
178                         timestamp = approxidate(at + 2);
179                         selector = SELECTOR_DATE;
180                 }
181                 else
182                         selector = SELECTOR_INDEX;
183         } else
184                 recno = 0;
185
186         item = string_list_lookup(&info->complete_reflogs, branch);
187         if (item)
188                 reflogs = item->util;
189         else {
190                 if (*branch == '\0') {
191                         struct object_id oid;
192                         free(branch);
193                         branch = resolve_refdup("HEAD", 0, oid.hash, NULL);
194                         if (!branch)
195                                 die ("No current branch");
196
197                 }
198                 reflogs = read_complete_reflog(branch);
199                 if (!reflogs || reflogs->nr == 0) {
200                         struct object_id oid;
201                         char *b;
202                         int ret = dwim_log(branch, strlen(branch),
203                                            oid.hash, &b);
204                         if (ret > 1)
205                                 free(b);
206                         else if (ret == 1) {
207                                 free_complete_reflog(reflogs);
208                                 free(branch);
209                                 branch = b;
210                                 reflogs = read_complete_reflog(branch);
211                         }
212                 }
213                 if (!reflogs || reflogs->nr == 0) {
214                         free_complete_reflog(reflogs);
215                         free(branch);
216                         return -1;
217                 }
218                 string_list_insert(&info->complete_reflogs, branch)->util
219                         = reflogs;
220         }
221         free(branch);
222
223         commit_reflog = xcalloc(1, sizeof(struct commit_reflog));
224         if (recno < 0) {
225                 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
226                 if (commit_reflog->recno < 0) {
227                         free(commit_reflog);
228                         return -1;
229                 }
230         } else
231                 commit_reflog->recno = reflogs->nr - recno - 1;
232         commit_reflog->selector = selector;
233         commit_reflog->reflogs = reflogs;
234
235         add_commit_info(commit, commit_reflog, &info->reflogs);
236         return 0;
237 }
238
239 void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
240 {
241         struct commit_info *commit_info =
242                 get_commit_info(commit, &info->reflogs, 0);
243         struct commit_reflog *commit_reflog;
244         struct object *logobj;
245         struct reflog_info *reflog;
246
247         info->last_commit_reflog = NULL;
248         if (!commit_info)
249                 return;
250
251         commit_reflog = commit_info->util;
252         if (commit_reflog->recno < 0) {
253                 commit->parents = NULL;
254                 return;
255         }
256         info->last_commit_reflog = commit_reflog;
257
258         do {
259                 reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
260                 commit_reflog->recno--;
261                 logobj = parse_object(&reflog->ooid);
262         } while (commit_reflog->recno && (logobj && logobj->type != OBJ_COMMIT));
263
264         if (!logobj && commit_reflog->recno >= 0 && is_null_oid(&reflog->ooid)) {
265                 /* a root commit, but there are still more entries to show */
266                 reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
267                 logobj = parse_object(&reflog->noid);
268                 if (!logobj)
269                         logobj = parse_object(&reflog->ooid);
270         }
271
272         if (!logobj || logobj->type != OBJ_COMMIT) {
273                 commit_info->commit = NULL;
274                 commit->parents = NULL;
275                 return;
276         }
277         commit_info->commit = (struct commit *)logobj;
278
279         commit->parents = xcalloc(1, sizeof(struct commit_list));
280         commit->parents->item = commit_info->commit;
281 }
282
283 void get_reflog_selector(struct strbuf *sb,
284                          struct reflog_walk_info *reflog_info,
285                          const struct date_mode *dmode, int force_date,
286                          int shorten)
287 {
288         struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
289         struct reflog_info *info;
290         const char *printed_ref;
291
292         if (!commit_reflog)
293                 return;
294
295         if (shorten) {
296                 if (!commit_reflog->reflogs->short_ref)
297                         commit_reflog->reflogs->short_ref
298                                 = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
299                 printed_ref = commit_reflog->reflogs->short_ref;
300         } else {
301                 printed_ref = commit_reflog->reflogs->ref;
302         }
303
304         strbuf_addf(sb, "%s@{", printed_ref);
305         if (commit_reflog->selector == SELECTOR_DATE ||
306             (commit_reflog->selector == SELECTOR_NONE && force_date)) {
307                 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
308                 strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
309         } else {
310                 strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
311                             - 2 - commit_reflog->recno);
312         }
313
314         strbuf_addch(sb, '}');
315 }
316
317 void get_reflog_message(struct strbuf *sb,
318                         struct reflog_walk_info *reflog_info)
319 {
320         struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
321         struct reflog_info *info;
322         size_t len;
323
324         if (!commit_reflog)
325                 return;
326
327         info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
328         len = strlen(info->message);
329         if (len > 0)
330                 len--; /* strip away trailing newline */
331         strbuf_add(sb, info->message, len);
332 }
333
334 const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
335 {
336         struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
337         struct reflog_info *info;
338
339         if (!commit_reflog)
340                 return NULL;
341
342         info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
343         return info->email;
344 }
345
346 void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
347                          const struct date_mode *dmode, int force_date)
348 {
349         if (reflog_info && reflog_info->last_commit_reflog) {
350                 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
351                 struct reflog_info *info;
352                 struct strbuf selector = STRBUF_INIT;
353
354                 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
355                 get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
356                 if (oneline) {
357                         printf("%s: %s", selector.buf, info->message);
358                 }
359                 else {
360                         printf("Reflog: %s (%s)\nReflog message: %s",
361                                selector.buf, info->email, info->message);
362                 }
363
364                 strbuf_release(&selector);
365         }
366 }