Merge branch 'sm/ssl-key-type-config'
[alt-git.git] / walker.c
blobc046936378903fbc5f6cf31abb2c0cc0bcf41933
1 #include "cache.h"
2 #include "hex.h"
3 #include "walker.h"
4 #include "repository.h"
5 #include "object-store.h"
6 #include "commit.h"
7 #include "tree.h"
8 #include "tree-walk.h"
9 #include "tag.h"
10 #include "blob.h"
11 #include "refs.h"
12 #include "progress.h"
14 static struct object_id current_commit_oid;
16 void walker_say(struct walker *walker, const char *fmt, ...)
18 if (walker->get_verbosely) {
19 va_list ap;
20 va_start(ap, fmt);
21 vfprintf(stderr, fmt, ap);
22 va_end(ap);
26 static void report_missing(const struct object *obj)
28 fprintf(stderr, "Cannot obtain needed %s %s\n",
29 obj->type ? type_name(obj->type): "object",
30 oid_to_hex(&obj->oid));
31 if (!is_null_oid(&current_commit_oid))
32 fprintf(stderr, "while processing commit %s.\n",
33 oid_to_hex(&current_commit_oid));
36 static int process(struct walker *walker, struct object *obj);
38 static int process_tree(struct walker *walker, struct tree *tree)
40 struct tree_desc desc;
41 struct name_entry entry;
43 if (parse_tree(tree))
44 return -1;
46 init_tree_desc(&desc, tree->buffer, tree->size);
47 while (tree_entry(&desc, &entry)) {
48 struct object *obj = NULL;
50 /* submodule commits are not stored in the superproject */
51 if (S_ISGITLINK(entry.mode))
52 continue;
53 if (S_ISDIR(entry.mode)) {
54 struct tree *tree = lookup_tree(the_repository,
55 &entry.oid);
56 if (tree)
57 obj = &tree->object;
59 else {
60 struct blob *blob = lookup_blob(the_repository,
61 &entry.oid);
62 if (blob)
63 obj = &blob->object;
65 if (!obj || process(walker, obj))
66 return -1;
68 free_tree_buffer(tree);
69 return 0;
72 /* Remember to update object flag allocation in object.h */
73 #define COMPLETE (1U << 0)
74 #define SEEN (1U << 1)
75 #define TO_SCAN (1U << 2)
77 static struct commit_list *complete = NULL;
79 static int process_commit(struct walker *walker, struct commit *commit)
81 struct commit_list *parents;
83 if (parse_commit(commit))
84 return -1;
86 while (complete && complete->item->date >= commit->date) {
87 pop_most_recent_commit(&complete, COMPLETE);
90 if (commit->object.flags & COMPLETE)
91 return 0;
93 oidcpy(&current_commit_oid, &commit->object.oid);
95 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
97 if (process(walker, &get_commit_tree(commit)->object))
98 return -1;
100 for (parents = commit->parents; parents; parents = parents->next) {
101 if (process(walker, &parents->item->object))
102 return -1;
105 return 0;
108 static int process_tag(struct walker *walker, struct tag *tag)
110 if (parse_tag(tag))
111 return -1;
112 return process(walker, tag->tagged);
115 static struct object_list *process_queue = NULL;
116 static struct object_list **process_queue_end = &process_queue;
118 static int process_object(struct walker *walker, struct object *obj)
120 if (obj->type == OBJ_COMMIT) {
121 if (process_commit(walker, (struct commit *)obj))
122 return -1;
123 return 0;
125 if (obj->type == OBJ_TREE) {
126 if (process_tree(walker, (struct tree *)obj))
127 return -1;
128 return 0;
130 if (obj->type == OBJ_BLOB) {
131 return 0;
133 if (obj->type == OBJ_TAG) {
134 if (process_tag(walker, (struct tag *)obj))
135 return -1;
136 return 0;
138 return error("Unable to determine requirements "
139 "of type %s for %s",
140 type_name(obj->type), oid_to_hex(&obj->oid));
143 static int process(struct walker *walker, struct object *obj)
145 if (obj->flags & SEEN)
146 return 0;
147 obj->flags |= SEEN;
149 if (has_object_file(&obj->oid)) {
150 /* We already have it, so we should scan it now. */
151 obj->flags |= TO_SCAN;
153 else {
154 if (obj->flags & COMPLETE)
155 return 0;
156 walker->prefetch(walker, obj->oid.hash);
159 object_list_insert(obj, process_queue_end);
160 process_queue_end = &(*process_queue_end)->next;
161 return 0;
164 static int loop(struct walker *walker)
166 struct object_list *elem;
167 struct progress *progress = NULL;
168 uint64_t nr = 0;
170 if (walker->get_progress)
171 progress = start_delayed_progress(_("Fetching objects"), 0);
173 while (process_queue) {
174 struct object *obj = process_queue->item;
175 elem = process_queue;
176 process_queue = elem->next;
177 free(elem);
178 if (!process_queue)
179 process_queue_end = &process_queue;
181 /* If we are not scanning this object, we placed it in
182 * the queue because we needed to fetch it first.
184 if (! (obj->flags & TO_SCAN)) {
185 if (walker->fetch(walker, obj->oid.hash)) {
186 stop_progress(&progress);
187 report_missing(obj);
188 return -1;
191 if (!obj->type)
192 parse_object(the_repository, &obj->oid);
193 if (process_object(walker, obj)) {
194 stop_progress(&progress);
195 return -1;
197 display_progress(progress, ++nr);
199 stop_progress(&progress);
200 return 0;
203 static int interpret_target(struct walker *walker, char *target, struct object_id *oid)
205 if (!get_oid_hex(target, oid))
206 return 0;
207 if (!check_refname_format(target, 0)) {
208 struct ref *ref = alloc_ref(target);
209 if (!walker->fetch_ref(walker, ref)) {
210 oidcpy(oid, &ref->old_oid);
211 free(ref);
212 return 0;
214 free(ref);
216 return -1;
219 static int mark_complete(const char *path UNUSED,
220 const struct object_id *oid,
221 int flag UNUSED,
222 void *cb_data UNUSED)
224 struct commit *commit = lookup_commit_reference_gently(the_repository,
225 oid, 1);
227 if (commit) {
228 commit->object.flags |= COMPLETE;
229 commit_list_insert(commit, &complete);
231 return 0;
234 int walker_targets_stdin(char ***target, const char ***write_ref)
236 int targets = 0, targets_alloc = 0;
237 struct strbuf buf = STRBUF_INIT;
238 *target = NULL; *write_ref = NULL;
239 while (1) {
240 char *rf_one = NULL;
241 char *tg_one;
243 if (strbuf_getline_lf(&buf, stdin) == EOF)
244 break;
245 tg_one = buf.buf;
246 rf_one = strchr(tg_one, '\t');
247 if (rf_one)
248 *rf_one++ = 0;
250 if (targets >= targets_alloc) {
251 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
252 REALLOC_ARRAY(*target, targets_alloc);
253 REALLOC_ARRAY(*write_ref, targets_alloc);
255 (*target)[targets] = xstrdup(tg_one);
256 (*write_ref)[targets] = xstrdup_or_null(rf_one);
257 targets++;
259 strbuf_release(&buf);
260 return targets;
263 void walker_targets_free(int targets, char **target, const char **write_ref)
265 while (targets--) {
266 free(target[targets]);
267 if (write_ref)
268 free((char *) write_ref[targets]);
272 int walker_fetch(struct walker *walker, int targets, char **target,
273 const char **write_ref, const char *write_ref_log_details)
275 struct strbuf refname = STRBUF_INIT;
276 struct strbuf err = STRBUF_INIT;
277 struct ref_transaction *transaction = NULL;
278 struct object_id *oids;
279 char *msg = NULL;
280 int i, ret = -1;
282 save_commit_buffer = 0;
284 ALLOC_ARRAY(oids, targets);
286 if (write_ref) {
287 transaction = ref_transaction_begin(&err);
288 if (!transaction) {
289 error("%s", err.buf);
290 goto done;
294 if (!walker->get_recover) {
295 for_each_ref(mark_complete, NULL);
296 commit_list_sort_by_date(&complete);
299 for (i = 0; i < targets; i++) {
300 if (interpret_target(walker, target[i], oids + i)) {
301 error("Could not interpret response from server '%s' as something to pull", target[i]);
302 goto done;
304 if (process(walker, lookup_unknown_object(the_repository, &oids[i])))
305 goto done;
308 if (loop(walker))
309 goto done;
310 if (!write_ref) {
311 ret = 0;
312 goto done;
314 if (write_ref_log_details) {
315 msg = xstrfmt("fetch from %s", write_ref_log_details);
316 } else {
317 msg = NULL;
319 for (i = 0; i < targets; i++) {
320 if (!write_ref[i])
321 continue;
322 strbuf_reset(&refname);
323 strbuf_addf(&refname, "refs/%s", write_ref[i]);
324 if (ref_transaction_update(transaction, refname.buf,
325 oids + i, NULL, 0,
326 msg ? msg : "fetch (unknown)",
327 &err)) {
328 error("%s", err.buf);
329 goto done;
332 if (ref_transaction_commit(transaction, &err)) {
333 error("%s", err.buf);
334 goto done;
337 ret = 0;
339 done:
340 ref_transaction_free(transaction);
341 free(msg);
342 free(oids);
343 strbuf_release(&err);
344 strbuf_release(&refname);
345 return ret;
348 void walker_free(struct walker *walker)
350 walker->cleanup(walker);
351 free(walker);