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