]>
Commit | Line | Data |
---|---|---|
4250a5e5 | 1 | #include "cache.h" |
30ae764b | 2 | #include "walker.h" |
4250a5e5 DB |
3 | #include "commit.h" |
4 | #include "tree.h" | |
1bc995a3 | 5 | #include "tree-walk.h" |
3173bd49 DB |
6 | #include "tag.h" |
7 | #include "blob.h" | |
cd541a68 DB |
8 | #include "refs.h" |
9 | ||
b2d62f16 | 10 | static unsigned char current_commit_sha1[20]; |
4250a5e5 | 11 | |
30ae764b | 12 | void walker_say(struct walker *walker, const char *fmt, const char *hex) |
1e8be59d | 13 | { |
30ae764b | 14 | if (walker->get_verbosely) |
e78d9772 JH |
15 | fprintf(stderr, fmt, hex); |
16 | } | |
17 | ||
0d7a6e4e | 18 | static void report_missing(const struct object *obj) |
b2d62f16 JH |
19 | { |
20 | char missing_hex[41]; | |
0d7a6e4e AR |
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)); | |
b2d62f16 JH |
27 | } |
28 | ||
30ae764b | 29 | static int process(struct walker *walker, struct object *obj); |
3173bd49 | 30 | |
30ae764b | 31 | static int process_tree(struct walker *walker, struct tree *tree) |
4250a5e5 | 32 | { |
1bc995a3 | 33 | struct tree_desc desc; |
4c068a98 | 34 | struct name_entry entry; |
4250a5e5 DB |
35 | |
36 | if (parse_tree(tree)) | |
37 | return -1; | |
38 | ||
6fda5e51 | 39 | init_tree_desc(&desc, tree->buffer, tree->size); |
4c068a98 | 40 | while (tree_entry(&desc, &entry)) { |
6f9012b6 JH |
41 | struct object *obj = NULL; |
42 | ||
582c7393 | 43 | /* submodule commits are not stored in the superproject */ |
68fb4650 | 44 | if (S_ISGITLINK(entry.mode)) |
582c7393 | 45 | continue; |
4c068a98 LT |
46 | if (S_ISDIR(entry.mode)) { |
47 | struct tree *tree = lookup_tree(entry.sha1); | |
6f9012b6 JH |
48 | if (tree) |
49 | obj = &tree->object; | |
50 | } | |
51 | else { | |
4c068a98 | 52 | struct blob *blob = lookup_blob(entry.sha1); |
6f9012b6 JH |
53 | if (blob) |
54 | obj = &blob->object; | |
2d9c58c6 | 55 | } |
30ae764b | 56 | if (!obj || process(walker, obj)) |
4250a5e5 | 57 | return -1; |
4250a5e5 | 58 | } |
2d9c58c6 LT |
59 | free(tree->buffer); |
60 | tree->buffer = NULL; | |
1bc995a3 | 61 | tree->size = 0; |
4250a5e5 DB |
62 | return 0; |
63 | } | |
64 | ||
24451c31 SV |
65 | #define COMPLETE (1U << 0) |
66 | #define SEEN (1U << 1) | |
67 | #define TO_SCAN (1U << 2) | |
85d106c2 | 68 | |
d0ac30f2 | 69 | static struct commit_list *complete = NULL; |
22c6e1d0 | 70 | |
30ae764b | 71 | static int process_commit(struct walker *walker, struct commit *commit) |
4250a5e5 | 72 | { |
1e8be59d | 73 | if (parse_commit(commit)) |
4250a5e5 DB |
74 | return -1; |
75 | ||
22c6e1d0 | 76 | while (complete && complete->item->date >= commit->date) { |
d0ac30f2 | 77 | pop_most_recent_commit(&complete, COMPLETE); |
22c6e1d0 | 78 | } |
22c6e1d0 | 79 | |
d0ac30f2 | 80 | if (commit->object.flags & COMPLETE) |
22c6e1d0 DB |
81 | return 0; |
82 | ||
e702496e | 83 | hashcpy(current_commit_sha1, commit->object.sha1); |
4250a5e5 | 84 | |
30ae764b | 85 | walker_say(walker, "walk %s\n", sha1_to_hex(commit->object.sha1)); |
85d106c2 | 86 | |
30ae764b DB |
87 | if (walker->get_tree) { |
88 | if (process(walker, &commit->tree->object)) | |
4250a5e5 | 89 | return -1; |
30ae764b DB |
90 | if (!walker->get_all) |
91 | walker->get_tree = 0; | |
4250a5e5 | 92 | } |
30ae764b | 93 | if (walker->get_history) { |
1e8be59d | 94 | struct commit_list *parents = commit->parents; |
4250a5e5 | 95 | for (; parents; parents = parents->next) { |
30ae764b | 96 | if (process(walker, &parents->item->object)) |
4250a5e5 DB |
97 | return -1; |
98 | } | |
99 | } | |
100 | return 0; | |
101 | } | |
102 | ||
30ae764b | 103 | static int process_tag(struct walker *walker, struct tag *tag) |
3173bd49 | 104 | { |
1e8be59d | 105 | if (parse_tag(tag)) |
3173bd49 | 106 | return -1; |
30ae764b | 107 | return process(walker, tag->tagged); |
3173bd49 DB |
108 | } |
109 | ||
1e8be59d DB |
110 | static struct object_list *process_queue = NULL; |
111 | static struct object_list **process_queue_end = &process_queue; | |
112 | ||
30ae764b | 113 | static int process_object(struct walker *walker, struct object *obj) |
3173bd49 | 114 | { |
1974632c | 115 | if (obj->type == OBJ_COMMIT) { |
30ae764b | 116 | if (process_commit(walker, (struct commit *)obj)) |
f88fcf8b DB |
117 | return -1; |
118 | return 0; | |
119 | } | |
1974632c | 120 | if (obj->type == OBJ_TREE) { |
30ae764b | 121 | if (process_tree(walker, (struct tree *)obj)) |
f88fcf8b DB |
122 | return -1; |
123 | return 0; | |
124 | } | |
1974632c | 125 | if (obj->type == OBJ_BLOB) { |
f88fcf8b DB |
126 | return 0; |
127 | } | |
1974632c | 128 | if (obj->type == OBJ_TAG) { |
30ae764b | 129 | if (process_tag(walker, (struct tag *)obj)) |
f88fcf8b | 130 | return -1; |
3173bd49 | 131 | return 0; |
f88fcf8b DB |
132 | } |
133 | return error("Unable to determine requirements " | |
134 | "of type %s for %s", | |
885a86ab | 135 | typename(obj->type), sha1_to_hex(obj->sha1)); |
f88fcf8b DB |
136 | } |
137 | ||
30ae764b | 138 | static int process(struct walker *walker, struct object *obj) |
f88fcf8b | 139 | { |
a82d07e5 SV |
140 | if (obj->flags & SEEN) |
141 | return 0; | |
142 | obj->flags |= SEEN; | |
143 | ||
80077f07 | 144 | if (has_sha1_file(obj->sha1)) { |
f88fcf8b | 145 | /* We already have it, so we should scan it now. */ |
85d106c2 | 146 | obj->flags |= TO_SCAN; |
e5f38ec3 JH |
147 | } |
148 | else { | |
7b64d06b SV |
149 | if (obj->flags & COMPLETE) |
150 | return 0; | |
30ae764b | 151 | walker->prefetch(walker, obj->sha1); |
f88fcf8b | 152 | } |
a6080a0a | 153 | |
1e8be59d DB |
154 | object_list_insert(obj, process_queue_end); |
155 | process_queue_end = &(*process_queue_end)->next; | |
1e8be59d DB |
156 | return 0; |
157 | } | |
158 | ||
30ae764b | 159 | static int loop(struct walker *walker) |
1e8be59d | 160 | { |
85d106c2 JH |
161 | struct object_list *elem; |
162 | ||
1e8be59d DB |
163 | while (process_queue) { |
164 | struct object *obj = process_queue->item; | |
85d106c2 JH |
165 | elem = process_queue; |
166 | process_queue = elem->next; | |
167 | free(elem); | |
1e8be59d DB |
168 | if (!process_queue) |
169 | process_queue_end = &process_queue; | |
170 | ||
85d106c2 JH |
171 | /* If we are not scanning this object, we placed it in |
172 | * the queue because we needed to fetch it first. | |
173 | */ | |
174 | if (! (obj->flags & TO_SCAN)) { | |
30ae764b | 175 | if (walker->fetch(walker, obj->sha1)) { |
0d7a6e4e | 176 | report_missing(obj); |
85d106c2 JH |
177 | return -1; |
178 | } | |
179 | } | |
1e8be59d DB |
180 | if (!obj->type) |
181 | parse_object(obj->sha1); | |
30ae764b | 182 | if (process_object(walker, obj)) |
f88fcf8b | 183 | return -1; |
1e8be59d DB |
184 | } |
185 | return 0; | |
3173bd49 DB |
186 | } |
187 | ||
30ae764b | 188 | static int interpret_target(struct walker *walker, char *target, unsigned char *sha1) |
cd541a68 DB |
189 | { |
190 | if (!get_sha1_hex(target, sha1)) | |
191 | return 0; | |
192 | if (!check_ref_format(target)) { | |
737922aa | 193 | struct ref *ref = alloc_ref_from_str(target); |
c13b2633 DB |
194 | if (!walker->fetch_ref(walker, ref)) { |
195 | hashcpy(sha1, ref->old_sha1); | |
196 | free(ref); | |
cd541a68 DB |
197 | return 0; |
198 | } | |
c13b2633 | 199 | free(ref); |
cd541a68 DB |
200 | } |
201 | return -1; | |
202 | } | |
203 | ||
8da19775 | 204 | static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
22c6e1d0 | 205 | { |
d0ac30f2 JH |
206 | struct commit *commit = lookup_commit_reference_gently(sha1, 1); |
207 | if (commit) { | |
208 | commit->object.flags |= COMPLETE; | |
209 | insert_by_date(commit, &complete); | |
22c6e1d0 DB |
210 | } |
211 | return 0; | |
212 | } | |
cd541a68 | 213 | |
30ae764b | 214 | int walker_targets_stdin(char ***target, const char ***write_ref) |
8e87ca66 PB |
215 | { |
216 | int targets = 0, targets_alloc = 0; | |
217 | struct strbuf buf; | |
218 | *target = NULL; *write_ref = NULL; | |
f1696ee3 | 219 | strbuf_init(&buf, 0); |
8e87ca66 PB |
220 | while (1) { |
221 | char *rf_one = NULL; | |
222 | char *tg_one; | |
223 | ||
e6c019d0 | 224 | if (strbuf_getline(&buf, stdin, '\n') == EOF) |
8e87ca66 PB |
225 | break; |
226 | tg_one = buf.buf; | |
227 | rf_one = strchr(tg_one, '\t'); | |
228 | if (rf_one) | |
229 | *rf_one++ = 0; | |
230 | ||
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 | } | |
9befac47 SP |
236 | (*target)[targets] = xstrdup(tg_one); |
237 | (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL; | |
8e87ca66 PB |
238 | targets++; |
239 | } | |
e6c019d0 | 240 | strbuf_release(&buf); |
8e87ca66 PB |
241 | return targets; |
242 | } | |
243 | ||
30ae764b | 244 | void walker_targets_free(int targets, char **target, const char **write_ref) |
8e87ca66 PB |
245 | { |
246 | while (targets--) { | |
247 | free(target[targets]); | |
1b03dfed | 248 | if (write_ref && write_ref[targets]) |
8e87ca66 PB |
249 | free((char *) write_ref[targets]); |
250 | } | |
251 | } | |
252 | ||
30ae764b DB |
253 | int walker_fetch(struct walker *walker, int targets, char **target, |
254 | const char **write_ref, const char *write_ref_log_details) | |
4250a5e5 | 255 | { |
4211e4d1 PB |
256 | struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *)); |
257 | unsigned char *sha1 = xmalloc(targets * 20); | |
d0740d92 SP |
258 | char *msg; |
259 | int ret; | |
4211e4d1 | 260 | int i; |
cd541a68 | 261 | |
98533b90 | 262 | save_commit_buffer = 0; |
4211e4d1 PB |
263 | |
264 | for (i = 0; i < targets; i++) { | |
1b03dfed | 265 | if (!write_ref || !write_ref[i]) |
4211e4d1 PB |
266 | continue; |
267 | ||
4431fcc4 | 268 | lock[i] = lock_ref_sha1(write_ref[i], NULL); |
4211e4d1 PB |
269 | if (!lock[i]) { |
270 | error("Can't lock ref %s", write_ref[i]); | |
271 | goto unlock_and_fail; | |
d0740d92 | 272 | } |
cd541a68 DB |
273 | } |
274 | ||
30ae764b | 275 | if (!walker->get_recover) |
cb5d709f | 276 | for_each_ref(mark_complete, NULL); |
22c6e1d0 | 277 | |
4211e4d1 | 278 | for (i = 0; i < targets; i++) { |
30ae764b | 279 | if (interpret_target(walker, target[i], &sha1[20 * i])) { |
5f48741a | 280 | error("Could not interpret response from server '%s' as something to pull", target[i]); |
4211e4d1 PB |
281 | goto unlock_and_fail; |
282 | } | |
30ae764b | 283 | if (process(walker, lookup_unknown_object(&sha1[20 * i]))) |
4211e4d1 | 284 | goto unlock_and_fail; |
4bd18c43 | 285 | } |
4211e4d1 | 286 | |
30ae764b | 287 | if (loop(walker)) |
4211e4d1 PB |
288 | goto unlock_and_fail; |
289 | ||
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; | |
4bd18c43 | 295 | } |
4211e4d1 | 296 | for (i = 0; i < targets; i++) { |
1b03dfed | 297 | if (!write_ref || !write_ref[i]) |
4211e4d1 PB |
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; | |
4bd18c43 | 303 | } |
4cac42b1 | 304 | free(msg); |
4bd18c43 | 305 | |
cd541a68 | 306 | return 0; |
4211e4d1 | 307 | |
4211e4d1 PB |
308 | unlock_and_fail: |
309 | for (i = 0; i < targets; i++) | |
310 | if (lock[i]) | |
311 | unlock_ref(lock[i]); | |
30ae764b | 312 | |
4211e4d1 | 313 | return -1; |
4250a5e5 | 314 | } |
30ae764b DB |
315 | |
316 | void walker_free(struct walker *walker) | |
317 | { | |
318 | walker->cleanup(walker); | |
319 | free(walker); | |
320 | } |