]>
Commit | Line | Data |
---|---|---|
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 | |
9 | struct 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 | 22 | static 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 | ||
41 | static 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 | ||
71 | static 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 | ||
81 | struct commit_info_lifo { | |
82 | struct commit_info { | |
83 | struct commit *commit; | |
84 | void *util; | |
85 | } *items; | |
86 | int nr, alloc; | |
87 | }; | |
88 | ||
89 | static 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 | ||
109 | static 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 | ||
120 | struct 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 | ||
130 | struct 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 | 136 | void init_reflog_walk(struct reflog_walk_info **info) |
8860fd42 | 137 | { |
8e1aa2f7 | 138 | *info = xcalloc(1, sizeof(struct reflog_walk_info)); |
8860fd42 JS |
139 | } |
140 | ||
7b69b873 | 141 | int add_reflog_for_walk(struct reflog_walk_info *info, |
8860fd42 JS |
142 | struct commit *commit, const char *name) |
143 | { | |
144 | unsigned long timestamp = 0; | |
145 | int recno = -1; | |
c455c87c | 146 | struct string_list_item *item; |
8860fd42 JS |
147 | struct complete_reflogs *reflogs; |
148 | char *branch, *at = strchr(name, '@'); | |
149 | struct commit_reflog *commit_reflog; | |
a763126b | 150 | enum selector_type selector = SELECTOR_NONE; |
8860fd42 | 151 | |
db055e65 JS |
152 | if (commit->object.flags & UNINTERESTING) |
153 | die ("Cannot walk reflogs for %s", name); | |
154 | ||
8860fd42 JS |
155 | branch = xstrdup(name); |
156 | if (at && at[1] == '{') { | |
157 | char *ep; | |
158 | branch[at - name] = '\0'; | |
159 | recno = strtoul(at + 2, &ep, 10); | |
160 | if (*ep != '}') { | |
161 | recno = -1; | |
162 | timestamp = approxidate(at + 2); | |
a763126b | 163 | selector = SELECTOR_DATE; |
8860fd42 | 164 | } |
a763126b JK |
165 | else |
166 | selector = SELECTOR_INDEX; | |
8860fd42 JS |
167 | } else |
168 | recno = 0; | |
169 | ||
e8c8b713 | 170 | item = string_list_lookup(&info->complete_reflogs, branch); |
8860fd42 JS |
171 | if (item) |
172 | reflogs = item->util; | |
173 | else { | |
d271fd53 | 174 | if (*branch == '\0') { |
2928325f | 175 | struct object_id oid; |
d271fd53 | 176 | free(branch); |
2928325f | 177 | branch = resolve_refdup("HEAD", 0, oid.hash, NULL); |
96ec7b1e NTND |
178 | if (!branch) |
179 | die ("No current branch"); | |
180 | ||
d271fd53 | 181 | } |
8860fd42 | 182 | reflogs = read_complete_reflog(branch); |
eb3a4822 | 183 | if (!reflogs || reflogs->nr == 0) { |
2928325f | 184 | struct object_id oid; |
eb3a4822 | 185 | char *b; |
5026b471 JS |
186 | int ret = dwim_log(branch, strlen(branch), |
187 | oid.hash, &b); | |
188 | if (ret > 1) | |
189 | free(b); | |
190 | else if (ret == 1) { | |
eb3a4822 JS |
191 | if (reflogs) { |
192 | free(reflogs->ref); | |
193 | free(reflogs); | |
194 | } | |
195 | free(branch); | |
196 | branch = b; | |
197 | reflogs = read_complete_reflog(branch); | |
198 | } | |
199 | } | |
5026b471 JS |
200 | if (!reflogs || reflogs->nr == 0) { |
201 | if (reflogs) { | |
202 | free(reflogs->ref); | |
203 | free(reflogs); | |
204 | } | |
205 | free(branch); | |
7b69b873 | 206 | return -1; |
5026b471 | 207 | } |
78a395d3 | 208 | string_list_insert(&info->complete_reflogs, branch)->util |
8860fd42 JS |
209 | = reflogs; |
210 | } | |
5026b471 | 211 | free(branch); |
8860fd42 | 212 | |
8e1aa2f7 | 213 | commit_reflog = xcalloc(1, sizeof(struct commit_reflog)); |
8860fd42 | 214 | if (recno < 0) { |
8860fd42 JS |
215 | commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp); |
216 | if (commit_reflog->recno < 0) { | |
5026b471 JS |
217 | if (reflogs) { |
218 | free(reflogs->ref); | |
219 | free(reflogs); | |
220 | } | |
8860fd42 | 221 | free(commit_reflog); |
7b69b873 | 222 | return -1; |
8860fd42 JS |
223 | } |
224 | } else | |
225 | commit_reflog->recno = reflogs->nr - recno - 1; | |
a763126b | 226 | commit_reflog->selector = selector; |
8860fd42 JS |
227 | commit_reflog->reflogs = reflogs; |
228 | ||
229 | add_commit_info(commit, commit_reflog, &info->reflogs); | |
7b69b873 | 230 | return 0; |
8860fd42 JS |
231 | } |
232 | ||
233 | void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit) | |
234 | { | |
235 | struct commit_info *commit_info = | |
236 | get_commit_info(commit, &info->reflogs, 0); | |
237 | struct commit_reflog *commit_reflog; | |
aecad374 | 238 | struct object *logobj; |
8860fd42 JS |
239 | struct reflog_info *reflog; |
240 | ||
241 | info->last_commit_reflog = NULL; | |
242 | if (!commit_info) | |
243 | return; | |
244 | ||
245 | commit_reflog = commit_info->util; | |
246 | if (commit_reflog->recno < 0) { | |
247 | commit->parents = NULL; | |
248 | return; | |
249 | } | |
8860fd42 | 250 | info->last_commit_reflog = commit_reflog; |
aecad374 DK |
251 | |
252 | do { | |
253 | reflog = &commit_reflog->reflogs->items[commit_reflog->recno]; | |
254 | commit_reflog->recno--; | |
8ebc3fd0 | 255 | logobj = parse_object(reflog->ooid.hash); |
aecad374 DK |
256 | } while (commit_reflog->recno && (logobj && logobj->type != OBJ_COMMIT)); |
257 | ||
8ebc3fd0 | 258 | if (!logobj && commit_reflog->recno >= 0 && is_null_sha1(reflog->ooid.hash)) { |
71abeb75 SG |
259 | /* a root commit, but there are still more entries to show */ |
260 | reflog = &commit_reflog->reflogs->items[commit_reflog->recno]; | |
8ebc3fd0 | 261 | logobj = parse_object(reflog->noid.hash); |
71abeb75 SG |
262 | } |
263 | ||
aecad374 DK |
264 | if (!logobj || logobj->type != OBJ_COMMIT) { |
265 | commit_info->commit = NULL; | |
8860fd42 JS |
266 | commit->parents = NULL; |
267 | return; | |
268 | } | |
aecad374 | 269 | commit_info->commit = (struct commit *)logobj; |
8860fd42 | 270 | |
8e1aa2f7 | 271 | commit->parents = xcalloc(1, sizeof(struct commit_list)); |
8860fd42 | 272 | commit->parents->item = commit_info->commit; |
8860fd42 JS |
273 | } |
274 | ||
72b103fe TR |
275 | void get_reflog_selector(struct strbuf *sb, |
276 | struct reflog_walk_info *reflog_info, | |
a5481a6c | 277 | const struct date_mode *dmode, int force_date, |
8f8f5476 | 278 | int shorten) |
72b103fe TR |
279 | { |
280 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
281 | struct reflog_info *info; | |
8f8f5476 | 282 | const char *printed_ref; |
72b103fe TR |
283 | |
284 | if (!commit_reflog) | |
285 | return; | |
286 | ||
8f8f5476 TR |
287 | if (shorten) { |
288 | if (!commit_reflog->reflogs->short_ref) | |
289 | commit_reflog->reflogs->short_ref | |
290 | = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0); | |
291 | printed_ref = commit_reflog->reflogs->short_ref; | |
292 | } else { | |
293 | printed_ref = commit_reflog->reflogs->ref; | |
294 | } | |
295 | ||
296 | strbuf_addf(sb, "%s@{", printed_ref); | |
794151e9 | 297 | if (commit_reflog->selector == SELECTOR_DATE || |
55ccf85a | 298 | (commit_reflog->selector == SELECTOR_NONE && force_date)) { |
72b103fe TR |
299 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; |
300 | strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode)); | |
301 | } else { | |
302 | strbuf_addf(sb, "%d", commit_reflog->reflogs->nr | |
303 | - 2 - commit_reflog->recno); | |
304 | } | |
305 | ||
306 | strbuf_addch(sb, '}'); | |
307 | } | |
308 | ||
8f8f5476 TR |
309 | void get_reflog_message(struct strbuf *sb, |
310 | struct reflog_walk_info *reflog_info) | |
311 | { | |
312 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
313 | struct reflog_info *info; | |
314 | size_t len; | |
315 | ||
316 | if (!commit_reflog) | |
317 | return; | |
318 | ||
319 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; | |
320 | len = strlen(info->message); | |
321 | if (len > 0) | |
322 | len--; /* strip away trailing newline */ | |
323 | strbuf_add(sb, info->message, len); | |
324 | } | |
325 | ||
cd1957f5 JK |
326 | const char *get_reflog_ident(struct reflog_walk_info *reflog_info) |
327 | { | |
328 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
329 | struct reflog_info *info; | |
330 | ||
331 | if (!commit_reflog) | |
332 | return NULL; | |
333 | ||
334 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; | |
335 | return info->email; | |
336 | } | |
337 | ||
72b103fe | 338 | void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline, |
a5481a6c | 339 | const struct date_mode *dmode, int force_date) |
8860fd42 | 340 | { |
72b103fe TR |
341 | if (reflog_info && reflog_info->last_commit_reflog) { |
342 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
8860fd42 | 343 | struct reflog_info *info; |
72b103fe | 344 | struct strbuf selector = STRBUF_INIT; |
8860fd42 | 345 | |
4d12a471 | 346 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; |
55ccf85a | 347 | get_reflog_selector(&selector, reflog_info, dmode, force_date, 0); |
4d12a471 | 348 | if (oneline) { |
72b103fe | 349 | printf("%s: %s", selector.buf, info->message); |
4d12a471 JH |
350 | } |
351 | else { | |
72b103fe TR |
352 | printf("Reflog: %s (%s)\nReflog message: %s", |
353 | selector.buf, info->email, info->message); | |
4d12a471 | 354 | } |
72b103fe TR |
355 | |
356 | strbuf_release(&selector); | |
8860fd42 JS |
357 | } |
358 | } |