shallow: migrate shallow information into the object parser
[git/debian.git] / walker.c
blob3fb5fb2a0beab9bad36c756845cd39ed8221a0f8
1 #include "cache.h"
2 #include "walker.h"
3 #include "object-store.h"
4 #include "commit.h"
5 #include "tree.h"
6 #include "tree-walk.h"
7 #include "tag.h"
8 #include "blob.h"
9 #include "refs.h"
11 static struct object_id current_commit_oid;
13 void walker_say(struct walker *walker, const char *fmt, ...)
15 if (walker->get_verbosely) {
16 va_list ap;
17 va_start(ap, fmt);
18 vfprintf(stderr, fmt, ap);
19 va_end(ap);
23 static void report_missing(const struct object *obj)
25 fprintf(stderr, "Cannot obtain needed %s %s\n",
26 obj->type ? type_name(obj->type): "object",
27 oid_to_hex(&obj->oid));
28 if (!is_null_oid(&current_commit_oid))
29 fprintf(stderr, "while processing commit %s.\n",
30 oid_to_hex(&current_commit_oid));
33 static int process(struct walker *walker, struct object *obj);
35 static int process_tree(struct walker *walker, struct tree *tree)
37 struct tree_desc desc;
38 struct name_entry entry;
40 if (parse_tree(tree))
41 return -1;
43 init_tree_desc(&desc, tree->buffer, tree->size);
44 while (tree_entry(&desc, &entry)) {
45 struct object *obj = NULL;
47 /* submodule commits are not stored in the superproject */
48 if (S_ISGITLINK(entry.mode))
49 continue;
50 if (S_ISDIR(entry.mode)) {
51 struct tree *tree = lookup_tree(entry.oid);
52 if (tree)
53 obj = &tree->object;
55 else {
56 struct blob *blob = lookup_blob(entry.oid);
57 if (blob)
58 obj = &blob->object;
60 if (!obj || process(walker, obj))
61 return -1;
63 free_tree_buffer(tree);
64 return 0;
67 /* Remember to update object flag allocation in object.h */
68 #define COMPLETE (1U << 0)
69 #define SEEN (1U << 1)
70 #define TO_SCAN (1U << 2)
72 static struct commit_list *complete = NULL;
74 static int process_commit(struct walker *walker, struct commit *commit)
76 if (parse_commit(commit))
77 return -1;
79 while (complete && complete->item->date >= commit->date) {
80 pop_most_recent_commit(&complete, COMPLETE);
83 if (commit->object.flags & COMPLETE)
84 return 0;
86 oidcpy(&current_commit_oid, &commit->object.oid);
88 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
90 if (walker->get_tree) {
91 if (process(walker, &commit->tree->object))
92 return -1;
93 if (!walker->get_all)
94 walker->get_tree = 0;
96 if (walker->get_history) {
97 struct commit_list *parents = commit->parents;
98 for (; parents; parents = parents->next) {
99 if (process(walker, &parents->item->object))
100 return -1;
103 return 0;
106 static int process_tag(struct walker *walker, struct tag *tag)
108 if (parse_tag(tag))
109 return -1;
110 return process(walker, tag->tagged);
113 static struct object_list *process_queue = NULL;
114 static struct object_list **process_queue_end = &process_queue;
116 static int process_object(struct walker *walker, struct object *obj)
118 if (obj->type == OBJ_COMMIT) {
119 if (process_commit(walker, (struct commit *)obj))
120 return -1;
121 return 0;
123 if (obj->type == OBJ_TREE) {
124 if (process_tree(walker, (struct tree *)obj))
125 return -1;
126 return 0;
128 if (obj->type == OBJ_BLOB) {
129 return 0;
131 if (obj->type == OBJ_TAG) {
132 if (process_tag(walker, (struct tag *)obj))
133 return -1;
134 return 0;
136 return error("Unable to determine requirements "
137 "of type %s for %s",
138 type_name(obj->type), oid_to_hex(&obj->oid));
141 static int process(struct walker *walker, struct object *obj)
143 if (obj->flags & SEEN)
144 return 0;
145 obj->flags |= SEEN;
147 if (has_object_file(&obj->oid)) {
148 /* We already have it, so we should scan it now. */
149 obj->flags |= TO_SCAN;
151 else {
152 if (obj->flags & COMPLETE)
153 return 0;
154 walker->prefetch(walker, obj->oid.hash);
157 object_list_insert(obj, process_queue_end);
158 process_queue_end = &(*process_queue_end)->next;
159 return 0;
162 static int loop(struct walker *walker)
164 struct object_list *elem;
166 while (process_queue) {
167 struct object *obj = process_queue->item;
168 elem = process_queue;
169 process_queue = elem->next;
170 free(elem);
171 if (!process_queue)
172 process_queue_end = &process_queue;
174 /* If we are not scanning this object, we placed it in
175 * the queue because we needed to fetch it first.
177 if (! (obj->flags & TO_SCAN)) {
178 if (walker->fetch(walker, obj->oid.hash)) {
179 report_missing(obj);
180 return -1;
183 if (!obj->type)
184 parse_object(&obj->oid);
185 if (process_object(walker, obj))
186 return -1;
188 return 0;
191 static int interpret_target(struct walker *walker, char *target, struct object_id *oid)
193 if (!get_oid_hex(target, oid))
194 return 0;
195 if (!check_refname_format(target, 0)) {
196 struct ref *ref = alloc_ref(target);
197 if (!walker->fetch_ref(walker, ref)) {
198 oidcpy(oid, &ref->old_oid);
199 free(ref);
200 return 0;
202 free(ref);
204 return -1;
207 static int mark_complete(const char *path, const struct object_id *oid,
208 int flag, void *cb_data)
210 struct commit *commit = lookup_commit_reference_gently(oid, 1);
212 if (commit) {
213 commit->object.flags |= COMPLETE;
214 commit_list_insert(commit, &complete);
216 return 0;
219 int walker_targets_stdin(char ***target, const char ***write_ref)
221 int targets = 0, targets_alloc = 0;
222 struct strbuf buf = STRBUF_INIT;
223 *target = NULL; *write_ref = NULL;
224 while (1) {
225 char *rf_one = NULL;
226 char *tg_one;
228 if (strbuf_getline_lf(&buf, stdin) == EOF)
229 break;
230 tg_one = buf.buf;
231 rf_one = strchr(tg_one, '\t');
232 if (rf_one)
233 *rf_one++ = 0;
235 if (targets >= targets_alloc) {
236 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
237 REALLOC_ARRAY(*target, targets_alloc);
238 REALLOC_ARRAY(*write_ref, targets_alloc);
240 (*target)[targets] = xstrdup(tg_one);
241 (*write_ref)[targets] = xstrdup_or_null(rf_one);
242 targets++;
244 strbuf_release(&buf);
245 return targets;
248 void walker_targets_free(int targets, char **target, const char **write_ref)
250 while (targets--) {
251 free(target[targets]);
252 if (write_ref)
253 free((char *) write_ref[targets]);
257 int walker_fetch(struct walker *walker, int targets, char **target,
258 const char **write_ref, const char *write_ref_log_details)
260 struct strbuf refname = STRBUF_INIT;
261 struct strbuf err = STRBUF_INIT;
262 struct ref_transaction *transaction = NULL;
263 struct object_id *oids = xmalloc(targets * sizeof(struct object_id));
264 char *msg = NULL;
265 int i, ret = -1;
267 save_commit_buffer = 0;
269 if (write_ref) {
270 transaction = ref_transaction_begin(&err);
271 if (!transaction) {
272 error("%s", err.buf);
273 goto done;
277 if (!walker->get_recover) {
278 for_each_ref(mark_complete, NULL);
279 commit_list_sort_by_date(&complete);
282 for (i = 0; i < targets; i++) {
283 if (interpret_target(walker, target[i], oids + i)) {
284 error("Could not interpret response from server '%s' as something to pull", target[i]);
285 goto done;
287 if (process(walker, lookup_unknown_object(oids[i].hash)))
288 goto done;
291 if (loop(walker))
292 goto done;
293 if (!write_ref) {
294 ret = 0;
295 goto done;
297 if (write_ref_log_details) {
298 msg = xstrfmt("fetch from %s", write_ref_log_details);
299 } else {
300 msg = NULL;
302 for (i = 0; i < targets; i++) {
303 if (!write_ref[i])
304 continue;
305 strbuf_reset(&refname);
306 strbuf_addf(&refname, "refs/%s", write_ref[i]);
307 if (ref_transaction_update(transaction, refname.buf,
308 oids + i, NULL, 0,
309 msg ? msg : "fetch (unknown)",
310 &err)) {
311 error("%s", err.buf);
312 goto done;
315 if (ref_transaction_commit(transaction, &err)) {
316 error("%s", err.buf);
317 goto done;
320 ret = 0;
322 done:
323 ref_transaction_free(transaction);
324 free(msg);
325 free(oids);
326 strbuf_release(&err);
327 strbuf_release(&refname);
328 return ret;
331 void walker_free(struct walker *walker)
333 walker->cleanup(walker);
334 free(walker);