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