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