Merge 'fix-is-exe' into HEAD
[git/mingw/4msysgit.git] / walker.c
blob633596e06fcaa1154980f95c858c61379c968d49
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;
51 else {
52 struct blob *blob = lookup_blob(entry.sha1);
53 if (blob)
54 obj = &blob->object;
56 if (!obj || process(walker, obj))
57 return -1;
59 free_tree_buffer(tree);
60 return 0;
63 #define COMPLETE (1U << 0)
64 #define SEEN (1U << 1)
65 #define TO_SCAN (1U << 2)
67 static struct commit_list *complete = NULL;
69 static int process_commit(struct walker *walker, struct commit *commit)
71 if (parse_commit(commit))
72 return -1;
74 while (complete && complete->item->date >= commit->date) {
75 pop_most_recent_commit(&complete, COMPLETE);
78 if (commit->object.flags & COMPLETE)
79 return 0;
81 hashcpy(current_commit_sha1, commit->object.sha1);
83 walker_say(walker, "walk %s\n", sha1_to_hex(commit->object.sha1));
85 if (walker->get_tree) {
86 if (process(walker, &commit->tree->object))
87 return -1;
88 if (!walker->get_all)
89 walker->get_tree = 0;
91 if (walker->get_history) {
92 struct commit_list *parents = commit->parents;
93 for (; parents; parents = parents->next) {
94 if (process(walker, &parents->item->object))
95 return -1;
98 return 0;
101 static int process_tag(struct walker *walker, struct tag *tag)
103 if (parse_tag(tag))
104 return -1;
105 return process(walker, tag->tagged);
108 static struct object_list *process_queue = NULL;
109 static struct object_list **process_queue_end = &process_queue;
111 static int process_object(struct walker *walker, struct object *obj)
113 if (obj->type == OBJ_COMMIT) {
114 if (process_commit(walker, (struct commit *)obj))
115 return -1;
116 return 0;
118 if (obj->type == OBJ_TREE) {
119 if (process_tree(walker, (struct tree *)obj))
120 return -1;
121 return 0;
123 if (obj->type == OBJ_BLOB) {
124 return 0;
126 if (obj->type == OBJ_TAG) {
127 if (process_tag(walker, (struct tag *)obj))
128 return -1;
129 return 0;
131 return error("Unable to determine requirements "
132 "of type %s for %s",
133 typename(obj->type), sha1_to_hex(obj->sha1));
136 static int process(struct walker *walker, struct object *obj)
138 if (obj->flags & SEEN)
139 return 0;
140 obj->flags |= SEEN;
142 if (has_sha1_file(obj->sha1)) {
143 /* We already have it, so we should scan it now. */
144 obj->flags |= TO_SCAN;
146 else {
147 if (obj->flags & COMPLETE)
148 return 0;
149 walker->prefetch(walker, obj->sha1);
152 object_list_insert(obj, process_queue_end);
153 process_queue_end = &(*process_queue_end)->next;
154 return 0;
157 static int loop(struct walker *walker)
159 struct object_list *elem;
161 while (process_queue) {
162 struct object *obj = process_queue->item;
163 elem = process_queue;
164 process_queue = elem->next;
165 free(elem);
166 if (!process_queue)
167 process_queue_end = &process_queue;
169 /* If we are not scanning this object, we placed it in
170 * the queue because we needed to fetch it first.
172 if (! (obj->flags & TO_SCAN)) {
173 if (walker->fetch(walker, obj->sha1)) {
174 report_missing(obj);
175 return -1;
178 if (!obj->type)
179 parse_object(obj->sha1);
180 if (process_object(walker, obj))
181 return -1;
183 return 0;
186 static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
188 if (!get_sha1_hex(target, sha1))
189 return 0;
190 if (!check_refname_format(target, 0)) {
191 struct ref *ref = alloc_ref(target);
192 if (!walker->fetch_ref(walker, ref)) {
193 hashcpy(sha1, ref->old_sha1);
194 free(ref);
195 return 0;
197 free(ref);
199 return -1;
202 static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
204 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
205 if (commit) {
206 commit->object.flags |= COMPLETE;
207 commit_list_insert_by_date(commit, &complete);
209 return 0;
212 int walker_targets_stdin(char ***target, const char ***write_ref)
214 int targets = 0, targets_alloc = 0;
215 struct strbuf buf = STRBUF_INIT;
216 *target = NULL; *write_ref = NULL;
217 while (1) {
218 char *rf_one = NULL;
219 char *tg_one;
221 if (strbuf_getline(&buf, stdin, '\n') == EOF)
222 break;
223 tg_one = buf.buf;
224 rf_one = strchr(tg_one, '\t');
225 if (rf_one)
226 *rf_one++ = 0;
228 if (targets >= targets_alloc) {
229 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
230 *target = xrealloc(*target, targets_alloc * sizeof(**target));
231 *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
233 (*target)[targets] = xstrdup(tg_one);
234 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
235 targets++;
237 strbuf_release(&buf);
238 return targets;
241 void walker_targets_free(int targets, char **target, const char **write_ref)
243 while (targets--) {
244 free(target[targets]);
245 if (write_ref)
246 free((char *) write_ref[targets]);
250 int walker_fetch(struct walker *walker, int targets, char **target,
251 const char **write_ref, const char *write_ref_log_details)
253 struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
254 unsigned char *sha1 = xmalloc(targets * 20);
255 char *msg;
256 int ret;
257 int i;
259 save_commit_buffer = 0;
261 for (i = 0; i < targets; i++) {
262 if (!write_ref || !write_ref[i])
263 continue;
265 lock[i] = lock_ref_sha1(write_ref[i], NULL);
266 if (!lock[i]) {
267 error("Can't lock ref %s", write_ref[i]);
268 goto unlock_and_fail;
272 if (!walker->get_recover)
273 for_each_ref(mark_complete, NULL);
275 for (i = 0; i < targets; i++) {
276 if (interpret_target(walker, target[i], &sha1[20 * i])) {
277 error("Could not interpret response from server '%s' as something to pull", target[i]);
278 goto unlock_and_fail;
280 if (process(walker, lookup_unknown_object(&sha1[20 * i])))
281 goto unlock_and_fail;
284 if (loop(walker))
285 goto unlock_and_fail;
287 if (write_ref_log_details) {
288 msg = xmalloc(strlen(write_ref_log_details) + 12);
289 sprintf(msg, "fetch from %s", write_ref_log_details);
290 } else {
291 msg = NULL;
293 for (i = 0; i < targets; i++) {
294 if (!write_ref || !write_ref[i])
295 continue;
296 ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)");
297 lock[i] = NULL;
298 if (ret)
299 goto unlock_and_fail;
301 free(msg);
303 return 0;
305 unlock_and_fail:
306 for (i = 0; i < targets; i++)
307 if (lock[i])
308 unlock_ref(lock[i]);
310 return -1;
313 void walker_free(struct walker *walker)
315 walker->cleanup(walker);
316 free(walker);