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