]> git.ipfire.org Git - thirdparty/git.git/blob - walker.c
Sync with 2.16.6
[thirdparty/git.git] / walker.c
1 #include "cache.h"
2 #include "walker.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "tree-walk.h"
6 #include "tag.h"
7 #include "blob.h"
8 #include "refs.h"
9
10 static struct object_id current_commit_oid;
11
12 void walker_say(struct walker *walker, const char *fmt, ...)
13 {
14 if (walker->get_verbosely) {
15 va_list ap;
16 va_start(ap, fmt);
17 vfprintf(stderr, fmt, ap);
18 va_end(ap);
19 }
20 }
21
22 static void report_missing(const struct object *obj)
23 {
24 fprintf(stderr, "Cannot obtain needed %s %s\n",
25 obj->type ? type_name(obj->type): "object",
26 oid_to_hex(&obj->oid));
27 if (!is_null_oid(&current_commit_oid))
28 fprintf(stderr, "while processing commit %s.\n",
29 oid_to_hex(&current_commit_oid));
30 }
31
32 static int process(struct walker *walker, struct object *obj);
33
34 static int process_tree(struct walker *walker, struct tree *tree)
35 {
36 struct tree_desc desc;
37 struct name_entry entry;
38
39 if (parse_tree(tree))
40 return -1;
41
42 init_tree_desc(&desc, tree->buffer, tree->size);
43 while (tree_entry(&desc, &entry)) {
44 struct object *obj = NULL;
45
46 /* submodule commits are not stored in the superproject */
47 if (S_ISGITLINK(entry.mode))
48 continue;
49 if (S_ISDIR(entry.mode)) {
50 struct tree *tree = lookup_tree(entry.oid);
51 if (tree)
52 obj = &tree->object;
53 }
54 else {
55 struct blob *blob = lookup_blob(entry.oid);
56 if (blob)
57 obj = &blob->object;
58 }
59 if (!obj || process(walker, obj))
60 return -1;
61 }
62 free_tree_buffer(tree);
63 return 0;
64 }
65
66 /* Remember to update object flag allocation in object.h */
67 #define COMPLETE (1U << 0)
68 #define SEEN (1U << 1)
69 #define TO_SCAN (1U << 2)
70
71 static struct commit_list *complete = NULL;
72
73 static int process_commit(struct walker *walker, struct commit *commit)
74 {
75 if (parse_commit(commit))
76 return -1;
77
78 while (complete && complete->item->date >= commit->date) {
79 pop_most_recent_commit(&complete, COMPLETE);
80 }
81
82 if (commit->object.flags & COMPLETE)
83 return 0;
84
85 oidcpy(&current_commit_oid, &commit->object.oid);
86
87 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
88
89 if (walker->get_tree) {
90 if (process(walker, &commit->tree->object))
91 return -1;
92 if (!walker->get_all)
93 walker->get_tree = 0;
94 }
95 if (walker->get_history) {
96 struct commit_list *parents = commit->parents;
97 for (; parents; parents = parents->next) {
98 if (process(walker, &parents->item->object))
99 return -1;
100 }
101 }
102 return 0;
103 }
104
105 static int process_tag(struct walker *walker, struct tag *tag)
106 {
107 if (parse_tag(tag))
108 return -1;
109 return process(walker, tag->tagged);
110 }
111
112 static struct object_list *process_queue = NULL;
113 static struct object_list **process_queue_end = &process_queue;
114
115 static int process_object(struct walker *walker, struct object *obj)
116 {
117 if (obj->type == OBJ_COMMIT) {
118 if (process_commit(walker, (struct commit *)obj))
119 return -1;
120 return 0;
121 }
122 if (obj->type == OBJ_TREE) {
123 if (process_tree(walker, (struct tree *)obj))
124 return -1;
125 return 0;
126 }
127 if (obj->type == OBJ_BLOB) {
128 return 0;
129 }
130 if (obj->type == OBJ_TAG) {
131 if (process_tag(walker, (struct tag *)obj))
132 return -1;
133 return 0;
134 }
135 return error("Unable to determine requirements "
136 "of type %s for %s",
137 type_name(obj->type), oid_to_hex(&obj->oid));
138 }
139
140 static int process(struct walker *walker, struct object *obj)
141 {
142 if (obj->flags & SEEN)
143 return 0;
144 obj->flags |= SEEN;
145
146 if (has_object_file(&obj->oid)) {
147 /* We already have it, so we should scan it now. */
148 obj->flags |= TO_SCAN;
149 }
150 else {
151 if (obj->flags & COMPLETE)
152 return 0;
153 walker->prefetch(walker, obj->oid.hash);
154 }
155
156 object_list_insert(obj, process_queue_end);
157 process_queue_end = &(*process_queue_end)->next;
158 return 0;
159 }
160
161 static int loop(struct walker *walker)
162 {
163 struct object_list *elem;
164
165 while (process_queue) {
166 struct object *obj = process_queue->item;
167 elem = process_queue;
168 process_queue = elem->next;
169 free(elem);
170 if (!process_queue)
171 process_queue_end = &process_queue;
172
173 /* If we are not scanning this object, we placed it in
174 * the queue because we needed to fetch it first.
175 */
176 if (! (obj->flags & TO_SCAN)) {
177 if (walker->fetch(walker, obj->oid.hash)) {
178 report_missing(obj);
179 return -1;
180 }
181 }
182 if (!obj->type)
183 parse_object(&obj->oid);
184 if (process_object(walker, obj))
185 return -1;
186 }
187 return 0;
188 }
189
190 static int interpret_target(struct walker *walker, char *target, struct object_id *oid)
191 {
192 if (!get_oid_hex(target, oid))
193 return 0;
194 if (!check_refname_format(target, 0)) {
195 struct ref *ref = alloc_ref(target);
196 if (!walker->fetch_ref(walker, ref)) {
197 oidcpy(oid, &ref->old_oid);
198 free(ref);
199 return 0;
200 }
201 free(ref);
202 }
203 return -1;
204 }
205
206 static int mark_complete(const char *path, const struct object_id *oid,
207 int flag, void *cb_data)
208 {
209 struct commit *commit = lookup_commit_reference_gently(oid, 1);
210
211 if (commit) {
212 commit->object.flags |= COMPLETE;
213 commit_list_insert(commit, &complete);
214 }
215 return 0;
216 }
217
218 int walker_targets_stdin(char ***target, const char ***write_ref)
219 {
220 int targets = 0, targets_alloc = 0;
221 struct strbuf buf = STRBUF_INIT;
222 *target = NULL; *write_ref = NULL;
223 while (1) {
224 char *rf_one = NULL;
225 char *tg_one;
226
227 if (strbuf_getline_lf(&buf, stdin) == EOF)
228 break;
229 tg_one = buf.buf;
230 rf_one = strchr(tg_one, '\t');
231 if (rf_one)
232 *rf_one++ = 0;
233
234 if (targets >= targets_alloc) {
235 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
236 REALLOC_ARRAY(*target, targets_alloc);
237 REALLOC_ARRAY(*write_ref, targets_alloc);
238 }
239 (*target)[targets] = xstrdup(tg_one);
240 (*write_ref)[targets] = xstrdup_or_null(rf_one);
241 targets++;
242 }
243 strbuf_release(&buf);
244 return targets;
245 }
246
247 void walker_targets_free(int targets, char **target, const char **write_ref)
248 {
249 while (targets--) {
250 free(target[targets]);
251 if (write_ref)
252 free((char *) write_ref[targets]);
253 }
254 }
255
256 int walker_fetch(struct walker *walker, int targets, char **target,
257 const char **write_ref, const char *write_ref_log_details)
258 {
259 struct strbuf refname = STRBUF_INIT;
260 struct strbuf err = STRBUF_INIT;
261 struct ref_transaction *transaction = NULL;
262 struct object_id *oids = xmalloc(targets * sizeof(struct object_id));
263 char *msg = NULL;
264 int i, ret = -1;
265
266 save_commit_buffer = 0;
267
268 if (write_ref) {
269 transaction = ref_transaction_begin(&err);
270 if (!transaction) {
271 error("%s", err.buf);
272 goto done;
273 }
274 }
275
276 if (!walker->get_recover) {
277 for_each_ref(mark_complete, NULL);
278 commit_list_sort_by_date(&complete);
279 }
280
281 for (i = 0; i < targets; i++) {
282 if (interpret_target(walker, target[i], oids + i)) {
283 error("Could not interpret response from server '%s' as something to pull", target[i]);
284 goto done;
285 }
286 if (process(walker, lookup_unknown_object(oids[i].hash)))
287 goto done;
288 }
289
290 if (loop(walker))
291 goto done;
292 if (!write_ref) {
293 ret = 0;
294 goto done;
295 }
296 if (write_ref_log_details) {
297 msg = xstrfmt("fetch from %s", write_ref_log_details);
298 } else {
299 msg = NULL;
300 }
301 for (i = 0; i < targets; i++) {
302 if (!write_ref[i])
303 continue;
304 strbuf_reset(&refname);
305 strbuf_addf(&refname, "refs/%s", write_ref[i]);
306 if (ref_transaction_update(transaction, refname.buf,
307 oids + i, NULL, 0,
308 msg ? msg : "fetch (unknown)",
309 &err)) {
310 error("%s", err.buf);
311 goto done;
312 }
313 }
314 if (ref_transaction_commit(transaction, &err)) {
315 error("%s", err.buf);
316 goto done;
317 }
318
319 ret = 0;
320
321 done:
322 ref_transaction_free(transaction);
323 free(msg);
324 free(oids);
325 strbuf_release(&err);
326 strbuf_release(&refname);
327 return ret;
328 }
329
330 void walker_free(struct walker *walker)
331 {
332 walker->cleanup(walker);
333 free(walker);
334 }