3 #include "object-store.h"
11 static struct object_id current_commit_oid
;
13 void walker_say(struct walker
*walker
, const char *fmt
, ...)
15 if (walker
->get_verbosely
) {
18 vfprintf(stderr
, fmt
, ap
);
23 static void report_missing(const struct object
*obj
)
25 fprintf(stderr
, "Cannot obtain needed %s %s\n",
26 obj
->type
? type_name(obj
->type
): "object",
27 oid_to_hex(&obj
->oid
));
28 if (!is_null_oid(¤t_commit_oid
))
29 fprintf(stderr
, "while processing commit %s.\n",
30 oid_to_hex(¤t_commit_oid
));
33 static int process(struct walker
*walker
, struct object
*obj
);
35 static int process_tree(struct walker
*walker
, struct tree
*tree
)
37 struct tree_desc desc
;
38 struct name_entry entry
;
43 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
44 while (tree_entry(&desc
, &entry
)) {
45 struct object
*obj
= NULL
;
47 /* submodule commits are not stored in the superproject */
48 if (S_ISGITLINK(entry
.mode
))
50 if (S_ISDIR(entry
.mode
)) {
51 struct tree
*tree
= lookup_tree(entry
.oid
);
56 struct blob
*blob
= lookup_blob(entry
.oid
);
60 if (!obj
|| process(walker
, obj
))
63 free_tree_buffer(tree
);
67 /* Remember to update object flag allocation in object.h */
68 #define COMPLETE (1U << 0)
69 #define SEEN (1U << 1)
70 #define TO_SCAN (1U << 2)
72 static struct commit_list
*complete
= NULL
;
74 static int process_commit(struct walker
*walker
, struct commit
*commit
)
76 struct commit_list
*parents
;
78 if (parse_commit(commit
))
81 while (complete
&& complete
->item
->date
>= commit
->date
) {
82 pop_most_recent_commit(&complete
, COMPLETE
);
85 if (commit
->object
.flags
& COMPLETE
)
88 oidcpy(¤t_commit_oid
, &commit
->object
.oid
);
90 walker_say(walker
, "walk %s\n", oid_to_hex(&commit
->object
.oid
));
92 if (process(walker
, &get_commit_tree(commit
)->object
))
95 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
96 if (process(walker
, &parents
->item
->object
))
103 static int process_tag(struct walker
*walker
, struct tag
*tag
)
107 return process(walker
, tag
->tagged
);
110 static struct object_list
*process_queue
= NULL
;
111 static struct object_list
**process_queue_end
= &process_queue
;
113 static int process_object(struct walker
*walker
, struct object
*obj
)
115 if (obj
->type
== OBJ_COMMIT
) {
116 if (process_commit(walker
, (struct commit
*)obj
))
120 if (obj
->type
== OBJ_TREE
) {
121 if (process_tree(walker
, (struct tree
*)obj
))
125 if (obj
->type
== OBJ_BLOB
) {
128 if (obj
->type
== OBJ_TAG
) {
129 if (process_tag(walker
, (struct tag
*)obj
))
133 return error("Unable to determine requirements "
135 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
138 static int process(struct walker
*walker
, struct object
*obj
)
140 if (obj
->flags
& SEEN
)
144 if (has_object_file(&obj
->oid
)) {
145 /* We already have it, so we should scan it now. */
146 obj
->flags
|= TO_SCAN
;
149 if (obj
->flags
& COMPLETE
)
151 walker
->prefetch(walker
, obj
->oid
.hash
);
154 object_list_insert(obj
, process_queue_end
);
155 process_queue_end
= &(*process_queue_end
)->next
;
159 static int loop(struct walker
*walker
)
161 struct object_list
*elem
;
163 while (process_queue
) {
164 struct object
*obj
= process_queue
->item
;
165 elem
= process_queue
;
166 process_queue
= elem
->next
;
169 process_queue_end
= &process_queue
;
171 /* If we are not scanning this object, we placed it in
172 * the queue because we needed to fetch it first.
174 if (! (obj
->flags
& TO_SCAN
)) {
175 if (walker
->fetch(walker
, obj
->oid
.hash
)) {
181 parse_object(&obj
->oid
);
182 if (process_object(walker
, obj
))
188 static int interpret_target(struct walker
*walker
, char *target
, struct object_id
*oid
)
190 if (!get_oid_hex(target
, oid
))
192 if (!check_refname_format(target
, 0)) {
193 struct ref
*ref
= alloc_ref(target
);
194 if (!walker
->fetch_ref(walker
, ref
)) {
195 oidcpy(oid
, &ref
->old_oid
);
204 static int mark_complete(const char *path
, const struct object_id
*oid
,
205 int flag
, void *cb_data
)
207 struct commit
*commit
= lookup_commit_reference_gently(oid
, 1);
210 commit
->object
.flags
|= COMPLETE
;
211 commit_list_insert(commit
, &complete
);
216 int walker_targets_stdin(char ***target
, const char ***write_ref
)
218 int targets
= 0, targets_alloc
= 0;
219 struct strbuf buf
= STRBUF_INIT
;
220 *target
= NULL
; *write_ref
= NULL
;
225 if (strbuf_getline_lf(&buf
, stdin
) == EOF
)
228 rf_one
= strchr(tg_one
, '\t');
232 if (targets
>= targets_alloc
) {
233 targets_alloc
= targets_alloc
? targets_alloc
* 2 : 64;
234 REALLOC_ARRAY(*target
, targets_alloc
);
235 REALLOC_ARRAY(*write_ref
, targets_alloc
);
237 (*target
)[targets
] = xstrdup(tg_one
);
238 (*write_ref
)[targets
] = xstrdup_or_null(rf_one
);
241 strbuf_release(&buf
);
245 void walker_targets_free(int targets
, char **target
, const char **write_ref
)
248 free(target
[targets
]);
250 free((char *) write_ref
[targets
]);
254 int walker_fetch(struct walker
*walker
, int targets
, char **target
,
255 const char **write_ref
, const char *write_ref_log_details
)
257 struct strbuf refname
= STRBUF_INIT
;
258 struct strbuf err
= STRBUF_INIT
;
259 struct ref_transaction
*transaction
= NULL
;
260 struct object_id
*oids
= xmalloc(targets
* sizeof(struct object_id
));
264 save_commit_buffer
= 0;
267 transaction
= ref_transaction_begin(&err
);
269 error("%s", err
.buf
);
274 if (!walker
->get_recover
) {
275 for_each_ref(mark_complete
, NULL
);
276 commit_list_sort_by_date(&complete
);
279 for (i
= 0; i
< targets
; i
++) {
280 if (interpret_target(walker
, target
[i
], oids
+ i
)) {
281 error("Could not interpret response from server '%s' as something to pull", target
[i
]);
284 if (process(walker
, lookup_unknown_object(oids
[i
].hash
)))
294 if (write_ref_log_details
) {
295 msg
= xstrfmt("fetch from %s", write_ref_log_details
);
299 for (i
= 0; i
< targets
; i
++) {
302 strbuf_reset(&refname
);
303 strbuf_addf(&refname
, "refs/%s", write_ref
[i
]);
304 if (ref_transaction_update(transaction
, refname
.buf
,
306 msg
? msg
: "fetch (unknown)",
308 error("%s", err
.buf
);
312 if (ref_transaction_commit(transaction
, &err
)) {
313 error("%s", err
.buf
);
320 ref_transaction_free(transaction
);
323 strbuf_release(&err
);
324 strbuf_release(&refname
);
328 void walker_free(struct walker
*walker
)
330 walker
->cleanup(walker
);