]>
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 | 69 | reflogs->ref = xstrdup(ref); |
2e5c4758 PS |
70 | refs_for_each_reflog_ent(get_main_ref_store(the_repository), ref, |
71 | read_one_reflog, reflogs); | |
8860fd42 | 72 | if (reflogs->nr == 0) { |
96ec7b1e NTND |
73 | const char *name; |
74 | void *name_to_free; | |
2e5c4758 PS |
75 | name = name_to_free = refs_resolve_refdup(get_main_ref_store(the_repository), |
76 | ref, | |
77 | RESOLVE_REF_READING, | |
78 | NULL, NULL); | |
d5a35c11 | 79 | if (name) { |
2e5c4758 PS |
80 | refs_for_each_reflog_ent(get_main_ref_store(the_repository), |
81 | name, read_one_reflog, | |
82 | reflogs); | |
96ec7b1e | 83 | free(name_to_free); |
d5a35c11 | 84 | } |
8860fd42 JS |
85 | } |
86 | if (reflogs->nr == 0) { | |
75faa45a | 87 | char *refname = xstrfmt("refs/%s", ref); |
2e5c4758 PS |
88 | refs_for_each_reflog_ent(get_main_ref_store(the_repository), |
89 | refname, read_one_reflog, reflogs); | |
8860fd42 | 90 | if (reflogs->nr == 0) { |
75faa45a JK |
91 | free(refname); |
92 | refname = xstrfmt("refs/heads/%s", ref); | |
2e5c4758 PS |
93 | refs_for_each_reflog_ent(get_main_ref_store(the_repository), |
94 | refname, read_one_reflog, | |
95 | reflogs); | |
8860fd42 JS |
96 | } |
97 | free(refname); | |
98 | } | |
99 | return reflogs; | |
100 | } | |
101 | ||
102 | static int get_reflog_recno_by_time(struct complete_reflogs *array, | |
dddbad72 | 103 | timestamp_t timestamp) |
8860fd42 JS |
104 | { |
105 | int i; | |
40ab7c33 | 106 | for (i = array->nr - 1; i >= 0; i--) |
8860fd42 JS |
107 | if (timestamp >= array->items[i].timestamp) |
108 | return i; | |
109 | return -1; | |
110 | } | |
111 | ||
8860fd42 | 112 | struct commit_reflog { |
a763126b JK |
113 | int recno; |
114 | enum selector_type { | |
115 | SELECTOR_NONE, | |
116 | SELECTOR_INDEX, | |
117 | SELECTOR_DATE | |
118 | } selector; | |
8860fd42 JS |
119 | struct complete_reflogs *reflogs; |
120 | }; | |
121 | ||
122 | struct reflog_walk_info { | |
d08565bf JK |
123 | struct commit_reflog **logs; |
124 | size_t nr, alloc; | |
c455c87c | 125 | struct string_list complete_reflogs; |
8860fd42 JS |
126 | struct commit_reflog *last_commit_reflog; |
127 | }; | |
128 | ||
24d36f14 | 129 | void init_reflog_walk(struct reflog_walk_info **info) |
8860fd42 | 130 | { |
ca56dadb | 131 | CALLOC_ARRAY(*info, 1); |
75afe7ac | 132 | (*info)->complete_reflogs.strdup_strings = 1; |
8860fd42 JS |
133 | } |
134 | ||
81ffbf83 ÆAB |
135 | void reflog_walk_info_release(struct reflog_walk_info *info) |
136 | { | |
137 | size_t i; | |
138 | ||
139 | if (!info) | |
140 | return; | |
141 | ||
142 | for (i = 0; i < info->nr; i++) | |
143 | free(info->logs[i]); | |
144 | string_list_clear_func(&info->complete_reflogs, | |
145 | complete_reflogs_clear); | |
146 | free(info->logs); | |
147 | free(info); | |
148 | } | |
149 | ||
7b69b873 | 150 | int add_reflog_for_walk(struct reflog_walk_info *info, |
8860fd42 JS |
151 | struct commit *commit, const char *name) |
152 | { | |
dddbad72 | 153 | timestamp_t timestamp = 0; |
8860fd42 | 154 | int recno = -1; |
c455c87c | 155 | struct string_list_item *item; |
8860fd42 JS |
156 | struct complete_reflogs *reflogs; |
157 | char *branch, *at = strchr(name, '@'); | |
158 | struct commit_reflog *commit_reflog; | |
a763126b | 159 | enum selector_type selector = SELECTOR_NONE; |
8860fd42 | 160 | |
db055e65 | 161 | if (commit->object.flags & UNINTERESTING) |
1a07e59c | 162 | die("cannot walk reflogs for %s", name); |
db055e65 | 163 | |
8860fd42 JS |
164 | branch = xstrdup(name); |
165 | if (at && at[1] == '{') { | |
166 | char *ep; | |
167 | branch[at - name] = '\0'; | |
168 | recno = strtoul(at + 2, &ep, 10); | |
169 | if (*ep != '}') { | |
170 | recno = -1; | |
171 | timestamp = approxidate(at + 2); | |
a763126b | 172 | selector = SELECTOR_DATE; |
8860fd42 | 173 | } |
a763126b JK |
174 | else |
175 | selector = SELECTOR_INDEX; | |
8860fd42 JS |
176 | } else |
177 | recno = 0; | |
178 | ||
e8c8b713 | 179 | item = string_list_lookup(&info->complete_reflogs, branch); |
8860fd42 JS |
180 | if (item) |
181 | reflogs = item->util; | |
182 | else { | |
d271fd53 | 183 | if (*branch == '\0') { |
d271fd53 | 184 | free(branch); |
2e5c4758 PS |
185 | branch = refs_resolve_refdup(get_main_ref_store(the_repository), |
186 | "HEAD", 0, NULL, NULL); | |
96ec7b1e | 187 | if (!branch) |
1a07e59c | 188 | die("no current branch"); |
96ec7b1e | 189 | |
d271fd53 | 190 | } |
8860fd42 | 191 | reflogs = read_complete_reflog(branch); |
eb3a4822 | 192 | if (!reflogs || reflogs->nr == 0) { |
eb3a4822 | 193 | char *b; |
5026b471 | 194 | int ret = dwim_log(branch, strlen(branch), |
6f45ec88 | 195 | NULL, &b); |
5026b471 JS |
196 | if (ret > 1) |
197 | free(b); | |
198 | else if (ret == 1) { | |
e30d463d | 199 | free_complete_reflog(reflogs); |
eb3a4822 JS |
200 | free(branch); |
201 | branch = b; | |
202 | reflogs = read_complete_reflog(branch); | |
203 | } | |
204 | } | |
5026b471 | 205 | if (!reflogs || reflogs->nr == 0) { |
e30d463d | 206 | free_complete_reflog(reflogs); |
5026b471 | 207 | free(branch); |
7b69b873 | 208 | return -1; |
5026b471 | 209 | } |
78a395d3 | 210 | string_list_insert(&info->complete_reflogs, branch)->util |
8860fd42 JS |
211 | = reflogs; |
212 | } | |
5026b471 | 213 | free(branch); |
8860fd42 | 214 | |
ca56dadb | 215 | CALLOC_ARRAY(commit_reflog, 1); |
8860fd42 | 216 | if (recno < 0) { |
8860fd42 JS |
217 | commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp); |
218 | if (commit_reflog->recno < 0) { | |
8860fd42 | 219 | free(commit_reflog); |
7b69b873 | 220 | return -1; |
8860fd42 JS |
221 | } |
222 | } else | |
223 | commit_reflog->recno = reflogs->nr - recno - 1; | |
a763126b | 224 | commit_reflog->selector = selector; |
8860fd42 JS |
225 | commit_reflog->reflogs = reflogs; |
226 | ||
d08565bf JK |
227 | ALLOC_GROW(info->logs, info->nr + 1, info->alloc); |
228 | info->logs[info->nr++] = commit_reflog; | |
8860fd42 | 229 | |
d08565bf | 230 | return 0; |
8860fd42 JS |
231 | } |
232 | ||
72b103fe TR |
233 | void get_reflog_selector(struct strbuf *sb, |
234 | struct reflog_walk_info *reflog_info, | |
9720d23e | 235 | struct date_mode dmode, int force_date, |
8f8f5476 | 236 | int shorten) |
72b103fe TR |
237 | { |
238 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
239 | struct reflog_info *info; | |
8f8f5476 | 240 | const char *printed_ref; |
72b103fe TR |
241 | |
242 | if (!commit_reflog) | |
243 | return; | |
244 | ||
8f8f5476 TR |
245 | if (shorten) { |
246 | if (!commit_reflog->reflogs->short_ref) | |
247 | commit_reflog->reflogs->short_ref | |
2e5c4758 PS |
248 | = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), |
249 | commit_reflog->reflogs->ref, | |
250 | 0); | |
8f8f5476 TR |
251 | printed_ref = commit_reflog->reflogs->short_ref; |
252 | } else { | |
253 | printed_ref = commit_reflog->reflogs->ref; | |
254 | } | |
255 | ||
256 | strbuf_addf(sb, "%s@{", printed_ref); | |
794151e9 | 257 | if (commit_reflog->selector == SELECTOR_DATE || |
55ccf85a | 258 | (commit_reflog->selector == SELECTOR_NONE && force_date)) { |
72b103fe TR |
259 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; |
260 | strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode)); | |
261 | } else { | |
262 | strbuf_addf(sb, "%d", commit_reflog->reflogs->nr | |
263 | - 2 - commit_reflog->recno); | |
264 | } | |
265 | ||
266 | strbuf_addch(sb, '}'); | |
267 | } | |
268 | ||
8f8f5476 TR |
269 | void get_reflog_message(struct strbuf *sb, |
270 | struct reflog_walk_info *reflog_info) | |
271 | { | |
272 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
273 | struct reflog_info *info; | |
274 | size_t len; | |
275 | ||
276 | if (!commit_reflog) | |
277 | return; | |
278 | ||
279 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; | |
280 | len = strlen(info->message); | |
281 | if (len > 0) | |
282 | len--; /* strip away trailing newline */ | |
283 | strbuf_add(sb, info->message, len); | |
284 | } | |
285 | ||
cd1957f5 JK |
286 | const char *get_reflog_ident(struct reflog_walk_info *reflog_info) |
287 | { | |
288 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
289 | struct reflog_info *info; | |
290 | ||
291 | if (!commit_reflog) | |
292 | return NULL; | |
293 | ||
294 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; | |
295 | return info->email; | |
296 | } | |
297 | ||
de239446 JK |
298 | timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info) |
299 | { | |
300 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
301 | struct reflog_info *info; | |
302 | ||
303 | if (!commit_reflog) | |
304 | return 0; | |
305 | ||
306 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; | |
307 | return info->timestamp; | |
308 | } | |
309 | ||
72b103fe | 310 | void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline, |
9720d23e | 311 | struct date_mode dmode, int force_date) |
8860fd42 | 312 | { |
72b103fe TR |
313 | if (reflog_info && reflog_info->last_commit_reflog) { |
314 | struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog; | |
8860fd42 | 315 | struct reflog_info *info; |
72b103fe | 316 | struct strbuf selector = STRBUF_INIT; |
8860fd42 | 317 | |
4d12a471 | 318 | info = &commit_reflog->reflogs->items[commit_reflog->recno+1]; |
55ccf85a | 319 | get_reflog_selector(&selector, reflog_info, dmode, force_date, 0); |
4d12a471 | 320 | if (oneline) { |
72b103fe | 321 | printf("%s: %s", selector.buf, info->message); |
4d12a471 JH |
322 | } |
323 | else { | |
72b103fe TR |
324 | printf("Reflog: %s (%s)\nReflog message: %s", |
325 | selector.buf, info->email, info->message); | |
4d12a471 | 326 | } |
72b103fe TR |
327 | |
328 | strbuf_release(&selector); | |
8860fd42 JS |
329 | } |
330 | } | |
7f97de5e JK |
331 | |
332 | int reflog_walk_empty(struct reflog_walk_info *info) | |
333 | { | |
d08565bf JK |
334 | return !info || !info->nr; |
335 | } | |
336 | ||
337 | static struct commit *next_reflog_commit(struct commit_reflog *log) | |
338 | { | |
339 | for (; log->recno >= 0; log->recno--) { | |
340 | struct reflog_info *entry = &log->reflogs->items[log->recno]; | |
109cd76d SB |
341 | struct object *obj = parse_object(the_repository, |
342 | &entry->noid); | |
d08565bf JK |
343 | |
344 | if (obj && obj->type == OBJ_COMMIT) | |
345 | return (struct commit *)obj; | |
346 | } | |
347 | return NULL; | |
348 | } | |
349 | ||
350 | static timestamp_t log_timestamp(struct commit_reflog *log) | |
351 | { | |
352 | return log->reflogs->items[log->recno].timestamp; | |
353 | } | |
354 | ||
355 | struct commit *next_reflog_entry(struct reflog_walk_info *walk) | |
356 | { | |
357 | struct commit_reflog *best = NULL; | |
358 | struct commit *best_commit = NULL; | |
359 | size_t i; | |
360 | ||
361 | for (i = 0; i < walk->nr; i++) { | |
362 | struct commit_reflog *log = walk->logs[i]; | |
363 | struct commit *commit = next_reflog_commit(log); | |
364 | ||
365 | if (!commit) | |
366 | continue; | |
367 | ||
368 | if (!best || log_timestamp(log) > log_timestamp(best)) { | |
369 | best = log; | |
370 | best_commit = commit; | |
371 | } | |
372 | } | |
373 | ||
374 | if (best) { | |
375 | best->recno--; | |
376 | walk->last_commit_reflog = best; | |
377 | return best_commit; | |
378 | } | |
379 | ||
380 | return NULL; | |
7f97de5e | 381 | } |