]>
Commit | Line | Data |
---|---|---|
215a7ad1 | 1 | #include "fetch.h" |
4250a5e5 DB |
2 | |
3 | #include "cache.h" | |
4 | #include "commit.h" | |
5 | #include "tree.h" | |
1bc995a3 | 6 | #include "tree-walk.h" |
3173bd49 DB |
7 | #include "tag.h" |
8 | #include "blob.h" | |
cd541a68 DB |
9 | #include "refs.h" |
10 | ||
11 | const char *write_ref = NULL; | |
12 | ||
4250a5e5 DB |
13 | int get_tree = 0; |
14 | int get_history = 0; | |
15 | int get_all = 0; | |
e78d9772 | 16 | int get_verbosely = 0; |
820eca68 | 17 | int get_recover = 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 | ||
80077f07 | 36 | static int process(struct object *obj); |
3173bd49 | 37 | |
1e8be59d | 38 | static int process_tree(struct tree *tree) |
4250a5e5 | 39 | { |
1bc995a3 | 40 | struct tree_desc desc; |
4250a5e5 DB |
41 | |
42 | if (parse_tree(tree)) | |
43 | return -1; | |
44 | ||
1bc995a3 LT |
45 | desc.buf = tree->buffer; |
46 | desc.size = tree->size; | |
47 | while (desc.size) { | |
48 | unsigned mode; | |
49 | const char *name; | |
50 | const unsigned char *sha1; | |
2d9c58c6 | 51 | |
1bc995a3 LT |
52 | sha1 = tree_entry_extract(&desc, &name, &mode); |
53 | update_tree_entry(&desc); | |
54 | ||
55 | if (S_ISDIR(mode)) { | |
56 | struct tree *tree = lookup_tree(sha1); | |
2d9c58c6 LT |
57 | process_tree(tree); |
58 | } else { | |
1bc995a3 | 59 | struct blob *blob = lookup_blob(sha1); |
2d9c58c6 LT |
60 | process(&blob->object); |
61 | } | |
4250a5e5 | 62 | } |
2d9c58c6 LT |
63 | free(tree->buffer); |
64 | tree->buffer = NULL; | |
1bc995a3 | 65 | tree->size = 0; |
4250a5e5 DB |
66 | return 0; |
67 | } | |
68 | ||
24451c31 SV |
69 | #define COMPLETE (1U << 0) |
70 | #define SEEN (1U << 1) | |
71 | #define TO_SCAN (1U << 2) | |
85d106c2 | 72 | |
d0ac30f2 | 73 | static struct commit_list *complete = NULL; |
22c6e1d0 | 74 | |
1e8be59d | 75 | static int process_commit(struct commit *commit) |
4250a5e5 | 76 | { |
1e8be59d | 77 | if (parse_commit(commit)) |
4250a5e5 DB |
78 | return -1; |
79 | ||
22c6e1d0 | 80 | while (complete && complete->item->date >= commit->date) { |
d0ac30f2 | 81 | pop_most_recent_commit(&complete, COMPLETE); |
22c6e1d0 | 82 | } |
22c6e1d0 | 83 | |
d0ac30f2 | 84 | if (commit->object.flags & COMPLETE) |
22c6e1d0 DB |
85 | return 0; |
86 | ||
1e8be59d | 87 | memcpy(current_commit_sha1, commit->object.sha1, 20); |
4250a5e5 | 88 | |
85d106c2 JH |
89 | pull_say("walk %s\n", sha1_to_hex(commit->object.sha1)); |
90 | ||
4250a5e5 | 91 | if (get_tree) { |
80077f07 | 92 | if (process(&commit->tree->object)) |
4250a5e5 DB |
93 | return -1; |
94 | if (!get_all) | |
95 | get_tree = 0; | |
96 | } | |
97 | if (get_history) { | |
1e8be59d | 98 | struct commit_list *parents = commit->parents; |
4250a5e5 | 99 | for (; parents; parents = parents->next) { |
80077f07 | 100 | if (process(&parents->item->object)) |
4250a5e5 DB |
101 | return -1; |
102 | } | |
103 | } | |
104 | return 0; | |
105 | } | |
106 | ||
1e8be59d | 107 | static int process_tag(struct tag *tag) |
3173bd49 | 108 | { |
1e8be59d | 109 | if (parse_tag(tag)) |
3173bd49 | 110 | return -1; |
80077f07 | 111 | return process(tag->tagged); |
3173bd49 DB |
112 | } |
113 | ||
1e8be59d DB |
114 | static struct object_list *process_queue = NULL; |
115 | static struct object_list **process_queue_end = &process_queue; | |
116 | ||
f88fcf8b | 117 | static int process_object(struct object *obj) |
3173bd49 | 118 | { |
f88fcf8b DB |
119 | if (obj->type == commit_type) { |
120 | if (process_commit((struct commit *)obj)) | |
121 | return -1; | |
122 | return 0; | |
123 | } | |
124 | if (obj->type == tree_type) { | |
125 | if (process_tree((struct tree *)obj)) | |
126 | return -1; | |
127 | return 0; | |
128 | } | |
129 | if (obj->type == blob_type) { | |
130 | return 0; | |
131 | } | |
132 | if (obj->type == tag_type) { | |
133 | if (process_tag((struct tag *)obj)) | |
134 | return -1; | |
3173bd49 | 135 | return 0; |
f88fcf8b DB |
136 | } |
137 | return error("Unable to determine requirements " | |
138 | "of type %s for %s", | |
139 | obj->type, sha1_to_hex(obj->sha1)); | |
140 | } | |
141 | ||
80077f07 | 142 | static int process(struct object *obj) |
f88fcf8b | 143 | { |
a82d07e5 SV |
144 | if (obj->flags & SEEN) |
145 | return 0; | |
146 | obj->flags |= SEEN; | |
147 | ||
80077f07 | 148 | if (has_sha1_file(obj->sha1)) { |
f88fcf8b | 149 | /* We already have it, so we should scan it now. */ |
85d106c2 | 150 | obj->flags |= TO_SCAN; |
7b64d06b SV |
151 | } else { |
152 | if (obj->flags & COMPLETE) | |
153 | return 0; | |
154 | prefetch(obj->sha1); | |
f88fcf8b | 155 | } |
7b64d06b | 156 | |
1e8be59d DB |
157 | object_list_insert(obj, process_queue_end); |
158 | process_queue_end = &(*process_queue_end)->next; | |
1e8be59d DB |
159 | return 0; |
160 | } | |
161 | ||
162 | static int loop(void) | |
163 | { | |
85d106c2 JH |
164 | struct object_list *elem; |
165 | ||
1e8be59d DB |
166 | while (process_queue) { |
167 | struct object *obj = process_queue->item; | |
85d106c2 JH |
168 | elem = process_queue; |
169 | process_queue = elem->next; | |
170 | free(elem); | |
1e8be59d DB |
171 | if (!process_queue) |
172 | process_queue_end = &process_queue; | |
173 | ||
85d106c2 JH |
174 | /* If we are not scanning this object, we placed it in |
175 | * the queue because we needed to fetch it first. | |
176 | */ | |
177 | if (! (obj->flags & TO_SCAN)) { | |
11f0dafe | 178 | if (fetch(obj->sha1)) { |
85d106c2 JH |
179 | report_missing(obj->type |
180 | ? obj->type | |
181 | : "object", obj->sha1); | |
182 | return -1; | |
183 | } | |
184 | } | |
1e8be59d DB |
185 | if (!obj->type) |
186 | parse_object(obj->sha1); | |
f88fcf8b DB |
187 | if (process_object(obj)) |
188 | return -1; | |
1e8be59d DB |
189 | } |
190 | return 0; | |
3173bd49 DB |
191 | } |
192 | ||
cd541a68 DB |
193 | static int interpret_target(char *target, unsigned char *sha1) |
194 | { | |
195 | if (!get_sha1_hex(target, sha1)) | |
196 | return 0; | |
197 | if (!check_ref_format(target)) { | |
198 | if (!fetch_ref(target, sha1)) { | |
199 | return 0; | |
200 | } | |
201 | } | |
202 | return -1; | |
203 | } | |
204 | ||
22c6e1d0 DB |
205 | static int mark_complete(const char *path, const unsigned char *sha1) |
206 | { | |
d0ac30f2 JH |
207 | struct commit *commit = lookup_commit_reference_gently(sha1, 1); |
208 | if (commit) { | |
209 | commit->object.flags |= COMPLETE; | |
210 | insert_by_date(commit, &complete); | |
22c6e1d0 DB |
211 | } |
212 | return 0; | |
213 | } | |
cd541a68 | 214 | |
4250a5e5 DB |
215 | int pull(char *target) |
216 | { | |
4250a5e5 | 217 | unsigned char sha1[20]; |
cd541a68 | 218 | |
98533b90 | 219 | save_commit_buffer = 0; |
a95cb6fb | 220 | track_object_refs = 0; |
cd541a68 | 221 | |
84c667ff | 222 | if (!get_recover) |
820eca68 | 223 | for_each_ref(mark_complete); |
22c6e1d0 | 224 | |
cd541a68 DB |
225 | if (interpret_target(target, sha1)) |
226 | return error("Could not interpret %s as something to pull", | |
227 | target); | |
80077f07 | 228 | if (process(lookup_unknown_object(sha1))) |
1e8be59d DB |
229 | return -1; |
230 | if (loop()) | |
cd541a68 DB |
231 | return -1; |
232 | ||
84c667ff JH |
233 | if (write_ref) |
234 | write_ref_sha1_unlocked(write_ref, sha1); | |
cd541a68 | 235 | return 0; |
4250a5e5 | 236 | } |