🌐 AI搜索 & 代理 主页
blob: c426c049974fa1bf1aceea9d43d2e2c5ce194e06 [file] [log] [blame]
Junio C Hamano215a7ad2005-09-08 00:26:231#include "fetch.h"
Daniel Barkalow4250a5e2005-04-30 23:53:562
3#include "cache.h"
4#include "commit.h"
5#include "tree.h"
Linus Torvalds1bc995a2006-05-29 19:20:486#include "tree-walk.h"
Daniel Barkalow3173bd42005-06-22 00:35:537#include "tag.h"
8#include "blob.h"
Daniel Barkalowcd541a62005-06-06 20:38:269#include "refs.h"
Petr Baudis8e87ca62006-07-27 21:56:1910#include "strbuf.h"
Daniel Barkalowcd541a62005-06-06 20:38:2611
Daniel Barkalow4250a5e2005-04-30 23:53:5612int get_tree = 0;
13int get_history = 0;
14int get_all = 0;
Junio C Hamanoe78d9772005-05-06 08:37:2115int get_verbosely = 0;
Daniel Barkalow820eca62005-09-27 01:38:0816int get_recover = 0;
Junio C Hamanob2d62f12005-05-04 08:26:2417static unsigned char current_commit_sha1[20];
Daniel Barkalow4250a5e2005-04-30 23:53:5618
barkalow@iabervon.org1e8be592005-08-02 23:46:1019void pull_say(const char *fmt, const char *hex)
20{
Junio C Hamanoe78d9772005-05-06 08:37:2121 if (get_verbosely)
22 fprintf(stderr, fmt, hex);
23}
24
Junio C Hamanob2d62f12005-05-04 08:26:2425static void report_missing(const char *what, const unsigned char *missing)
Junio C Hamanoee4f4392005-05-02 04:07:4026{
Junio C Hamanob2d62f12005-05-04 08:26:2427 char missing_hex[41];
28
29 strcpy(missing_hex, sha1_to_hex(missing));;
30 fprintf(stderr,
31 "Cannot obtain needed %s %s\nwhile processing commit %s.\n",
32 what, missing_hex, sha1_to_hex(current_commit_sha1));
33}
34
Sergey Vlasov80077f02005-09-21 16:33:5435static int process(struct object *obj);
Daniel Barkalow3173bd42005-06-22 00:35:5336
barkalow@iabervon.org1e8be592005-08-02 23:46:1037static int process_tree(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 Torvalds1bc995a2006-05-29 19:20:4845 desc.buf = tree->buffer;
46 desc.size = tree->size;
Linus Torvalds4c068a92006-05-30 16:45:4547 while (tree_entry(&desc, &entry)) {
Junio C Hamano6f9012b2006-06-02 22:23:4748 struct object *obj = NULL;
49
Linus Torvalds4c068a92006-05-30 16:45:4550 if (S_ISDIR(entry.mode)) {
51 struct tree *tree = lookup_tree(entry.sha1);
Junio C Hamano6f9012b2006-06-02 22:23:4752 if (tree)
53 obj = &tree->object;
Linus Torvalds2d9c58c2006-05-29 19:18:3354 }
Junio C Hamano6f9012b2006-06-02 22:23:4755 else {
56 struct blob *blob = lookup_blob(entry.sha1);
57 if (blob)
58 obj = &blob->object;
59 }
60 if (!obj || process(obj))
Daniel Barkalow4250a5e2005-04-30 23:53:5661 return -1;
Daniel Barkalow4250a5e2005-04-30 23:53:5662 }
Linus Torvalds2d9c58c2006-05-29 19:18:3363 free(tree->buffer);
64 tree->buffer = NULL;
Linus Torvalds1bc995a2006-05-29 19:20:4865 tree->size = 0;
Daniel Barkalow4250a5e2005-04-30 23:53:5666 return 0;
67}
68
Sergey Vlasov24451c32005-09-21 16:34:2469#define COMPLETE (1U << 0)
70#define SEEN (1U << 1)
71#define TO_SCAN (1U << 2)
Junio C Hamano85d106c2005-09-18 08:01:0772
Junio C Hamanod0ac30f2005-09-16 21:30:2973static struct commit_list *complete = NULL;
Daniel Barkalow22c6e1d2005-09-15 01:31:4274
barkalow@iabervon.org1e8be592005-08-02 23:46:1075static int process_commit(struct commit *commit)
Daniel Barkalow4250a5e2005-04-30 23:53:5676{
barkalow@iabervon.org1e8be592005-08-02 23:46:1077 if (parse_commit(commit))
Daniel Barkalow4250a5e2005-04-30 23:53:5678 return -1;
79
Daniel Barkalow22c6e1d2005-09-15 01:31:4280 while (complete && complete->item->date >= commit->date) {
Junio C Hamanod0ac30f2005-09-16 21:30:2981 pop_most_recent_commit(&complete, COMPLETE);
Daniel Barkalow22c6e1d2005-09-15 01:31:4282 }
Daniel Barkalow22c6e1d2005-09-15 01:31:4283
Junio C Hamanod0ac30f2005-09-16 21:30:2984 if (commit->object.flags & COMPLETE)
Daniel Barkalow22c6e1d2005-09-15 01:31:4285 return 0;
86
Shawn Pearcee7024962006-08-23 06:49:0087 hashcpy(current_commit_sha1, commit->object.sha1);
Daniel Barkalow4250a5e2005-04-30 23:53:5688
Junio C Hamano85d106c2005-09-18 08:01:0789 pull_say("walk %s\n", sha1_to_hex(commit->object.sha1));
90
Daniel Barkalow4250a5e2005-04-30 23:53:5691 if (get_tree) {
Sergey Vlasov80077f02005-09-21 16:33:5492 if (process(&commit->tree->object))
Daniel Barkalow4250a5e2005-04-30 23:53:5693 return -1;
94 if (!get_all)
95 get_tree = 0;
96 }
97 if (get_history) {
barkalow@iabervon.org1e8be592005-08-02 23:46:1098 struct commit_list *parents = commit->parents;
Daniel Barkalow4250a5e2005-04-30 23:53:5699 for (; parents; parents = parents->next) {
Sergey Vlasov80077f02005-09-21 16:33:54100 if (process(&parents->item->object))
Daniel Barkalow4250a5e2005-04-30 23:53:56101 return -1;
102 }
103 }
104 return 0;
105}
106
barkalow@iabervon.org1e8be592005-08-02 23:46:10107static int process_tag(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;
Sergey Vlasov80077f02005-09-21 16:33:54111 return process(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 Barkalowf88fcf82005-08-11 23:38:09117static int process_object(struct object *obj)
118{
Linus Torvalds19746322006-07-12 03:45:31119 if (obj->type == OBJ_COMMIT) {
Daniel Barkalowf88fcf82005-08-11 23:38:09120 if (process_commit((struct commit *)obj))
121 return -1;
122 return 0;
123 }
Linus Torvalds19746322006-07-12 03:45:31124 if (obj->type == OBJ_TREE) {
Daniel Barkalowf88fcf82005-08-11 23:38:09125 if (process_tree((struct tree *)obj))
126 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 Barkalowf88fcf82005-08-11 23:38:09133 if (process_tag((struct tag *)obj))
134 return -1;
135 return 0;
136 }
137 return error("Unable to determine requirements "
138 "of type %s for %s",
Linus Torvalds885a86a2006-06-14 23:45:13139 typename(obj->type), sha1_to_hex(obj->sha1));
Daniel Barkalowf88fcf82005-08-11 23:38:09140}
141
Sergey Vlasov80077f02005-09-21 16:33:54142static int process(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
Sergey Vlasov80077f02005-09-21 16:33:54148 if (has_sha1_file(obj->sha1)) {
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;
155 prefetch(obj->sha1);
Daniel Barkalowf88fcf82005-08-11 23:38:09156 }
Sergey Vlasov7b64d062005-09-21 16:34:14157
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
163static int loop(void)
164{
Junio C Hamano85d106c2005-09-18 08:01:07165 struct object_list *elem;
166
barkalow@iabervon.org1e8be592005-08-02 23:46:10167 while (process_queue) {
168 struct object *obj = process_queue->item;
Junio C Hamano85d106c2005-09-18 08:01:07169 elem = process_queue;
170 process_queue = elem->next;
171 free(elem);
barkalow@iabervon.org1e8be592005-08-02 23:46:10172 if (!process_queue)
173 process_queue_end = &process_queue;
174
Junio C Hamano85d106c2005-09-18 08:01:07175 /* If we are not scanning this object, we placed it in
176 * the queue because we needed to fetch it first.
177 */
178 if (! (obj->flags & TO_SCAN)) {
Nick Hengeveld11f0daf2005-10-11 06:22:01179 if (fetch(obj->sha1)) {
Linus Torvalds885a86a2006-06-14 23:45:13180 report_missing(typename(obj->type), obj->sha1);
Junio C Hamano85d106c2005-09-18 08:01:07181 return -1;
182 }
183 }
barkalow@iabervon.org1e8be592005-08-02 23:46:10184 if (!obj->type)
185 parse_object(obj->sha1);
Daniel Barkalowf88fcf82005-08-11 23:38:09186 if (process_object(obj))
187 return -1;
barkalow@iabervon.org1e8be592005-08-02 23:46:10188 }
189 return 0;
Daniel Barkalow3173bd42005-06-22 00:35:53190}
191
Daniel Barkalowcd541a62005-06-06 20:38:26192static int interpret_target(char *target, unsigned char *sha1)
193{
194 if (!get_sha1_hex(target, sha1))
195 return 0;
196 if (!check_ref_format(target)) {
197 if (!fetch_ref(target, sha1)) {
198 return 0;
199 }
200 }
201 return -1;
202}
203
Junio C Hamano8da19772006-09-21 05:02:01204static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
Daniel Barkalow22c6e1d2005-09-15 01:31:42205{
Junio C Hamanod0ac30f2005-09-16 21:30:29206 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
207 if (commit) {
208 commit->object.flags |= COMPLETE;
209 insert_by_date(commit, &complete);
Daniel Barkalow22c6e1d2005-09-15 01:31:42210 }
211 return 0;
212}
Daniel Barkalowcd541a62005-06-06 20:38:26213
Petr Baudis8e87ca62006-07-27 21:56:19214int pull_targets_stdin(char ***target, const char ***write_ref)
215{
216 int targets = 0, targets_alloc = 0;
217 struct strbuf buf;
218 *target = NULL; *write_ref = NULL;
219 strbuf_init(&buf);
220 while (1) {
221 char *rf_one = NULL;
222 char *tg_one;
223
224 read_line(&buf, stdin, '\n');
225 if (buf.eof)
226 break;
227 tg_one = buf.buf;
228 rf_one = strchr(tg_one, '\t');
229 if (rf_one)
230 *rf_one++ = 0;
231
232 if (targets >= targets_alloc) {
233 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
234 *target = xrealloc(*target, targets_alloc * sizeof(**target));
235 *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
236 }
Shawn Pearce9befac42006-09-02 04:16:31237 (*target)[targets] = xstrdup(tg_one);
238 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
Petr Baudis8e87ca62006-07-27 21:56:19239 targets++;
240 }
241 return targets;
242}
243
244void pull_targets_free(int targets, char **target, const char **write_ref)
245{
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
Petr Baudis4211e4d2006-07-27 21:56:17253int pull(int targets, char **target, const char **write_ref,
Petr Baudisc6b69bd2006-07-27 21:56:14254 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;
Sergey Vlasova95cb6f2005-09-23 12:28:13263 track_object_refs = 0;
Petr Baudis4211e4d2006-07-27 21:56:17264
265 for (i = 0; i < targets; i++) {
Johannes Schindelin1b03dfe2006-07-29 00:10:07266 if (!write_ref || !write_ref[i])
Petr Baudis4211e4d2006-07-27 21:56:17267 continue;
268
Junio C Hamano4431fcc2006-09-27 08:09:18269 lock[i] = lock_ref_sha1(write_ref[i], NULL);
Petr Baudis4211e4d2006-07-27 21:56:17270 if (!lock[i]) {
271 error("Can't lock ref %s", write_ref[i]);
272 goto unlock_and_fail;
Shawn Pearced0740d92006-05-19 07:29:26273 }
Daniel Barkalowcd541a62005-06-06 20:38:26274 }
275
Junio C Hamano84c667f2006-05-24 23:42:38276 if (!get_recover)
Junio C Hamanocb5d7092006-09-21 04:47:42277 for_each_ref(mark_complete, NULL);
Daniel Barkalow22c6e1d2005-09-15 01:31:42278
Petr Baudis4211e4d2006-07-27 21:56:17279 for (i = 0; i < targets; i++) {
280 if (interpret_target(target[i], &sha1[20 * i])) {
281 error("Could not interpret %s as something to pull", target[i]);
282 goto unlock_and_fail;
283 }
284 if (process(lookup_unknown_object(&sha1[20 * i])))
285 goto unlock_and_fail;
Shawn Pearce4bd18c42006-05-17 09:55:02286 }
287
Petr Baudis4211e4d2006-07-27 21:56:17288 if (loop())
289 goto unlock_and_fail;
290
291 if (write_ref_log_details) {
292 msg = xmalloc(strlen(write_ref_log_details) + 12);
293 sprintf(msg, "fetch from %s", write_ref_log_details);
294 } else {
295 msg = NULL;
Daniel Barkalowcd541a62005-06-06 20:38:26296 }
Petr Baudis4211e4d2006-07-27 21:56:17297 for (i = 0; i < targets; i++) {
Johannes Schindelin1b03dfe2006-07-29 00:10:07298 if (!write_ref || !write_ref[i])
Petr Baudis4211e4d2006-07-27 21:56:17299 continue;
300 ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)");
301 lock[i] = NULL;
302 if (ret)
303 goto unlock_and_fail;
304 }
Junio C Hamano4cac42b2006-08-28 04:19:39305 free(msg);
Petr Baudis4211e4d2006-07-27 21:56:17306
Daniel Barkalowcd541a62005-06-06 20:38:26307 return 0;
Petr Baudis4211e4d2006-07-27 21:56:17308
309
310unlock_and_fail:
311 for (i = 0; i < targets; i++)
312 if (lock[i])
313 unlock_ref(lock[i]);
314 return -1;
Daniel Barkalow4250a5e2005-04-30 23:53:56315}