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