Recent MinGW has a C99 implementation of snprintf functions
[git/dscho.git] / walker.c
blob11d9052ed86ab17b56ab86ef21b7c98095e410dc
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, const char *hex)
14         if (walker->get_verbosely)
15                 fprintf(stderr, fmt, hex);
18 static void report_missing(const struct object *obj)
20         char missing_hex[41];
21         strcpy(missing_hex, sha1_to_hex(obj->sha1));
22         fprintf(stderr, "Cannot obtain needed %s %s\n",
23                 obj->type ? typename(obj->type): "object", missing_hex);
24         if (!is_null_sha1(current_commit_sha1))
25                 fprintf(stderr, "while processing commit %s.\n",
26                         sha1_to_hex(current_commit_sha1));
29 static int process(struct walker *walker, struct object *obj);
31 static int process_tree(struct walker *walker, struct tree *tree)
33         struct tree_desc desc;
34         struct name_entry entry;
36         if (parse_tree(tree))
37                 return -1;
39         init_tree_desc(&desc, tree->buffer, tree->size);
40         while (tree_entry(&desc, &entry)) {
41                 struct object *obj = NULL;
43                 /* submodule commits are not stored in the superproject */
44                 if (S_ISGITLINK(entry.mode))
45                         continue;
46                 if (S_ISDIR(entry.mode)) {
47                         struct tree *tree = lookup_tree(entry.sha1);
48                         if (tree)
49                                 obj = &tree->object;
50                 }
51                 else {
52                         struct blob *blob = lookup_blob(entry.sha1);
53                         if (blob)
54                                 obj = &blob->object;
55                 }
56                 if (!obj || process(walker, obj))
57                         return -1;
58         }
59         free(tree->buffer);
60         tree->buffer = NULL;
61         tree->size = 0;
62         tree->object.parsed = 0;
63         return 0;
66 #define COMPLETE        (1U << 0)
67 #define SEEN            (1U << 1)
68 #define TO_SCAN         (1U << 2)
70 static struct commit_list *complete = NULL;
72 static int process_commit(struct walker *walker, struct commit *commit)
74         if (parse_commit(commit))
75                 return -1;
77         while (complete && complete->item->date >= commit->date) {
78                 pop_most_recent_commit(&complete, COMPLETE);
79         }
81         if (commit->object.flags & COMPLETE)
82                 return 0;
84         hashcpy(current_commit_sha1, commit->object.sha1);
86         walker_say(walker, "walk %s\n", sha1_to_hex(commit->object.sha1));
88         if (walker->get_tree) {
89                 if (process(walker, &commit->tree->object))
90                         return -1;
91                 if (!walker->get_all)
92                         walker->get_tree = 0;
93         }
94         if (walker->get_history) {
95                 struct commit_list *parents = commit->parents;
96                 for (; parents; parents = parents->next) {
97                         if (process(walker, &parents->item->object))
98                                 return -1;
99                 }
100         }
101         return 0;
104 static int process_tag(struct walker *walker, struct tag *tag)
106         if (parse_tag(tag))
107                 return -1;
108         return process(walker, tag->tagged);
111 static struct object_list *process_queue = NULL;
112 static struct object_list **process_queue_end = &process_queue;
114 static int process_object(struct walker *walker, struct object *obj)
116         if (obj->type == OBJ_COMMIT) {
117                 if (process_commit(walker, (struct commit *)obj))
118                         return -1;
119                 return 0;
120         }
121         if (obj->type == OBJ_TREE) {
122                 if (process_tree(walker, (struct tree *)obj))
123                         return -1;
124                 return 0;
125         }
126         if (obj->type == OBJ_BLOB) {
127                 return 0;
128         }
129         if (obj->type == OBJ_TAG) {
130                 if (process_tag(walker, (struct tag *)obj))
131                         return -1;
132                 return 0;
133         }
134         return error("Unable to determine requirements "
135                      "of type %s for %s",
136                      typename(obj->type), sha1_to_hex(obj->sha1));
139 static int process(struct walker *walker, struct object *obj)
141         if (obj->flags & SEEN)
142                 return 0;
143         obj->flags |= SEEN;
145         if (has_sha1_file(obj->sha1)) {
146                 /* We already have it, so we should scan it now. */
147                 obj->flags |= TO_SCAN;
148         }
149         else {
150                 if (obj->flags & COMPLETE)
151                         return 0;
152                 walker->prefetch(walker, obj->sha1);
153         }
155         object_list_insert(obj, process_queue_end);
156         process_queue_end = &(*process_queue_end)->next;
157         return 0;
160 static int loop(struct walker *walker)
162         struct object_list *elem;
164         while (process_queue) {
165                 struct object *obj = process_queue->item;
166                 elem = process_queue;
167                 process_queue = elem->next;
168                 free(elem);
169                 if (!process_queue)
170                         process_queue_end = &process_queue;
172                 /* If we are not scanning this object, we placed it in
173                  * the queue because we needed to fetch it first.
174                  */
175                 if (! (obj->flags & TO_SCAN)) {
176                         if (walker->fetch(walker, obj->sha1)) {
177                                 report_missing(obj);
178                                 return -1;
179                         }
180                 }
181                 if (!obj->type)
182                         parse_object(obj->sha1);
183                 if (process_object(walker, obj))
184                         return -1;
185         }
186         return 0;
189 static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
191         if (!get_sha1_hex(target, sha1))
192                 return 0;
193         if (!check_ref_format(target)) {
194                 struct ref *ref = alloc_ref(target);
195                 if (!walker->fetch_ref(walker, ref)) {
196                         hashcpy(sha1, ref->old_sha1);
197                         free(ref);
198                         return 0;
199                 }
200                 free(ref);
201         }
202         return -1;
205 static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
207         struct commit *commit = lookup_commit_reference_gently(sha1, 1);
208         if (commit) {
209                 commit->object.flags |= COMPLETE;
210                 insert_by_date(commit, &complete);
211         }
212         return 0;
215 int walker_targets_stdin(char ***target, const char ***write_ref)
217         int targets = 0, targets_alloc = 0;
218         struct strbuf buf = STRBUF_INIT;
219         *target = NULL; *write_ref = NULL;
220         while (1) {
221                 char *rf_one = NULL;
222                 char *tg_one;
224                 if (strbuf_getline(&buf, stdin, '\n') == EOF)
225                         break;
226                 tg_one = buf.buf;
227                 rf_one = strchr(tg_one, '\t');
228                 if (rf_one)
229                         *rf_one++ = 0;
231                 if (targets >= targets_alloc) {
232                         targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
233                         *target = xrealloc(*target, targets_alloc * sizeof(**target));
234                         *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
235                 }
236                 (*target)[targets] = xstrdup(tg_one);
237                 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
238                 targets++;
239         }
240         strbuf_release(&buf);
241         return targets;
244 void walker_targets_free(int targets, char **target, const char **write_ref)
246         while (targets--) {
247                 free(target[targets]);
248                 if (write_ref)
249                         free((char *) write_ref[targets]);
250         }
253 int walker_fetch(struct walker *walker, int targets, char **target,
254                  const char **write_ref, const char *write_ref_log_details)
256         struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
257         unsigned char *sha1 = xmalloc(targets * 20);
258         char *msg;
259         int ret;
260         int i;
262         save_commit_buffer = 0;
264         for (i = 0; i < targets; i++) {
265                 if (!write_ref || !write_ref[i])
266                         continue;
268                 lock[i] = lock_ref_sha1(write_ref[i], NULL);
269                 if (!lock[i]) {
270                         error("Can't lock ref %s", write_ref[i]);
271                         goto unlock_and_fail;
272                 }
273         }
275         if (!walker->get_recover)
276                 for_each_ref(mark_complete, NULL);
278         for (i = 0; i < targets; i++) {
279                 if (interpret_target(walker, target[i], &sha1[20 * i])) {
280                         error("Could not interpret response from server '%s' as something to pull", target[i]);
281                         goto unlock_and_fail;
282                 }
283                 if (process(walker, lookup_unknown_object(&sha1[20 * i])))
284                         goto unlock_and_fail;
285         }
287         if (loop(walker))
288                 goto unlock_and_fail;
290         if (write_ref_log_details) {
291                 msg = xmalloc(strlen(write_ref_log_details) + 12);
292                 sprintf(msg, "fetch from %s", write_ref_log_details);
293         } else {
294                 msg = NULL;
295         }
296         for (i = 0; i < targets; i++) {
297                 if (!write_ref || !write_ref[i])
298                         continue;
299                 ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)");
300                 lock[i] = NULL;
301                 if (ret)
302                         goto unlock_and_fail;
303         }
304         free(msg);
306         return 0;
308 unlock_and_fail:
309         for (i = 0; i < targets; i++)
310                 if (lock[i])
311                         unlock_ref(lock[i]);
313         return -1;
316 void walker_free(struct walker *walker)
318         walker->cleanup(walker);
319         free(walker);