object: add repository argument to object_as_type
[git/git.git] / reachable.c
CommitLineData
94421474
JH
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"
dc347195 10#include "progress.h"
5f78a431 11#include "list-objects.h"
7709f468 12#include "packfile.h"
d0c39a49 13#include "worktree.h"
a80d72db 14#include "object-store.h"
94421474 15
0b26abc0
JK
16struct connectivity_progress {
17 struct progress *progress;
18 unsigned long count;
19};
20
21static void update_progress(struct connectivity_progress *cp)
22{
23 cp->count++;
24 if ((cp->count & 1023) == 0)
25 display_progress(cp->progress, cp->count);
26}
27
635170f2
MH
28static int add_one_ref(const char *path, const struct object_id *oid,
29 int flag, void *cb_data)
94421474 30{
94421474 31 struct rev_info *revs = (struct rev_info *)cb_data;
14886b40 32 struct object *object;
94421474 33
14886b40
JS
34 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
35 warning("symbolic ref is dangling: %s", path);
36 return 0;
37 }
38
c251c83d 39 object = parse_object_or_die(oid, path);
94421474
JH
40 add_pending_object(revs, object, "");
41
42 return 0;
43}
44
5f78a431
JK
45/*
46 * The traversal will have already marked us as SEEN, so we
47 * only need to handle any progress reporting here.
48 */
de1e67d0 49static void mark_object(struct object *obj, const char *name, void *data)
5f78a431
JK
50{
51 update_progress(data);
52}
53
54static void mark_commit(struct commit *c, void *data)
55{
de1e67d0 56 mark_object(&c->object, NULL, data);
5f78a431
JK
57}
58
d3038d22
JK
59struct recent_data {
60 struct rev_info *revs;
dddbad72 61 timestamp_t timestamp;
d3038d22
JK
62};
63
76c1d9a0 64static void add_recent_object(const struct object_id *oid,
dddbad72 65 timestamp_t mtime,
d3038d22
JK
66 struct recent_data *data)
67{
68 struct object *obj;
69 enum object_type type;
70
71 if (mtime <= data->timestamp)
72 return;
73
74 /*
75 * We do not want to call parse_object here, because
76 * inflating blobs and trees could be very expensive.
77 * However, we do need to know the correct type for
78 * later processing, and the revision machinery expects
79 * commits and tags to have been parsed.
80 */
0df8e965 81 type = oid_object_info(the_repository, oid, NULL);
d3038d22 82 if (type < 0)
76c1d9a0 83 die("unable to get object info for %s", oid_to_hex(oid));
d3038d22
JK
84
85 switch (type) {
86 case OBJ_TAG:
87 case OBJ_COMMIT:
c251c83d 88 obj = parse_object_or_die(oid, NULL);
d3038d22
JK
89 break;
90 case OBJ_TREE:
740ee055 91 obj = (struct object *)lookup_tree(oid);
d3038d22
JK
92 break;
93 case OBJ_BLOB:
3aca1fc6 94 obj = (struct object *)lookup_blob(oid);
d3038d22
JK
95 break;
96 default:
97 die("unknown object type for %s: %s",
debca9d2 98 oid_to_hex(oid), type_name(type));
d3038d22
JK
99 }
100
101 if (!obj)
76c1d9a0 102 die("unable to lookup %s", oid_to_hex(oid));
d3038d22
JK
103
104 add_pending_object(data->revs, obj, "");
105}
106
76c1d9a0 107static int add_recent_loose(const struct object_id *oid,
d3038d22
JK
108 const char *path, void *data)
109{
110 struct stat st;
5abddd1e 111 struct object *obj = lookup_object(the_repository, oid->hash);
d3038d22
JK
112
113 if (obj && obj->flags & SEEN)
114 return 0;
115
116 if (stat(path, &st) < 0) {
117 /*
118 * It's OK if an object went away during our iteration; this
119 * could be due to a simultaneous repack. But anything else
120 * we should abort, since we might then fail to mark objects
121 * which should not be pruned.
122 */
123 if (errno == ENOENT)
124 return 0;
76c1d9a0 125 return error_errno("unable to stat %s", oid_to_hex(oid));
d3038d22
JK
126 }
127
76c1d9a0 128 add_recent_object(oid, st.st_mtime, data);
d3038d22
JK
129 return 0;
130}
131
76c1d9a0 132static int add_recent_packed(const struct object_id *oid,
d3038d22
JK
133 struct packed_git *p, uint32_t pos,
134 void *data)
135{
5abddd1e 136 struct object *obj = lookup_object(the_repository, oid->hash);
d3038d22
JK
137
138 if (obj && obj->flags & SEEN)
139 return 0;
76c1d9a0 140 add_recent_object(oid, p->mtime, data);
d3038d22
JK
141 return 0;
142}
143
abcb8655 144int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
dddbad72 145 timestamp_t timestamp)
d3038d22
JK
146{
147 struct recent_data data;
148 int r;
149
150 data.revs = revs;
151 data.timestamp = timestamp;
152
1385bb7b
JK
153 r = for_each_loose_object(add_recent_loose, &data,
154 FOR_EACH_OBJECT_LOCAL_ONLY);
d3038d22
JK
155 if (r)
156 return r;
1385bb7b
JK
157 return for_each_packed_object(add_recent_packed, &data,
158 FOR_EACH_OBJECT_LOCAL_ONLY);
d3038d22
JK
159}
160
dc347195 161void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
dddbad72 162 timestamp_t mark_recent, struct progress *progress)
94421474 163{
0b26abc0
JK
164 struct connectivity_progress cp;
165
94421474
JH
166 /*
167 * Set up revision parsing, and mark us as being interested
168 * in all object types, not just commits.
169 */
170 revs->tag_objects = 1;
171 revs->blob_objects = 1;
172 revs->tree_objects = 1;
173
174 /* Add all refs from the index file */
1be111d8 175 add_index_objects_to_pending(revs, 0);
94421474
JH
176
177 /* Add all external refs */
635170f2 178 for_each_ref(add_one_ref, revs);
94421474 179
c40fdd01 180 /* detached HEAD is not included in the list above */
635170f2 181 head_ref(add_one_ref, revs);
d0c39a49 182 other_head_refs(add_one_ref, revs);
c40fdd01 183
eb8381c8 184 /* Add all reflog info */
94421474 185 if (mark_reflog)
718ccc97 186 add_reflogs_to_pending(revs, 0);
94421474 187
0b26abc0
JK
188 cp.progress = progress;
189 cp.count = 0;
190
94421474
JH
191 /*
192 * Set up the revision walk - this will move all commits
193 * from the pending list to the commit walking list.
194 */
3d51e1b5
MK
195 if (prepare_revision_walk(revs))
196 die("revision walk setup failed");
5f78a431 197 traverse_commit_list(revs, mark_commit, mark_object, &cp);
d3038d22
JK
198
199 if (mark_recent) {
200 revs->ignore_missing_links = 1;
201 if (add_unseen_recent_objects_to_traversal(revs, mark_recent))
202 die("unable to mark recent objects");
203 if (prepare_revision_walk(revs))
204 die("revision walk setup failed");
205 traverse_commit_list(revs, mark_commit, mark_object, &cp);
206 }
207
0b26abc0 208 display_progress(cp.progress, cp.count);
94421474 209}