blob: 1a33ae984fe999096b0e06fb66155a19be001346 [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"
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
10const char *write_ref = NULL;
11
12const unsigned char *current_ref = NULL;
Daniel Barkalow4250a5e2005-04-30 23:53:5613
14int get_tree = 0;
15int get_history = 0;
16int get_all = 0;
Junio C Hamanoe78d9772005-05-06 08:37:2117int get_verbosely = 0;
Junio C Hamanob2d62f12005-05-04 08:26:2418static unsigned char current_commit_sha1[20];
Daniel Barkalow4250a5e2005-04-30 23:53:5619
barkalow@iabervon.org1e8be592005-08-02 23:46:1020void pull_say(const char *fmt, const char *hex)
21{
Junio C Hamanoe78d9772005-05-06 08:37:2122 if (get_verbosely)
23 fprintf(stderr, fmt, hex);
24}
25
Junio C Hamanob2d62f12005-05-04 08:26:2426static void report_missing(const char *what, const unsigned char *missing)
Junio C Hamanoee4f4392005-05-02 04:07:4027{
Junio C Hamanob2d62f12005-05-04 08:26:2428 char missing_hex[41];
29
30 strcpy(missing_hex, sha1_to_hex(missing));;
31 fprintf(stderr,
32 "Cannot obtain needed %s %s\nwhile processing commit %s.\n",
33 what, missing_hex, sha1_to_hex(current_commit_sha1));
34}
35
Sergey Vlasov80077f02005-09-21 16:33:5436static int process(struct object *obj);
Daniel Barkalow3173bd42005-06-22 00:35:5337
barkalow@iabervon.org1e8be592005-08-02 23:46:1038static int process_tree(struct tree *tree)
Daniel Barkalow4250a5e2005-04-30 23:53:5639{
Junio C Hamano85d106c2005-09-18 08:01:0740 struct tree_entry_list *entry;
Daniel Barkalow4250a5e2005-04-30 23:53:5641
42 if (parse_tree(tree))
43 return -1;
44
Junio C Hamano85d106c2005-09-18 08:01:0745 entry = tree->entries;
46 tree->entries = NULL;
47 while (entry) {
48 struct tree_entry_list *next = entry->next;
Sergey Vlasov80077f02005-09-21 16:33:5449 if (process(entry->item.any))
Daniel Barkalow4250a5e2005-04-30 23:53:5650 return -1;
Sergey Vlasovd35bbe02005-09-23 12:28:1851 free(entry->name);
Junio C Hamano85d106c2005-09-18 08:01:0752 free(entry);
53 entry = next;
Daniel Barkalow4250a5e2005-04-30 23:53:5654 }
55 return 0;
56}
57
Sergey Vlasov24451c32005-09-21 16:34:2458#define COMPLETE (1U << 0)
59#define SEEN (1U << 1)
60#define TO_SCAN (1U << 2)
Junio C Hamano85d106c2005-09-18 08:01:0761
Junio C Hamanod0ac30f2005-09-16 21:30:2962static struct commit_list *complete = NULL;
Daniel Barkalow22c6e1d2005-09-15 01:31:4263
barkalow@iabervon.org1e8be592005-08-02 23:46:1064static int process_commit(struct commit *commit)
Daniel Barkalow4250a5e2005-04-30 23:53:5665{
barkalow@iabervon.org1e8be592005-08-02 23:46:1066 if (parse_commit(commit))
Daniel Barkalow4250a5e2005-04-30 23:53:5667 return -1;
68
Daniel Barkalow22c6e1d2005-09-15 01:31:4269 while (complete && complete->item->date >= commit->date) {
Junio C Hamanod0ac30f2005-09-16 21:30:2970 pop_most_recent_commit(&complete, COMPLETE);
Daniel Barkalow22c6e1d2005-09-15 01:31:4271 }
Daniel Barkalow22c6e1d2005-09-15 01:31:4272
Junio C Hamanod0ac30f2005-09-16 21:30:2973 if (commit->object.flags & COMPLETE)
Daniel Barkalow22c6e1d2005-09-15 01:31:4274 return 0;
75
barkalow@iabervon.org1e8be592005-08-02 23:46:1076 memcpy(current_commit_sha1, commit->object.sha1, 20);
Daniel Barkalow4250a5e2005-04-30 23:53:5677
Junio C Hamano85d106c2005-09-18 08:01:0778 pull_say("walk %s\n", sha1_to_hex(commit->object.sha1));
79
Daniel Barkalow4250a5e2005-04-30 23:53:5680 if (get_tree) {
Sergey Vlasov80077f02005-09-21 16:33:5481 if (process(&commit->tree->object))
Daniel Barkalow4250a5e2005-04-30 23:53:5682 return -1;
83 if (!get_all)
84 get_tree = 0;
85 }
86 if (get_history) {
barkalow@iabervon.org1e8be592005-08-02 23:46:1087 struct commit_list *parents = commit->parents;
Daniel Barkalow4250a5e2005-04-30 23:53:5688 for (; parents; parents = parents->next) {
Sergey Vlasov80077f02005-09-21 16:33:5489 if (process(&parents->item->object))
Daniel Barkalow4250a5e2005-04-30 23:53:5690 return -1;
91 }
92 }
93 return 0;
94}
95
barkalow@iabervon.org1e8be592005-08-02 23:46:1096static int process_tag(struct tag *tag)
Daniel Barkalow3173bd42005-06-22 00:35:5397{
barkalow@iabervon.org1e8be592005-08-02 23:46:1098 if (parse_tag(tag))
Daniel Barkalow3173bd42005-06-22 00:35:5399 return -1;
Sergey Vlasov80077f02005-09-21 16:33:54100 return process(tag->tagged);
Daniel Barkalow3173bd42005-06-22 00:35:53101}
102
barkalow@iabervon.org1e8be592005-08-02 23:46:10103static struct object_list *process_queue = NULL;
104static struct object_list **process_queue_end = &process_queue;
105
Daniel Barkalowf88fcf82005-08-11 23:38:09106static int process_object(struct object *obj)
107{
108 if (obj->type == commit_type) {
109 if (process_commit((struct commit *)obj))
110 return -1;
111 return 0;
112 }
113 if (obj->type == tree_type) {
114 if (process_tree((struct tree *)obj))
115 return -1;
116 return 0;
117 }
118 if (obj->type == blob_type) {
119 return 0;
120 }
121 if (obj->type == tag_type) {
122 if (process_tag((struct tag *)obj))
123 return -1;
124 return 0;
125 }
126 return error("Unable to determine requirements "
127 "of type %s for %s",
128 obj->type, sha1_to_hex(obj->sha1));
129}
130
Sergey Vlasov80077f02005-09-21 16:33:54131static int process(struct object *obj)
Daniel Barkalow3173bd42005-06-22 00:35:53132{
Sergey Vlasova82d07e2005-09-21 16:33:59133 if (obj->flags & SEEN)
134 return 0;
135 obj->flags |= SEEN;
136
Sergey Vlasov80077f02005-09-21 16:33:54137 if (has_sha1_file(obj->sha1)) {
Daniel Barkalowf88fcf82005-08-11 23:38:09138 /* We already have it, so we should scan it now. */
Junio C Hamano85d106c2005-09-18 08:01:07139 obj->flags |= TO_SCAN;
Sergey Vlasov7b64d062005-09-21 16:34:14140 } else {
141 if (obj->flags & COMPLETE)
142 return 0;
143 prefetch(obj->sha1);
Daniel Barkalowf88fcf82005-08-11 23:38:09144 }
Sergey Vlasov7b64d062005-09-21 16:34:14145
barkalow@iabervon.org1e8be592005-08-02 23:46:10146 object_list_insert(obj, process_queue_end);
147 process_queue_end = &(*process_queue_end)->next;
barkalow@iabervon.org1e8be592005-08-02 23:46:10148 return 0;
149}
150
151static int loop(void)
152{
Junio C Hamano85d106c2005-09-18 08:01:07153 struct object_list *elem;
154
barkalow@iabervon.org1e8be592005-08-02 23:46:10155 while (process_queue) {
156 struct object *obj = process_queue->item;
Junio C Hamano85d106c2005-09-18 08:01:07157 elem = process_queue;
158 process_queue = elem->next;
159 free(elem);
barkalow@iabervon.org1e8be592005-08-02 23:46:10160 if (!process_queue)
161 process_queue_end = &process_queue;
162
Junio C Hamano85d106c2005-09-18 08:01:07163 /* If we are not scanning this object, we placed it in
164 * the queue because we needed to fetch it first.
165 */
166 if (! (obj->flags & TO_SCAN)) {
Junio C Hamano029f6de2005-09-18 21:11:53167 if (!has_sha1_file(obj->sha1) && fetch(obj->sha1)) {
Junio C Hamano85d106c2005-09-18 08:01:07168 report_missing(obj->type
169 ? obj->type
170 : "object", obj->sha1);
171 return -1;
172 }
173 }
barkalow@iabervon.org1e8be592005-08-02 23:46:10174 if (!obj->type)
175 parse_object(obj->sha1);
Daniel Barkalowf88fcf82005-08-11 23:38:09176 if (process_object(obj))
177 return -1;
barkalow@iabervon.org1e8be592005-08-02 23:46:10178 }
179 return 0;
Daniel Barkalow3173bd42005-06-22 00:35:53180}
181
Daniel Barkalowcd541a62005-06-06 20:38:26182static int interpret_target(char *target, unsigned char *sha1)
183{
184 if (!get_sha1_hex(target, sha1))
185 return 0;
186 if (!check_ref_format(target)) {
187 if (!fetch_ref(target, sha1)) {
188 return 0;
189 }
190 }
191 return -1;
192}
193
Daniel Barkalow22c6e1d2005-09-15 01:31:42194static int mark_complete(const char *path, const unsigned char *sha1)
195{
Junio C Hamanod0ac30f2005-09-16 21:30:29196 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
197 if (commit) {
198 commit->object.flags |= COMPLETE;
199 insert_by_date(commit, &complete);
Daniel Barkalow22c6e1d2005-09-15 01:31:42200 }
201 return 0;
202}
Daniel Barkalowcd541a62005-06-06 20:38:26203
Daniel Barkalow4250a5e2005-04-30 23:53:56204int pull(char *target)
205{
Daniel Barkalow4250a5e2005-04-30 23:53:56206 unsigned char sha1[20];
Daniel Barkalowcd541a62005-06-06 20:38:26207 int fd = -1;
208
Junio C Hamano98533b92005-09-15 22:06:39209 save_commit_buffer = 0;
Sergey Vlasova95cb6f2005-09-23 12:28:13210 track_object_refs = 0;
Daniel Barkalowcd541a62005-06-06 20:38:26211 if (write_ref && current_ref) {
212 fd = lock_ref_sha1(write_ref, current_ref);
213 if (fd < 0)
214 return -1;
215 }
216
Daniel Barkalow22c6e1d2005-09-15 01:31:42217 for_each_ref(mark_complete);
218
Daniel Barkalowcd541a62005-06-06 20:38:26219 if (interpret_target(target, sha1))
220 return error("Could not interpret %s as something to pull",
221 target);
Sergey Vlasov80077f02005-09-21 16:33:54222 if (process(lookup_unknown_object(sha1)))
barkalow@iabervon.org1e8be592005-08-02 23:46:10223 return -1;
224 if (loop())
Daniel Barkalowcd541a62005-06-06 20:38:26225 return -1;
226
227 if (write_ref) {
228 if (current_ref) {
229 write_ref_sha1(write_ref, fd, sha1);
230 } else {
231 write_ref_sha1_unlocked(write_ref, sha1);
232 }
233 }
234 return 0;
Daniel Barkalow4250a5e2005-04-30 23:53:56235}