Merge branch 'rs/nth-switch-code-simplification'
[git/git.git] / walker.c
CommitLineData
4250a5e5 1#include "cache.h"
30ae764b 2#include "walker.h"
109cd76d 3#include "repository.h"
cbd53a21 4#include "object-store.h"
4250a5e5
DB
5#include "commit.h"
6#include "tree.h"
1bc995a3 7#include "tree-walk.h"
3173bd49
DB
8#include "tag.h"
9#include "blob.h"
cd541a68
DB
10#include "refs.h"
11
94f5a121 12static struct object_id current_commit_oid;
4250a5e5 13
fa262cac 14void walker_say(struct walker *walker, const char *fmt, ...)
1e8be59d 15{
fa262cac
JK
16 if (walker->get_verbosely) {
17 va_list ap;
18 va_start(ap, fmt);
19 vfprintf(stderr, fmt, ap);
20 va_end(ap);
21 }
e78d9772
JH
22}
23
0d7a6e4e 24static void report_missing(const struct object *obj)
b2d62f16 25{
0d7a6e4e 26 fprintf(stderr, "Cannot obtain needed %s %s\n",
debca9d2 27 obj->type ? type_name(obj->type): "object",
f2fd0760 28 oid_to_hex(&obj->oid));
94f5a121 29 if (!is_null_oid(&current_commit_oid))
0d7a6e4e 30 fprintf(stderr, "while processing commit %s.\n",
94f5a121 31 oid_to_hex(&current_commit_oid));
b2d62f16
JH
32}
33
30ae764b 34static int process(struct walker *walker, struct object *obj);
3173bd49 35
30ae764b 36static int process_tree(struct walker *walker, struct tree *tree)
4250a5e5 37{
1bc995a3 38 struct tree_desc desc;
4c068a98 39 struct name_entry entry;
4250a5e5
DB
40
41 if (parse_tree(tree))
42 return -1;
43
6fda5e51 44 init_tree_desc(&desc, tree->buffer, tree->size);
4c068a98 45 while (tree_entry(&desc, &entry)) {
6f9012b6
JH
46 struct object *obj = NULL;
47
582c7393 48 /* submodule commits are not stored in the superproject */
68fb4650 49 if (S_ISGITLINK(entry.mode))
582c7393 50 continue;
4c068a98 51 if (S_ISDIR(entry.mode)) {
f86bcc7b 52 struct tree *tree = lookup_tree(the_repository,
ea82b2a0 53 &entry.oid);
6f9012b6
JH
54 if (tree)
55 obj = &tree->object;
56 }
57 else {
da14a7ff 58 struct blob *blob = lookup_blob(the_repository,
ea82b2a0 59 &entry.oid);
6f9012b6
JH
60 if (blob)
61 obj = &blob->object;
2d9c58c6 62 }
30ae764b 63 if (!obj || process(walker, obj))
4250a5e5 64 return -1;
4250a5e5 65 }
6e454b9a 66 free_tree_buffer(tree);
4250a5e5
DB
67 return 0;
68}
69
208acbfb 70/* Remember to update object flag allocation in object.h */
24451c31
SV
71#define COMPLETE (1U << 0)
72#define SEEN (1U << 1)
73#define TO_SCAN (1U << 2)
85d106c2 74
d0ac30f2 75static struct commit_list *complete = NULL;
22c6e1d0 76
30ae764b 77static int process_commit(struct walker *walker, struct commit *commit)
4250a5e5 78{
0b6b3429
79 struct commit_list *parents;
80
1e8be59d 81 if (parse_commit(commit))
4250a5e5
DB
82 return -1;
83
22c6e1d0 84 while (complete && complete->item->date >= commit->date) {
d0ac30f2 85 pop_most_recent_commit(&complete, COMPLETE);
22c6e1d0 86 }
22c6e1d0 87
d0ac30f2 88 if (commit->object.flags & COMPLETE)
22c6e1d0
DB
89 return 0;
90
94f5a121 91 oidcpy(&current_commit_oid, &commit->object.oid);
4250a5e5 92
f2fd0760 93 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
85d106c2 94
c89b6e13 95 if (process(walker, &get_commit_tree(commit)->object))
0b6b3429
96 return -1;
97
98 for (parents = commit->parents; parents; parents = parents->next) {
99 if (process(walker, &parents->item->object))
4250a5e5 100 return -1;
4250a5e5 101 }
0b6b3429 102
4250a5e5
DB
103 return 0;
104}
105
30ae764b 106static int process_tag(struct walker *walker, struct tag *tag)
3173bd49 107{
1e8be59d 108 if (parse_tag(tag))
3173bd49 109 return -1;
30ae764b 110 return process(walker, tag->tagged);
3173bd49
DB
111}
112
1e8be59d
DB
113static struct object_list *process_queue = NULL;
114static struct object_list **process_queue_end = &process_queue;
115
30ae764b 116static int process_object(struct walker *walker, struct object *obj)
3173bd49 117{
1974632c 118 if (obj->type == OBJ_COMMIT) {
30ae764b 119 if (process_commit(walker, (struct commit *)obj))
f88fcf8b
DB
120 return -1;
121 return 0;
122 }
1974632c 123 if (obj->type == OBJ_TREE) {
30ae764b 124 if (process_tree(walker, (struct tree *)obj))
f88fcf8b
DB
125 return -1;
126 return 0;
127 }
1974632c 128 if (obj->type == OBJ_BLOB) {
f88fcf8b
DB
129 return 0;
130 }
1974632c 131 if (obj->type == OBJ_TAG) {
30ae764b 132 if (process_tag(walker, (struct tag *)obj))
f88fcf8b 133 return -1;
3173bd49 134 return 0;
f88fcf8b
DB
135 }
136 return error("Unable to determine requirements "
137 "of type %s for %s",
debca9d2 138 type_name(obj->type), oid_to_hex(&obj->oid));
f88fcf8b
DB
139}
140
30ae764b 141static int process(struct walker *walker, struct object *obj)
f88fcf8b 142{
a82d07e5
SV
143 if (obj->flags & SEEN)
144 return 0;
145 obj->flags |= SEEN;
146
f2fd0760 147 if (has_object_file(&obj->oid)) {
f88fcf8b 148 /* We already have it, so we should scan it now. */
85d106c2 149 obj->flags |= TO_SCAN;
e5f38ec3
JH
150 }
151 else {
7b64d06b
SV
152 if (obj->flags & COMPLETE)
153 return 0;
ed1c9977 154 walker->prefetch(walker, obj->oid.hash);
f88fcf8b 155 }
a6080a0a 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
30ae764b 162static int loop(struct walker *walker)
1e8be59d 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)) {
ed1c9977 178 if (walker->fetch(walker, obj->oid.hash)) {
0d7a6e4e 179 report_missing(obj);
85d106c2
JH
180 return -1;
181 }
182 }
1e8be59d 183 if (!obj->type)
109cd76d 184 parse_object(the_repository, &obj->oid);
30ae764b 185 if (process_object(walker, obj))
f88fcf8b 186 return -1;
1e8be59d
DB
187 }
188 return 0;
3173bd49
DB
189}
190
94f5a121 191static int interpret_target(struct walker *walker, char *target, struct object_id *oid)
cd541a68 192{
94f5a121 193 if (!get_oid_hex(target, oid))
cd541a68 194 return 0;
8d9c5010 195 if (!check_refname_format(target, 0)) {
59c69c0c 196 struct ref *ref = alloc_ref(target);
c13b2633 197 if (!walker->fetch_ref(walker, ref)) {
94f5a121 198 oidcpy(oid, &ref->old_oid);
c13b2633 199 free(ref);
cd541a68
DB
200 return 0;
201 }
c13b2633 202 free(ref);
cd541a68
DB
203 }
204 return -1;
205}
206
b4ebaf9e
MH
207static int mark_complete(const char *path, const struct object_id *oid,
208 int flag, void *cb_data)
22c6e1d0 209{
21e1ee8f
SB
210 struct commit *commit = lookup_commit_reference_gently(the_repository,
211 oid, 1);
b4ebaf9e 212
d0ac30f2
JH
213 if (commit) {
214 commit->object.flags |= COMPLETE;
3bc7a05b 215 commit_list_insert(commit, &complete);
22c6e1d0
DB
216 }
217 return 0;
218}
cd541a68 219
30ae764b 220int walker_targets_stdin(char ***target, const char ***write_ref)
8e87ca66
PB
221{
222 int targets = 0, targets_alloc = 0;
f285a2d7 223 struct strbuf buf = STRBUF_INIT;
8e87ca66 224 *target = NULL; *write_ref = NULL;
8e87ca66
PB
225 while (1) {
226 char *rf_one = NULL;
227 char *tg_one;
228
8f309aeb 229 if (strbuf_getline_lf(&buf, stdin) == EOF)
8e87ca66
PB
230 break;
231 tg_one = buf.buf;
232 rf_one = strchr(tg_one, '\t');
233 if (rf_one)
234 *rf_one++ = 0;
235
236 if (targets >= targets_alloc) {
237 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
2756ca43
RS
238 REALLOC_ARRAY(*target, targets_alloc);
239 REALLOC_ARRAY(*write_ref, targets_alloc);
8e87ca66 240 }
9befac47 241 (*target)[targets] = xstrdup(tg_one);
8c53f071 242 (*write_ref)[targets] = xstrdup_or_null(rf_one);
8e87ca66
PB
243 targets++;
244 }
e6c019d0 245 strbuf_release(&buf);
8e87ca66
PB
246 return targets;
247}
248
30ae764b 249void walker_targets_free(int targets, char **target, const char **write_ref)
8e87ca66
PB
250{
251 while (targets--) {
252 free(target[targets]);
24deea52 253 if (write_ref)
8e87ca66
PB
254 free((char *) write_ref[targets]);
255 }
256}
257
30ae764b
DB
258int walker_fetch(struct walker *walker, int targets, char **target,
259 const char **write_ref, const char *write_ref_log_details)
4250a5e5 260{
b6b10bb4
RS
261 struct strbuf refname = STRBUF_INIT;
262 struct strbuf err = STRBUF_INIT;
263 struct ref_transaction *transaction = NULL;
94f5a121 264 struct object_id *oids = xmalloc(targets * sizeof(struct object_id));
b6b10bb4
RS
265 char *msg = NULL;
266 int i, ret = -1;
cd541a68 267
98533b90 268 save_commit_buffer = 0;
4211e4d1 269
b6b10bb4
RS
270 if (write_ref) {
271 transaction = ref_transaction_begin(&err);
272 if (!transaction) {
273 error("%s", err.buf);
274 goto done;
d0740d92 275 }
cd541a68
DB
276 }
277
3bc7a05b 278 if (!walker->get_recover) {
b4ebaf9e 279 for_each_ref(mark_complete, NULL);
3bc7a05b
RS
280 commit_list_sort_by_date(&complete);
281 }
22c6e1d0 282
4211e4d1 283 for (i = 0; i < targets; i++) {
94f5a121 284 if (interpret_target(walker, target[i], oids + i)) {
5f48741a 285 error("Could not interpret response from server '%s' as something to pull", target[i]);
b6b10bb4 286 goto done;
4211e4d1 287 }
0ebbcf70 288 if (process(walker, lookup_unknown_object(&oids[i])))
b6b10bb4 289 goto done;
4bd18c43 290 }
4211e4d1 291
30ae764b 292 if (loop(walker))
b6b10bb4
RS
293 goto done;
294 if (!write_ref) {
295 ret = 0;
296 goto done;
297 }
4211e4d1 298 if (write_ref_log_details) {
01d678a2 299 msg = xstrfmt("fetch from %s", write_ref_log_details);
4211e4d1
PB
300 } else {
301 msg = NULL;
4bd18c43 302 }
4211e4d1 303 for (i = 0; i < targets; i++) {
b6b10bb4 304 if (!write_ref[i])
4211e4d1 305 continue;
b6b10bb4
RS
306 strbuf_reset(&refname);
307 strbuf_addf(&refname, "refs/%s", write_ref[i]);
308 if (ref_transaction_update(transaction, refname.buf,
89f3bbdd 309 oids + i, NULL, 0,
db7516ab 310 msg ? msg : "fetch (unknown)",
b6b10bb4
RS
311 &err)) {
312 error("%s", err.buf);
313 goto done;
314 }
315 }
db7516ab 316 if (ref_transaction_commit(transaction, &err)) {
b6b10bb4
RS
317 error("%s", err.buf);
318 goto done;
4bd18c43 319 }
4211e4d1 320
b6b10bb4 321 ret = 0;
30ae764b 322
b6b10bb4
RS
323done:
324 ref_transaction_free(transaction);
325 free(msg);
94f5a121 326 free(oids);
b6b10bb4
RS
327 strbuf_release(&err);
328 strbuf_release(&refname);
329 return ret;
4250a5e5 330}
30ae764b
DB
331
332void walker_free(struct walker *walker)
333{
334 walker->cleanup(walker);
335 free(walker);
336}