reflog-walk: don't free reflogs added to cache
[git/debian.git] / reflog-walk.c
blob2a43537326208343b6dea87b0136d782cd802b76
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 unsigned long 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, unsigned long 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 struct complete_reflogs *read_complete_reflog(const char *ref)
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);
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);
66 free(refname);
68 return reflogs;
71 static int get_reflog_recno_by_time(struct complete_reflogs *array,
72 unsigned long timestamp)
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;
81 struct commit_info_lifo {
82 struct commit_info {
83 struct commit *commit;
84 void *util;
85 } *items;
86 int nr, alloc;
89 static struct commit_info *get_commit_info(struct commit *commit,
90 struct commit_info_lifo *lifo, int pop)
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--;
104 return result;
106 return NULL;
109 static void add_commit_info(struct commit *commit, void *util,
110 struct commit_info_lifo *lifo)
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++;
120 struct 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;
130 struct reflog_walk_info {
131 struct commit_info_lifo reflogs;
132 struct string_list complete_reflogs;
133 struct commit_reflog *last_commit_reflog;
136 void init_reflog_walk(struct reflog_walk_info **info)
138 *info = xcalloc(1, sizeof(struct reflog_walk_info));
139 (*info)->complete_reflogs.strdup_strings = 1;
142 int add_reflog_for_walk(struct reflog_walk_info *info,
143 struct commit *commit, const char *name)
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;
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 struct object_id oid;
177 free(branch);
178 branch = resolve_refdup("HEAD", 0, oid.hash, NULL);
179 if (!branch)
180 die ("No current branch");
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);
196 free(branch);
197 branch = b;
198 reflogs = read_complete_reflog(branch);
201 if (!reflogs || reflogs->nr == 0) {
202 if (reflogs) {
203 free(reflogs->ref);
204 free(reflogs);
206 free(branch);
207 return -1;
209 string_list_insert(&info->complete_reflogs, branch)->util
210 = reflogs;
212 free(branch);
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;
221 } else
222 commit_reflog->recno = reflogs->nr - recno - 1;
223 commit_reflog->selector = selector;
224 commit_reflog->reflogs = reflogs;
226 add_commit_info(commit, commit_reflog, &info->reflogs);
227 return 0;
230 void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
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;
238 info->last_commit_reflog = NULL;
239 if (!commit_info)
240 return;
242 commit_reflog = commit_info->util;
243 if (commit_reflog->recno < 0) {
244 commit->parents = NULL;
245 return;
247 info->last_commit_reflog = commit_reflog;
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));
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);
263 if (!logobj || logobj->type != OBJ_COMMIT) {
264 commit_info->commit = NULL;
265 commit->parents = NULL;
266 return;
268 commit_info->commit = (struct commit *)logobj;
270 commit->parents = xcalloc(1, sizeof(struct commit_list));
271 commit->parents->item = commit_info->commit;
274 void 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)
279 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
280 struct reflog_info *info;
281 const char *printed_ref;
283 if (!commit_reflog)
284 return;
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;
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);
305 strbuf_addch(sb, '}');
308 void get_reflog_message(struct strbuf *sb,
309 struct reflog_walk_info *reflog_info)
311 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
312 struct reflog_info *info;
313 size_t len;
315 if (!commit_reflog)
316 return;
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);
325 const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
327 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
328 struct reflog_info *info;
330 if (!commit_reflog)
331 return NULL;
333 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
334 return info->email;
337 void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
338 const struct date_mode *dmode, int force_date)
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;
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);
350 else {
351 printf("Reflog: %s (%s)\nReflog message: %s",
352 selector.buf, info->email, info->message);
355 strbuf_release(&selector);