🌐 AI搜索 & 代理 主页
blob: 4984bf8b3d6658dc2571f66bd5142e67c0fe7d37 [file] [log] [blame]
Daniel Barkalow4250a5e2005-04-30 23:53:561#include "cache.h"
Daniel Barkalow30ae7642007-09-11 03:02:452#include "walker.h"
Stefan Beller109cd762018-06-29 01:21:513#include "repository.h"
Stefan Bellercbd53a22018-05-15 23:42:154#include "object-store.h"
Daniel Barkalow4250a5e2005-04-30 23:53:565#include "commit.h"
6#include "tree.h"
Linus Torvalds1bc995a2006-05-29 19:20:487#include "tree-walk.h"
Daniel Barkalow3173bd42005-06-22 00:35:538#include "tag.h"
9#include "blob.h"
Daniel Barkalowcd541a62005-06-06 20:38:2610#include "refs.h"
René Scharfe7655b412020-03-03 20:55:3411#include "progress.h"
Daniel Barkalowcd541a62005-06-06 20:38:2612
brian m. carlson94f5a122017-10-15 22:06:4813static struct object_id current_commit_oid;
Daniel Barkalow4250a5e2005-04-30 23:53:5614
Jeff Kingfa262ca2016-07-08 09:25:2315void walker_say(struct walker *walker, const char *fmt, ...)
barkalow@iabervon.org1e8be592005-08-02 23:46:1016{
Jeff Kingfa262ca2016-07-08 09:25:2317 if (walker->get_verbosely) {
18 va_list ap;
19 va_start(ap, fmt);
20 vfprintf(stderr, fmt, ap);
21 va_end(ap);
22 }
Junio C Hamanoe78d9772005-05-06 08:37:2123}
24
Alex Riesen0d7a6e42006-12-12 17:34:0225static void report_missing(const struct object *obj)
Junio C Hamanoee4f4392005-05-02 04:07:4026{
Alex Riesen0d7a6e42006-12-12 17:34:0227 fprintf(stderr, "Cannot obtain needed %s %s\n",
Brandon Williamsdebca9d2018-02-14 18:59:2428 obj->type ? type_name(obj->type): "object",
brian m. carlsonf2fd0762015-11-10 02:22:2829 oid_to_hex(&obj->oid));
brian m. carlson94f5a122017-10-15 22:06:4830 if (!is_null_oid(&current_commit_oid))
Alex Riesen0d7a6e42006-12-12 17:34:0231 fprintf(stderr, "while processing commit %s.\n",
brian m. carlson94f5a122017-10-15 22:06:4832 oid_to_hex(&current_commit_oid));
Junio C Hamanob2d62f12005-05-04 08:26:2433}
34
Daniel Barkalow30ae7642007-09-11 03:02:4535static int process(struct walker *walker, struct object *obj);
Daniel Barkalow3173bd42005-06-22 00:35:5336
Daniel Barkalow30ae7642007-09-11 03:02:4537static int process_tree(struct walker *walker, struct tree *tree)
Daniel Barkalow4250a5e2005-04-30 23:53:5638{
Linus Torvalds1bc995a2006-05-29 19:20:4839 struct tree_desc desc;
Linus Torvalds4c068a92006-05-30 16:45:4540 struct name_entry entry;
Daniel Barkalow4250a5e2005-04-30 23:53:5641
42 if (parse_tree(tree))
43 return -1;
44
Linus Torvalds6fda5e52007-03-21 17:08:2545 init_tree_desc(&desc, tree->buffer, tree->size);
Linus Torvalds4c068a92006-05-30 16:45:4546 while (tree_entry(&desc, &entry)) {
Junio C Hamano6f9012b2006-06-02 22:23:4747 struct object *obj = NULL;
48
Sven Verdoolaege582c7392007-06-26 21:19:4149 /* submodule commits are not stored in the superproject */
Junio C Hamano68fb4652007-06-27 01:33:2450 if (S_ISGITLINK(entry.mode))
Sven Verdoolaege582c7392007-06-26 21:19:4151 continue;
Linus Torvalds4c068a92006-05-30 16:45:4552 if (S_ISDIR(entry.mode)) {
Stefan Bellerf86bcc72018-06-29 01:21:5653 struct tree *tree = lookup_tree(the_repository,
brian m. carlsonea82b2a2019-01-15 00:39:4454 &entry.oid);
Junio C Hamano6f9012b2006-06-02 22:23:4755 if (tree)
56 obj = &tree->object;
Linus Torvalds2d9c58c2006-05-29 19:18:3357 }
Junio C Hamano6f9012b2006-06-02 22:23:4758 else {
Stefan Bellerda14a7f2018-06-29 01:21:5559 struct blob *blob = lookup_blob(the_repository,
brian m. carlsonea82b2a2019-01-15 00:39:4460 &entry.oid);
Junio C Hamano6f9012b2006-06-02 22:23:4761 if (blob)
62 obj = &blob->object;
63 }
Daniel Barkalow30ae7642007-09-11 03:02:4564 if (!obj || process(walker, obj))
Daniel Barkalow4250a5e2005-04-30 23:53:5665 return -1;
Daniel Barkalow4250a5e2005-04-30 23:53:5666 }
Jeff King6e454b92013-06-05 22:37:3967 free_tree_buffer(tree);
Daniel Barkalow4250a5e2005-04-30 23:53:5668 return 0;
69}
70
Nguyễn Thái Ngọc Duy208acbf2014-03-25 13:23:2671/* Remember to update object flag allocation in object.h */
Sergey Vlasov24451c32005-09-21 16:34:2472#define COMPLETE (1U << 0)
73#define SEEN (1U << 1)
74#define TO_SCAN (1U << 2)
Junio C Hamano85d106c2005-09-18 08:01:0775
Junio C Hamanod0ac30f2005-09-16 21:30:2976static struct commit_list *complete = NULL;
Daniel Barkalow22c6e1d2005-09-15 01:31:4277
Daniel Barkalow30ae7642007-09-11 03:02:4578static int process_commit(struct walker *walker, struct commit *commit)
Daniel Barkalow4250a5e2005-04-30 23:53:5679{
Martin Ågren0b6b3422018-04-22 18:12:5080 struct commit_list *parents;
81
barkalow@iabervon.org1e8be592005-08-02 23:46:1082 if (parse_commit(commit))
Daniel Barkalow4250a5e2005-04-30 23:53:5683 return -1;
84
Daniel Barkalow22c6e1d2005-09-15 01:31:4285 while (complete && complete->item->date >= commit->date) {
Junio C Hamanod0ac30f2005-09-16 21:30:2986 pop_most_recent_commit(&complete, COMPLETE);
Daniel Barkalow22c6e1d2005-09-15 01:31:4287 }
Daniel Barkalow22c6e1d2005-09-15 01:31:4288
Junio C Hamanod0ac30f2005-09-16 21:30:2989 if (commit->object.flags & COMPLETE)
Daniel Barkalow22c6e1d2005-09-15 01:31:4290 return 0;
91
brian m. carlson94f5a122017-10-15 22:06:4892 oidcpy(&current_commit_oid, &commit->object.oid);
Daniel Barkalow4250a5e2005-04-30 23:53:5693
brian m. carlsonf2fd0762015-11-10 02:22:2894 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
Junio C Hamano85d106c2005-09-18 08:01:0795
Junio C Hamanoc89b6e12018-05-23 05:38:1396 if (process(walker, &get_commit_tree(commit)->object))
Martin Ågren0b6b3422018-04-22 18:12:5097 return -1;
98
99 for (parents = commit->parents; parents; parents = parents->next) {
100 if (process(walker, &parents->item->object))
Daniel Barkalow4250a5e2005-04-30 23:53:56101 return -1;
Daniel Barkalow4250a5e2005-04-30 23:53:56102 }
Martin Ågren0b6b3422018-04-22 18:12:50103
Daniel Barkalow4250a5e2005-04-30 23:53:56104 return 0;
105}
106
Daniel Barkalow30ae7642007-09-11 03:02:45107static int process_tag(struct walker *walker, struct tag *tag)
Daniel Barkalow3173bd42005-06-22 00:35:53108{
barkalow@iabervon.org1e8be592005-08-02 23:46:10109 if (parse_tag(tag))
Daniel Barkalow3173bd42005-06-22 00:35:53110 return -1;
Daniel Barkalow30ae7642007-09-11 03:02:45111 return process(walker, tag->tagged);
Daniel Barkalow3173bd42005-06-22 00:35:53112}
113
barkalow@iabervon.org1e8be592005-08-02 23:46:10114static struct object_list *process_queue = NULL;
115static struct object_list **process_queue_end = &process_queue;
116
Daniel Barkalow30ae7642007-09-11 03:02:45117static int process_object(struct walker *walker, struct object *obj)
Daniel Barkalowf88fcf82005-08-11 23:38:09118{
Linus Torvalds19746322006-07-12 03:45:31119 if (obj->type == OBJ_COMMIT) {
Daniel Barkalow30ae7642007-09-11 03:02:45120 if (process_commit(walker, (struct commit *)obj))
Daniel Barkalowf88fcf82005-08-11 23:38:09121 return -1;
122 return 0;
123 }
Linus Torvalds19746322006-07-12 03:45:31124 if (obj->type == OBJ_TREE) {
Daniel Barkalow30ae7642007-09-11 03:02:45125 if (process_tree(walker, (struct tree *)obj))
Daniel Barkalowf88fcf82005-08-11 23:38:09126 return -1;
127 return 0;
128 }
Linus Torvalds19746322006-07-12 03:45:31129 if (obj->type == OBJ_BLOB) {
Daniel Barkalowf88fcf82005-08-11 23:38:09130 return 0;
131 }
Linus Torvalds19746322006-07-12 03:45:31132 if (obj->type == OBJ_TAG) {
Daniel Barkalow30ae7642007-09-11 03:02:45133 if (process_tag(walker, (struct tag *)obj))
Daniel Barkalowf88fcf82005-08-11 23:38:09134 return -1;
135 return 0;
136 }
137 return error("Unable to determine requirements "
138 "of type %s for %s",
Brandon Williamsdebca9d2018-02-14 18:59:24139 type_name(obj->type), oid_to_hex(&obj->oid));
Daniel Barkalowf88fcf82005-08-11 23:38:09140}
141
Daniel Barkalow30ae7642007-09-11 03:02:45142static int process(struct walker *walker, struct object *obj)
Daniel Barkalow3173bd42005-06-22 00:35:53143{
Sergey Vlasova82d07e2005-09-21 16:33:59144 if (obj->flags & SEEN)
145 return 0;
146 obj->flags |= SEEN;
147
brian m. carlsonf2fd0762015-11-10 02:22:28148 if (has_object_file(&obj->oid)) {
Daniel Barkalowf88fcf82005-08-11 23:38:09149 /* We already have it, so we should scan it now. */
Junio C Hamano85d106c2005-09-18 08:01:07150 obj->flags |= TO_SCAN;
Junio C Hamanoe5f38ec2006-06-06 21:04:17151 }
152 else {
Sergey Vlasov7b64d062005-09-21 16:34:14153 if (obj->flags & COMPLETE)
154 return 0;
brian m. carlsoned1c9972015-11-10 02:22:29155 walker->prefetch(walker, obj->oid.hash);
Daniel Barkalowf88fcf82005-08-11 23:38:09156 }
Junio C Hamanoa6080a02007-06-07 07:04:01157
barkalow@iabervon.org1e8be592005-08-02 23:46:10158 object_list_insert(obj, process_queue_end);
159 process_queue_end = &(*process_queue_end)->next;
barkalow@iabervon.org1e8be592005-08-02 23:46:10160 return 0;
161}
162
Daniel Barkalow30ae7642007-09-11 03:02:45163static int loop(struct walker *walker)
barkalow@iabervon.org1e8be592005-08-02 23:46:10164{
Junio C Hamano85d106c2005-09-18 08:01:07165 struct object_list *elem;
René Scharfe7655b412020-03-03 20:55:34166 struct progress *progress = NULL;
167 uint64_t nr = 0;
168
169 if (walker->get_progress)
170 progress = start_delayed_progress(_("Fetching objects"), 0);
Junio C Hamano85d106c2005-09-18 08:01:07171
barkalow@iabervon.org1e8be592005-08-02 23:46:10172 while (process_queue) {
173 struct object *obj = process_queue->item;
Junio C Hamano85d106c2005-09-18 08:01:07174 elem = process_queue;
175 process_queue = elem->next;
176 free(elem);
barkalow@iabervon.org1e8be592005-08-02 23:46:10177 if (!process_queue)
178 process_queue_end = &process_queue;
179
Junio C Hamano85d106c2005-09-18 08:01:07180 /* If we are not scanning this object, we placed it in
181 * the queue because we needed to fetch it first.
182 */
183 if (! (obj->flags & TO_SCAN)) {
brian m. carlsoned1c9972015-11-10 02:22:29184 if (walker->fetch(walker, obj->oid.hash)) {
René Scharfe7655b412020-03-03 20:55:34185 stop_progress(&progress);
Alex Riesen0d7a6e42006-12-12 17:34:02186 report_missing(obj);
Junio C Hamano85d106c2005-09-18 08:01:07187 return -1;
188 }
189 }
barkalow@iabervon.org1e8be592005-08-02 23:46:10190 if (!obj->type)
Stefan Beller109cd762018-06-29 01:21:51191 parse_object(the_repository, &obj->oid);
René Scharfe7655b412020-03-03 20:55:34192 if (process_object(walker, obj)) {
193 stop_progress(&progress);
Daniel Barkalowf88fcf82005-08-11 23:38:09194 return -1;
René Scharfe7655b412020-03-03 20:55:34195 }
196 display_progress(progress, ++nr);
barkalow@iabervon.org1e8be592005-08-02 23:46:10197 }
René Scharfe7655b412020-03-03 20:55:34198 stop_progress(&progress);
barkalow@iabervon.org1e8be592005-08-02 23:46:10199 return 0;
Daniel Barkalow3173bd42005-06-22 00:35:53200}
201
brian m. carlson94f5a122017-10-15 22:06:48202static int interpret_target(struct walker *walker, char *target, struct object_id *oid)
Daniel Barkalowcd541a62005-06-06 20:38:26203{
brian m. carlson94f5a122017-10-15 22:06:48204 if (!get_oid_hex(target, oid))
Daniel Barkalowcd541a62005-06-06 20:38:26205 return 0;
Michael Haggerty8d9c5012011-09-15 21:10:25206 if (!check_refname_format(target, 0)) {
René Scharfe59c69c02008-10-18 08:44:18207 struct ref *ref = alloc_ref(target);
Daniel Barkalowc13b2632008-04-26 19:53:09208 if (!walker->fetch_ref(walker, ref)) {
brian m. carlson94f5a122017-10-15 22:06:48209 oidcpy(oid, &ref->old_oid);
Daniel Barkalowc13b2632008-04-26 19:53:09210 free(ref);
Daniel Barkalowcd541a62005-06-06 20:38:26211 return 0;
212 }
Daniel Barkalowc13b2632008-04-26 19:53:09213 free(ref);
Daniel Barkalowcd541a62005-06-06 20:38:26214 }
215 return -1;
216}
217
Michael Haggertyb4ebaf92015-05-25 18:39:14218static int mark_complete(const char *path, const struct object_id *oid,
219 int flag, void *cb_data)
Daniel Barkalow22c6e1d2005-09-15 01:31:42220{
Stefan Beller21e1ee82018-06-29 01:21:57221 struct commit *commit = lookup_commit_reference_gently(the_repository,
222 oid, 1);
Michael Haggertyb4ebaf92015-05-25 18:39:14223
Junio C Hamanod0ac30f2005-09-16 21:30:29224 if (commit) {
225 commit->object.flags |= COMPLETE;
René Scharfe3bc7a052014-08-21 18:30:24226 commit_list_insert(commit, &complete);
Daniel Barkalow22c6e1d2005-09-15 01:31:42227 }
228 return 0;
229}
Daniel Barkalowcd541a62005-06-06 20:38:26230
Daniel Barkalow30ae7642007-09-11 03:02:45231int walker_targets_stdin(char ***target, const char ***write_ref)
Petr Baudis8e87ca62006-07-27 21:56:19232{
233 int targets = 0, targets_alloc = 0;
Brandon Caseyf285a2d2008-10-09 19:12:12234 struct strbuf buf = STRBUF_INIT;
Petr Baudis8e87ca62006-07-27 21:56:19235 *target = NULL; *write_ref = NULL;
Petr Baudis8e87ca62006-07-27 21:56:19236 while (1) {
237 char *rf_one = NULL;
238 char *tg_one;
239
Junio C Hamano8f309ae2016-01-13 23:31:17240 if (strbuf_getline_lf(&buf, stdin) == EOF)
Petr Baudis8e87ca62006-07-27 21:56:19241 break;
242 tg_one = buf.buf;
243 rf_one = strchr(tg_one, '\t');
244 if (rf_one)
245 *rf_one++ = 0;
246
247 if (targets >= targets_alloc) {
248 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
René Scharfe2756ca42014-09-16 18:56:57249 REALLOC_ARRAY(*target, targets_alloc);
250 REALLOC_ARRAY(*write_ref, targets_alloc);
Petr Baudis8e87ca62006-07-27 21:56:19251 }
Shawn Pearce9befac42006-09-02 04:16:31252 (*target)[targets] = xstrdup(tg_one);
Jeff King8c53f072015-01-13 01:59:09253 (*write_ref)[targets] = xstrdup_or_null(rf_one);
Petr Baudis8e87ca62006-07-27 21:56:19254 targets++;
255 }
Pierre Habouzite6c019d2007-09-17 09:19:04256 strbuf_release(&buf);
Petr Baudis8e87ca62006-07-27 21:56:19257 return targets;
258}
259
Daniel Barkalow30ae7642007-09-11 03:02:45260void walker_targets_free(int targets, char **target, const char **write_ref)
Petr Baudis8e87ca62006-07-27 21:56:19261{
262 while (targets--) {
263 free(target[targets]);
Pierre Habouzit24deea52009-07-22 21:51:55264 if (write_ref)
Petr Baudis8e87ca62006-07-27 21:56:19265 free((char *) write_ref[targets]);
266 }
267}
268
Daniel Barkalow30ae7642007-09-11 03:02:45269int walker_fetch(struct walker *walker, int targets, char **target,
270 const char **write_ref, const char *write_ref_log_details)
Daniel Barkalow4250a5e2005-04-30 23:53:56271{
Ronnie Sahlbergb6b10bb2014-04-17 18:31:06272 struct strbuf refname = STRBUF_INIT;
273 struct strbuf err = STRBUF_INIT;
274 struct ref_transaction *transaction = NULL;
Jeff King667b76e2020-01-30 09:52:32275 struct object_id *oids;
Ronnie Sahlbergb6b10bb2014-04-17 18:31:06276 char *msg = NULL;
277 int i, ret = -1;
Daniel Barkalowcd541a62005-06-06 20:38:26278
Junio C Hamano98533b92005-09-15 22:06:39279 save_commit_buffer = 0;
Petr Baudis4211e4d2006-07-27 21:56:17280
Jeff King667b76e2020-01-30 09:52:32281 ALLOC_ARRAY(oids, targets);
282
Ronnie Sahlbergb6b10bb2014-04-17 18:31:06283 if (write_ref) {
284 transaction = ref_transaction_begin(&err);
285 if (!transaction) {
286 error("%s", err.buf);
287 goto done;
Shawn Pearced0740d92006-05-19 07:29:26288 }
Daniel Barkalowcd541a62005-06-06 20:38:26289 }
290
René Scharfe3bc7a052014-08-21 18:30:24291 if (!walker->get_recover) {
Michael Haggertyb4ebaf92015-05-25 18:39:14292 for_each_ref(mark_complete, NULL);
René Scharfe3bc7a052014-08-21 18:30:24293 commit_list_sort_by_date(&complete);
294 }
Daniel Barkalow22c6e1d2005-09-15 01:31:42295
Petr Baudis4211e4d2006-07-27 21:56:17296 for (i = 0; i < targets; i++) {
brian m. carlson94f5a122017-10-15 22:06:48297 if (interpret_target(walker, target[i], oids + i)) {
Sam Vilain5f487412007-12-17 12:00:43298 error("Could not interpret response from server '%s' as something to pull", target[i]);
Ronnie Sahlbergb6b10bb2014-04-17 18:31:06299 goto done;
Petr Baudis4211e4d2006-07-27 21:56:17300 }
Jeff King0ebbcf72019-06-20 07:41:10301 if (process(walker, lookup_unknown_object(&oids[i])))
Ronnie Sahlbergb6b10bb2014-04-17 18:31:06302 goto done;
Shawn Pearce4bd18c42006-05-17 09:55:02303 }
304
Daniel Barkalow30ae7642007-09-11 03:02:45305 if (loop(walker))
Ronnie Sahlbergb6b10bb2014-04-17 18:31:06306 goto done;
307 if (!write_ref) {
308 ret = 0;
309 goto done;
Petr Baudis4211e4d2006-07-27 21:56:17310 }
Shawn Pearced0740d92006-05-19 07:29:26311 if (write_ref_log_details) {
Junio C Hamano01d678a2014-09-11 17:33:30312 msg = xstrfmt("fetch from %s", write_ref_log_details);
Shawn Pearced0740d92006-05-19 07:29:26313 } else {
314 msg = NULL;
Daniel Barkalowcd541a62005-06-06 20:38:26315 }
Petr Baudis4211e4d2006-07-27 21:56:17316 for (i = 0; i < targets; i++) {
Ronnie Sahlbergb6b10bb2014-04-17 18:31:06317 if (!write_ref[i])
Petr Baudis4211e4d2006-07-27 21:56:17318 continue;
Ronnie Sahlbergb6b10bb2014-04-17 18:31:06319 strbuf_reset(&refname);
320 strbuf_addf(&refname, "refs/%s", write_ref[i]);
321 if (ref_transaction_update(transaction, refname.buf,
brian m. carlson89f3bbd2017-10-15 22:06:53322 oids + i, NULL, 0,
Ronnie Sahlbergdb7516a2014-04-30 19:22:42323 msg ? msg : "fetch (unknown)",
Ronnie Sahlbergb6b10bb2014-04-17 18:31:06324 &err)) {
325 error("%s", err.buf);
326 goto done;
327 }
Petr Baudis4211e4d2006-07-27 21:56:17328 }
Ronnie Sahlbergdb7516a2014-04-30 19:22:42329 if (ref_transaction_commit(transaction, &err)) {
Ronnie Sahlbergb6b10bb2014-04-17 18:31:06330 error("%s", err.buf);
331 goto done;
332 }
Petr Baudis4211e4d2006-07-27 21:56:17333
Ronnie Sahlbergb6b10bb2014-04-17 18:31:06334 ret = 0;
Petr Baudis4211e4d2006-07-27 21:56:17335
Ronnie Sahlbergb6b10bb2014-04-17 18:31:06336done:
337 ref_transaction_free(transaction);
Daniel Barkalow4250a5e2005-04-30 23:53:56338 free(msg);
brian m. carlson94f5a122017-10-15 22:06:48339 free(oids);
Ronnie Sahlbergb6b10bb2014-04-17 18:31:06340 strbuf_release(&err);
341 strbuf_release(&refname);
342 return ret;
Daniel Barkalow4250a5e2005-04-30 23:53:56343}
Daniel Barkalow30ae7642007-09-11 03:02:45344
345void walker_free(struct walker *walker)
346{
347 walker->cleanup(walker);
348 free(walker);
349}