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