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