🌐 AI搜索 & 代理 主页
blob: e57630e983488e5c0222dc47a5e32d6efb184762 [file] [log] [blame]
Daniel Barkalow4250a5e2005-04-30 23:53:561#include "cache.h"
Daniel Barkalow30ae7642007-09-11 03:02:452#include "walker.h"
Daniel Barkalow4250a5e2005-04-30 23:53:563#include "commit.h"
4#include "tree.h"
Linus Torvalds1bc995a2006-05-29 19:20:485#include "tree-walk.h"
Daniel Barkalow3173bd42005-06-22 00:35:536#include "tag.h"
7#include "blob.h"
Daniel Barkalowcd541a62005-06-06 20:38:268#include "refs.h"
9
Junio C Hamanob2d62f12005-05-04 08:26:2410static unsigned char current_commit_sha1[20];
Daniel Barkalow4250a5e2005-04-30 23:53:5611
Daniel Barkalow30ae7642007-09-11 03:02:4512void walker_say(struct walker *walker, const char *fmt, const char *hex)
barkalow@iabervon.org1e8be592005-08-02 23:46:1013{
Daniel Barkalow30ae7642007-09-11 03:02:4514 if (walker->get_verbosely)
Junio C Hamanoe78d9772005-05-06 08:37:2115 fprintf(stderr, fmt, hex);
16}
17
Alex Riesen0d7a6e42006-12-12 17:34:0218static void report_missing(const struct object *obj)
Junio C Hamanoee4f4392005-05-02 04:07:4019{
Junio C Hamanob2d62f12005-05-04 08:26:2420 char missing_hex[41];
Junio C Hamanoba19a802009-02-11 01:42:0421 strcpy(missing_hex, sha1_to_hex(obj->sha1));
Alex Riesen0d7a6e42006-12-12 17:34:0222 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));
Junio C Hamanob2d62f12005-05-04 08:26:2427}
28
Daniel Barkalow30ae7642007-09-11 03:02:4529static int process(struct walker *walker, struct object *obj);
Daniel Barkalow3173bd42005-06-22 00:35:5330
Daniel Barkalow30ae7642007-09-11 03:02:4531static int process_tree(struct walker *walker, struct tree *tree)
Daniel Barkalow4250a5e2005-04-30 23:53:5632{
Linus Torvalds1bc995a2006-05-29 19:20:4833 struct tree_desc desc;
Linus Torvalds4c068a92006-05-30 16:45:4534 struct name_entry entry;
Daniel Barkalow4250a5e2005-04-30 23:53:5635
36 if (parse_tree(tree))
37 return -1;
38
Linus Torvalds6fda5e52007-03-21 17:08:2539 init_tree_desc(&desc, tree->buffer, tree->size);
Linus Torvalds4c068a92006-05-30 16:45:4540 while (tree_entry(&desc, &entry)) {
Junio C Hamano6f9012b2006-06-02 22:23:4741 struct object *obj = NULL;
42
Sven Verdoolaege582c7392007-06-26 21:19:4143 /* submodule commits are not stored in the superproject */
Junio C Hamano68fb4652007-06-27 01:33:2444 if (S_ISGITLINK(entry.mode))
Sven Verdoolaege582c7392007-06-26 21:19:4145 continue;
Linus Torvalds4c068a92006-05-30 16:45:4546 if (S_ISDIR(entry.mode)) {
47 struct tree *tree = lookup_tree(entry.sha1);
Junio C Hamano6f9012b2006-06-02 22:23:4748 if (tree)
49 obj = &tree->object;
Linus Torvalds2d9c58c2006-05-29 19:18:3350 }
Junio C Hamano6f9012b2006-06-02 22:23:4751 else {
52 struct blob *blob = lookup_blob(entry.sha1);
53 if (blob)
54 obj = &blob->object;
55 }
Daniel Barkalow30ae7642007-09-11 03:02:4556 if (!obj || process(walker, obj))
Daniel Barkalow4250a5e2005-04-30 23:53:5657 return -1;
Daniel Barkalow4250a5e2005-04-30 23:53:5658 }
Linus Torvalds2d9c58c2006-05-29 19:18:3359 free(tree->buffer);
60 tree->buffer = NULL;
Linus Torvalds1bc995a2006-05-29 19:20:4861 tree->size = 0;
Jeff King541fc212008-06-04 18:38:5862 tree->object.parsed = 0;
Daniel Barkalow4250a5e2005-04-30 23:53:5663 return 0;
64}
65
Sergey Vlasov24451c32005-09-21 16:34:2466#define COMPLETE (1U << 0)
67#define SEEN (1U << 1)
68#define TO_SCAN (1U << 2)
Junio C Hamano85d106c2005-09-18 08:01:0769
Junio C Hamanod0ac30f2005-09-16 21:30:2970static struct commit_list *complete = NULL;
Daniel Barkalow22c6e1d2005-09-15 01:31:4271
Daniel Barkalow30ae7642007-09-11 03:02:4572static int process_commit(struct walker *walker, struct commit *commit)
Daniel Barkalow4250a5e2005-04-30 23:53:5673{
barkalow@iabervon.org1e8be592005-08-02 23:46:1074 if (parse_commit(commit))
Daniel Barkalow4250a5e2005-04-30 23:53:5675 return -1;
76
Daniel Barkalow22c6e1d2005-09-15 01:31:4277 while (complete && complete->item->date >= commit->date) {
Junio C Hamanod0ac30f2005-09-16 21:30:2978 pop_most_recent_commit(&complete, COMPLETE);
Daniel Barkalow22c6e1d2005-09-15 01:31:4279 }
Daniel Barkalow22c6e1d2005-09-15 01:31:4280
Junio C Hamanod0ac30f2005-09-16 21:30:2981 if (commit->object.flags & COMPLETE)
Daniel Barkalow22c6e1d2005-09-15 01:31:4282 return 0;
83
Shawn Pearcee7024962006-08-23 06:49:0084 hashcpy(current_commit_sha1, commit->object.sha1);
Daniel Barkalow4250a5e2005-04-30 23:53:5685
Daniel Barkalow30ae7642007-09-11 03:02:4586 walker_say(walker, "walk %s\n", sha1_to_hex(commit->object.sha1));
Junio C Hamano85d106c2005-09-18 08:01:0787
Daniel Barkalow30ae7642007-09-11 03:02:4588 if (walker->get_tree) {
89 if (process(walker, &commit->tree->object))
Daniel Barkalow4250a5e2005-04-30 23:53:5690 return -1;
Daniel Barkalow30ae7642007-09-11 03:02:4591 if (!walker->get_all)
92 walker->get_tree = 0;
Daniel Barkalow4250a5e2005-04-30 23:53:5693 }
Daniel Barkalow30ae7642007-09-11 03:02:4594 if (walker->get_history) {
barkalow@iabervon.org1e8be592005-08-02 23:46:1095 struct commit_list *parents = commit->parents;
Daniel Barkalow4250a5e2005-04-30 23:53:5696 for (; parents; parents = parents->next) {
Daniel Barkalow30ae7642007-09-11 03:02:4597 if (process(walker, &parents->item->object))
Daniel Barkalow4250a5e2005-04-30 23:53:5698 return -1;
99 }
100 }
101 return 0;
102}
103
Daniel Barkalow30ae7642007-09-11 03:02:45104static int process_tag(struct walker *walker, struct tag *tag)
Daniel Barkalow3173bd42005-06-22 00:35:53105{
barkalow@iabervon.org1e8be592005-08-02 23:46:10106 if (parse_tag(tag))
Daniel Barkalow3173bd42005-06-22 00:35:53107 return -1;
Daniel Barkalow30ae7642007-09-11 03:02:45108 return process(walker, tag->tagged);
Daniel Barkalow3173bd42005-06-22 00:35:53109}
110
barkalow@iabervon.org1e8be592005-08-02 23:46:10111static struct object_list *process_queue = NULL;
112static struct object_list **process_queue_end = &process_queue;
113
Daniel Barkalow30ae7642007-09-11 03:02:45114static int process_object(struct walker *walker, struct object *obj)
Daniel Barkalowf88fcf82005-08-11 23:38:09115{
Linus Torvalds19746322006-07-12 03:45:31116 if (obj->type == OBJ_COMMIT) {
Daniel Barkalow30ae7642007-09-11 03:02:45117 if (process_commit(walker, (struct commit *)obj))
Daniel Barkalowf88fcf82005-08-11 23:38:09118 return -1;
119 return 0;
120 }
Linus Torvalds19746322006-07-12 03:45:31121 if (obj->type == OBJ_TREE) {
Daniel Barkalow30ae7642007-09-11 03:02:45122 if (process_tree(walker, (struct tree *)obj))
Daniel Barkalowf88fcf82005-08-11 23:38:09123 return -1;
124 return 0;
125 }
Linus Torvalds19746322006-07-12 03:45:31126 if (obj->type == OBJ_BLOB) {
Daniel Barkalowf88fcf82005-08-11 23:38:09127 return 0;
128 }
Linus Torvalds19746322006-07-12 03:45:31129 if (obj->type == OBJ_TAG) {
Daniel Barkalow30ae7642007-09-11 03:02:45130 if (process_tag(walker, (struct tag *)obj))
Daniel Barkalowf88fcf82005-08-11 23:38:09131 return -1;
132 return 0;
133 }
134 return error("Unable to determine requirements "
135 "of type %s for %s",
Linus Torvalds885a86a2006-06-14 23:45:13136 typename(obj->type), sha1_to_hex(obj->sha1));
Daniel Barkalowf88fcf82005-08-11 23:38:09137}
138
Daniel Barkalow30ae7642007-09-11 03:02:45139static int process(struct walker *walker, struct object *obj)
Daniel Barkalow3173bd42005-06-22 00:35:53140{
Sergey Vlasova82d07e2005-09-21 16:33:59141 if (obj->flags & SEEN)
142 return 0;
143 obj->flags |= SEEN;
144
Sergey Vlasov80077f02005-09-21 16:33:54145 if (has_sha1_file(obj->sha1)) {
Daniel Barkalowf88fcf82005-08-11 23:38:09146 /* We already have it, so we should scan it now. */
Junio C Hamano85d106c2005-09-18 08:01:07147 obj->flags |= TO_SCAN;
Junio C Hamanoe5f38ec2006-06-06 21:04:17148 }
149 else {
Sergey Vlasov7b64d062005-09-21 16:34:14150 if (obj->flags & COMPLETE)
151 return 0;
Daniel Barkalow30ae7642007-09-11 03:02:45152 walker->prefetch(walker, obj->sha1);
Daniel Barkalowf88fcf82005-08-11 23:38:09153 }
Junio C Hamanoa6080a02007-06-07 07:04:01154
barkalow@iabervon.org1e8be592005-08-02 23:46:10155 object_list_insert(obj, process_queue_end);
156 process_queue_end = &(*process_queue_end)->next;
barkalow@iabervon.org1e8be592005-08-02 23:46:10157 return 0;
158}
159
Daniel Barkalow30ae7642007-09-11 03:02:45160static int loop(struct walker *walker)
barkalow@iabervon.org1e8be592005-08-02 23:46:10161{
Junio C Hamano85d106c2005-09-18 08:01:07162 struct object_list *elem;
163
barkalow@iabervon.org1e8be592005-08-02 23:46:10164 while (process_queue) {
165 struct object *obj = process_queue->item;
Junio C Hamano85d106c2005-09-18 08:01:07166 elem = process_queue;
167 process_queue = elem->next;
168 free(elem);
barkalow@iabervon.org1e8be592005-08-02 23:46:10169 if (!process_queue)
170 process_queue_end = &process_queue;
171
Junio C Hamano85d106c2005-09-18 08:01:07172 /* If we are not scanning this object, we placed it in
173 * the queue because we needed to fetch it first.
174 */
175 if (! (obj->flags & TO_SCAN)) {
Daniel Barkalow30ae7642007-09-11 03:02:45176 if (walker->fetch(walker, obj->sha1)) {
Alex Riesen0d7a6e42006-12-12 17:34:02177 report_missing(obj);
Junio C Hamano85d106c2005-09-18 08:01:07178 return -1;
179 }
180 }
barkalow@iabervon.org1e8be592005-08-02 23:46:10181 if (!obj->type)
182 parse_object(obj->sha1);
Daniel Barkalow30ae7642007-09-11 03:02:45183 if (process_object(walker, obj))
Daniel Barkalowf88fcf82005-08-11 23:38:09184 return -1;
barkalow@iabervon.org1e8be592005-08-02 23:46:10185 }
186 return 0;
Daniel Barkalow3173bd42005-06-22 00:35:53187}
188
Daniel Barkalow30ae7642007-09-11 03:02:45189static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
Daniel Barkalowcd541a62005-06-06 20:38:26190{
191 if (!get_sha1_hex(target, sha1))
192 return 0;
193 if (!check_ref_format(target)) {
René Scharfe59c69c02008-10-18 08:44:18194 struct ref *ref = alloc_ref(target);
Daniel Barkalowc13b2632008-04-26 19:53:09195 if (!walker->fetch_ref(walker, ref)) {
196 hashcpy(sha1, ref->old_sha1);
197 free(ref);
Daniel Barkalowcd541a62005-06-06 20:38:26198 return 0;
199 }
Daniel Barkalowc13b2632008-04-26 19:53:09200 free(ref);
Daniel Barkalowcd541a62005-06-06 20:38:26201 }
202 return -1;
203}
204
Junio C Hamano8da19772006-09-21 05:02:01205static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
Daniel Barkalow22c6e1d2005-09-15 01:31:42206{
Junio C Hamanod0ac30f2005-09-16 21:30:29207 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
208 if (commit) {
209 commit->object.flags |= COMPLETE;
210 insert_by_date(commit, &complete);
Daniel Barkalow22c6e1d2005-09-15 01:31:42211 }
212 return 0;
213}
Daniel Barkalowcd541a62005-06-06 20:38:26214
Daniel Barkalow30ae7642007-09-11 03:02:45215int walker_targets_stdin(char ***target, const char ***write_ref)
Petr Baudis8e87ca62006-07-27 21:56:19216{
217 int targets = 0, targets_alloc = 0;
Brandon Caseyf285a2d2008-10-09 19:12:12218 struct strbuf buf = STRBUF_INIT;
Petr Baudis8e87ca62006-07-27 21:56:19219 *target = NULL; *write_ref = NULL;
Petr Baudis8e87ca62006-07-27 21:56:19220 while (1) {
221 char *rf_one = NULL;
222 char *tg_one;
223
Pierre Habouzite6c019d2007-09-17 09:19:04224 if (strbuf_getline(&buf, stdin, '\n') == EOF)
Petr Baudis8e87ca62006-07-27 21:56:19225 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 }
Shawn Pearce9befac42006-09-02 04:16:31236 (*target)[targets] = xstrdup(tg_one);
237 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
Petr Baudis8e87ca62006-07-27 21:56:19238 targets++;
239 }
Pierre Habouzite6c019d2007-09-17 09:19:04240 strbuf_release(&buf);
Petr Baudis8e87ca62006-07-27 21:56:19241 return targets;
242}
243
Daniel Barkalow30ae7642007-09-11 03:02:45244void walker_targets_free(int targets, char **target, const char **write_ref)
Petr Baudis8e87ca62006-07-27 21:56:19245{
246 while (targets--) {
247 free(target[targets]);
Johannes Schindelin1b03dfe2006-07-29 00:10:07248 if (write_ref && write_ref[targets])
Petr Baudis8e87ca62006-07-27 21:56:19249 free((char *) write_ref[targets]);
250 }
251}
252
Daniel Barkalow30ae7642007-09-11 03:02:45253int walker_fetch(struct walker *walker, int targets, char **target,
254 const char **write_ref, const char *write_ref_log_details)
Daniel Barkalow4250a5e2005-04-30 23:53:56255{
Petr Baudis4211e4d2006-07-27 21:56:17256 struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
257 unsigned char *sha1 = xmalloc(targets * 20);
Shawn Pearced0740d92006-05-19 07:29:26258 char *msg;
259 int ret;
Petr Baudis4211e4d2006-07-27 21:56:17260 int i;
Daniel Barkalowcd541a62005-06-06 20:38:26261
Junio C Hamano98533b92005-09-15 22:06:39262 save_commit_buffer = 0;
Petr Baudis4211e4d2006-07-27 21:56:17263
264 for (i = 0; i < targets; i++) {
Johannes Schindelin1b03dfe2006-07-29 00:10:07265 if (!write_ref || !write_ref[i])
Petr Baudis4211e4d2006-07-27 21:56:17266 continue;
267
Junio C Hamano4431fcc2006-09-27 08:09:18268 lock[i] = lock_ref_sha1(write_ref[i], NULL);
Petr Baudis4211e4d2006-07-27 21:56:17269 if (!lock[i]) {
270 error("Can't lock ref %s", write_ref[i]);
271 goto unlock_and_fail;
Shawn Pearced0740d92006-05-19 07:29:26272 }
Daniel Barkalowcd541a62005-06-06 20:38:26273 }
274
Daniel Barkalow30ae7642007-09-11 03:02:45275 if (!walker->get_recover)
Junio C Hamanocb5d7092006-09-21 04:47:42276 for_each_ref(mark_complete, NULL);
Daniel Barkalow22c6e1d2005-09-15 01:31:42277
Petr Baudis4211e4d2006-07-27 21:56:17278 for (i = 0; i < targets; i++) {
Daniel Barkalow30ae7642007-09-11 03:02:45279 if (interpret_target(walker, target[i], &sha1[20 * i])) {
Sam Vilain5f487412007-12-17 12:00:43280 error("Could not interpret response from server '%s' as something to pull", target[i]);
Petr Baudis4211e4d2006-07-27 21:56:17281 goto unlock_and_fail;
282 }
Daniel Barkalow30ae7642007-09-11 03:02:45283 if (process(walker, lookup_unknown_object(&sha1[20 * i])))
Petr Baudis4211e4d2006-07-27 21:56:17284 goto unlock_and_fail;
Shawn Pearce4bd18c42006-05-17 09:55:02285 }
286
Daniel Barkalow30ae7642007-09-11 03:02:45287 if (loop(walker))
Petr Baudis4211e4d2006-07-27 21:56:17288 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;
Daniel Barkalowcd541a62005-06-06 20:38:26295 }
Petr Baudis4211e4d2006-07-27 21:56:17296 for (i = 0; i < targets; i++) {
Johannes Schindelin1b03dfe2006-07-29 00:10:07297 if (!write_ref || !write_ref[i])
Petr Baudis4211e4d2006-07-27 21:56:17298 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;
303 }
Junio C Hamano4cac42b2006-08-28 04:19:39304 free(msg);
Petr Baudis4211e4d2006-07-27 21:56:17305
Daniel Barkalowcd541a62005-06-06 20:38:26306 return 0;
Petr Baudis4211e4d2006-07-27 21:56:17307
Petr Baudis4211e4d2006-07-27 21:56:17308unlock_and_fail:
309 for (i = 0; i < targets; i++)
310 if (lock[i])
311 unlock_ref(lock[i]);
Daniel Barkalow30ae7642007-09-11 03:02:45312
Petr Baudis4211e4d2006-07-27 21:56:17313 return -1;
Daniel Barkalow4250a5e2005-04-30 23:53:56314}
Daniel Barkalow30ae7642007-09-11 03:02:45315
316void walker_free(struct walker *walker)
317{
318 walker->cleanup(walker);
319 free(walker);
320}