Merge branch 'nd/prune-in-worktree'
[alt-git.git] / reachable.c
blob88d7d679da9c837463f74d9a9277025b008635af
1 #include "cache.h"
2 #include "refs.h"
3 #include "tag.h"
4 #include "commit.h"
5 #include "blob.h"
6 #include "diff.h"
7 #include "revision.h"
8 #include "reachable.h"
9 #include "cache-tree.h"
10 #include "progress.h"
11 #include "list-objects.h"
12 #include "packfile.h"
13 #include "worktree.h"
15 struct connectivity_progress {
16 struct progress *progress;
17 unsigned long count;
20 static void update_progress(struct connectivity_progress *cp)
22 cp->count++;
23 if ((cp->count & 1023) == 0)
24 display_progress(cp->progress, cp->count);
27 static int add_one_ref(const char *path, const struct object_id *oid,
28 int flag, void *cb_data)
30 struct rev_info *revs = (struct rev_info *)cb_data;
31 struct object *object;
33 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
34 warning("symbolic ref is dangling: %s", path);
35 return 0;
38 object = parse_object_or_die(oid, path);
39 add_pending_object(revs, object, "");
41 return 0;
45 * The traversal will have already marked us as SEEN, so we
46 * only need to handle any progress reporting here.
48 static void mark_object(struct object *obj, const char *name, void *data)
50 update_progress(data);
53 static void mark_commit(struct commit *c, void *data)
55 mark_object(&c->object, NULL, data);
58 struct recent_data {
59 struct rev_info *revs;
60 timestamp_t timestamp;
63 static void add_recent_object(const struct object_id *oid,
64 timestamp_t mtime,
65 struct recent_data *data)
67 struct object *obj;
68 enum object_type type;
70 if (mtime <= data->timestamp)
71 return;
74 * We do not want to call parse_object here, because
75 * inflating blobs and trees could be very expensive.
76 * However, we do need to know the correct type for
77 * later processing, and the revision machinery expects
78 * commits and tags to have been parsed.
80 type = sha1_object_info(oid->hash, NULL);
81 if (type < 0)
82 die("unable to get object info for %s", oid_to_hex(oid));
84 switch (type) {
85 case OBJ_TAG:
86 case OBJ_COMMIT:
87 obj = parse_object_or_die(oid, NULL);
88 break;
89 case OBJ_TREE:
90 obj = (struct object *)lookup_tree(oid);
91 break;
92 case OBJ_BLOB:
93 obj = (struct object *)lookup_blob(oid);
94 break;
95 default:
96 die("unknown object type for %s: %s",
97 oid_to_hex(oid), typename(type));
100 if (!obj)
101 die("unable to lookup %s", oid_to_hex(oid));
103 add_pending_object(data->revs, obj, "");
106 static int add_recent_loose(const struct object_id *oid,
107 const char *path, void *data)
109 struct stat st;
110 struct object *obj = lookup_object(oid->hash);
112 if (obj && obj->flags & SEEN)
113 return 0;
115 if (stat(path, &st) < 0) {
117 * It's OK if an object went away during our iteration; this
118 * could be due to a simultaneous repack. But anything else
119 * we should abort, since we might then fail to mark objects
120 * which should not be pruned.
122 if (errno == ENOENT)
123 return 0;
124 return error_errno("unable to stat %s", oid_to_hex(oid));
127 add_recent_object(oid, st.st_mtime, data);
128 return 0;
131 static int add_recent_packed(const struct object_id *oid,
132 struct packed_git *p, uint32_t pos,
133 void *data)
135 struct object *obj = lookup_object(oid->hash);
137 if (obj && obj->flags & SEEN)
138 return 0;
139 add_recent_object(oid, p->mtime, data);
140 return 0;
143 int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
144 timestamp_t timestamp)
146 struct recent_data data;
147 int r;
149 data.revs = revs;
150 data.timestamp = timestamp;
152 r = for_each_loose_object(add_recent_loose, &data,
153 FOR_EACH_OBJECT_LOCAL_ONLY);
154 if (r)
155 return r;
156 return for_each_packed_object(add_recent_packed, &data,
157 FOR_EACH_OBJECT_LOCAL_ONLY);
160 void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
161 timestamp_t mark_recent, struct progress *progress)
163 struct connectivity_progress cp;
166 * Set up revision parsing, and mark us as being interested
167 * in all object types, not just commits.
169 revs->tag_objects = 1;
170 revs->blob_objects = 1;
171 revs->tree_objects = 1;
173 /* Add all refs from the index file */
174 add_index_objects_to_pending(revs, 0);
176 /* Add all external refs */
177 for_each_ref(add_one_ref, revs);
179 /* detached HEAD is not included in the list above */
180 head_ref(add_one_ref, revs);
181 other_head_refs(add_one_ref, revs);
183 /* Add all reflog info */
184 if (mark_reflog)
185 add_reflogs_to_pending(revs, 0);
187 cp.progress = progress;
188 cp.count = 0;
191 * Set up the revision walk - this will move all commits
192 * from the pending list to the commit walking list.
194 if (prepare_revision_walk(revs))
195 die("revision walk setup failed");
196 traverse_commit_list(revs, mark_commit, mark_object, &cp);
198 if (mark_recent) {
199 revs->ignore_missing_links = 1;
200 if (add_unseen_recent_objects_to_traversal(revs, mark_recent))
201 die("unable to mark recent objects");
202 if (prepare_revision_walk(revs))
203 die("revision walk setup failed");
204 traverse_commit_list(revs, mark_commit, mark_object, &cp);
207 display_progress(cp.progress, cp.count);