1 #include "git-compat-util.h"
3 #include "object-store.h"
9 /* Remember to update object flag allocation in object.h */
10 #define INCOMPLETE (1u<<10)
11 #define STUDYING (1u<<11)
12 #define REACHABLE (1u<<12)
14 static int tree_is_complete(const struct object_id
*oid
)
16 struct tree_desc desc
;
17 struct name_entry entry
;
21 tree
= lookup_tree(the_repository
, oid
);
24 if (tree
->object
.flags
& SEEN
)
26 if (tree
->object
.flags
& INCOMPLETE
)
30 enum object_type type
;
32 void *data
= read_object_file(oid
, &type
, &size
);
34 tree
->object
.flags
|= INCOMPLETE
;
40 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
42 while (tree_entry(&desc
, &entry
)) {
43 if (!has_object_file(&entry
.oid
) ||
44 (S_ISDIR(entry
.mode
) && !tree_is_complete(&entry
.oid
))) {
45 tree
->object
.flags
|= INCOMPLETE
;
49 free_tree_buffer(tree
);
52 tree
->object
.flags
|= SEEN
;
56 static int commit_is_complete(struct commit
*commit
)
58 struct object_array study
;
59 struct object_array found
;
60 int is_incomplete
= 0;
64 if (commit
->object
.flags
& SEEN
)
66 if (commit
->object
.flags
& INCOMPLETE
)
69 * Find all commits that are reachable and are not marked as
70 * SEEN. Then make sure the trees and blobs contained are
71 * complete. After that, mark these commits also as SEEN.
72 * If some of the objects that are needed to complete this
73 * commit are missing, mark this commit as INCOMPLETE.
75 memset(&study
, 0, sizeof(study
));
76 memset(&found
, 0, sizeof(found
));
77 add_object_array(&commit
->object
, NULL
, &study
);
78 add_object_array(&commit
->object
, NULL
, &found
);
79 commit
->object
.flags
|= STUDYING
;
82 struct commit_list
*parent
;
84 c
= (struct commit
*)object_array_pop(&study
);
85 if (!c
->object
.parsed
&& !parse_object(the_repository
, &c
->object
.oid
))
86 c
->object
.flags
|= INCOMPLETE
;
88 if (c
->object
.flags
& INCOMPLETE
) {
92 else if (c
->object
.flags
& SEEN
)
94 for (parent
= c
->parents
; parent
; parent
= parent
->next
) {
95 struct commit
*p
= parent
->item
;
96 if (p
->object
.flags
& STUDYING
)
98 p
->object
.flags
|= STUDYING
;
99 add_object_array(&p
->object
, NULL
, &study
);
100 add_object_array(&p
->object
, NULL
, &found
);
103 if (!is_incomplete
) {
105 * make sure all commits in "found" array have all the
108 for (i
= 0; i
< found
.nr
; i
++) {
110 (struct commit
*)found
.objects
[i
].item
;
111 if (!tree_is_complete(get_commit_tree_oid(c
))) {
113 c
->object
.flags
|= INCOMPLETE
;
116 if (!is_incomplete
) {
117 /* mark all found commits as complete, iow SEEN */
118 for (i
= 0; i
< found
.nr
; i
++)
119 found
.objects
[i
].item
->flags
|= SEEN
;
122 /* clear flags from the objects we traversed */
123 for (i
= 0; i
< found
.nr
; i
++)
124 found
.objects
[i
].item
->flags
&= ~STUDYING
;
126 commit
->object
.flags
|= INCOMPLETE
;
129 * If we come here, we have (1) traversed the ancestry chain
130 * from the "commit" until we reach SEEN commits (which are
131 * known to be complete), and (2) made sure that the commits
132 * encountered during the above traversal refer to trees that
133 * are complete. Which means that we know *all* the commits
134 * we have seen during this process are complete.
136 for (i
= 0; i
< found
.nr
; i
++)
137 found
.objects
[i
].item
->flags
|= SEEN
;
139 /* free object arrays */
140 object_array_clear(&study
);
141 object_array_clear(&found
);
142 return !is_incomplete
;
145 static int keep_entry(struct commit
**it
, struct object_id
*oid
)
147 struct commit
*commit
;
149 if (is_null_oid(oid
))
151 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
156 * Make sure everything in this commit exists.
158 * We have walked all the objects reachable from the refs
159 * and cache earlier. The commits reachable by this commit
160 * must meet SEEN commits -- and then we should mark them as
163 if (!commit_is_complete(commit
))
170 * Starting from commits in the cb->mark_list, mark commits that are
171 * reachable from them. Stop the traversal at commits older than
172 * the expire_limit and queue them back, so that the caller can call
173 * us again to restart the traversal with longer expire_limit.
175 static void mark_reachable(struct expire_reflog_policy_cb
*cb
)
177 struct commit_list
*pending
;
178 timestamp_t expire_limit
= cb
->mark_limit
;
179 struct commit_list
*leftover
= NULL
;
181 for (pending
= cb
->mark_list
; pending
; pending
= pending
->next
)
182 pending
->item
->object
.flags
&= ~REACHABLE
;
184 pending
= cb
->mark_list
;
186 struct commit_list
*parent
;
187 struct commit
*commit
= pop_commit(&pending
);
188 if (commit
->object
.flags
& REACHABLE
)
190 if (parse_commit(commit
))
192 commit
->object
.flags
|= REACHABLE
;
193 if (commit
->date
< expire_limit
) {
194 commit_list_insert(commit
, &leftover
);
197 parent
= commit
->parents
;
199 commit
= parent
->item
;
200 parent
= parent
->next
;
201 if (commit
->object
.flags
& REACHABLE
)
203 commit_list_insert(commit
, &pending
);
206 cb
->mark_list
= leftover
;
209 static int unreachable(struct expire_reflog_policy_cb
*cb
, struct commit
*commit
, struct object_id
*oid
)
212 * We may or may not have the commit yet - if not, look it
213 * up using the supplied sha1.
216 if (is_null_oid(oid
))
219 commit
= lookup_commit_reference_gently(the_repository
, oid
,
222 /* Not a commit -- keep it */
227 /* Reachable from the current ref? Don't prune. */
228 if (commit
->object
.flags
& REACHABLE
)
231 if (cb
->mark_list
&& cb
->mark_limit
) {
232 cb
->mark_limit
= 0; /* dig down to the root */
236 return !(commit
->object
.flags
& REACHABLE
);
240 * Return true iff the specified reflog entry should be expired.
242 int should_expire_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
243 const char *email UNUSED
,
244 timestamp_t timestamp
, int tz UNUSED
,
245 const char *message UNUSED
, void *cb_data
)
247 struct expire_reflog_policy_cb
*cb
= cb_data
;
248 struct commit
*old_commit
, *new_commit
;
250 if (timestamp
< cb
->cmd
.expire_total
)
253 old_commit
= new_commit
= NULL
;
254 if (cb
->cmd
.stalefix
&&
255 (!keep_entry(&old_commit
, ooid
) || !keep_entry(&new_commit
, noid
)))
258 if (timestamp
< cb
->cmd
.expire_unreachable
) {
259 switch (cb
->unreachable_expire_kind
) {
264 if (unreachable(cb
, old_commit
, ooid
) || unreachable(cb
, new_commit
, noid
))
270 if (cb
->cmd
.recno
&& --(cb
->cmd
.recno
) == 0)
276 int should_expire_reflog_ent_verbose(struct object_id
*ooid
,
277 struct object_id
*noid
,
279 timestamp_t timestamp
, int tz
,
280 const char *message
, void *cb_data
)
282 struct expire_reflog_policy_cb
*cb
= cb_data
;
285 expire
= should_expire_reflog_ent(ooid
, noid
, email
, timestamp
, tz
,
289 printf("keep %s", message
);
290 else if (cb
->dry_run
)
291 printf("would prune %s", message
);
293 printf("prune %s", message
);
298 static int push_tip_to_list(const char *refname UNUSED
,
299 const struct object_id
*oid
,
300 int flags
, void *cb_data
)
302 struct commit_list
**list
= cb_data
;
303 struct commit
*tip_commit
;
304 if (flags
& REF_ISSYMREF
)
306 tip_commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
309 commit_list_insert(tip_commit
, list
);
313 static int is_head(const char *refname
)
315 const char *stripped_refname
;
316 parse_worktree_ref(refname
, NULL
, NULL
, &stripped_refname
);
317 return !strcmp(stripped_refname
, "HEAD");
320 void reflog_expiry_prepare(const char *refname
,
321 const struct object_id
*oid
,
324 struct expire_reflog_policy_cb
*cb
= cb_data
;
325 struct commit_list
*elem
;
326 struct commit
*commit
= NULL
;
328 if (!cb
->cmd
.expire_unreachable
|| is_head(refname
)) {
329 cb
->unreachable_expire_kind
= UE_HEAD
;
331 commit
= lookup_commit(the_repository
, oid
);
332 if (commit
&& is_null_oid(&commit
->object
.oid
))
334 cb
->unreachable_expire_kind
= commit
? UE_NORMAL
: UE_ALWAYS
;
337 if (cb
->cmd
.expire_unreachable
<= cb
->cmd
.expire_total
)
338 cb
->unreachable_expire_kind
= UE_ALWAYS
;
340 switch (cb
->unreachable_expire_kind
) {
344 for_each_ref(push_tip_to_list
, &cb
->tips
);
345 for (elem
= cb
->tips
; elem
; elem
= elem
->next
)
346 commit_list_insert(elem
->item
, &cb
->mark_list
);
349 commit_list_insert(commit
, &cb
->mark_list
);
350 /* For reflog_expiry_cleanup() below */
351 cb
->tip_commit
= commit
;
353 cb
->mark_limit
= cb
->cmd
.expire_total
;
357 void reflog_expiry_cleanup(void *cb_data
)
359 struct expire_reflog_policy_cb
*cb
= cb_data
;
360 struct commit_list
*elem
;
362 switch (cb
->unreachable_expire_kind
) {
366 for (elem
= cb
->tips
; elem
; elem
= elem
->next
)
367 clear_commit_marks(elem
->item
, REACHABLE
);
368 free_commit_list(cb
->tips
);
371 clear_commit_marks(cb
->tip_commit
, REACHABLE
);
374 for (elem
= cb
->mark_list
; elem
; elem
= elem
->next
)
375 clear_commit_marks(elem
->item
, REACHABLE
);
376 free_commit_list(cb
->mark_list
);
379 int count_reflog_ent(struct object_id
*ooid UNUSED
,
380 struct object_id
*noid UNUSED
,
381 const char *email UNUSED
,
382 timestamp_t timestamp
, int tz UNUSED
,
383 const char *message UNUSED
, void *cb_data
)
385 struct cmd_reflog_expire_cb
*cb
= cb_data
;
386 if (!cb
->expire_total
|| timestamp
< cb
->expire_total
)
391 int reflog_delete(const char *rev
, enum expire_reflog_flags flags
, int verbose
)
393 struct cmd_reflog_expire_cb cmd
= { 0 };
395 reflog_expiry_should_prune_fn
*should_prune_fn
= should_expire_reflog_ent
;
396 const char *spec
= strstr(rev
, "@{");
399 struct expire_reflog_policy_cb cb
= {
400 .dry_run
= !!(flags
& EXPIRE_REFLOGS_DRY_RUN
),
404 should_prune_fn
= should_expire_reflog_ent_verbose
;
407 return error(_("not a reflog: %s"), rev
);
409 if (!dwim_log(rev
, spec
- rev
, NULL
, &ref
)) {
410 status
|= error(_("no reflog for '%s'"), rev
);
414 recno
= strtoul(spec
+ 2, &ep
, 10);
417 for_each_reflog_ent(ref
, count_reflog_ent
, &cmd
);
419 cmd
.expire_total
= approxidate(spec
+ 2);
420 for_each_reflog_ent(ref
, count_reflog_ent
, &cmd
);
421 cmd
.expire_total
= 0;
425 status
|= reflog_expire(ref
, flags
,
426 reflog_expiry_prepare
,
428 reflog_expiry_cleanup
,