format-patch: ensure that --rfc and -k are mutually exclusive
[alt-git.git] / reflog.c
blob9ad50e7d93e40c7a5f9c7ad0cecb0b7d1766105b
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "object-store-ll.h"
4 #include "reflog.h"
5 #include "refs.h"
6 #include "revision.h"
7 #include "tree.h"
8 #include "tree-walk.h"
9 #include "worktree.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;
20 int complete;
21 struct tree *tree;
23 tree = lookup_tree(the_repository, oid);
24 if (!tree)
25 return 0;
26 if (tree->object.flags & SEEN)
27 return 1;
28 if (tree->object.flags & INCOMPLETE)
29 return 0;
31 if (!tree->buffer) {
32 enum object_type type;
33 unsigned long size;
34 void *data = repo_read_object_file(the_repository, oid, &type,
35 &size);
36 if (!data) {
37 tree->object.flags |= INCOMPLETE;
38 return 0;
40 tree->buffer = data;
41 tree->size = size;
43 init_tree_desc(&desc, tree->buffer, tree->size);
44 complete = 1;
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;
49 complete = 0;
52 free_tree_buffer(tree);
54 if (complete)
55 tree->object.flags |= SEEN;
56 return complete;
59 static int commit_is_complete(struct commit *commit)
61 struct object_array study;
62 struct object_array found;
63 int is_incomplete = 0;
64 int i;
66 /* early return */
67 if (commit->object.flags & SEEN)
68 return 1;
69 if (commit->object.flags & INCOMPLETE)
70 return 0;
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;
83 while (study.nr) {
84 struct commit *c;
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) {
92 is_incomplete = 1;
93 break;
95 else if (c->object.flags & SEEN)
96 continue;
97 for (parent = c->parents; parent; parent = parent->next) {
98 struct commit *p = parent->item;
99 if (p->object.flags & STUDYING)
100 continue;
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
109 * necessary objects.
111 for (i = 0; i < found.nr; i++) {
112 struct commit *c =
113 (struct commit *)found.objects[i].item;
114 if (!tree_is_complete(get_commit_tree_oid(c))) {
115 is_incomplete = 1;
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;
128 if (is_incomplete)
129 commit->object.flags |= INCOMPLETE;
130 else {
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))
153 return 1;
154 commit = lookup_commit_reference_gently(the_repository, oid, 1);
155 if (!commit)
156 return 0;
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
164 * SEEN as well.
166 if (!commit_is_complete(commit))
167 return 0;
168 *it = commit;
169 return 1;
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;
188 while (pending) {
189 struct commit_list *parent;
190 struct commit *commit = pop_commit(&pending);
191 if (commit->object.flags & REACHABLE)
192 continue;
193 if (repo_parse_commit(the_repository, commit))
194 continue;
195 commit->object.flags |= REACHABLE;
196 if (commit->date < expire_limit) {
197 commit_list_insert(commit, &leftover);
198 continue;
200 parent = commit->parents;
201 while (parent) {
202 commit = parent->item;
203 parent = parent->next;
204 if (commit->object.flags & REACHABLE)
205 continue;
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.
218 if (!commit) {
219 if (is_null_oid(oid))
220 return 0;
222 commit = lookup_commit_reference_gently(the_repository, oid,
225 /* Not a commit -- keep it */
226 if (!commit)
227 return 0;
230 /* Reachable from the current ref? Don't prune. */
231 if (commit->object.flags & REACHABLE)
232 return 0;
234 if (cb->mark_list && cb->mark_limit) {
235 cb->mark_limit = 0; /* dig down to the root */
236 mark_reachable(cb);
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)
254 return 1;
256 old_commit = new_commit = NULL;
257 if (cb->cmd.stalefix &&
258 (!keep_entry(&old_commit, ooid) || !keep_entry(&new_commit, noid)))
259 return 1;
261 if (timestamp < cb->cmd.expire_unreachable) {
262 switch (cb->unreachable_expire_kind) {
263 case UE_ALWAYS:
264 return 1;
265 case UE_NORMAL:
266 case UE_HEAD:
267 if (unreachable(cb, old_commit, ooid) || unreachable(cb, new_commit, noid))
268 return 1;
269 break;
273 if (cb->cmd.recno && --(cb->cmd.recno) == 0)
274 return 1;
276 return 0;
279 int should_expire_reflog_ent_verbose(struct object_id *ooid,
280 struct object_id *noid,
281 const char *email,
282 timestamp_t timestamp, int tz,
283 const char *message, void *cb_data)
285 struct expire_reflog_policy_cb *cb = cb_data;
286 int expire;
288 expire = should_expire_reflog_ent(ooid, noid, email, timestamp, tz,
289 message, cb);
291 if (!expire)
292 printf("keep %s", message);
293 else if (cb->dry_run)
294 printf("would prune %s", message);
295 else
296 printf("prune %s", message);
298 return expire;
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)
308 return 0;
309 tip_commit = lookup_commit_reference_gently(the_repository, oid, 1);
310 if (!tip_commit)
311 return 0;
312 commit_list_insert(tip_commit, list);
313 return 0;
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,
325 void *cb_data)
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;
333 } else {
334 commit = lookup_commit(the_repository, oid);
335 if (commit && is_null_oid(&commit->object.oid))
336 commit = NULL;
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) {
344 case UE_ALWAYS:
345 return;
346 case UE_HEAD:
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);
350 break;
351 case UE_NORMAL:
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;
357 mark_reachable(cb);
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) {
366 case UE_ALWAYS:
367 return;
368 case UE_HEAD:
369 for (elem = cb->tips; elem; elem = elem->next)
370 clear_commit_marks(elem->item, REACHABLE);
371 free_commit_list(cb->tips);
372 break;
373 case UE_NORMAL:
374 clear_commit_marks(cb->tip_commit, REACHABLE);
375 break;
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)
390 cb->recno++;
391 return 0;
394 int reflog_delete(const char *rev, enum expire_reflog_flags flags, int verbose)
396 struct cmd_reflog_expire_cb cmd = { 0 };
397 int status = 0;
398 reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
399 const char *spec = strstr(rev, "@{");
400 char *ep, *ref;
401 int recno;
402 struct expire_reflog_policy_cb cb = {
403 .dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
406 if (verbose)
407 should_prune_fn = should_expire_reflog_ent_verbose;
409 if (!spec)
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);
414 goto cleanup;
417 recno = strtoul(spec + 2, &ep, 10);
418 if (*ep == '}') {
419 cmd.recno = -recno;
420 for_each_reflog_ent(ref, count_reflog_ent, &cmd);
421 } else {
422 cmd.expire_total = approxidate(spec + 2);
423 for_each_reflog_ent(ref, count_reflog_ent, &cmd);
424 cmd.expire_total = 0;
427 cb.cmd = cmd;
428 status |= reflog_expire(ref, flags,
429 reflog_expiry_prepare,
430 should_prune_fn,
431 reflog_expiry_cleanup,
432 &cb);
434 cleanup:
435 free(ref);
436 return status;