Merge branch 'jk/squelch-false-warning-from-gcc-o3'
[git/git-svn.git] / walker.c
blob2c86e406f92d9bf0063e6be4f9d76fdbbac17c89
1 #include "cache.h"
2 #include "walker.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "tree-walk.h"
6 #include "tag.h"
7 #include "blob.h"
8 #include "refs.h"
10 static unsigned char current_commit_sha1[20];
12 void walker_say(struct walker *walker, const char *fmt, ...)
14 if (walker->get_verbosely) {
15 va_list ap;
16 va_start(ap, fmt);
17 vfprintf(stderr, fmt, ap);
18 va_end(ap);
22 static void report_missing(const struct object *obj)
24 fprintf(stderr, "Cannot obtain needed %s %s\n",
25 obj->type ? typename(obj->type): "object",
26 oid_to_hex(&obj->oid));
27 if (!is_null_sha1(current_commit_sha1))
28 fprintf(stderr, "while processing commit %s.\n",
29 sha1_to_hex(current_commit_sha1));
32 static int process(struct walker *walker, struct object *obj);
34 static int process_tree(struct walker *walker, struct tree *tree)
36 struct tree_desc desc;
37 struct name_entry entry;
39 if (parse_tree(tree))
40 return -1;
42 init_tree_desc(&desc, tree->buffer, tree->size);
43 while (tree_entry(&desc, &entry)) {
44 struct object *obj = NULL;
46 /* submodule commits are not stored in the superproject */
47 if (S_ISGITLINK(entry.mode))
48 continue;
49 if (S_ISDIR(entry.mode)) {
50 struct tree *tree = lookup_tree(entry.oid->hash);
51 if (tree)
52 obj = &tree->object;
54 else {
55 struct blob *blob = lookup_blob(entry.oid->hash);
56 if (blob)
57 obj = &blob->object;
59 if (!obj || process(walker, obj))
60 return -1;
62 free_tree_buffer(tree);
63 return 0;
66 /* Remember to update object flag allocation in object.h */
67 #define COMPLETE (1U << 0)
68 #define SEEN (1U << 1)
69 #define TO_SCAN (1U << 2)
71 static struct commit_list *complete = NULL;
73 static int process_commit(struct walker *walker, struct commit *commit)
75 if (parse_commit(commit))
76 return -1;
78 while (complete && complete->item->date >= commit->date) {
79 pop_most_recent_commit(&complete, COMPLETE);
82 if (commit->object.flags & COMPLETE)
83 return 0;
85 hashcpy(current_commit_sha1, commit->object.oid.hash);
87 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
89 if (walker->get_tree) {
90 if (process(walker, &commit->tree->object))
91 return -1;
92 if (!walker->get_all)
93 walker->get_tree = 0;
95 if (walker->get_history) {
96 struct commit_list *parents = commit->parents;
97 for (; parents; parents = parents->next) {
98 if (process(walker, &parents->item->object))
99 return -1;
102 return 0;
105 static int process_tag(struct walker *walker, struct tag *tag)
107 if (parse_tag(tag))
108 return -1;
109 return process(walker, tag->tagged);
112 static struct object_list *process_queue = NULL;
113 static struct object_list **process_queue_end = &process_queue;
115 static int process_object(struct walker *walker, struct object *obj)
117 if (obj->type == OBJ_COMMIT) {
118 if (process_commit(walker, (struct commit *)obj))
119 return -1;
120 return 0;
122 if (obj->type == OBJ_TREE) {
123 if (process_tree(walker, (struct tree *)obj))
124 return -1;
125 return 0;
127 if (obj->type == OBJ_BLOB) {
128 return 0;
130 if (obj->type == OBJ_TAG) {
131 if (process_tag(walker, (struct tag *)obj))
132 return -1;
133 return 0;
135 return error("Unable to determine requirements "
136 "of type %s for %s",
137 typename(obj->type), oid_to_hex(&obj->oid));
140 static int process(struct walker *walker, struct object *obj)
142 if (obj->flags & SEEN)
143 return 0;
144 obj->flags |= SEEN;
146 if (has_object_file(&obj->oid)) {
147 /* We already have it, so we should scan it now. */
148 obj->flags |= TO_SCAN;
150 else {
151 if (obj->flags & COMPLETE)
152 return 0;
153 walker->prefetch(walker, obj->oid.hash);
156 object_list_insert(obj, process_queue_end);
157 process_queue_end = &(*process_queue_end)->next;
158 return 0;
161 static int loop(struct walker *walker)
163 struct object_list *elem;
165 while (process_queue) {
166 struct object *obj = process_queue->item;
167 elem = process_queue;
168 process_queue = elem->next;
169 free(elem);
170 if (!process_queue)
171 process_queue_end = &process_queue;
173 /* If we are not scanning this object, we placed it in
174 * the queue because we needed to fetch it first.
176 if (! (obj->flags & TO_SCAN)) {
177 if (walker->fetch(walker, obj->oid.hash)) {
178 report_missing(obj);
179 return -1;
182 if (!obj->type)
183 parse_object(obj->oid.hash);
184 if (process_object(walker, obj))
185 return -1;
187 return 0;
190 static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
192 if (!get_sha1_hex(target, sha1))
193 return 0;
194 if (!check_refname_format(target, 0)) {
195 struct ref *ref = alloc_ref(target);
196 if (!walker->fetch_ref(walker, ref)) {
197 hashcpy(sha1, ref->old_oid.hash);
198 free(ref);
199 return 0;
201 free(ref);
203 return -1;
206 static int mark_complete(const char *path, const struct object_id *oid,
207 int flag, void *cb_data)
209 struct commit *commit = lookup_commit_reference_gently(oid->hash, 1);
211 if (commit) {
212 commit->object.flags |= COMPLETE;
213 commit_list_insert(commit, &complete);
215 return 0;
218 int walker_targets_stdin(char ***target, const char ***write_ref)
220 int targets = 0, targets_alloc = 0;
221 struct strbuf buf = STRBUF_INIT;
222 *target = NULL; *write_ref = NULL;
223 while (1) {
224 char *rf_one = NULL;
225 char *tg_one;
227 if (strbuf_getline_lf(&buf, stdin) == EOF)
228 break;
229 tg_one = buf.buf;
230 rf_one = strchr(tg_one, '\t');
231 if (rf_one)
232 *rf_one++ = 0;
234 if (targets >= targets_alloc) {
235 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
236 REALLOC_ARRAY(*target, targets_alloc);
237 REALLOC_ARRAY(*write_ref, targets_alloc);
239 (*target)[targets] = xstrdup(tg_one);
240 (*write_ref)[targets] = xstrdup_or_null(rf_one);
241 targets++;
243 strbuf_release(&buf);
244 return targets;
247 void walker_targets_free(int targets, char **target, const char **write_ref)
249 while (targets--) {
250 free(target[targets]);
251 if (write_ref)
252 free((char *) write_ref[targets]);
256 int walker_fetch(struct walker *walker, int targets, char **target,
257 const char **write_ref, const char *write_ref_log_details)
259 struct strbuf refname = STRBUF_INIT;
260 struct strbuf err = STRBUF_INIT;
261 struct ref_transaction *transaction = NULL;
262 unsigned char *sha1 = xmalloc(targets * 20);
263 char *msg = NULL;
264 int i, ret = -1;
266 save_commit_buffer = 0;
268 if (write_ref) {
269 transaction = ref_transaction_begin(&err);
270 if (!transaction) {
271 error("%s", err.buf);
272 goto done;
276 if (!walker->get_recover) {
277 for_each_ref(mark_complete, NULL);
278 commit_list_sort_by_date(&complete);
281 for (i = 0; i < targets; i++) {
282 if (interpret_target(walker, target[i], &sha1[20 * i])) {
283 error("Could not interpret response from server '%s' as something to pull", target[i]);
284 goto done;
286 if (process(walker, lookup_unknown_object(&sha1[20 * i])))
287 goto done;
290 if (loop(walker))
291 goto done;
292 if (!write_ref) {
293 ret = 0;
294 goto done;
296 if (write_ref_log_details) {
297 msg = xstrfmt("fetch from %s", write_ref_log_details);
298 } else {
299 msg = NULL;
301 for (i = 0; i < targets; i++) {
302 if (!write_ref[i])
303 continue;
304 strbuf_reset(&refname);
305 strbuf_addf(&refname, "refs/%s", write_ref[i]);
306 if (ref_transaction_update(transaction, refname.buf,
307 &sha1[20 * i], NULL, 0,
308 msg ? msg : "fetch (unknown)",
309 &err)) {
310 error("%s", err.buf);
311 goto done;
314 if (ref_transaction_commit(transaction, &err)) {
315 error("%s", err.buf);
316 goto done;
319 ret = 0;
321 done:
322 ref_transaction_free(transaction);
323 free(msg);
324 free(sha1);
325 strbuf_release(&err);
326 strbuf_release(&refname);
327 return ret;
330 void walker_free(struct walker *walker)
332 walker->cleanup(walker);
333 free(walker);