]> git.ipfire.org Git - thirdparty/git.git/blob - walker.c
cmake: use test names instead of full paths
[thirdparty/git.git] / walker.c
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "hex.h"
4 #include "walker.h"
5 #include "repository.h"
6 #include "object-store.h"
7 #include "commit.h"
8 #include "tree.h"
9 #include "tree-walk.h"
10 #include "tag.h"
11 #include "blob.h"
12 #include "refs.h"
13 #include "progress.h"
14
15 static struct object_id current_commit_oid;
16
17 void walker_say(struct walker *walker, const char *fmt, ...)
18 {
19 if (walker->get_verbosely) {
20 va_list ap;
21 va_start(ap, fmt);
22 vfprintf(stderr, fmt, ap);
23 va_end(ap);
24 }
25 }
26
27 static void report_missing(const struct object *obj)
28 {
29 fprintf(stderr, "Cannot obtain needed %s %s\n",
30 obj->type ? type_name(obj->type): "object",
31 oid_to_hex(&obj->oid));
32 if (!is_null_oid(&current_commit_oid))
33 fprintf(stderr, "while processing commit %s.\n",
34 oid_to_hex(&current_commit_oid));
35 }
36
37 static int process(struct walker *walker, struct object *obj);
38
39 static int process_tree(struct walker *walker, struct tree *tree)
40 {
41 struct tree_desc desc;
42 struct name_entry entry;
43
44 if (parse_tree(tree))
45 return -1;
46
47 init_tree_desc(&desc, tree->buffer, tree->size);
48 while (tree_entry(&desc, &entry)) {
49 struct object *obj = NULL;
50
51 /* submodule commits are not stored in the superproject */
52 if (S_ISGITLINK(entry.mode))
53 continue;
54 if (S_ISDIR(entry.mode)) {
55 struct tree *tree = lookup_tree(the_repository,
56 &entry.oid);
57 if (tree)
58 obj = &tree->object;
59 }
60 else {
61 struct blob *blob = lookup_blob(the_repository,
62 &entry.oid);
63 if (blob)
64 obj = &blob->object;
65 }
66 if (!obj || process(walker, obj))
67 return -1;
68 }
69 free_tree_buffer(tree);
70 return 0;
71 }
72
73 /* Remember to update object flag allocation in object.h */
74 #define COMPLETE (1U << 0)
75 #define SEEN (1U << 1)
76 #define TO_SCAN (1U << 2)
77
78 static struct commit_list *complete = NULL;
79
80 static int process_commit(struct walker *walker, struct commit *commit)
81 {
82 struct commit_list *parents;
83
84 if (repo_parse_commit(the_repository, commit))
85 return -1;
86
87 while (complete && complete->item->date >= commit->date) {
88 pop_most_recent_commit(&complete, COMPLETE);
89 }
90
91 if (commit->object.flags & COMPLETE)
92 return 0;
93
94 oidcpy(&current_commit_oid, &commit->object.oid);
95
96 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
97
98 if (process(walker, &repo_get_commit_tree(the_repository, commit)->object))
99 return -1;
100
101 for (parents = commit->parents; parents; parents = parents->next) {
102 if (process(walker, &parents->item->object))
103 return -1;
104 }
105
106 return 0;
107 }
108
109 static int process_tag(struct walker *walker, struct tag *tag)
110 {
111 if (parse_tag(tag))
112 return -1;
113 return process(walker, tag->tagged);
114 }
115
116 static struct object_list *process_queue = NULL;
117 static struct object_list **process_queue_end = &process_queue;
118
119 static int process_object(struct walker *walker, struct object *obj)
120 {
121 if (obj->type == OBJ_COMMIT) {
122 if (process_commit(walker, (struct commit *)obj))
123 return -1;
124 return 0;
125 }
126 if (obj->type == OBJ_TREE) {
127 if (process_tree(walker, (struct tree *)obj))
128 return -1;
129 return 0;
130 }
131 if (obj->type == OBJ_BLOB) {
132 return 0;
133 }
134 if (obj->type == OBJ_TAG) {
135 if (process_tag(walker, (struct tag *)obj))
136 return -1;
137 return 0;
138 }
139 return error("Unable to determine requirements "
140 "of type %s for %s",
141 type_name(obj->type), oid_to_hex(&obj->oid));
142 }
143
144 static int process(struct walker *walker, struct object *obj)
145 {
146 if (obj->flags & SEEN)
147 return 0;
148 obj->flags |= SEEN;
149
150 if (repo_has_object_file(the_repository, &obj->oid)) {
151 /* We already have it, so we should scan it now. */
152 obj->flags |= TO_SCAN;
153 }
154 else {
155 if (obj->flags & COMPLETE)
156 return 0;
157 walker->prefetch(walker, obj->oid.hash);
158 }
159
160 object_list_insert(obj, process_queue_end);
161 process_queue_end = &(*process_queue_end)->next;
162 return 0;
163 }
164
165 static int loop(struct walker *walker)
166 {
167 struct object_list *elem;
168 struct progress *progress = NULL;
169 uint64_t nr = 0;
170
171 if (walker->get_progress)
172 progress = start_delayed_progress(_("Fetching objects"), 0);
173
174 while (process_queue) {
175 struct object *obj = process_queue->item;
176 elem = process_queue;
177 process_queue = elem->next;
178 free(elem);
179 if (!process_queue)
180 process_queue_end = &process_queue;
181
182 /* If we are not scanning this object, we placed it in
183 * the queue because we needed to fetch it first.
184 */
185 if (! (obj->flags & TO_SCAN)) {
186 if (walker->fetch(walker, obj->oid.hash)) {
187 stop_progress(&progress);
188 report_missing(obj);
189 return -1;
190 }
191 }
192 if (!obj->type)
193 parse_object(the_repository, &obj->oid);
194 if (process_object(walker, obj)) {
195 stop_progress(&progress);
196 return -1;
197 }
198 display_progress(progress, ++nr);
199 }
200 stop_progress(&progress);
201 return 0;
202 }
203
204 static int interpret_target(struct walker *walker, char *target, struct object_id *oid)
205 {
206 if (!get_oid_hex(target, oid))
207 return 0;
208 if (!check_refname_format(target, 0)) {
209 struct ref *ref = alloc_ref(target);
210 if (!walker->fetch_ref(walker, ref)) {
211 oidcpy(oid, &ref->old_oid);
212 free(ref);
213 return 0;
214 }
215 free(ref);
216 }
217 return -1;
218 }
219
220 static int mark_complete(const char *path UNUSED,
221 const struct object_id *oid,
222 int flag UNUSED,
223 void *cb_data UNUSED)
224 {
225 struct commit *commit = lookup_commit_reference_gently(the_repository,
226 oid, 1);
227
228 if (commit) {
229 commit->object.flags |= COMPLETE;
230 commit_list_insert(commit, &complete);
231 }
232 return 0;
233 }
234
235 int walker_targets_stdin(char ***target, const char ***write_ref)
236 {
237 int targets = 0, targets_alloc = 0;
238 struct strbuf buf = STRBUF_INIT;
239 *target = NULL; *write_ref = NULL;
240 while (1) {
241 char *rf_one = NULL;
242 char *tg_one;
243
244 if (strbuf_getline_lf(&buf, stdin) == EOF)
245 break;
246 tg_one = buf.buf;
247 rf_one = strchr(tg_one, '\t');
248 if (rf_one)
249 *rf_one++ = 0;
250
251 if (targets >= targets_alloc) {
252 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
253 REALLOC_ARRAY(*target, targets_alloc);
254 REALLOC_ARRAY(*write_ref, targets_alloc);
255 }
256 (*target)[targets] = xstrdup(tg_one);
257 (*write_ref)[targets] = xstrdup_or_null(rf_one);
258 targets++;
259 }
260 strbuf_release(&buf);
261 return targets;
262 }
263
264 void walker_targets_free(int targets, char **target, const char **write_ref)
265 {
266 while (targets--) {
267 free(target[targets]);
268 if (write_ref)
269 free((char *) write_ref[targets]);
270 }
271 }
272
273 int walker_fetch(struct walker *walker, int targets, char **target,
274 const char **write_ref, const char *write_ref_log_details)
275 {
276 struct strbuf refname = STRBUF_INIT;
277 struct strbuf err = STRBUF_INIT;
278 struct ref_transaction *transaction = NULL;
279 struct object_id *oids;
280 char *msg = NULL;
281 int i, ret = -1;
282
283 save_commit_buffer = 0;
284
285 ALLOC_ARRAY(oids, targets);
286
287 if (write_ref) {
288 transaction = ref_transaction_begin(&err);
289 if (!transaction) {
290 error("%s", err.buf);
291 goto done;
292 }
293 }
294
295 if (!walker->get_recover) {
296 for_each_ref(mark_complete, NULL);
297 commit_list_sort_by_date(&complete);
298 }
299
300 for (i = 0; i < targets; i++) {
301 if (interpret_target(walker, target[i], oids + i)) {
302 error("Could not interpret response from server '%s' as something to pull", target[i]);
303 goto done;
304 }
305 if (process(walker, lookup_unknown_object(the_repository, &oids[i])))
306 goto done;
307 }
308
309 if (loop(walker))
310 goto done;
311 if (!write_ref) {
312 ret = 0;
313 goto done;
314 }
315 if (write_ref_log_details) {
316 msg = xstrfmt("fetch from %s", write_ref_log_details);
317 } else {
318 msg = NULL;
319 }
320 for (i = 0; i < targets; i++) {
321 if (!write_ref[i])
322 continue;
323 strbuf_reset(&refname);
324 strbuf_addf(&refname, "refs/%s", write_ref[i]);
325 if (ref_transaction_update(transaction, refname.buf,
326 oids + i, NULL, 0,
327 msg ? msg : "fetch (unknown)",
328 &err)) {
329 error("%s", err.buf);
330 goto done;
331 }
332 }
333 if (ref_transaction_commit(transaction, &err)) {
334 error("%s", err.buf);
335 goto done;
336 }
337
338 ret = 0;
339
340 done:
341 ref_transaction_free(transaction);
342 free(msg);
343 free(oids);
344 strbuf_release(&err);
345 strbuf_release(&refname);
346 return ret;
347 }
348
349 void walker_free(struct walker *walker)
350 {
351 walker->cleanup(walker);
352 free(walker);
353 }