]>
Commit | Line | Data |
---|---|---|
36bf1958 | 1 | #include "git-compat-util.h" |
8860fd42 JS |
2 | #include "commit.h" |
3 | #include "refs.h" | |
4 | #include "diff.h" | |
df6e8744 | 5 | #include "repository.h" |
8860fd42 | 6 | #include "revision.h" |
c455c87c | 7 | #include "string-list.h" |
53645a3a | 8 | #include "reflog-walk.h" |
8860fd42 JS |
9 | |
10 | struct complete_reflogs { | |
11 | char *ref; | |
81ffbf83 | 12 | char *short_ref; |
8860fd42 | 13 | struct reflog_info { |
8ebc3fd0 | 14 | struct object_id ooid, noid; |
8860fd42 | 15 | char *email; |
dddbad72 | 16 | timestamp_t timestamp; |
8860fd42 JS |
17 | int tz; |
18 | char *message; | |
19 | } *items; | |
20 | int nr, alloc; | |
21 | }; | |
22 | ||
9461d272 | 23 | static int read_one_reflog(struct object_id *ooid, struct object_id *noid, |
dddbad72 | 24 | const char *email, timestamp_t timestamp, int tz, |
8860fd42 JS |
25 | const char *message, void *cb_data) |
26 | { | |
27 | struct complete_reflogs *array = cb_data; | |
28 | struct reflog_info *item; | |
29 | ||
6647cc26 | 30 | ALLOC_GROW(array->items, array->nr + 1, array->alloc); |
8860fd42 | 31 | item = array->items + array->nr; |
9461d272 | 32 | oidcpy(&item->ooid, ooid); |
33 | oidcpy(&item->noid, noid); | |
8860fd42 JS |
34 | item->email = xstrdup(email); |
35 | item->timestamp = timestamp; | |
36 | item->tz = tz; | |
37 | item->message = xstrdup(message); | |
38 | array->nr++; | |
39 | return 0; | |
40 | } | |
41 | ||
e30d463d JK |
42 | static void free_complete_reflog(struct complete_reflogs *array) |
43 | { | |
44 | int i; | |
45 | ||
46 | if (!array) | |
47 | return; | |
48 | ||
49 | for (i = 0; i < array->nr; i++) { | |
50 | free(array->items[i].email); | |
51 | free(array->items[i].message); | |
52 | } | |
53 | free(array->items); | |
54 | free(array->ref); | |
81ffbf83 | 55 | free(array->short_ref); |
e30d463d JK |
56 | free(array); |
57 | } | |
58 | ||
1ee34710 | 59 | static void complete_reflogs_clear(void *util, const char *str UNUSED) |
81ffbf83 ÆAB |
60 | { |
61 | struct complete_reflogs *array = util; | |
62 | free_complete_reflog(array); | |
63 | } | |
64 | ||
8860fd42 JS |
65 | static struct complete_reflogs *read_complete_reflog(const char *ref) |
66 | { | |
67 | struct complete_reflogs *reflogs = | |
8e1aa2f7 | 68 | xcalloc(1, sizeof(struct complete_reflogs)); |
8860fd42 JS |
69 | reflogs->ref = xstrdup(ref); |
70 | for_each_reflog_ent(ref, read_one_reflog, reflogs); | |
71 | if (reflogs->nr == 0) { | |
96ec7b1e NTND |
72 | const char *name; |
73 | void *name_to_free; | |
7695d118 | 74 | name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING, |
efbd4fdf | 75 | NULL, NULL); |
d5a35c11 | 76 | if (name) { |
8860fd42 | 77 | for_each_reflog_ent(name, read_one_reflog, reflogs); |
96ec7b1e | 78 | free(name_to_free); |
d5a35c11 | 79 | } |
8860fd42 JS |
80 | } |
81 | if (reflogs->nr == 0) { | |
75faa45a | 82 | char *refname = xstrfmt("refs/%s", ref); |
8860fd42 JS |
83 | for_each_reflog_ent(refname, read_one_reflog, reflogs); |
84 | if (reflogs->nr == 0) { | |
75faa45a JK |
85 | free(refname); |
86 | refname = xstrfmt("refs/heads/%s", ref); | |
8860fd42 JS |
87 | for_each_reflog_ent(refname, read_one_reflog, reflogs); |
88 | } | |
89 | free(refname); | |
90 | } | |
91 | return reflogs; | |
92 | } | |
93 | ||
94 | static int get_reflog_recno_by_time(struct complete_reflogs *array, | |
dddbad72 | 95 | timestamp_t timestamp) |
8860fd42 JS |
96 | { |
97 | int i; | |
40ab7c33 | 98 | for (i = array->nr - 1; i >= 0; i--) |
8860fd42 JS |
99 | if (timestamp >= array->items[i].timestamp) |
100 | return i; | |
101 | return -1; | |
102 | } | |
103 | ||
8860fd42 | 104 | struct commit_reflog { |
a763126b JK |
105 | int recno; |
106 | enum selector_type { | |
107 | SELECTOR_NONE, | |
108 | SELECTOR_INDEX, | |
109 | SELECTOR_DATE | |
110 | } selector; | |
8860fd42 JS |
111 | struct complete_reflogs *reflogs; |
112 | }; | |
113 | ||
114 | struct reflog_walk_info { | |
d08565bf JK |
115 | struct commit_reflog **logs; |
116 | size_t nr, alloc; | |
c455c87c | 117 | struct string_list complete_reflogs; |
8860fd42 JS |
118 | struct commit_reflog *last_commit_reflog; |
119 | }; | |
120 | ||
24d36f14 | 121 | void init_reflog_walk(struct reflog_walk_info **info) |
8860fd42 | 122 | { |
ca56dadb | 123 | CALLOC_ARRAY(*info, 1); |
75afe7ac | 124 | (*info)->complete_reflogs.strdup_strings = 1; |
8860fd42 JS |
125 | } |
126 | ||
81ffbf83 ÆAB |
127 | void reflog_walk_info_release(struct reflog_walk_info *info) |
128 | { | |
129 | size_t i; | |
130 | ||
131 | if (!info) | |
132 | return; | |
133 | ||
134 | for (i = 0; i < info->nr; i++) | |
135 | free(info->logs[i]); | |
136 | string_list_clear_func(&info->complete_reflogs, | |
137 | complete_reflogs_clear); | |
138 | free(info->logs); | |
139 | free(info); | |
140 | } | |
141 | ||
7b69b873 | 142 | int add_reflog_for_walk(struct reflog_walk_info *info, |
8860fd42 JS |
143 | struct commit *commit, const char *name) |
144 | { | |
dddbad72 | 145 | timestamp_t timestamp = 0; |
8860fd42 | 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 | 153 | if (commit->object.flags & UNINTERESTING) |
1a07e59c | 154 | die("cannot walk reflogs for %s", name); |
db055e65 | 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') { |
d271fd53 | 176 | free(branch); |
efbd4fdf | 177 | branch = resolve_refdup("HEAD", 0, NULL, NULL); |
96ec7b1e | 178 | if (!branch) |
1a07e59c | 179 | die("no current branch"); |
96ec7b1e | 180 | |
d271fd53 | 181 | } |
8860fd42 | 182 | reflogs = read_complete_reflog(branch); |
eb3a4822 | 183 | if (!reflogs || reflogs->nr == 0) { |
eb3a4822 | 184 | char *b; |
5026b471 | 185 | int ret = dwim_log(branch, strlen(branch), |
6f45ec88 | 186 | NULL, &b); |
5026b471 JS |
187 | if (ret > 1) |
188 | free(b); | |
189 | else if (ret == 1) { | |
e30d463d | 190 | free_complete_reflog(reflogs); |
eb3a4822 JS |
191 | free(branch); |
192 | branch = b; | |
193 | reflogs = read_complete_reflog(branch); | |
194 | } | |
195 | } | |
5026b471 | 196 | if (!reflogs || reflogs->nr == 0) { |
e30d463d | 197 | free_complete_reflog(reflogs); |
5026b471 | 198 | free(branch); |
7b69b873 | 199 | return -1; |
5026b471 | 200 | } |
78a395d3 | 201 | string_list_insert(&info->complete_reflogs, branch)->util |
8860fd42 JS |
202 | = reflogs; |
203 | } | |
5026b471 | 204 | free(branch); |
8860fd42 | 205 | |
ca56dadb | 206 | CALLOC_ARRAY(commit_reflog, 1); |
8860fd42 | 207 | if (recno < 0) { |
8860fd42 JS |
208 | commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp); |
209 | if (commit_reflog->recno < 0) { | |
8860fd42 | 210 | free(commit_reflog); |
7b69b873 | 211 | return -1; |
8860fd42 JS |
212 | } |
213 | } else | |
214 | commit_reflog->recno = reflogs->nr - recno - 1; | |
a763126b | 215 | commit_reflog->selector = selector; |
8860fd42 JS |
216 | commit_reflog->reflogs = reflogs; |
217 | ||
d08565bf JK |
218 | ALLOC_GROW(info->logs, info->nr + 1, info->alloc); |
219 | info->logs[info->nr++] = commit_reflog; | |
8860fd42 | 220 | |
d08565bf | 221 | return 0; |
8860fd42 JS |
222 | } |
223 | ||
72b103fe TR |
224 | void get_reflog_selector(struct strbuf *sb, |
225 | struct reflog_walk_info *reflog_info, | |
a5481a6c | 226 | const struct date_mode *dmode, int force_date, |
8f8f5476 | 227 | int shorten) |
72b103fe TR |
228 | { |
229 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
230 | struct reflog_info *info; | |
8f8f5476 | 231 | const char *printed_ref; |
72b103fe TR |
232 | |
233 | if (!commit_reflog) | |
234 | return; | |
235 | ||
8f8f5476 TR |
236 | if (shorten) { |
237 | if (!commit_reflog->reflogs->short_ref) | |
238 | commit_reflog->reflogs->short_ref | |
239 | = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0); | |
240 | printed_ref = commit_reflog->reflogs->short_ref; | |
241 | } else { | |
242 | printed_ref = commit_reflog->reflogs->ref; | |
243 | } | |
244 | ||
245 | strbuf_addf(sb, "%s@{", printed_ref); | |
794151e9 | 246 | if (commit_reflog->selector == SELECTOR_DATE || |
55ccf85a | 247 | (commit_reflog->selector == SELECTOR_NONE && force_date)) { |
72b103fe TR |
248 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; |
249 | strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode)); | |
250 | } else { | |
251 | strbuf_addf(sb, "%d", commit_reflog->reflogs->nr | |
252 | - 2 - commit_reflog->recno); | |
253 | } | |
254 | ||
255 | strbuf_addch(sb, '}'); | |
256 | } | |
257 | ||
8f8f5476 TR |
258 | void get_reflog_message(struct strbuf *sb, |
259 | struct reflog_walk_info *reflog_info) | |
260 | { | |
261 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
262 | struct reflog_info *info; | |
263 | size_t len; | |
264 | ||
265 | if (!commit_reflog) | |
266 | return; | |
267 | ||
268 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; | |
269 | len = strlen(info->message); | |
270 | if (len > 0) | |
271 | len--; /* strip away trailing newline */ | |
272 | strbuf_add(sb, info->message, len); | |
273 | } | |
274 | ||
cd1957f5 JK |
275 | const char *get_reflog_ident(struct reflog_walk_info *reflog_info) |
276 | { | |
277 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
278 | struct reflog_info *info; | |
279 | ||
280 | if (!commit_reflog) | |
281 | return NULL; | |
282 | ||
283 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; | |
284 | return info->email; | |
285 | } | |
286 | ||
de239446 JK |
287 | timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info) |
288 | { | |
289 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
290 | struct reflog_info *info; | |
291 | ||
292 | if (!commit_reflog) | |
293 | return 0; | |
294 | ||
295 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; | |
296 | return info->timestamp; | |
297 | } | |
298 | ||
72b103fe | 299 | void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline, |
a5481a6c | 300 | const struct date_mode *dmode, int force_date) |
8860fd42 | 301 | { |
72b103fe TR |
302 | if (reflog_info && reflog_info->last_commit_reflog) { |
303 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
8860fd42 | 304 | struct reflog_info *info; |
72b103fe | 305 | struct strbuf selector = STRBUF_INIT; |
8860fd42 | 306 | |
4d12a471 | 307 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; |
55ccf85a | 308 | get_reflog_selector(&selector, reflog_info, dmode, force_date, 0); |
4d12a471 | 309 | if (oneline) { |
72b103fe | 310 | printf("%s: %s", selector.buf, info->message); |
4d12a471 JH |
311 | } |
312 | else { | |
72b103fe TR |
313 | printf("Reflog: %s (%s)\nReflog message: %s", |
314 | selector.buf, info->email, info->message); | |
4d12a471 | 315 | } |
72b103fe TR |
316 | |
317 | strbuf_release(&selector); | |
8860fd42 JS |
318 | } |
319 | } | |
7f97de5e JK |
320 | |
321 | int reflog_walk_empty(struct reflog_walk_info *info) | |
322 | { | |
d08565bf JK |
323 | return !info || !info->nr; |
324 | } | |
325 | ||
326 | static struct commit *next_reflog_commit(struct commit_reflog *log) | |
327 | { | |
328 | for (; log->recno >= 0; log->recno--) { | |
329 | struct reflog_info *entry = &log->reflogs->items[log->recno]; | |
109cd76d SB |
330 | struct object *obj = parse_object(the_repository, |
331 | &entry->noid); | |
d08565bf JK |
332 | |
333 | if (obj && obj->type == OBJ_COMMIT) | |
334 | return (struct commit *)obj; | |
335 | } | |
336 | return NULL; | |
337 | } | |
338 | ||
339 | static timestamp_t log_timestamp(struct commit_reflog *log) | |
340 | { | |
341 | return log->reflogs->items[log->recno].timestamp; | |
342 | } | |
343 | ||
344 | struct commit *next_reflog_entry(struct reflog_walk_info *walk) | |
345 | { | |
346 | struct commit_reflog *best = NULL; | |
347 | struct commit *best_commit = NULL; | |
348 | size_t i; | |
349 | ||
350 | for (i = 0; i < walk->nr; i++) { | |
351 | struct commit_reflog *log = walk->logs[i]; | |
352 | struct commit *commit = next_reflog_commit(log); | |
353 | ||
354 | if (!commit) | |
355 | continue; | |
356 | ||
357 | if (!best || log_timestamp(log) > log_timestamp(best)) { | |
358 | best = log; | |
359 | best_commit = commit; | |
360 | } | |
361 | } | |
362 | ||
363 | if (best) { | |
364 | best->recno--; | |
365 | walk->last_commit_reflog = best; | |
366 | return best_commit; | |
367 | } | |
368 | ||
369 | return NULL; | |
7f97de5e | 370 | } |