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