]>
Commit | Line | Data |
---|---|---|
215a7ad1 | 1 | #include "fetch.h" |
4250a5e5 DB |
2 | |
3 | #include "cache.h" | |
4 | #include "commit.h" | |
5 | #include "tree.h" | |
3173bd49 DB |
6 | #include "tag.h" |
7 | #include "blob.h" | |
cd541a68 DB |
8 | #include "refs.h" |
9 | ||
10 | const char *write_ref = NULL; | |
11 | ||
12 | const unsigned char *current_ref = NULL; | |
4250a5e5 DB |
13 | |
14 | int get_tree = 0; | |
15 | int get_history = 0; | |
16 | int get_all = 0; | |
e78d9772 | 17 | int get_verbosely = 0; |
b2d62f16 | 18 | static unsigned char current_commit_sha1[20]; |
4250a5e5 | 19 | |
1e8be59d DB |
20 | void pull_say(const char *fmt, const char *hex) |
21 | { | |
e78d9772 JH |
22 | if (get_verbosely) |
23 | fprintf(stderr, fmt, hex); | |
24 | } | |
25 | ||
b2d62f16 JH |
26 | static void report_missing(const char *what, const unsigned char *missing) |
27 | { | |
28 | 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 | ||
36 | static int make_sure_we_have_it(const char *what, unsigned char *sha1) | |
ee4f439f | 37 | { |
a48e1d67 JH |
38 | int status = 0; |
39 | ||
40 | if (!has_sha1_file(sha1)) { | |
41 | status = fetch(sha1); | |
42 | if (status && what) | |
43 | report_missing(what, sha1); | |
44 | } | |
b2d62f16 | 45 | return status; |
ee4f439f JH |
46 | } |
47 | ||
1e8be59d | 48 | static int process(unsigned char *sha1, const char *type); |
3173bd49 | 49 | |
1e8be59d | 50 | static int process_tree(struct tree *tree) |
4250a5e5 | 51 | { |
4250a5e5 DB |
52 | struct tree_entry_list *entries; |
53 | ||
54 | if (parse_tree(tree)) | |
55 | return -1; | |
56 | ||
57 | for (entries = tree->entries; entries; entries = entries->next) { | |
1e8be59d DB |
58 | if (process(entries->item.any->sha1, |
59 | entries->directory ? tree_type : blob_type)) | |
4250a5e5 | 60 | return -1; |
4250a5e5 DB |
61 | } |
62 | return 0; | |
63 | } | |
64 | ||
22c6e1d0 DB |
65 | struct commit_list *complete = NULL; |
66 | ||
1e8be59d | 67 | static int process_commit(struct commit *commit) |
4250a5e5 | 68 | { |
1e8be59d | 69 | if (parse_commit(commit)) |
4250a5e5 DB |
70 | return -1; |
71 | ||
22c6e1d0 DB |
72 | while (complete && complete->item->date >= commit->date) { |
73 | pop_most_recent_commit(&complete, 1); | |
74 | } | |
75 | ||
76 | ||
77 | if (commit->object.flags & 1) | |
78 | return 0; | |
79 | ||
1e8be59d | 80 | memcpy(current_commit_sha1, commit->object.sha1, 20); |
4250a5e5 DB |
81 | |
82 | if (get_tree) { | |
1e8be59d | 83 | if (process(commit->tree->object.sha1, tree_type)) |
4250a5e5 DB |
84 | return -1; |
85 | if (!get_all) | |
86 | get_tree = 0; | |
87 | } | |
88 | if (get_history) { | |
1e8be59d | 89 | struct commit_list *parents = commit->parents; |
4250a5e5 | 90 | for (; parents; parents = parents->next) { |
1e8be59d DB |
91 | if (process(parents->item->object.sha1, |
92 | commit_type)) | |
4250a5e5 DB |
93 | return -1; |
94 | } | |
95 | } | |
96 | return 0; | |
97 | } | |
98 | ||
1e8be59d | 99 | static int process_tag(struct tag *tag) |
3173bd49 | 100 | { |
1e8be59d | 101 | if (parse_tag(tag)) |
3173bd49 | 102 | return -1; |
1e8be59d | 103 | return process(tag->tagged->sha1, NULL); |
3173bd49 DB |
104 | } |
105 | ||
1e8be59d DB |
106 | static struct object_list *process_queue = NULL; |
107 | static struct object_list **process_queue_end = &process_queue; | |
108 | ||
f88fcf8b | 109 | static int process_object(struct object *obj) |
3173bd49 | 110 | { |
f88fcf8b DB |
111 | if (obj->type == commit_type) { |
112 | if (process_commit((struct commit *)obj)) | |
113 | return -1; | |
114 | return 0; | |
115 | } | |
116 | if (obj->type == tree_type) { | |
117 | if (process_tree((struct tree *)obj)) | |
118 | return -1; | |
119 | return 0; | |
120 | } | |
121 | if (obj->type == blob_type) { | |
122 | return 0; | |
123 | } | |
124 | if (obj->type == tag_type) { | |
125 | if (process_tag((struct tag *)obj)) | |
126 | return -1; | |
3173bd49 | 127 | return 0; |
f88fcf8b DB |
128 | } |
129 | return error("Unable to determine requirements " | |
130 | "of type %s for %s", | |
131 | obj->type, sha1_to_hex(obj->sha1)); | |
132 | } | |
133 | ||
134 | static int process(unsigned char *sha1, const char *type) | |
135 | { | |
136 | struct object *obj = lookup_object_type(sha1, type); | |
22c6e1d0 | 137 | |
f88fcf8b | 138 | if (has_sha1_file(sha1)) { |
0d62fb56 | 139 | parse_object(sha1); |
f88fcf8b DB |
140 | /* We already have it, so we should scan it now. */ |
141 | return process_object(obj); | |
142 | } | |
1e8be59d DB |
143 | if (object_list_contains(process_queue, obj)) |
144 | return 0; | |
145 | object_list_insert(obj, process_queue_end); | |
146 | process_queue_end = &(*process_queue_end)->next; | |
147 | ||
148 | //fprintf(stderr, "prefetch %s\n", sha1_to_hex(sha1)); | |
149 | prefetch(sha1); | |
150 | ||
151 | return 0; | |
152 | } | |
153 | ||
154 | static int loop(void) | |
155 | { | |
156 | while (process_queue) { | |
157 | struct object *obj = process_queue->item; | |
158 | /* | |
159 | fprintf(stderr, "%d objects to pull\n", | |
160 | object_list_length(process_queue)); | |
161 | */ | |
162 | process_queue = process_queue->next; | |
163 | if (!process_queue) | |
164 | process_queue_end = &process_queue; | |
165 | ||
166 | //fprintf(stderr, "fetch %s\n", sha1_to_hex(obj->sha1)); | |
167 | ||
c7c81b3a | 168 | if (make_sure_we_have_it(obj->type ? obj->type : "object", |
1e8be59d DB |
169 | obj->sha1)) |
170 | return -1; | |
171 | if (!obj->type) | |
172 | parse_object(obj->sha1); | |
f88fcf8b DB |
173 | if (process_object(obj)) |
174 | return -1; | |
1e8be59d DB |
175 | } |
176 | return 0; | |
3173bd49 DB |
177 | } |
178 | ||
cd541a68 DB |
179 | static int interpret_target(char *target, unsigned char *sha1) |
180 | { | |
181 | if (!get_sha1_hex(target, sha1)) | |
182 | return 0; | |
183 | if (!check_ref_format(target)) { | |
184 | if (!fetch_ref(target, sha1)) { | |
185 | return 0; | |
186 | } | |
187 | } | |
188 | return -1; | |
189 | } | |
190 | ||
22c6e1d0 DB |
191 | static int mark_complete(const char *path, const unsigned char *sha1) |
192 | { | |
193 | struct object *obj = parse_object(sha1); | |
194 | while (obj->type == tag_type) { | |
195 | obj = ((struct tag *) obj)->tagged; | |
196 | parse_object(obj->sha1); | |
197 | } | |
198 | if (obj->type == commit_type) { | |
199 | obj->flags |= 1; | |
200 | insert_by_date((struct commit *) obj, &complete); | |
201 | } | |
202 | return 0; | |
203 | } | |
cd541a68 | 204 | |
4250a5e5 DB |
205 | int pull(char *target) |
206 | { | |
4250a5e5 | 207 | unsigned char sha1[20]; |
cd541a68 DB |
208 | int fd = -1; |
209 | ||
98533b90 | 210 | save_commit_buffer = 0; |
cd541a68 DB |
211 | if (write_ref && current_ref) { |
212 | fd = lock_ref_sha1(write_ref, current_ref); | |
213 | if (fd < 0) | |
214 | return -1; | |
215 | } | |
216 | ||
22c6e1d0 DB |
217 | for_each_ref(mark_complete); |
218 | ||
cd541a68 DB |
219 | if (interpret_target(target, sha1)) |
220 | return error("Could not interpret %s as something to pull", | |
221 | target); | |
1e8be59d DB |
222 | if (process(sha1, NULL)) |
223 | return -1; | |
224 | if (loop()) | |
cd541a68 DB |
225 | 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; | |
4250a5e5 | 235 | } |