refs.c: make close_ref() and commit_ref() non-static
[git/git.git] / builtin-reflog.c
CommitLineData
4264dc15
JH
1#include "cache.h"
2#include "builtin.h"
3#include "commit.h"
4#include "refs.h"
5#include "dir.h"
8d8b9f62 6#include "tree-walk.h"
1389d9dd
JH
7#include "diff.h"
8#include "revision.h"
9#include "reachable.h"
10
11/*
12 * reflog expire
13 */
14
15static const char reflog_expire_usage[] =
cf39f54e 16"git-reflog (show|expire) [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
3c386aa3 17static const char reflog_delete_usage[] =
2b81fab2 18"git-reflog delete [--verbose] [--dry-run] [--rewrite] <refs>...";
4264dc15 19
4aec56d1
JH
20static unsigned long default_reflog_expire;
21static unsigned long default_reflog_expire_unreachable;
22
1389d9dd
JH
23struct cmd_reflog_expire_cb {
24 struct rev_info revs;
25 int dry_run;
26 int stalefix;
2b81fab2 27 int rewrite;
1389d9dd
JH
28 int verbose;
29 unsigned long expire_total;
30 unsigned long expire_unreachable;
552cecc2 31 int recno;
1389d9dd
JH
32};
33
4264dc15
JH
34struct expire_reflog_cb {
35 FILE *newlog;
36 const char *ref;
37 struct commit *ref_commit;
1389d9dd 38 struct cmd_reflog_expire_cb *cmd;
2b81fab2 39 unsigned char last_kept_sha1[20];
4264dc15
JH
40};
41
bda3a31c
JH
42struct collected_reflog {
43 unsigned char sha1[20];
44 char reflog[FLEX_ARRAY];
45};
46struct collect_reflog_cb {
47 struct collected_reflog **e;
48 int alloc;
49 int nr;
50};
51
cd1f9c36
JH
52#define INCOMPLETE (1u<<10)
53#define STUDYING (1u<<11)
54
8d8b9f62
JH
55static int tree_is_complete(const unsigned char *sha1)
56{
57 struct tree_desc desc;
cd1f9c36
JH
58 struct name_entry entry;
59 int complete;
60 struct tree *tree;
8d8b9f62 61
cd1f9c36
JH
62 tree = lookup_tree(sha1);
63 if (!tree)
8d8b9f62 64 return 0;
cd1f9c36
JH
65 if (tree->object.flags & SEEN)
66 return 1;
67 if (tree->object.flags & INCOMPLETE)
68 return 0;
69
6fda5e51 70 if (!tree->buffer) {
21666f1a 71 enum object_type type;
6fda5e51
LT
72 unsigned long size;
73 void *data = read_sha1_file(sha1, &type, &size);
cd1f9c36
JH
74 if (!data) {
75 tree->object.flags |= INCOMPLETE;
8d8b9f62
JH
76 return 0;
77 }
cd1f9c36 78 tree->buffer = data;
6fda5e51 79 tree->size = size;
8d8b9f62 80 }
6fda5e51 81 init_tree_desc(&desc, tree->buffer, tree->size);
cd1f9c36
JH
82 complete = 1;
83 while (tree_entry(&desc, &entry)) {
84 if (!has_sha1_file(entry.sha1) ||
85 (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
86 tree->object.flags |= INCOMPLETE;
87 complete = 0;
88 }
89 }
90 free(tree->buffer);
91 tree->buffer = NULL;
8d8b9f62 92
cd1f9c36
JH
93 if (complete)
94 tree->object.flags |= SEEN;
95 return complete;
96}
1389d9dd
JH
97
98static int commit_is_complete(struct commit *commit)
99{
100 struct object_array study;
101 struct object_array found;
102 int is_incomplete = 0;
103 int i;
104
105 /* early return */
106 if (commit->object.flags & SEEN)
107 return 1;
108 if (commit->object.flags & INCOMPLETE)
109 return 0;
110 /*
111 * Find all commits that are reachable and are not marked as
112 * SEEN. Then make sure the trees and blobs contained are
113 * complete. After that, mark these commits also as SEEN.
114 * If some of the objects that are needed to complete this
115 * commit are missing, mark this commit as INCOMPLETE.
116 */
117 memset(&study, 0, sizeof(study));
118 memset(&found, 0, sizeof(found));
119 add_object_array(&commit->object, NULL, &study);
120 add_object_array(&commit->object, NULL, &found);
121 commit->object.flags |= STUDYING;
122 while (study.nr) {
123 struct commit *c;
124 struct commit_list *parent;
125
126 c = (struct commit *)study.objects[--study.nr].item;
127 if (!c->object.parsed && !parse_object(c->object.sha1))
128 c->object.flags |= INCOMPLETE;
129
130 if (c->object.flags & INCOMPLETE) {
131 is_incomplete = 1;
132 break;
133 }
134 else if (c->object.flags & SEEN)
135 continue;
136 for (parent = c->parents; parent; parent = parent->next) {
137 struct commit *p = parent->item;
138 if (p->object.flags & STUDYING)
139 continue;
140 p->object.flags |= STUDYING;
141 add_object_array(&p->object, NULL, &study);
142 add_object_array(&p->object, NULL, &found);
143 }
144 }
145 if (!is_incomplete) {
cd1f9c36
JH
146 /*
147 * make sure all commits in "found" array have all the
1389d9dd
JH
148 * necessary objects.
149 */
cd1f9c36 150 for (i = 0; i < found.nr; i++) {
1389d9dd
JH
151 struct commit *c =
152 (struct commit *)found.objects[i].item;
cd1f9c36 153 if (!tree_is_complete(c->tree->object.sha1)) {
1389d9dd 154 is_incomplete = 1;
cd1f9c36
JH
155 c->object.flags |= INCOMPLETE;
156 }
1389d9dd
JH
157 }
158 if (!is_incomplete) {
159 /* mark all found commits as complete, iow SEEN */
160 for (i = 0; i < found.nr; i++)
161 found.objects[i].item->flags |= SEEN;
162 }
163 }
164 /* clear flags from the objects we traversed */
165 for (i = 0; i < found.nr; i++)
166 found.objects[i].item->flags &= ~STUDYING;
167 if (is_incomplete)
168 commit->object.flags |= INCOMPLETE;
cd1f9c36
JH
169 else {
170 /*
171 * If we come here, we have (1) traversed the ancestry chain
172 * from the "commit" until we reach SEEN commits (which are
173 * known to be complete), and (2) made sure that the commits
174 * encountered during the above traversal refer to trees that
175 * are complete. Which means that we know *all* the commits
176 * we have seen during this process are complete.
177 */
178 for (i = 0; i < found.nr; i++)
179 found.objects[i].item->flags |= SEEN;
180 }
1389d9dd
JH
181 /* free object arrays */
182 free(study.objects);
183 free(found.objects);
184 return !is_incomplete;
185}
186
4264dc15
JH
187static int keep_entry(struct commit **it, unsigned char *sha1)
188{
8d8b9f62
JH
189 struct commit *commit;
190
4264dc15
JH
191 if (is_null_sha1(sha1))
192 return 1;
8d8b9f62
JH
193 commit = lookup_commit_reference_gently(sha1, 1);
194 if (!commit)
195 return 0;
196
1389d9dd
JH
197 /*
198 * Make sure everything in this commit exists.
199 *
200 * We have walked all the objects reachable from the refs
201 * and cache earlier. The commits reachable by this commit
202 * must meet SEEN commits -- and then we should mark them as
203 * SEEN as well.
204 */
205 if (!commit_is_complete(commit))
8d8b9f62
JH
206 return 0;
207 *it = commit;
208 return 1;
4264dc15
JH
209}
210
211static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
883d60fa
JS
212 const char *email, unsigned long timestamp, int tz,
213 const char *message, void *cb_data)
4264dc15
JH
214{
215 struct expire_reflog_cb *cb = cb_data;
4264dc15
JH
216 struct commit *old, *new;
217
1389d9dd 218 if (timestamp < cb->cmd->expire_total)
4264dc15
JH
219 goto prune;
220
2b81fab2
BC
221 if (cb->cmd->rewrite)
222 osha1 = cb->last_kept_sha1;
223
9bbaa6cc 224 old = new = NULL;
1389d9dd
JH
225 if (cb->cmd->stalefix &&
226 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
4264dc15
JH
227 goto prune;
228
9bbaa6cc
JH
229 if (timestamp < cb->cmd->expire_unreachable) {
230 if (!cb->ref_commit)
231 goto prune;
232 if (!old && !is_null_sha1(osha1))
233 old = lookup_commit_reference_gently(osha1, 1);
234 if (!new && !is_null_sha1(nsha1))
235 new = lookup_commit_reference_gently(nsha1, 1);
4a164d48
JH
236 if ((old && !in_merge_bases(old, &cb->ref_commit, 1)) ||
237 (new && !in_merge_bases(new, &cb->ref_commit, 1)))
9bbaa6cc
JH
238 goto prune;
239 }
4264dc15 240
552cecc2
JS
241 if (cb->cmd->recno && --(cb->cmd->recno) == 0)
242 goto prune;
243
883d60fa
JS
244 if (cb->newlog) {
245 char sign = (tz < 0) ? '-' : '+';
246 int zone = (tz < 0) ? (-tz) : tz;
247 fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
248 sha1_to_hex(osha1), sha1_to_hex(nsha1),
249 email, timestamp, sign, zone,
250 message);
2b81fab2 251 hashcpy(cb->last_kept_sha1, nsha1);
883d60fa 252 }
1389d9dd 253 if (cb->cmd->verbose)
883d60fa 254 printf("keep %s", message);
4264dc15
JH
255 return 0;
256 prune:
1389d9dd 257 if (!cb->newlog || cb->cmd->verbose)
883d60fa 258 printf("%sprune %s", cb->newlog ? "" : "would ", message);
4264dc15
JH
259 return 0;
260}
261
4264dc15
JH
262static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
263{
264 struct cmd_reflog_expire_cb *cmd = cb_data;
265 struct expire_reflog_cb cb;
266 struct ref_lock *lock;
9a13f0b7 267 char *log_file, *newlog_path = NULL;
4264dc15
JH
268 int status = 0;
269
4264dc15
JH
270 memset(&cb, 0, sizeof(cb));
271 /* we take the lock for the ref itself to prevent it from
272 * getting updated.
273 */
68db31cc 274 lock = lock_any_ref_for_update(ref, sha1, 0);
4264dc15
JH
275 if (!lock)
276 return error("cannot lock ref '%s'", ref);
9a13f0b7
NP
277 log_file = xstrdup(git_path("logs/%s", ref));
278 if (!file_exists(log_file))
4264dc15
JH
279 goto finish;
280 if (!cmd->dry_run) {
281 newlog_path = xstrdup(git_path("logs/%s.lock", ref));
282 cb.newlog = fopen(newlog_path, "w");
283 }
284
285 cb.ref_commit = lookup_commit_reference_gently(sha1, 1);
4264dc15 286 cb.ref = ref;
1389d9dd 287 cb.cmd = cmd;
4264dc15
JH
288 for_each_reflog_ent(ref, expire_reflog_ent, &cb);
289 finish:
290 if (cb.newlog) {
4cd883d7 291 if (fclose(cb.newlog)) {
4264dc15
JH
292 status |= error("%s: %s", strerror(errno),
293 newlog_path);
4cd883d7
BC
294 unlink(newlog_path);
295 } else if (rename(newlog_path, log_file)) {
4264dc15 296 status |= error("cannot rename %s to %s",
9a13f0b7 297 newlog_path, log_file);
4264dc15
JH
298 unlink(newlog_path);
299 }
300 }
301 free(newlog_path);
9a13f0b7 302 free(log_file);
4264dc15
JH
303 unlock_ref(lock);
304 return status;
305}
306
bda3a31c
JH
307static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
308{
309 struct collected_reflog *e;
310 struct collect_reflog_cb *cb = cb_data;
311 size_t namelen = strlen(ref);
312
313 e = xmalloc(sizeof(*e) + namelen + 1);
314 hashcpy(e->sha1, sha1);
315 memcpy(e->reflog, ref, namelen + 1);
316 ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
317 cb->e[cb->nr++] = e;
318 return 0;
319}
320
4aec56d1
JH
321static int reflog_expire_config(const char *var, const char *value)
322{
4f342b96
JH
323 if (!strcmp(var, "gc.reflogexpire")) {
324 if (!value)
325 config_error_nonbool(var);
4aec56d1 326 default_reflog_expire = approxidate(value);
4f342b96
JH
327 return 0;
328 }
329 if (!strcmp(var, "gc.reflogexpireunreachable")) {
330 if (!value)
331 config_error_nonbool(var);
4aec56d1 332 default_reflog_expire_unreachable = approxidate(value);
4f342b96
JH
333 return 0;
334 }
335 return git_default_config(var, value);
4aec56d1
JH
336}
337
4264dc15
JH
338static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
339{
340 struct cmd_reflog_expire_cb cb;
341 unsigned long now = time(NULL);
342 int i, status, do_all;
343
4aec56d1
JH
344 git_config(reflog_expire_config);
345
4264dc15
JH
346 save_commit_buffer = 0;
347 do_all = status = 0;
348 memset(&cb, 0, sizeof(cb));
4aec56d1
JH
349
350 if (!default_reflog_expire_unreachable)
351 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
352 if (!default_reflog_expire)
353 default_reflog_expire = now - 90 * 24 * 3600;
354 cb.expire_total = default_reflog_expire;
355 cb.expire_unreachable = default_reflog_expire_unreachable;
4264dc15 356
1389d9dd
JH
357 /*
358 * We can trust the commits and objects reachable from refs
359 * even in older repository. We cannot trust what's reachable
360 * from reflog if the repository was pruned with older git.
361 */
362
4264dc15
JH
363 for (i = 1; i < argc; i++) {
364 const char *arg = argv[i];
365 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
366 cb.dry_run = 1;
cc44c765 367 else if (!prefixcmp(arg, "--expire="))
4264dc15 368 cb.expire_total = approxidate(arg + 9);
cc44c765 369 else if (!prefixcmp(arg, "--expire-unreachable="))
4264dc15 370 cb.expire_unreachable = approxidate(arg + 21);
1389d9dd
JH
371 else if (!strcmp(arg, "--stale-fix"))
372 cb.stalefix = 1;
2b81fab2
BC
373 else if (!strcmp(arg, "--rewrite"))
374 cb.rewrite = 1;
4264dc15
JH
375 else if (!strcmp(arg, "--all"))
376 do_all = 1;
1389d9dd
JH
377 else if (!strcmp(arg, "--verbose"))
378 cb.verbose = 1;
4264dc15
JH
379 else if (!strcmp(arg, "--")) {
380 i++;
381 break;
382 }
383 else if (arg[0] == '-')
384 usage(reflog_expire_usage);
385 else
386 break;
387 }
1389d9dd
JH
388 if (cb.stalefix) {
389 init_revisions(&cb.revs, prefix);
390 if (cb.verbose)
391 printf("Marking reachable objects...");
392 mark_reachable_objects(&cb.revs, 0);
393 if (cb.verbose)
394 putchar('\n');
395 }
396
bda3a31c
JH
397 if (do_all) {
398 struct collect_reflog_cb collected;
399 int i;
400
401 memset(&collected, 0, sizeof(collected));
402 for_each_reflog(collect_reflog, &collected);
403 for (i = 0; i < collected.nr; i++) {
404 struct collected_reflog *e = collected.e[i];
405 status |= expire_reflog(e->reflog, e->sha1, 0, &cb);
406 free(e);
407 }
408 free(collected.e);
409 }
410
4264dc15
JH
411 while (i < argc) {
412 const char *ref = argv[i++];
413 unsigned char sha1[20];
414 if (!resolve_ref(ref, sha1, 1, NULL)) {
415 status |= error("%s points nowhere!", ref);
416 continue;
417 }
418 status |= expire_reflog(ref, sha1, 0, &cb);
419 }
420 return status;
421}
422
552cecc2
JS
423static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
424 const char *email, unsigned long timestamp, int tz,
425 const char *message, void *cb_data)
426{
427 struct cmd_reflog_expire_cb *cb = cb_data;
428 if (!cb->expire_total || timestamp < cb->expire_total)
429 cb->recno++;
430 return 0;
431}
432
433static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
434{
435 struct cmd_reflog_expire_cb cb;
436 int i, status = 0;
437
552cecc2
JS
438 memset(&cb, 0, sizeof(cb));
439
440 for (i = 1; i < argc; i++) {
3c386aa3
BC
441 const char *arg = argv[i];
442 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
443 cb.dry_run = 1;
2b81fab2
BC
444 else if (!strcmp(arg, "--rewrite"))
445 cb.rewrite = 1;
3c386aa3
BC
446 else if (!strcmp(arg, "--verbose"))
447 cb.verbose = 1;
448 else if (!strcmp(arg, "--")) {
449 i++;
450 break;
451 }
452 else if (arg[0] == '-')
453 usage(reflog_delete_usage);
454 else
455 break;
456 }
457
458 if (argc - i < 1)
459 return error("Nothing to delete?");
460
461 for ( ; i < argc; i++) {
552cecc2
JS
462 const char *spec = strstr(argv[i], "@{");
463 unsigned char sha1[20];
464 char *ep, *ref;
465 int recno;
466
467 if (!spec) {
cb97cc9f 468 status |= error("Not a reflog: %s", argv[i]);
552cecc2
JS
469 continue;
470 }
471
472 if (!dwim_ref(argv[i], spec - argv[i], sha1, &ref)) {
473 status |= error("%s points nowhere!", argv[i]);
474 continue;
475 }
476
477 recno = strtoul(spec + 2, &ep, 10);
478 if (*ep == '}') {
479 cb.recno = -recno;
480 for_each_reflog_ent(ref, count_reflog_ent, &cb);
481 } else {
482 cb.expire_total = approxidate(spec + 2);
483 for_each_reflog_ent(ref, count_reflog_ent, &cb);
484 cb.expire_total = 0;
485 }
486
487 status |= expire_reflog(ref, sha1, 0, &cb);
488 free(ref);
489 }
490 return status;
491}
492
1389d9dd
JH
493/*
494 * main "reflog"
495 */
496
4264dc15
JH
497static const char reflog_usage[] =
498"git-reflog (expire | ...)";
499
500int cmd_reflog(int argc, const char **argv, const char *prefix)
501{
cf39f54e
LT
502 /* With no command, we default to showing it. */
503 if (argc < 2 || *argv[1] == '-')
504 return cmd_log_reflog(argc, argv, prefix);
505
506 if (!strcmp(argv[1], "show"))
507 return cmd_log_reflog(argc - 1, argv + 1, prefix);
508
509 if (!strcmp(argv[1], "expire"))
4264dc15 510 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
cf39f54e 511
552cecc2
JS
512 if (!strcmp(argv[1], "delete"))
513 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
514
cf39f54e
LT
515 /* Not a recognized reflog command..*/
516 usage(reflog_usage);
4264dc15 517}