1 #include "git-compat-util.h"
3 #include "object-store.h"
11 /* Remember to update object flag allocation in object.h */
12 #define INCOMPLETE (1u<<10)
13 #define STUDYING (1u<<11)
14 #define REACHABLE (1u<<12)
16 static int tree_is_complete(const struct object_id
*oid
)
18 struct tree_desc desc
;
19 struct name_entry entry
;
23 tree
= lookup_tree(the_repository
, oid
);
26 if (tree
->object
.flags
& SEEN
)
28 if (tree
->object
.flags
& INCOMPLETE
)
32 enum object_type type
;
34 void *data
= repo_read_object_file(the_repository
, oid
, &type
,
37 tree
->object
.flags
|= INCOMPLETE
;
43 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
45 while (tree_entry(&desc
, &entry
)) {
46 if (!repo_has_object_file(the_repository
, &entry
.oid
) ||
47 (S_ISDIR(entry
.mode
) && !tree_is_complete(&entry
.oid
))) {
48 tree
->object
.flags
|= INCOMPLETE
;
52 free_tree_buffer(tree
);
55 tree
->object
.flags
|= SEEN
;
59 static int commit_is_complete(struct commit
*commit
)
61 struct object_array study
;
62 struct object_array found
;
63 int is_incomplete
= 0;
67 if (commit
->object
.flags
& SEEN
)
69 if (commit
->object
.flags
& INCOMPLETE
)
72 * Find all commits that are reachable and are not marked as
73 * SEEN. Then make sure the trees and blobs contained are
74 * complete. After that, mark these commits also as SEEN.
75 * If some of the objects that are needed to complete this
76 * commit are missing, mark this commit as INCOMPLETE.
78 memset(&study
, 0, sizeof(study
));
79 memset(&found
, 0, sizeof(found
));
80 add_object_array(&commit
->object
, NULL
, &study
);
81 add_object_array(&commit
->object
, NULL
, &found
);
82 commit
->object
.flags
|= STUDYING
;
85 struct commit_list
*parent
;
87 c
= (struct commit
*)object_array_pop(&study
);
88 if (!c
->object
.parsed
&& !parse_object(the_repository
, &c
->object
.oid
))
89 c
->object
.flags
|= INCOMPLETE
;
91 if (c
->object
.flags
& INCOMPLETE
) {
95 else if (c
->object
.flags
& SEEN
)
97 for (parent
= c
->parents
; parent
; parent
= parent
->next
) {
98 struct commit
*p
= parent
->item
;
99 if (p
->object
.flags
& STUDYING
)
101 p
->object
.flags
|= STUDYING
;
102 add_object_array(&p
->object
, NULL
, &study
);
103 add_object_array(&p
->object
, NULL
, &found
);
106 if (!is_incomplete
) {
108 * make sure all commits in "found" array have all the
111 for (i
= 0; i
< found
.nr
; i
++) {
113 (struct commit
*)found
.objects
[i
].item
;
114 if (!tree_is_complete(get_commit_tree_oid(c
))) {
116 c
->object
.flags
|= INCOMPLETE
;
119 if (!is_incomplete
) {
120 /* mark all found commits as complete, iow SEEN */
121 for (i
= 0; i
< found
.nr
; i
++)
122 found
.objects
[i
].item
->flags
|= SEEN
;
125 /* clear flags from the objects we traversed */
126 for (i
= 0; i
< found
.nr
; i
++)
127 found
.objects
[i
].item
->flags
&= ~STUDYING
;
129 commit
->object
.flags
|= INCOMPLETE
;
132 * If we come here, we have (1) traversed the ancestry chain
133 * from the "commit" until we reach SEEN commits (which are
134 * known to be complete), and (2) made sure that the commits
135 * encountered during the above traversal refer to trees that
136 * are complete. Which means that we know *all* the commits
137 * we have seen during this process are complete.
139 for (i
= 0; i
< found
.nr
; i
++)
140 found
.objects
[i
].item
->flags
|= SEEN
;
142 /* free object arrays */
143 object_array_clear(&study
);
144 object_array_clear(&found
);
145 return !is_incomplete
;
148 static int keep_entry(struct commit
**it
, struct object_id
*oid
)
150 struct commit
*commit
;
152 if (is_null_oid(oid
))
154 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
159 * Make sure everything in this commit exists.
161 * We have walked all the objects reachable from the refs
162 * and cache earlier. The commits reachable by this commit
163 * must meet SEEN commits -- and then we should mark them as
166 if (!commit_is_complete(commit
))
173 * Starting from commits in the cb->mark_list, mark commits that are
174 * reachable from them. Stop the traversal at commits older than
175 * the expire_limit and queue them back, so that the caller can call
176 * us again to restart the traversal with longer expire_limit.
178 static void mark_reachable(struct expire_reflog_policy_cb
*cb
)
180 struct commit_list
*pending
;
181 timestamp_t expire_limit
= cb
->mark_limit
;
182 struct commit_list
*leftover
= NULL
;
184 for (pending
= cb
->mark_list
; pending
; pending
= pending
->next
)
185 pending
->item
->object
.flags
&= ~REACHABLE
;
187 pending
= cb
->mark_list
;
189 struct commit_list
*parent
;
190 struct commit
*commit
= pop_commit(&pending
);
191 if (commit
->object
.flags
& REACHABLE
)
193 if (repo_parse_commit(the_repository
, commit
))
195 commit
->object
.flags
|= REACHABLE
;
196 if (commit
->date
< expire_limit
) {
197 commit_list_insert(commit
, &leftover
);
200 parent
= commit
->parents
;
202 commit
= parent
->item
;
203 parent
= parent
->next
;
204 if (commit
->object
.flags
& REACHABLE
)
206 commit_list_insert(commit
, &pending
);
209 cb
->mark_list
= leftover
;
212 static int unreachable(struct expire_reflog_policy_cb
*cb
, struct commit
*commit
, struct object_id
*oid
)
215 * We may or may not have the commit yet - if not, look it
216 * up using the supplied sha1.
219 if (is_null_oid(oid
))
222 commit
= lookup_commit_reference_gently(the_repository
, oid
,
225 /* Not a commit -- keep it */
230 /* Reachable from the current ref? Don't prune. */
231 if (commit
->object
.flags
& REACHABLE
)
234 if (cb
->mark_list
&& cb
->mark_limit
) {
235 cb
->mark_limit
= 0; /* dig down to the root */
239 return !(commit
->object
.flags
& REACHABLE
);
243 * Return true iff the specified reflog entry should be expired.
245 int should_expire_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
246 const char *email UNUSED
,
247 timestamp_t timestamp
, int tz UNUSED
,
248 const char *message UNUSED
, void *cb_data
)
250 struct expire_reflog_policy_cb
*cb
= cb_data
;
251 struct commit
*old_commit
, *new_commit
;
253 if (timestamp
< cb
->cmd
.expire_total
)
256 old_commit
= new_commit
= NULL
;
257 if (cb
->cmd
.stalefix
&&
258 (!keep_entry(&old_commit
, ooid
) || !keep_entry(&new_commit
, noid
)))
261 if (timestamp
< cb
->cmd
.expire_unreachable
) {
262 switch (cb
->unreachable_expire_kind
) {
267 if (unreachable(cb
, old_commit
, ooid
) || unreachable(cb
, new_commit
, noid
))
273 if (cb
->cmd
.recno
&& --(cb
->cmd
.recno
) == 0)
279 int should_expire_reflog_ent_verbose(struct object_id
*ooid
,
280 struct object_id
*noid
,
282 timestamp_t timestamp
, int tz
,
283 const char *message
, void *cb_data
)
285 struct expire_reflog_policy_cb
*cb
= cb_data
;
288 expire
= should_expire_reflog_ent(ooid
, noid
, email
, timestamp
, tz
,
292 printf("keep %s", message
);
293 else if (cb
->dry_run
)
294 printf("would prune %s", message
);
296 printf("prune %s", message
);
301 static int push_tip_to_list(const char *refname UNUSED
,
302 const struct object_id
*oid
,
303 int flags
, void *cb_data
)
305 struct commit_list
**list
= cb_data
;
306 struct commit
*tip_commit
;
307 if (flags
& REF_ISSYMREF
)
309 tip_commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
312 commit_list_insert(tip_commit
, list
);
316 static int is_head(const char *refname
)
318 const char *stripped_refname
;
319 parse_worktree_ref(refname
, NULL
, NULL
, &stripped_refname
);
320 return !strcmp(stripped_refname
, "HEAD");
323 void reflog_expiry_prepare(const char *refname
,
324 const struct object_id
*oid
,
327 struct expire_reflog_policy_cb
*cb
= cb_data
;
328 struct commit_list
*elem
;
329 struct commit
*commit
= NULL
;
331 if (!cb
->cmd
.expire_unreachable
|| is_head(refname
)) {
332 cb
->unreachable_expire_kind
= UE_HEAD
;
334 commit
= lookup_commit(the_repository
, oid
);
335 if (commit
&& is_null_oid(&commit
->object
.oid
))
337 cb
->unreachable_expire_kind
= commit
? UE_NORMAL
: UE_ALWAYS
;
340 if (cb
->cmd
.expire_unreachable
<= cb
->cmd
.expire_total
)
341 cb
->unreachable_expire_kind
= UE_ALWAYS
;
343 switch (cb
->unreachable_expire_kind
) {
347 for_each_ref(push_tip_to_list
, &cb
->tips
);
348 for (elem
= cb
->tips
; elem
; elem
= elem
->next
)
349 commit_list_insert(elem
->item
, &cb
->mark_list
);
352 commit_list_insert(commit
, &cb
->mark_list
);
353 /* For reflog_expiry_cleanup() below */
354 cb
->tip_commit
= commit
;
356 cb
->mark_limit
= cb
->cmd
.expire_total
;
360 void reflog_expiry_cleanup(void *cb_data
)
362 struct expire_reflog_policy_cb
*cb
= cb_data
;
363 struct commit_list
*elem
;
365 switch (cb
->unreachable_expire_kind
) {
369 for (elem
= cb
->tips
; elem
; elem
= elem
->next
)
370 clear_commit_marks(elem
->item
, REACHABLE
);
371 free_commit_list(cb
->tips
);
374 clear_commit_marks(cb
->tip_commit
, REACHABLE
);
377 for (elem
= cb
->mark_list
; elem
; elem
= elem
->next
)
378 clear_commit_marks(elem
->item
, REACHABLE
);
379 free_commit_list(cb
->mark_list
);
382 int count_reflog_ent(struct object_id
*ooid UNUSED
,
383 struct object_id
*noid UNUSED
,
384 const char *email UNUSED
,
385 timestamp_t timestamp
, int tz UNUSED
,
386 const char *message UNUSED
, void *cb_data
)
388 struct cmd_reflog_expire_cb
*cb
= cb_data
;
389 if (!cb
->expire_total
|| timestamp
< cb
->expire_total
)
394 int reflog_delete(const char *rev
, enum expire_reflog_flags flags
, int verbose
)
396 struct cmd_reflog_expire_cb cmd
= { 0 };
398 reflog_expiry_should_prune_fn
*should_prune_fn
= should_expire_reflog_ent
;
399 const char *spec
= strstr(rev
, "@{");
402 struct expire_reflog_policy_cb cb
= {
403 .dry_run
= !!(flags
& EXPIRE_REFLOGS_DRY_RUN
),
407 should_prune_fn
= should_expire_reflog_ent_verbose
;
410 return error(_("not a reflog: %s"), rev
);
412 if (!dwim_log(rev
, spec
- rev
, NULL
, &ref
)) {
413 status
|= error(_("no reflog for '%s'"), rev
);
417 recno
= strtoul(spec
+ 2, &ep
, 10);
420 for_each_reflog_ent(ref
, count_reflog_ent
, &cmd
);
422 cmd
.expire_total
= approxidate(spec
+ 2);
423 for_each_reflog_ent(ref
, count_reflog_ent
, &cmd
);
424 cmd
.expire_total
= 0;
428 status
|= reflog_expire(ref
, flags
,
429 reflog_expiry_prepare
,
431 reflog_expiry_cleanup
,