Merge branch 'jk/branch-l-0-deprecation'
[git.git] / walker.c
blob86359ab0ab62990cf7097b0d59940f1f0b04fec5
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 struct commit_list *parents;
78 if (parse_commit(commit))
79 return -1;
81 while (complete && complete->item->date >= commit->date) {
82 pop_most_recent_commit(&complete, COMPLETE);
85 if (commit->object.flags & COMPLETE)
86 return 0;
88 oidcpy(&current_commit_oid, &commit->object.oid);
90 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
92 if (process(walker, &get_commit_tree(commit)->object))
93 return -1;
95 for (parents = commit->parents; parents; parents = parents->next) {
96 if (process(walker, &parents->item->object))
97 return -1;
100 return 0;
103 static int process_tag(struct walker *walker, struct tag *tag)
105 if (parse_tag(tag))
106 return -1;
107 return process(walker, tag->tagged);
110 static struct object_list *process_queue = NULL;
111 static struct object_list **process_queue_end = &process_queue;
113 static int process_object(struct walker *walker, struct object *obj)
115 if (obj->type == OBJ_COMMIT) {
116 if (process_commit(walker, (struct commit *)obj))
117 return -1;
118 return 0;
120 if (obj->type == OBJ_TREE) {
121 if (process_tree(walker, (struct tree *)obj))
122 return -1;
123 return 0;
125 if (obj->type == OBJ_BLOB) {
126 return 0;
128 if (obj->type == OBJ_TAG) {
129 if (process_tag(walker, (struct tag *)obj))
130 return -1;
131 return 0;
133 return error("Unable to determine requirements "
134 "of type %s for %s",
135 type_name(obj->type), oid_to_hex(&obj->oid));
138 static int process(struct walker *walker, struct object *obj)
140 if (obj->flags & SEEN)
141 return 0;
142 obj->flags |= SEEN;
144 if (has_object_file(&obj->oid)) {
145 /* We already have it, so we should scan it now. */
146 obj->flags |= TO_SCAN;
148 else {
149 if (obj->flags & COMPLETE)
150 return 0;
151 walker->prefetch(walker, obj->oid.hash);
154 object_list_insert(obj, process_queue_end);
155 process_queue_end = &(*process_queue_end)->next;
156 return 0;
159 static int loop(struct walker *walker)
161 struct object_list *elem;
163 while (process_queue) {
164 struct object *obj = process_queue->item;
165 elem = process_queue;
166 process_queue = elem->next;
167 free(elem);
168 if (!process_queue)
169 process_queue_end = &process_queue;
171 /* If we are not scanning this object, we placed it in
172 * the queue because we needed to fetch it first.
174 if (! (obj->flags & TO_SCAN)) {
175 if (walker->fetch(walker, obj->oid.hash)) {
176 report_missing(obj);
177 return -1;
180 if (!obj->type)
181 parse_object(&obj->oid);
182 if (process_object(walker, obj))
183 return -1;
185 return 0;
188 static int interpret_target(struct walker *walker, char *target, struct object_id *oid)
190 if (!get_oid_hex(target, oid))
191 return 0;
192 if (!check_refname_format(target, 0)) {
193 struct ref *ref = alloc_ref(target);
194 if (!walker->fetch_ref(walker, ref)) {
195 oidcpy(oid, &ref->old_oid);
196 free(ref);
197 return 0;
199 free(ref);
201 return -1;
204 static int mark_complete(const char *path, const struct object_id *oid,
205 int flag, void *cb_data)
207 struct commit *commit = lookup_commit_reference_gently(oid, 1);
209 if (commit) {
210 commit->object.flags |= COMPLETE;
211 commit_list_insert(commit, &complete);
213 return 0;
216 int walker_targets_stdin(char ***target, const char ***write_ref)
218 int targets = 0, targets_alloc = 0;
219 struct strbuf buf = STRBUF_INIT;
220 *target = NULL; *write_ref = NULL;
221 while (1) {
222 char *rf_one = NULL;
223 char *tg_one;
225 if (strbuf_getline_lf(&buf, stdin) == EOF)
226 break;
227 tg_one = buf.buf;
228 rf_one = strchr(tg_one, '\t');
229 if (rf_one)
230 *rf_one++ = 0;
232 if (targets >= targets_alloc) {
233 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
234 REALLOC_ARRAY(*target, targets_alloc);
235 REALLOC_ARRAY(*write_ref, targets_alloc);
237 (*target)[targets] = xstrdup(tg_one);
238 (*write_ref)[targets] = xstrdup_or_null(rf_one);
239 targets++;
241 strbuf_release(&buf);
242 return targets;
245 void walker_targets_free(int targets, char **target, const char **write_ref)
247 while (targets--) {
248 free(target[targets]);
249 if (write_ref)
250 free((char *) write_ref[targets]);
254 int walker_fetch(struct walker *walker, int targets, char **target,
255 const char **write_ref, const char *write_ref_log_details)
257 struct strbuf refname = STRBUF_INIT;
258 struct strbuf err = STRBUF_INIT;
259 struct ref_transaction *transaction = NULL;
260 struct object_id *oids = xmalloc(targets * sizeof(struct object_id));
261 char *msg = NULL;
262 int i, ret = -1;
264 save_commit_buffer = 0;
266 if (write_ref) {
267 transaction = ref_transaction_begin(&err);
268 if (!transaction) {
269 error("%s", err.buf);
270 goto done;
274 if (!walker->get_recover) {
275 for_each_ref(mark_complete, NULL);
276 commit_list_sort_by_date(&complete);
279 for (i = 0; i < targets; i++) {
280 if (interpret_target(walker, target[i], oids + i)) {
281 error("Could not interpret response from server '%s' as something to pull", target[i]);
282 goto done;
284 if (process(walker, lookup_unknown_object(oids[i].hash)))
285 goto done;
288 if (loop(walker))
289 goto done;
290 if (!write_ref) {
291 ret = 0;
292 goto done;
294 if (write_ref_log_details) {
295 msg = xstrfmt("fetch from %s", write_ref_log_details);
296 } else {
297 msg = NULL;
299 for (i = 0; i < targets; i++) {
300 if (!write_ref[i])
301 continue;
302 strbuf_reset(&refname);
303 strbuf_addf(&refname, "refs/%s", write_ref[i]);
304 if (ref_transaction_update(transaction, refname.buf,
305 oids + i, NULL, 0,
306 msg ? msg : "fetch (unknown)",
307 &err)) {
308 error("%s", err.buf);
309 goto done;
312 if (ref_transaction_commit(transaction, &err)) {
313 error("%s", err.buf);
314 goto done;
317 ret = 0;
319 done:
320 ref_transaction_free(transaction);
321 free(msg);
322 free(oids);
323 strbuf_release(&err);
324 strbuf_release(&refname);
325 return ret;
328 void walker_free(struct walker *walker)
330 walker->cleanup(walker);
331 free(walker);