object: add repository argument to object_as_type
[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)) {
740ee055 52 struct tree *tree = lookup_tree(entry.oid);
6f9012b6
JH
53 if (tree)
54 obj = &tree->object;
55 }
56 else {
3aca1fc6 57 struct blob *blob = lookup_blob(entry.oid);
6f9012b6
JH
58 if (blob)
59 obj = &blob->object;
2d9c58c6 60 }
30ae764b 61 if (!obj || process(walker, obj))
4250a5e5 62 return -1;
4250a5e5 63 }
6e454b9a 64 free_tree_buffer(tree);
4250a5e5
DB
65 return 0;
66}
67
208acbfb 68/* Remember to update object flag allocation in object.h */
24451c31
SV
69#define COMPLETE (1U << 0)
70#define SEEN (1U << 1)
71#define TO_SCAN (1U << 2)
85d106c2 72
d0ac30f2 73static struct commit_list *complete = NULL;
22c6e1d0 74
30ae764b 75static int process_commit(struct walker *walker, struct commit *commit)
4250a5e5 76{
0b6b3429
77 struct commit_list *parents;
78
1e8be59d 79 if (parse_commit(commit))
4250a5e5
DB
80 return -1;
81
22c6e1d0 82 while (complete && complete->item->date >= commit->date) {
d0ac30f2 83 pop_most_recent_commit(&complete, COMPLETE);
22c6e1d0 84 }
22c6e1d0 85
d0ac30f2 86 if (commit->object.flags & COMPLETE)
22c6e1d0
DB
87 return 0;
88
94f5a121 89 oidcpy(&current_commit_oid, &commit->object.oid);
4250a5e5 90
f2fd0760 91 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid));
85d106c2 92
c89b6e13 93 if (process(walker, &get_commit_tree(commit)->object))
0b6b3429
94 return -1;
95
96 for (parents = commit->parents; parents; parents = parents->next) {
97 if (process(walker, &parents->item->object))
4250a5e5 98 return -1;
4250a5e5 99 }
0b6b3429 100
4250a5e5
DB
101 return 0;
102}
103
30ae764b 104static int process_tag(struct walker *walker, struct tag *tag)
3173bd49 105{
1e8be59d 106 if (parse_tag(tag))
3173bd49 107 return -1;
30ae764b 108 return process(walker, tag->tagged);
3173bd49
DB
109}
110
1e8be59d
DB
111static struct object_list *process_queue = NULL;
112static struct object_list **process_queue_end = &process_queue;
113
30ae764b 114static int process_object(struct walker *walker, struct object *obj)
3173bd49 115{
1974632c 116 if (obj->type == OBJ_COMMIT) {
30ae764b 117 if (process_commit(walker, (struct commit *)obj))
f88fcf8b
DB
118 return -1;
119 return 0;
120 }
1974632c 121 if (obj->type == OBJ_TREE) {
30ae764b 122 if (process_tree(walker, (struct tree *)obj))
f88fcf8b
DB
123 return -1;
124 return 0;
125 }
1974632c 126 if (obj->type == OBJ_BLOB) {
f88fcf8b
DB
127 return 0;
128 }
1974632c 129 if (obj->type == OBJ_TAG) {
30ae764b 130 if (process_tag(walker, (struct tag *)obj))
f88fcf8b 131 return -1;
3173bd49 132 return 0;
f88fcf8b
DB
133 }
134 return error("Unable to determine requirements "
135 "of type %s for %s",
debca9d2 136 type_name(obj->type), oid_to_hex(&obj->oid));
f88fcf8b
DB
137}
138
30ae764b 139static int process(struct walker *walker, struct object *obj)
f88fcf8b 140{
a82d07e5
SV
141 if (obj->flags & SEEN)
142 return 0;
143 obj->flags |= SEEN;
144
f2fd0760 145 if (has_object_file(&obj->oid)) {
f88fcf8b 146 /* We already have it, so we should scan it now. */
85d106c2 147 obj->flags |= TO_SCAN;
e5f38ec3
JH
148 }
149 else {
7b64d06b
SV
150 if (obj->flags & COMPLETE)
151 return 0;
ed1c9977 152 walker->prefetch(walker, obj->oid.hash);
f88fcf8b 153 }
a6080a0a 154
1e8be59d
DB
155 object_list_insert(obj, process_queue_end);
156 process_queue_end = &(*process_queue_end)->next;
1e8be59d
DB
157 return 0;
158}
159
30ae764b 160static int loop(struct walker *walker)
1e8be59d 161{
85d106c2
JH
162 struct object_list *elem;
163
1e8be59d
DB
164 while (process_queue) {
165 struct object *obj = process_queue->item;
85d106c2
JH
166 elem = process_queue;
167 process_queue = elem->next;
168 free(elem);
1e8be59d
DB
169 if (!process_queue)
170 process_queue_end = &process_queue;
171
85d106c2
JH
172 /* If we are not scanning this object, we placed it in
173 * the queue because we needed to fetch it first.
174 */
175 if (! (obj->flags & TO_SCAN)) {
ed1c9977 176 if (walker->fetch(walker, obj->oid.hash)) {
0d7a6e4e 177 report_missing(obj);
85d106c2
JH
178 return -1;
179 }
180 }
1e8be59d 181 if (!obj->type)
109cd76d 182 parse_object(the_repository, &obj->oid);
30ae764b 183 if (process_object(walker, obj))
f88fcf8b 184 return -1;
1e8be59d
DB
185 }
186 return 0;
3173bd49
DB
187}
188
94f5a121 189static int interpret_target(struct walker *walker, char *target, struct object_id *oid)
cd541a68 190{
94f5a121 191 if (!get_oid_hex(target, oid))
cd541a68 192 return 0;
8d9c5010 193 if (!check_refname_format(target, 0)) {
59c69c0c 194 struct ref *ref = alloc_ref(target);
c13b2633 195 if (!walker->fetch_ref(walker, ref)) {
94f5a121 196 oidcpy(oid, &ref->old_oid);
c13b2633 197 free(ref);
cd541a68
DB
198 return 0;
199 }
c13b2633 200 free(ref);
cd541a68
DB
201 }
202 return -1;
203}
204
b4ebaf9e
MH
205static int mark_complete(const char *path, const struct object_id *oid,
206 int flag, void *cb_data)
22c6e1d0 207{
bc83266a 208 struct commit *commit = lookup_commit_reference_gently(oid, 1);
b4ebaf9e 209
d0ac30f2
JH
210 if (commit) {
211 commit->object.flags |= COMPLETE;
3bc7a05b 212 commit_list_insert(commit, &complete);
22c6e1d0
DB
213 }
214 return 0;
215}
cd541a68 216
30ae764b 217int walker_targets_stdin(char ***target, const char ***write_ref)
8e87ca66
PB
218{
219 int targets = 0, targets_alloc = 0;
f285a2d7 220 struct strbuf buf = STRBUF_INIT;
8e87ca66 221 *target = NULL; *write_ref = NULL;
8e87ca66
PB
222 while (1) {
223 char *rf_one = NULL;
224 char *tg_one;
225
8f309aeb 226 if (strbuf_getline_lf(&buf, stdin) == EOF)
8e87ca66
PB
227 break;
228 tg_one = buf.buf;
229 rf_one = strchr(tg_one, '\t');
230 if (rf_one)
231 *rf_one++ = 0;
232
233 if (targets >= targets_alloc) {
234 targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
2756ca43
RS
235 REALLOC_ARRAY(*target, targets_alloc);
236 REALLOC_ARRAY(*write_ref, targets_alloc);
8e87ca66 237 }
9befac47 238 (*target)[targets] = xstrdup(tg_one);
8c53f071 239 (*write_ref)[targets] = xstrdup_or_null(rf_one);
8e87ca66
PB
240 targets++;
241 }
e6c019d0 242 strbuf_release(&buf);
8e87ca66
PB
243 return targets;
244}
245
30ae764b 246void walker_targets_free(int targets, char **target, const char **write_ref)
8e87ca66
PB
247{
248 while (targets--) {
249 free(target[targets]);
24deea52 250 if (write_ref)
8e87ca66
PB
251 free((char *) write_ref[targets]);
252 }
253}
254
30ae764b
DB
255int walker_fetch(struct walker *walker, int targets, char **target,
256 const char **write_ref, const char *write_ref_log_details)
4250a5e5 257{
b6b10bb4
RS
258 struct strbuf refname = STRBUF_INIT;
259 struct strbuf err = STRBUF_INIT;
260 struct ref_transaction *transaction = NULL;
94f5a121 261 struct object_id *oids = xmalloc(targets * sizeof(struct object_id));
b6b10bb4
RS
262 char *msg = NULL;
263 int i, ret = -1;
cd541a68 264
98533b90 265 save_commit_buffer = 0;
4211e4d1 266
b6b10bb4
RS
267 if (write_ref) {
268 transaction = ref_transaction_begin(&err);
269 if (!transaction) {
270 error("%s", err.buf);
271 goto done;
d0740d92 272 }
cd541a68
DB
273 }
274
3bc7a05b 275 if (!walker->get_recover) {
b4ebaf9e 276 for_each_ref(mark_complete, NULL);
3bc7a05b
RS
277 commit_list_sort_by_date(&complete);
278 }
22c6e1d0 279
4211e4d1 280 for (i = 0; i < targets; i++) {
94f5a121 281 if (interpret_target(walker, target[i], oids + i)) {
5f48741a 282 error("Could not interpret response from server '%s' as something to pull", target[i]);
b6b10bb4 283 goto done;
4211e4d1 284 }
94f5a121 285 if (process(walker, lookup_unknown_object(oids[i].hash)))
b6b10bb4 286 goto done;
4bd18c43 287 }
4211e4d1 288
30ae764b 289 if (loop(walker))
b6b10bb4
RS
290 goto done;
291 if (!write_ref) {
292 ret = 0;
293 goto done;
294 }
4211e4d1 295 if (write_ref_log_details) {
01d678a2 296 msg = xstrfmt("fetch from %s", write_ref_log_details);
4211e4d1
PB
297 } else {
298 msg = NULL;
4bd18c43 299 }
4211e4d1 300 for (i = 0; i < targets; i++) {
b6b10bb4 301 if (!write_ref[i])
4211e4d1 302 continue;
b6b10bb4
RS
303 strbuf_reset(&refname);
304 strbuf_addf(&refname, "refs/%s", write_ref[i]);
305 if (ref_transaction_update(transaction, refname.buf,
89f3bbdd 306 oids + i, NULL, 0,
db7516ab 307 msg ? msg : "fetch (unknown)",
b6b10bb4
RS
308 &err)) {
309 error("%s", err.buf);
310 goto done;
311 }
312 }
db7516ab 313 if (ref_transaction_commit(transaction, &err)) {
b6b10bb4
RS
314 error("%s", err.buf);
315 goto done;
4bd18c43 316 }
4211e4d1 317
b6b10bb4 318 ret = 0;
30ae764b 319
b6b10bb4
RS
320done:
321 ref_transaction_free(transaction);
322 free(msg);
94f5a121 323 free(oids);
b6b10bb4
RS
324 strbuf_release(&err);
325 strbuf_release(&refname);
326 return ret;
4250a5e5 327}
30ae764b
DB
328
329void walker_free(struct walker *walker)
330{
331 walker->cleanup(walker);
332 free(walker);
333}