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