]>
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 | |
fa262cac | 12 | void walker_say(struct walker *walker, const char *fmt, ...) |
1e8be59d | 13 | { |
fa262cac JK |
14 | if (walker->get_verbosely) { |
15 | va_list ap; | |
16 | va_start(ap, fmt); | |
17 | vfprintf(stderr, fmt, ap); | |
18 | va_end(ap); | |
19 | } | |
e78d9772 JH |
20 | } |
21 | ||
0d7a6e4e | 22 | static void report_missing(const struct object *obj) |
b2d62f16 | 23 | { |
0d7a6e4e | 24 | fprintf(stderr, "Cannot obtain needed %s %s\n", |
2b87d3a8 | 25 | obj->type ? typename(obj->type): "object", |
f2fd0760 | 26 | oid_to_hex(&obj->oid)); |
0d7a6e4e AR |
27 | if (!is_null_sha1(current_commit_sha1)) |
28 | fprintf(stderr, "while processing commit %s.\n", | |
29 | sha1_to_hex(current_commit_sha1)); | |
b2d62f16 JH |
30 | } |
31 | ||
30ae764b | 32 | static int process(struct walker *walker, struct object *obj); |
3173bd49 | 33 | |
30ae764b | 34 | static int process_tree(struct walker *walker, struct tree *tree) |
4250a5e5 | 35 | { |
1bc995a3 | 36 | struct tree_desc desc; |
4c068a98 | 37 | struct name_entry entry; |
4250a5e5 DB |
38 | |
39 | if (parse_tree(tree)) | |
40 | return -1; | |
41 | ||
6fda5e51 | 42 | init_tree_desc(&desc, tree->buffer, tree->size); |
4c068a98 | 43 | while (tree_entry(&desc, &entry)) { |
6f9012b6 JH |
44 | struct object *obj = NULL; |
45 | ||
582c7393 | 46 | /* submodule commits are not stored in the superproject */ |
68fb4650 | 47 | if (S_ISGITLINK(entry.mode)) |
582c7393 | 48 | continue; |
4c068a98 | 49 | if (S_ISDIR(entry.mode)) { |
7d924c91 | 50 | struct tree *tree = lookup_tree(entry.oid->hash); |
6f9012b6 JH |
51 | if (tree) |
52 | obj = &tree->object; | |
53 | } | |
54 | else { | |
7d924c91 | 55 | struct blob *blob = lookup_blob(entry.oid->hash); |
6f9012b6 JH |
56 | if (blob) |
57 | obj = &blob->object; | |
2d9c58c6 | 58 | } |
30ae764b | 59 | if (!obj || process(walker, obj)) |
4250a5e5 | 60 | return -1; |
4250a5e5 | 61 | } |
6e454b9a | 62 | free_tree_buffer(tree); |
4250a5e5 DB |
63 | return 0; |
64 | } | |
65 | ||
208acbfb | 66 | /* Remember to update object flag allocation in object.h */ |
24451c31 SV |
67 | #define COMPLETE (1U << 0) |
68 | #define SEEN (1U << 1) | |
69 | #define TO_SCAN (1U << 2) | |
85d106c2 | 70 | |
d0ac30f2 | 71 | static struct commit_list *complete = NULL; |
22c6e1d0 | 72 | |
30ae764b | 73 | static int process_commit(struct walker *walker, struct commit *commit) |
4250a5e5 | 74 | { |
1e8be59d | 75 | if (parse_commit(commit)) |
4250a5e5 DB |
76 | return -1; |
77 | ||
22c6e1d0 | 78 | while (complete && complete->item->date >= commit->date) { |
d0ac30f2 | 79 | pop_most_recent_commit(&complete, COMPLETE); |
22c6e1d0 | 80 | } |
22c6e1d0 | 81 | |
d0ac30f2 | 82 | if (commit->object.flags & COMPLETE) |
22c6e1d0 DB |
83 | return 0; |
84 | ||
ed1c9977 | 85 | hashcpy(current_commit_sha1, commit->object.oid.hash); |
4250a5e5 | 86 | |
f2fd0760 | 87 | walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid)); |
85d106c2 | 88 | |
30ae764b DB |
89 | if (walker->get_tree) { |
90 | if (process(walker, &commit->tree->object)) | |
4250a5e5 | 91 | return -1; |
30ae764b DB |
92 | if (!walker->get_all) |
93 | walker->get_tree = 0; | |
4250a5e5 | 94 | } |
30ae764b | 95 | if (walker->get_history) { |
1e8be59d | 96 | struct commit_list *parents = commit->parents; |
4250a5e5 | 97 | for (; parents; parents = parents->next) { |
30ae764b | 98 | if (process(walker, &parents->item->object)) |
4250a5e5 DB |
99 | return -1; |
100 | } | |
101 | } | |
102 | return 0; | |
103 | } | |
104 | ||
30ae764b | 105 | static int process_tag(struct walker *walker, struct tag *tag) |
3173bd49 | 106 | { |
1e8be59d | 107 | if (parse_tag(tag)) |
3173bd49 | 108 | return -1; |
30ae764b | 109 | return process(walker, tag->tagged); |
3173bd49 DB |
110 | } |
111 | ||
1e8be59d DB |
112 | static struct object_list *process_queue = NULL; |
113 | static struct object_list **process_queue_end = &process_queue; | |
114 | ||
30ae764b | 115 | static int process_object(struct walker *walker, struct object *obj) |
3173bd49 | 116 | { |
1974632c | 117 | if (obj->type == OBJ_COMMIT) { |
30ae764b | 118 | if (process_commit(walker, (struct commit *)obj)) |
f88fcf8b DB |
119 | return -1; |
120 | return 0; | |
121 | } | |
1974632c | 122 | if (obj->type == OBJ_TREE) { |
30ae764b | 123 | if (process_tree(walker, (struct tree *)obj)) |
f88fcf8b DB |
124 | return -1; |
125 | return 0; | |
126 | } | |
1974632c | 127 | if (obj->type == OBJ_BLOB) { |
f88fcf8b DB |
128 | return 0; |
129 | } | |
1974632c | 130 | if (obj->type == OBJ_TAG) { |
30ae764b | 131 | if (process_tag(walker, (struct tag *)obj)) |
f88fcf8b | 132 | return -1; |
3173bd49 | 133 | return 0; |
f88fcf8b DB |
134 | } |
135 | return error("Unable to determine requirements " | |
136 | "of type %s for %s", | |
f2fd0760 | 137 | typename(obj->type), oid_to_hex(&obj->oid)); |
f88fcf8b DB |
138 | } |
139 | ||
30ae764b | 140 | static int process(struct walker *walker, struct object *obj) |
f88fcf8b | 141 | { |
a82d07e5 SV |
142 | if (obj->flags & SEEN) |
143 | return 0; | |
144 | obj->flags |= SEEN; | |
145 | ||
f2fd0760 | 146 | if (has_object_file(&obj->oid)) { |
f88fcf8b | 147 | /* We already have it, so we should scan it now. */ |
85d106c2 | 148 | obj->flags |= TO_SCAN; |
e5f38ec3 JH |
149 | } |
150 | else { | |
7b64d06b SV |
151 | if (obj->flags & COMPLETE) |
152 | return 0; | |
ed1c9977 | 153 | walker->prefetch(walker, obj->oid.hash); |
f88fcf8b | 154 | } |
a6080a0a | 155 | |
1e8be59d DB |
156 | object_list_insert(obj, process_queue_end); |
157 | process_queue_end = &(*process_queue_end)->next; | |
1e8be59d DB |
158 | return 0; |
159 | } | |
160 | ||
30ae764b | 161 | static int loop(struct walker *walker) |
1e8be59d | 162 | { |
85d106c2 JH |
163 | struct object_list *elem; |
164 | ||
1e8be59d DB |
165 | while (process_queue) { |
166 | struct object *obj = process_queue->item; | |
85d106c2 JH |
167 | elem = process_queue; |
168 | process_queue = elem->next; | |
169 | free(elem); | |
1e8be59d DB |
170 | if (!process_queue) |
171 | process_queue_end = &process_queue; | |
172 | ||
85d106c2 JH |
173 | /* If we are not scanning this object, we placed it in |
174 | * the queue because we needed to fetch it first. | |
175 | */ | |
176 | if (! (obj->flags & TO_SCAN)) { | |
ed1c9977 | 177 | if (walker->fetch(walker, obj->oid.hash)) { |
0d7a6e4e | 178 | report_missing(obj); |
85d106c2 JH |
179 | return -1; |
180 | } | |
181 | } | |
1e8be59d | 182 | if (!obj->type) |
ed1c9977 | 183 | parse_object(obj->oid.hash); |
30ae764b | 184 | if (process_object(walker, obj)) |
f88fcf8b | 185 | return -1; |
1e8be59d DB |
186 | } |
187 | return 0; | |
3173bd49 DB |
188 | } |
189 | ||
30ae764b | 190 | static int interpret_target(struct walker *walker, char *target, unsigned char *sha1) |
cd541a68 DB |
191 | { |
192 | if (!get_sha1_hex(target, sha1)) | |
193 | return 0; | |
8d9c5010 | 194 | if (!check_refname_format(target, 0)) { |
59c69c0c | 195 | struct ref *ref = alloc_ref(target); |
c13b2633 | 196 | if (!walker->fetch_ref(walker, ref)) { |
f4e54d02 | 197 | hashcpy(sha1, ref->old_oid.hash); |
c13b2633 | 198 | free(ref); |
cd541a68 DB |
199 | return 0; |
200 | } | |
c13b2633 | 201 | free(ref); |
cd541a68 DB |
202 | } |
203 | return -1; | |
204 | } | |
205 | ||
b4ebaf9e MH |
206 | static int mark_complete(const char *path, const struct object_id *oid, |
207 | int flag, void *cb_data) | |
22c6e1d0 | 208 | { |
b4ebaf9e MH |
209 | struct commit *commit = lookup_commit_reference_gently(oid->hash, 1); |
210 | ||
d0ac30f2 JH |
211 | if (commit) { |
212 | commit->object.flags |= COMPLETE; | |
3bc7a05b | 213 | commit_list_insert(commit, &complete); |
22c6e1d0 DB |
214 | } |
215 | return 0; | |
216 | } | |
cd541a68 | 217 | |
30ae764b | 218 | int walker_targets_stdin(char ***target, const char ***write_ref) |
8e87ca66 PB |
219 | { |
220 | int targets = 0, targets_alloc = 0; | |
f285a2d7 | 221 | struct strbuf buf = STRBUF_INIT; |
8e87ca66 | 222 | *target = NULL; *write_ref = NULL; |
8e87ca66 PB |
223 | while (1) { |
224 | char *rf_one = NULL; | |
225 | char *tg_one; | |
226 | ||
8f309aeb | 227 | if (strbuf_getline_lf(&buf, stdin) == EOF) |
8e87ca66 PB |
228 | break; |
229 | tg_one = buf.buf; | |
230 | rf_one = strchr(tg_one, '\t'); | |
231 | if (rf_one) | |
232 | *rf_one++ = 0; | |
233 | ||
234 | if (targets >= targets_alloc) { | |
235 | targets_alloc = targets_alloc ? targets_alloc * 2 : 64; | |
2756ca43 RS |
236 | REALLOC_ARRAY(*target, targets_alloc); |
237 | REALLOC_ARRAY(*write_ref, targets_alloc); | |
8e87ca66 | 238 | } |
9befac47 | 239 | (*target)[targets] = xstrdup(tg_one); |
8c53f071 | 240 | (*write_ref)[targets] = xstrdup_or_null(rf_one); |
8e87ca66 PB |
241 | targets++; |
242 | } | |
e6c019d0 | 243 | strbuf_release(&buf); |
8e87ca66 PB |
244 | return targets; |
245 | } | |
246 | ||
30ae764b | 247 | void walker_targets_free(int targets, char **target, const char **write_ref) |
8e87ca66 PB |
248 | { |
249 | while (targets--) { | |
250 | free(target[targets]); | |
24deea52 | 251 | if (write_ref) |
8e87ca66 PB |
252 | free((char *) write_ref[targets]); |
253 | } | |
254 | } | |
255 | ||
30ae764b DB |
256 | int walker_fetch(struct walker *walker, int targets, char **target, |
257 | const char **write_ref, const char *write_ref_log_details) | |
4250a5e5 | 258 | { |
b6b10bb4 RS |
259 | struct strbuf refname = STRBUF_INIT; |
260 | struct strbuf err = STRBUF_INIT; | |
261 | struct ref_transaction *transaction = NULL; | |
4211e4d1 | 262 | unsigned char *sha1 = xmalloc(targets * 20); |
b6b10bb4 RS |
263 | char *msg = NULL; |
264 | int i, ret = -1; | |
cd541a68 | 265 | |
98533b90 | 266 | save_commit_buffer = 0; |
4211e4d1 | 267 | |
b6b10bb4 RS |
268 | if (write_ref) { |
269 | transaction = ref_transaction_begin(&err); | |
270 | if (!transaction) { | |
271 | error("%s", err.buf); | |
272 | goto done; | |
d0740d92 | 273 | } |
cd541a68 DB |
274 | } |
275 | ||
3bc7a05b | 276 | if (!walker->get_recover) { |
b4ebaf9e | 277 | for_each_ref(mark_complete, NULL); |
3bc7a05b RS |
278 | commit_list_sort_by_date(&complete); |
279 | } | |
22c6e1d0 | 280 | |
4211e4d1 | 281 | for (i = 0; i < targets; i++) { |
30ae764b | 282 | if (interpret_target(walker, target[i], &sha1[20 * i])) { |
5f48741a | 283 | error("Could not interpret response from server '%s' as something to pull", target[i]); |
b6b10bb4 | 284 | goto done; |
4211e4d1 | 285 | } |
30ae764b | 286 | if (process(walker, lookup_unknown_object(&sha1[20 * i]))) |
b6b10bb4 | 287 | goto done; |
4bd18c43 | 288 | } |
4211e4d1 | 289 | |
30ae764b | 290 | if (loop(walker)) |
b6b10bb4 RS |
291 | goto done; |
292 | if (!write_ref) { | |
293 | ret = 0; | |
294 | goto done; | |
295 | } | |
4211e4d1 | 296 | if (write_ref_log_details) { |
01d678a2 | 297 | msg = xstrfmt("fetch from %s", write_ref_log_details); |
4211e4d1 PB |
298 | } else { |
299 | msg = NULL; | |
4bd18c43 | 300 | } |
4211e4d1 | 301 | for (i = 0; i < targets; i++) { |
b6b10bb4 | 302 | if (!write_ref[i]) |
4211e4d1 | 303 | continue; |
b6b10bb4 RS |
304 | strbuf_reset(&refname); |
305 | strbuf_addf(&refname, "refs/%s", write_ref[i]); | |
306 | if (ref_transaction_update(transaction, refname.buf, | |
1d147bdf | 307 | &sha1[20 * i], NULL, 0, |
db7516ab | 308 | msg ? msg : "fetch (unknown)", |
b6b10bb4 RS |
309 | &err)) { |
310 | error("%s", err.buf); | |
311 | goto done; | |
312 | } | |
313 | } | |
db7516ab | 314 | if (ref_transaction_commit(transaction, &err)) { |
b6b10bb4 RS |
315 | error("%s", err.buf); |
316 | goto done; | |
4bd18c43 | 317 | } |
4211e4d1 | 318 | |
b6b10bb4 | 319 | ret = 0; |
30ae764b | 320 | |
b6b10bb4 RS |
321 | done: |
322 | ref_transaction_free(transaction); | |
323 | free(msg); | |
324 | free(sha1); | |
325 | strbuf_release(&err); | |
326 | strbuf_release(&refname); | |
327 | return ret; | |
4250a5e5 | 328 | } |
30ae764b DB |
329 | |
330 | void walker_free(struct walker *walker) | |
331 | { | |
332 | walker->cleanup(walker); | |
333 | free(walker); | |
334 | } |