15 static const char reflog_expire_usage
[] =
16 "git reflog expire [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
17 static const char reflog_delete_usage
[] =
18 "git reflog delete [--verbose] [--dry-run] [--rewrite] [--updateref] <refs>...";
20 static unsigned long default_reflog_expire
;
21 static unsigned long default_reflog_expire_unreachable
;
23 struct cmd_reflog_expire_cb
{
30 unsigned long expire_total
;
31 unsigned long expire_unreachable
;
35 struct expire_reflog_cb
{
41 } unreachable_expire_kind
;
42 struct commit_list
*mark_list
;
43 unsigned long mark_limit
;
44 struct cmd_reflog_expire_cb
*cmd
;
45 unsigned char last_kept_sha1
[20];
46 struct commit
*tip_commit
;
47 struct commit_list
*tips
;
50 struct collected_reflog
{
51 unsigned char sha1
[20];
52 char reflog
[FLEX_ARRAY
];
54 struct collect_reflog_cb
{
55 struct collected_reflog
**e
;
60 #define INCOMPLETE (1u<<10)
61 #define STUDYING (1u<<11)
62 #define REACHABLE (1u<<12)
64 static int tree_is_complete(const unsigned char *sha1
)
66 struct tree_desc desc
;
67 struct name_entry entry
;
71 tree
= lookup_tree(sha1
);
74 if (tree
->object
.flags
& SEEN
)
76 if (tree
->object
.flags
& INCOMPLETE
)
80 enum object_type type
;
82 void *data
= read_sha1_file(sha1
, &type
, &size
);
84 tree
->object
.flags
|= INCOMPLETE
;
90 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
92 while (tree_entry(&desc
, &entry
)) {
93 if (!has_sha1_file(entry
.sha1
) ||
94 (S_ISDIR(entry
.mode
) && !tree_is_complete(entry
.sha1
))) {
95 tree
->object
.flags
|= INCOMPLETE
;
99 free_tree_buffer(tree
);
102 tree
->object
.flags
|= SEEN
;
106 static int commit_is_complete(struct commit
*commit
)
108 struct object_array study
;
109 struct object_array found
;
110 int is_incomplete
= 0;
114 if (commit
->object
.flags
& SEEN
)
116 if (commit
->object
.flags
& INCOMPLETE
)
119 * Find all commits that are reachable and are not marked as
120 * SEEN. Then make sure the trees and blobs contained are
121 * complete. After that, mark these commits also as SEEN.
122 * If some of the objects that are needed to complete this
123 * commit are missing, mark this commit as INCOMPLETE.
125 memset(&study
, 0, sizeof(study
));
126 memset(&found
, 0, sizeof(found
));
127 add_object_array(&commit
->object
, NULL
, &study
);
128 add_object_array(&commit
->object
, NULL
, &found
);
129 commit
->object
.flags
|= STUDYING
;
132 struct commit_list
*parent
;
134 c
= (struct commit
*)study
.objects
[--study
.nr
].item
;
135 if (!c
->object
.parsed
&& !parse_object(c
->object
.sha1
))
136 c
->object
.flags
|= INCOMPLETE
;
138 if (c
->object
.flags
& INCOMPLETE
) {
142 else if (c
->object
.flags
& SEEN
)
144 for (parent
= c
->parents
; parent
; parent
= parent
->next
) {
145 struct commit
*p
= parent
->item
;
146 if (p
->object
.flags
& STUDYING
)
148 p
->object
.flags
|= STUDYING
;
149 add_object_array(&p
->object
, NULL
, &study
);
150 add_object_array(&p
->object
, NULL
, &found
);
153 if (!is_incomplete
) {
155 * make sure all commits in "found" array have all the
158 for (i
= 0; i
< found
.nr
; i
++) {
160 (struct commit
*)found
.objects
[i
].item
;
161 if (!tree_is_complete(c
->tree
->object
.sha1
)) {
163 c
->object
.flags
|= INCOMPLETE
;
166 if (!is_incomplete
) {
167 /* mark all found commits as complete, iow SEEN */
168 for (i
= 0; i
< found
.nr
; i
++)
169 found
.objects
[i
].item
->flags
|= SEEN
;
172 /* clear flags from the objects we traversed */
173 for (i
= 0; i
< found
.nr
; i
++)
174 found
.objects
[i
].item
->flags
&= ~STUDYING
;
176 commit
->object
.flags
|= INCOMPLETE
;
179 * If we come here, we have (1) traversed the ancestry chain
180 * from the "commit" until we reach SEEN commits (which are
181 * known to be complete), and (2) made sure that the commits
182 * encountered during the above traversal refer to trees that
183 * are complete. Which means that we know *all* the commits
184 * we have seen during this process are complete.
186 for (i
= 0; i
< found
.nr
; i
++)
187 found
.objects
[i
].item
->flags
|= SEEN
;
189 /* free object arrays */
192 return !is_incomplete
;
195 static int keep_entry(struct commit
**it
, unsigned char *sha1
)
197 struct commit
*commit
;
199 if (is_null_sha1(sha1
))
201 commit
= lookup_commit_reference_gently(sha1
, 1);
206 * Make sure everything in this commit exists.
208 * We have walked all the objects reachable from the refs
209 * and cache earlier. The commits reachable by this commit
210 * must meet SEEN commits -- and then we should mark them as
213 if (!commit_is_complete(commit
))
220 * Starting from commits in the cb->mark_list, mark commits that are
221 * reachable from them. Stop the traversal at commits older than
222 * the expire_limit and queue them back, so that the caller can call
223 * us again to restart the traversal with longer expire_limit.
225 static void mark_reachable(struct expire_reflog_cb
*cb
)
227 struct commit
*commit
;
228 struct commit_list
*pending
;
229 unsigned long expire_limit
= cb
->mark_limit
;
230 struct commit_list
*leftover
= NULL
;
232 for (pending
= cb
->mark_list
; pending
; pending
= pending
->next
)
233 pending
->item
->object
.flags
&= ~REACHABLE
;
235 pending
= cb
->mark_list
;
237 struct commit_list
*entry
= pending
;
238 struct commit_list
*parent
;
239 pending
= entry
->next
;
240 commit
= entry
->item
;
242 if (commit
->object
.flags
& REACHABLE
)
244 if (parse_commit(commit
))
246 commit
->object
.flags
|= REACHABLE
;
247 if (commit
->date
< expire_limit
) {
248 commit_list_insert(commit
, &leftover
);
251 commit
->object
.flags
|= REACHABLE
;
252 parent
= commit
->parents
;
254 commit
= parent
->item
;
255 parent
= parent
->next
;
256 if (commit
->object
.flags
& REACHABLE
)
258 commit_list_insert(commit
, &pending
);
261 cb
->mark_list
= leftover
;
264 static int unreachable(struct expire_reflog_cb
*cb
, struct commit
*commit
, unsigned char *sha1
)
267 * We may or may not have the commit yet - if not, look it
268 * up using the supplied sha1.
271 if (is_null_sha1(sha1
))
274 commit
= lookup_commit_reference_gently(sha1
, 1);
276 /* Not a commit -- keep it */
281 /* Reachable from the current ref? Don't prune. */
282 if (commit
->object
.flags
& REACHABLE
)
285 if (cb
->mark_list
&& cb
->mark_limit
) {
286 cb
->mark_limit
= 0; /* dig down to the root */
290 return !(commit
->object
.flags
& REACHABLE
);
294 * Return true iff the specified reflog entry should be expired.
296 static int should_expire_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
297 const char *email
, unsigned long timestamp
, int tz
,
298 const char *message
, void *cb_data
)
300 struct expire_reflog_cb
*cb
= cb_data
;
301 struct commit
*old
, *new;
303 if (timestamp
< cb
->cmd
->expire_total
)
307 if (cb
->cmd
->stalefix
&&
308 (!keep_entry(&old
, osha1
) || !keep_entry(&new, nsha1
)))
311 if (timestamp
< cb
->cmd
->expire_unreachable
) {
312 if (cb
->unreachable_expire_kind
== UE_ALWAYS
)
314 if (unreachable(cb
, old
, osha1
) || unreachable(cb
, new, nsha1
))
318 if (cb
->cmd
->recno
&& --(cb
->cmd
->recno
) == 0)
324 static int expire_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
325 const char *email
, unsigned long timestamp
, int tz
,
326 const char *message
, void *cb_data
)
328 struct expire_reflog_cb
*cb
= cb_data
;
330 if (cb
->cmd
->rewrite
)
331 osha1
= cb
->last_kept_sha1
;
333 if (should_expire_reflog_ent(osha1
, nsha1
, email
, timestamp
, tz
,
336 printf("would prune %s", message
);
337 else if (cb
->cmd
->verbose
)
338 printf("prune %s", message
);
341 char sign
= (tz
< 0) ? '-' : '+';
342 int zone
= (tz
< 0) ? (-tz
) : tz
;
343 fprintf(cb
->newlog
, "%s %s %s %lu %c%04d\t%s",
344 sha1_to_hex(osha1
), sha1_to_hex(nsha1
),
345 email
, timestamp
, sign
, zone
,
347 hashcpy(cb
->last_kept_sha1
, nsha1
);
349 if (cb
->cmd
->verbose
)
350 printf("keep %s", message
);
355 static int push_tip_to_list(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
357 struct commit_list
**list
= cb_data
;
358 struct commit
*tip_commit
;
359 if (flags
& REF_ISSYMREF
)
361 tip_commit
= lookup_commit_reference_gently(sha1
, 1);
364 commit_list_insert(tip_commit
, list
);
368 static void reflog_expiry_prepare(const char *refname
,
369 const unsigned char *sha1
,
370 struct expire_reflog_cb
*cb
)
372 if (!cb
->cmd
->expire_unreachable
|| !strcmp(refname
, "HEAD")) {
373 cb
->tip_commit
= NULL
;
374 cb
->unreachable_expire_kind
= UE_HEAD
;
376 cb
->tip_commit
= lookup_commit_reference_gently(sha1
, 1);
378 cb
->unreachable_expire_kind
= UE_ALWAYS
;
380 cb
->unreachable_expire_kind
= UE_NORMAL
;
383 if (cb
->cmd
->expire_unreachable
<= cb
->cmd
->expire_total
)
384 cb
->unreachable_expire_kind
= UE_ALWAYS
;
386 cb
->mark_list
= NULL
;
388 if (cb
->unreachable_expire_kind
!= UE_ALWAYS
) {
389 if (cb
->unreachable_expire_kind
== UE_HEAD
) {
390 struct commit_list
*elem
;
391 for_each_ref(push_tip_to_list
, &cb
->tips
);
392 for (elem
= cb
->tips
; elem
; elem
= elem
->next
)
393 commit_list_insert(elem
->item
, &cb
->mark_list
);
395 commit_list_insert(cb
->tip_commit
, &cb
->mark_list
);
397 cb
->mark_limit
= cb
->cmd
->expire_total
;
402 static void reflog_expiry_cleanup(struct expire_reflog_cb
*cb
)
404 if (cb
->unreachable_expire_kind
!= UE_ALWAYS
) {
405 if (cb
->unreachable_expire_kind
== UE_HEAD
) {
406 struct commit_list
*elem
;
407 for (elem
= cb
->tips
; elem
; elem
= elem
->next
)
408 clear_commit_marks(elem
->item
, REACHABLE
);
409 free_commit_list(cb
->tips
);
411 clear_commit_marks(cb
->tip_commit
, REACHABLE
);
416 static int expire_reflog(const char *refname
, const unsigned char *sha1
,
417 struct cmd_reflog_expire_cb
*cmd
)
419 static struct lock_file reflog_lock
;
420 struct expire_reflog_cb cb
;
421 struct ref_lock
*lock
;
425 memset(&cb
, 0, sizeof(cb
));
428 * The reflog file is locked by holding the lock on the
429 * reference itself, plus we might need to update the
430 * reference if --updateref was specified:
432 lock
= lock_any_ref_for_update(refname
, sha1
, 0, NULL
);
434 return error("cannot lock ref '%s'", refname
);
435 if (!reflog_exists(refname
)) {
440 log_file
= git_pathdup("logs/%s", refname
);
443 * Even though holding $GIT_DIR/logs/$reflog.lock has
444 * no locking implications, we use the lock_file
445 * machinery here anyway because it does a lot of the
446 * work we need, including cleaning up if the program
447 * exits unexpectedly.
449 if (hold_lock_file_for_update(&reflog_lock
, log_file
, 0) < 0) {
450 struct strbuf err
= STRBUF_INIT
;
451 unable_to_lock_message(log_file
, errno
, &err
);
452 error("%s", err
.buf
);
453 strbuf_release(&err
);
456 cb
.newlog
= fdopen_lock_file(&reflog_lock
, "w");
458 error("cannot fdopen %s (%s)",
459 reflog_lock
.filename
.buf
, strerror(errno
));
466 reflog_expiry_prepare(refname
, sha1
, &cb
);
467 for_each_reflog_ent(refname
, expire_reflog_ent
, &cb
);
468 reflog_expiry_cleanup(&cb
);
471 if (close_lock_file(&reflog_lock
)) {
472 status
|= error("couldn't write %s: %s", log_file
,
474 } else if (cmd
->updateref
&&
475 (write_in_full(lock
->lock_fd
,
476 sha1_to_hex(cb
.last_kept_sha1
), 40) != 40 ||
477 write_str_in_full(lock
->lock_fd
, "\n") != 1 ||
478 close_ref(lock
) < 0)) {
479 status
|= error("couldn't write %s",
480 lock
->lk
->filename
.buf
);
481 rollback_lock_file(&reflog_lock
);
482 } else if (commit_lock_file(&reflog_lock
)) {
483 status
|= error("unable to commit reflog '%s' (%s)",
484 log_file
, strerror(errno
));
485 } else if (cmd
->updateref
&& commit_ref(lock
)) {
486 status
|= error("couldn't set %s", lock
->ref_name
);
494 rollback_lock_file(&reflog_lock
);
500 static int collect_reflog(const char *ref
, const unsigned char *sha1
, int unused
, void *cb_data
)
502 struct collected_reflog
*e
;
503 struct collect_reflog_cb
*cb
= cb_data
;
504 size_t namelen
= strlen(ref
);
506 e
= xmalloc(sizeof(*e
) + namelen
+ 1);
507 hashcpy(e
->sha1
, sha1
);
508 memcpy(e
->reflog
, ref
, namelen
+ 1);
509 ALLOC_GROW(cb
->e
, cb
->nr
+ 1, cb
->alloc
);
514 static struct reflog_expire_cfg
{
515 struct reflog_expire_cfg
*next
;
516 unsigned long expire_total
;
517 unsigned long expire_unreachable
;
519 char pattern
[FLEX_ARRAY
];
520 } *reflog_expire_cfg
, **reflog_expire_cfg_tail
;
522 static struct reflog_expire_cfg
*find_cfg_ent(const char *pattern
, size_t len
)
524 struct reflog_expire_cfg
*ent
;
526 if (!reflog_expire_cfg_tail
)
527 reflog_expire_cfg_tail
= &reflog_expire_cfg
;
529 for (ent
= reflog_expire_cfg
; ent
; ent
= ent
->next
)
530 if (ent
->len
== len
&&
531 !memcmp(ent
->pattern
, pattern
, len
))
534 ent
= xcalloc(1, (sizeof(*ent
) + len
));
535 memcpy(ent
->pattern
, pattern
, len
);
537 *reflog_expire_cfg_tail
= ent
;
538 reflog_expire_cfg_tail
= &(ent
->next
);
542 static int parse_expire_cfg_value(const char *var
, const char *value
, unsigned long *expire
)
545 return config_error_nonbool(var
);
546 if (parse_expiry_date(value
, expire
))
547 return error(_("%s' for '%s' is not a valid timestamp"),
552 /* expiry timer slot */
553 #define EXPIRE_TOTAL 01
554 #define EXPIRE_UNREACH 02
556 static int reflog_expire_config(const char *var
, const char *value
, void *cb
)
558 const char *pattern
, *key
;
560 unsigned long expire
;
562 struct reflog_expire_cfg
*ent
;
564 if (parse_config_key(var
, "gc", &pattern
, &pattern_len
, &key
) < 0)
565 return git_default_config(var
, value
, cb
);
567 if (!strcmp(key
, "reflogexpire")) {
569 if (parse_expire_cfg_value(var
, value
, &expire
))
571 } else if (!strcmp(key
, "reflogexpireunreachable")) {
572 slot
= EXPIRE_UNREACH
;
573 if (parse_expire_cfg_value(var
, value
, &expire
))
576 return git_default_config(var
, value
, cb
);
581 default_reflog_expire
= expire
;
584 default_reflog_expire_unreachable
= expire
;
590 ent
= find_cfg_ent(pattern
, pattern_len
);
595 ent
->expire_total
= expire
;
598 ent
->expire_unreachable
= expire
;
604 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb
*cb
, int slot
, const char *ref
)
606 struct reflog_expire_cfg
*ent
;
608 if (slot
== (EXPIRE_TOTAL
|EXPIRE_UNREACH
))
609 return; /* both given explicitly -- nothing to tweak */
611 for (ent
= reflog_expire_cfg
; ent
; ent
= ent
->next
) {
612 if (!wildmatch(ent
->pattern
, ref
, 0, NULL
)) {
613 if (!(slot
& EXPIRE_TOTAL
))
614 cb
->expire_total
= ent
->expire_total
;
615 if (!(slot
& EXPIRE_UNREACH
))
616 cb
->expire_unreachable
= ent
->expire_unreachable
;
622 * If unconfigured, make stash never expire
624 if (!strcmp(ref
, "refs/stash")) {
625 if (!(slot
& EXPIRE_TOTAL
))
626 cb
->expire_total
= 0;
627 if (!(slot
& EXPIRE_UNREACH
))
628 cb
->expire_unreachable
= 0;
632 /* Nothing matched -- use the default value */
633 if (!(slot
& EXPIRE_TOTAL
))
634 cb
->expire_total
= default_reflog_expire
;
635 if (!(slot
& EXPIRE_UNREACH
))
636 cb
->expire_unreachable
= default_reflog_expire_unreachable
;
639 static int cmd_reflog_expire(int argc
, const char **argv
, const char *prefix
)
641 struct cmd_reflog_expire_cb cb
;
642 unsigned long now
= time(NULL
);
643 int i
, status
, do_all
;
644 int explicit_expiry
= 0;
646 default_reflog_expire_unreachable
= now
- 30 * 24 * 3600;
647 default_reflog_expire
= now
- 90 * 24 * 3600;
648 git_config(reflog_expire_config
, NULL
);
650 save_commit_buffer
= 0;
652 memset(&cb
, 0, sizeof(cb
));
654 cb
.expire_total
= default_reflog_expire
;
655 cb
.expire_unreachable
= default_reflog_expire_unreachable
;
657 for (i
= 1; i
< argc
; i
++) {
658 const char *arg
= argv
[i
];
659 if (!strcmp(arg
, "--dry-run") || !strcmp(arg
, "-n"))
661 else if (starts_with(arg
, "--expire=")) {
662 if (parse_expiry_date(arg
+ 9, &cb
.expire_total
))
663 die(_("'%s' is not a valid timestamp"), arg
);
664 explicit_expiry
|= EXPIRE_TOTAL
;
666 else if (starts_with(arg
, "--expire-unreachable=")) {
667 if (parse_expiry_date(arg
+ 21, &cb
.expire_unreachable
))
668 die(_("'%s' is not a valid timestamp"), arg
);
669 explicit_expiry
|= EXPIRE_UNREACH
;
671 else if (!strcmp(arg
, "--stale-fix"))
673 else if (!strcmp(arg
, "--rewrite"))
675 else if (!strcmp(arg
, "--updateref"))
677 else if (!strcmp(arg
, "--all"))
679 else if (!strcmp(arg
, "--verbose"))
681 else if (!strcmp(arg
, "--")) {
685 else if (arg
[0] == '-')
686 usage(reflog_expire_usage
);
692 * We can trust the commits and objects reachable from refs
693 * even in older repository. We cannot trust what's reachable
694 * from reflog if the repository was pruned with older git.
697 init_revisions(&cb
.revs
, prefix
);
699 printf("Marking reachable objects...");
700 mark_reachable_objects(&cb
.revs
, 0, 0, NULL
);
706 struct collect_reflog_cb collected
;
709 memset(&collected
, 0, sizeof(collected
));
710 for_each_reflog(collect_reflog
, &collected
);
711 for (i
= 0; i
< collected
.nr
; i
++) {
712 struct collected_reflog
*e
= collected
.e
[i
];
713 set_reflog_expiry_param(&cb
, explicit_expiry
, e
->reflog
);
714 status
|= expire_reflog(e
->reflog
, e
->sha1
, &cb
);
720 for (; i
< argc
; i
++) {
722 unsigned char sha1
[20];
723 if (!dwim_log(argv
[i
], strlen(argv
[i
]), sha1
, &ref
)) {
724 status
|= error("%s points nowhere!", argv
[i
]);
727 set_reflog_expiry_param(&cb
, explicit_expiry
, ref
);
728 status
|= expire_reflog(ref
, sha1
, &cb
);
733 static int count_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
734 const char *email
, unsigned long timestamp
, int tz
,
735 const char *message
, void *cb_data
)
737 struct cmd_reflog_expire_cb
*cb
= cb_data
;
738 if (!cb
->expire_total
|| timestamp
< cb
->expire_total
)
743 static int cmd_reflog_delete(int argc
, const char **argv
, const char *prefix
)
745 struct cmd_reflog_expire_cb cb
;
748 memset(&cb
, 0, sizeof(cb
));
750 for (i
= 1; i
< argc
; i
++) {
751 const char *arg
= argv
[i
];
752 if (!strcmp(arg
, "--dry-run") || !strcmp(arg
, "-n"))
754 else if (!strcmp(arg
, "--rewrite"))
756 else if (!strcmp(arg
, "--updateref"))
758 else if (!strcmp(arg
, "--verbose"))
760 else if (!strcmp(arg
, "--")) {
764 else if (arg
[0] == '-')
765 usage(reflog_delete_usage
);
771 return error("Nothing to delete?");
773 for ( ; i
< argc
; i
++) {
774 const char *spec
= strstr(argv
[i
], "@{");
775 unsigned char sha1
[20];
780 status
|= error("Not a reflog: %s", argv
[i
]);
784 if (!dwim_log(argv
[i
], spec
- argv
[i
], sha1
, &ref
)) {
785 status
|= error("no reflog for '%s'", argv
[i
]);
789 recno
= strtoul(spec
+ 2, &ep
, 10);
792 for_each_reflog_ent(ref
, count_reflog_ent
, &cb
);
794 cb
.expire_total
= approxidate(spec
+ 2);
795 for_each_reflog_ent(ref
, count_reflog_ent
, &cb
);
799 status
|= expire_reflog(ref
, sha1
, &cb
);
809 static const char reflog_usage
[] =
810 "git reflog [ show | expire | delete ]";
812 int cmd_reflog(int argc
, const char **argv
, const char *prefix
)
814 if (argc
> 1 && !strcmp(argv
[1], "-h"))
817 /* With no command, we default to showing it. */
818 if (argc
< 2 || *argv
[1] == '-')
819 return cmd_log_reflog(argc
, argv
, prefix
);
821 if (!strcmp(argv
[1], "show"))
822 return cmd_log_reflog(argc
- 1, argv
+ 1, prefix
);
824 if (!strcmp(argv
[1], "expire"))
825 return cmd_reflog_expire(argc
- 1, argv
+ 1, prefix
);
827 if (!strcmp(argv
[1], "delete"))
828 return cmd_reflog_delete(argc
- 1, argv
+ 1, prefix
);
830 return cmd_log_reflog(argc
, argv
, prefix
);