]> git.ipfire.org Git - thirdparty/git.git/blame - reflog-walk.c
reflog-walk: don't free reflogs added to cache
[thirdparty/git.git] / reflog-walk.c
CommitLineData
8860fd42
JS
1#include "cache.h"
2#include "commit.h"
3#include "refs.h"
4#include "diff.h"
5#include "revision.h"
c455c87c 6#include "string-list.h"
53645a3a 7#include "reflog-walk.h"
8860fd42
JS
8
9struct complete_reflogs {
10 char *ref;
8f8f5476 11 const char *short_ref;
8860fd42 12 struct reflog_info {
8ebc3fd0 13 struct object_id ooid, noid;
8860fd42
JS
14 char *email;
15 unsigned long timestamp;
16 int tz;
17 char *message;
18 } *items;
19 int nr, alloc;
20};
21
9461d272 22static int read_one_reflog(struct object_id *ooid, struct object_id *noid,
8860fd42
JS
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
6647cc26 29 ALLOC_GROW(array->items, array->nr + 1, array->alloc);
8860fd42 30 item = array->items + array->nr;
9461d272 31 oidcpy(&item->ooid, ooid);
32 oidcpy(&item->noid, noid);
8860fd42
JS
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 =
8e1aa2f7 44 xcalloc(1, sizeof(struct complete_reflogs));
8860fd42
JS
45 reflogs->ref = xstrdup(ref);
46 for_each_reflog_ent(ref, read_one_reflog, reflogs);
47 if (reflogs->nr == 0) {
2928325f 48 struct object_id oid;
96ec7b1e
NTND
49 const char *name;
50 void *name_to_free;
7695d118 51 name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
2928325f 52 oid.hash, NULL);
d5a35c11 53 if (name) {
8860fd42 54 for_each_reflog_ent(name, read_one_reflog, reflogs);
96ec7b1e 55 free(name_to_free);
d5a35c11 56 }
8860fd42
JS
57 }
58 if (reflogs->nr == 0) {
75faa45a 59 char *refname = xstrfmt("refs/%s", ref);
8860fd42
JS
60 for_each_reflog_ent(refname, read_one_reflog, reflogs);
61 if (reflogs->nr == 0) {
75faa45a
JK
62 free(refname);
63 refname = xstrfmt("refs/heads/%s", ref);
8860fd42
JS
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;
40ab7c33 75 for (i = array->nr - 1; i >= 0; i--)
8860fd42
JS
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;
6647cc26 113 ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
8860fd42
JS
114 info = lifo->items + lifo->nr;
115 info->commit = commit;
116 info->util = util;
117 lifo->nr++;
118}
119
120struct commit_reflog {
a763126b
JK
121 int recno;
122 enum selector_type {
123 SELECTOR_NONE,
124 SELECTOR_INDEX,
125 SELECTOR_DATE
126 } selector;
8860fd42
JS
127 struct complete_reflogs *reflogs;
128};
129
130struct reflog_walk_info {
131 struct commit_info_lifo reflogs;
c455c87c 132 struct string_list complete_reflogs;
8860fd42
JS
133 struct commit_reflog *last_commit_reflog;
134};
135
24d36f14 136void init_reflog_walk(struct reflog_walk_info **info)
8860fd42 137{
8e1aa2f7 138 *info = xcalloc(1, sizeof(struct reflog_walk_info));
75afe7ac 139 (*info)->complete_reflogs.strdup_strings = 1;
8860fd42
JS
140}
141
7b69b873 142int add_reflog_for_walk(struct reflog_walk_info *info,
8860fd42
JS
143 struct commit *commit, const char *name)
144{
145 unsigned long timestamp = 0;
146 int recno = -1;
c455c87c 147 struct string_list_item *item;
8860fd42
JS
148 struct complete_reflogs *reflogs;
149 char *branch, *at = strchr(name, '@');
150 struct commit_reflog *commit_reflog;
a763126b 151 enum selector_type selector = SELECTOR_NONE;
8860fd42 152
db055e65
JS
153 if (commit->object.flags & UNINTERESTING)
154 die ("Cannot walk reflogs for %s", name);
155
8860fd42
JS
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);
a763126b 164 selector = SELECTOR_DATE;
8860fd42 165 }
a763126b
JK
166 else
167 selector = SELECTOR_INDEX;
8860fd42
JS
168 } else
169 recno = 0;
170
e8c8b713 171 item = string_list_lookup(&info->complete_reflogs, branch);
8860fd42
JS
172 if (item)
173 reflogs = item->util;
174 else {
d271fd53 175 if (*branch == '\0') {
2928325f 176 struct object_id oid;
d271fd53 177 free(branch);
2928325f 178 branch = resolve_refdup("HEAD", 0, oid.hash, NULL);
96ec7b1e
NTND
179 if (!branch)
180 die ("No current branch");
181
d271fd53 182 }
8860fd42 183 reflogs = read_complete_reflog(branch);
eb3a4822 184 if (!reflogs || reflogs->nr == 0) {
2928325f 185 struct object_id oid;
eb3a4822 186 char *b;
5026b471
JS
187 int ret = dwim_log(branch, strlen(branch),
188 oid.hash, &b);
189 if (ret > 1)
190 free(b);
191 else if (ret == 1) {
eb3a4822
JS
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 }
5026b471
JS
201 if (!reflogs || reflogs->nr == 0) {
202 if (reflogs) {
203 free(reflogs->ref);
204 free(reflogs);
205 }
206 free(branch);
7b69b873 207 return -1;
5026b471 208 }
78a395d3 209 string_list_insert(&info->complete_reflogs, branch)->util
8860fd42
JS
210 = reflogs;
211 }
5026b471 212 free(branch);
8860fd42 213
8e1aa2f7 214 commit_reflog = xcalloc(1, sizeof(struct commit_reflog));
8860fd42 215 if (recno < 0) {
8860fd42
JS
216 commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
217 if (commit_reflog->recno < 0) {
8860fd42 218 free(commit_reflog);
7b69b873 219 return -1;
8860fd42
JS
220 }
221 } else
222 commit_reflog->recno = reflogs->nr - recno - 1;
a763126b 223 commit_reflog->selector = selector;
8860fd42
JS
224 commit_reflog->reflogs = reflogs;
225
226 add_commit_info(commit, commit_reflog, &info->reflogs);
7b69b873 227 return 0;
8860fd42
JS
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;
aecad374 235 struct object *logobj;
8860fd42
JS
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 }
8860fd42 247 info->last_commit_reflog = commit_reflog;
aecad374
DK
248
249 do {
250 reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
251 commit_reflog->recno--;
8ebc3fd0 252 logobj = parse_object(reflog->ooid.hash);
aecad374
DK
253 } while (commit_reflog->recno && (logobj && logobj->type != OBJ_COMMIT));
254
8ebc3fd0 255 if (!logobj && commit_reflog->recno >= 0 && is_null_sha1(reflog->ooid.hash)) {
71abeb75
SG
256 /* a root commit, but there are still more entries to show */
257 reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
8ebc3fd0 258 logobj = parse_object(reflog->noid.hash);
2272d3e5
JK
259 if (!logobj)
260 logobj = parse_object(reflog->ooid.hash);
71abeb75
SG
261 }
262
aecad374
DK
263 if (!logobj || logobj->type != OBJ_COMMIT) {
264 commit_info->commit = NULL;
8860fd42
JS
265 commit->parents = NULL;
266 return;
267 }
aecad374 268 commit_info->commit = (struct commit *)logobj;
8860fd42 269
8e1aa2f7 270 commit->parents = xcalloc(1, sizeof(struct commit_list));
8860fd42 271 commit->parents->item = commit_info->commit;
8860fd42
JS
272}
273
72b103fe
TR
274void get_reflog_selector(struct strbuf *sb,
275 struct reflog_walk_info *reflog_info,
a5481a6c 276 const struct date_mode *dmode, int force_date,
8f8f5476 277 int shorten)
72b103fe
TR
278{
279 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
280 struct reflog_info *info;
8f8f5476 281 const char *printed_ref;
72b103fe
TR
282
283 if (!commit_reflog)
284 return;
285
8f8f5476
TR
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);
794151e9 296 if (commit_reflog->selector == SELECTOR_DATE ||
55ccf85a 297 (commit_reflog->selector == SELECTOR_NONE && force_date)) {
72b103fe
TR
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
8f8f5476
TR
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
cd1957f5
JK
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
72b103fe 337void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
a5481a6c 338 const struct date_mode *dmode, int force_date)
8860fd42 339{
72b103fe
TR
340 if (reflog_info && reflog_info->last_commit_reflog) {
341 struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
8860fd42 342 struct reflog_info *info;
72b103fe 343 struct strbuf selector = STRBUF_INIT;
8860fd42 344
4d12a471 345 info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
55ccf85a 346 get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
4d12a471 347 if (oneline) {
72b103fe 348 printf("%s: %s", selector.buf, info->message);
4d12a471
JH
349 }
350 else {
72b103fe
TR
351 printf("Reflog: %s (%s)\nReflog message: %s",
352 selector.buf, info->email, info->message);
4d12a471 353 }
72b103fe
TR
354
355 strbuf_release(&selector);
8860fd42
JS
356 }
357}