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