4 #include "repository.h"
5 #include "object-store.h"
14 static struct object_id current_commit_oid
;
16 void walker_say(struct walker
*walker
, const char *fmt
, ...)
18 if (walker
->get_verbosely
) {
21 vfprintf(stderr
, fmt
, ap
);
26 static void report_missing(const struct object
*obj
)
28 fprintf(stderr
, "Cannot obtain needed %s %s\n",
29 obj
->type
? type_name(obj
->type
): "object",
30 oid_to_hex(&obj
->oid
));
31 if (!is_null_oid(¤t_commit_oid
))
32 fprintf(stderr
, "while processing commit %s.\n",
33 oid_to_hex(¤t_commit_oid
));
36 static int process(struct walker
*walker
, struct object
*obj
);
38 static int process_tree(struct walker
*walker
, struct tree
*tree
)
40 struct tree_desc desc
;
41 struct name_entry entry
;
46 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
47 while (tree_entry(&desc
, &entry
)) {
48 struct object
*obj
= NULL
;
50 /* submodule commits are not stored in the superproject */
51 if (S_ISGITLINK(entry
.mode
))
53 if (S_ISDIR(entry
.mode
)) {
54 struct tree
*tree
= lookup_tree(the_repository
,
60 struct blob
*blob
= lookup_blob(the_repository
,
65 if (!obj
|| process(walker
, obj
))
68 free_tree_buffer(tree
);
72 /* Remember to update object flag allocation in object.h */
73 #define COMPLETE (1U << 0)
74 #define SEEN (1U << 1)
75 #define TO_SCAN (1U << 2)
77 static struct commit_list
*complete
= NULL
;
79 static int process_commit(struct walker
*walker
, struct commit
*commit
)
81 struct commit_list
*parents
;
83 if (parse_commit(commit
))
86 while (complete
&& complete
->item
->date
>= commit
->date
) {
87 pop_most_recent_commit(&complete
, COMPLETE
);
90 if (commit
->object
.flags
& COMPLETE
)
93 oidcpy(¤t_commit_oid
, &commit
->object
.oid
);
95 walker_say(walker
, "walk %s\n", oid_to_hex(&commit
->object
.oid
));
97 if (process(walker
, &get_commit_tree(commit
)->object
))
100 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
101 if (process(walker
, &parents
->item
->object
))
108 static int process_tag(struct walker
*walker
, struct tag
*tag
)
112 return process(walker
, tag
->tagged
);
115 static struct object_list
*process_queue
= NULL
;
116 static struct object_list
**process_queue_end
= &process_queue
;
118 static int process_object(struct walker
*walker
, struct object
*obj
)
120 if (obj
->type
== OBJ_COMMIT
) {
121 if (process_commit(walker
, (struct commit
*)obj
))
125 if (obj
->type
== OBJ_TREE
) {
126 if (process_tree(walker
, (struct tree
*)obj
))
130 if (obj
->type
== OBJ_BLOB
) {
133 if (obj
->type
== OBJ_TAG
) {
134 if (process_tag(walker
, (struct tag
*)obj
))
138 return error("Unable to determine requirements "
140 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
143 static int process(struct walker
*walker
, struct object
*obj
)
145 if (obj
->flags
& SEEN
)
149 if (has_object_file(&obj
->oid
)) {
150 /* We already have it, so we should scan it now. */
151 obj
->flags
|= TO_SCAN
;
154 if (obj
->flags
& COMPLETE
)
156 walker
->prefetch(walker
, obj
->oid
.hash
);
159 object_list_insert(obj
, process_queue_end
);
160 process_queue_end
= &(*process_queue_end
)->next
;
164 static int loop(struct walker
*walker
)
166 struct object_list
*elem
;
167 struct progress
*progress
= NULL
;
170 if (walker
->get_progress
)
171 progress
= start_delayed_progress(_("Fetching objects"), 0);
173 while (process_queue
) {
174 struct object
*obj
= process_queue
->item
;
175 elem
= process_queue
;
176 process_queue
= elem
->next
;
179 process_queue_end
= &process_queue
;
181 /* If we are not scanning this object, we placed it in
182 * the queue because we needed to fetch it first.
184 if (! (obj
->flags
& TO_SCAN
)) {
185 if (walker
->fetch(walker
, obj
->oid
.hash
)) {
186 stop_progress(&progress
);
192 parse_object(the_repository
, &obj
->oid
);
193 if (process_object(walker
, obj
)) {
194 stop_progress(&progress
);
197 display_progress(progress
, ++nr
);
199 stop_progress(&progress
);
203 static int interpret_target(struct walker
*walker
, char *target
, struct object_id
*oid
)
205 if (!get_oid_hex(target
, oid
))
207 if (!check_refname_format(target
, 0)) {
208 struct ref
*ref
= alloc_ref(target
);
209 if (!walker
->fetch_ref(walker
, ref
)) {
210 oidcpy(oid
, &ref
->old_oid
);
219 static int mark_complete(const char *path UNUSED
,
220 const struct object_id
*oid
,
222 void *cb_data UNUSED
)
224 struct commit
*commit
= lookup_commit_reference_gently(the_repository
,
228 commit
->object
.flags
|= COMPLETE
;
229 commit_list_insert(commit
, &complete
);
234 int walker_targets_stdin(char ***target
, const char ***write_ref
)
236 int targets
= 0, targets_alloc
= 0;
237 struct strbuf buf
= STRBUF_INIT
;
238 *target
= NULL
; *write_ref
= NULL
;
243 if (strbuf_getline_lf(&buf
, stdin
) == EOF
)
246 rf_one
= strchr(tg_one
, '\t');
250 if (targets
>= targets_alloc
) {
251 targets_alloc
= targets_alloc
? targets_alloc
* 2 : 64;
252 REALLOC_ARRAY(*target
, targets_alloc
);
253 REALLOC_ARRAY(*write_ref
, targets_alloc
);
255 (*target
)[targets
] = xstrdup(tg_one
);
256 (*write_ref
)[targets
] = xstrdup_or_null(rf_one
);
259 strbuf_release(&buf
);
263 void walker_targets_free(int targets
, char **target
, const char **write_ref
)
266 free(target
[targets
]);
268 free((char *) write_ref
[targets
]);
272 int walker_fetch(struct walker
*walker
, int targets
, char **target
,
273 const char **write_ref
, const char *write_ref_log_details
)
275 struct strbuf refname
= STRBUF_INIT
;
276 struct strbuf err
= STRBUF_INIT
;
277 struct ref_transaction
*transaction
= NULL
;
278 struct object_id
*oids
;
282 save_commit_buffer
= 0;
284 ALLOC_ARRAY(oids
, targets
);
287 transaction
= ref_transaction_begin(&err
);
289 error("%s", err
.buf
);
294 if (!walker
->get_recover
) {
295 for_each_ref(mark_complete
, NULL
);
296 commit_list_sort_by_date(&complete
);
299 for (i
= 0; i
< targets
; i
++) {
300 if (interpret_target(walker
, target
[i
], oids
+ i
)) {
301 error("Could not interpret response from server '%s' as something to pull", target
[i
]);
304 if (process(walker
, lookup_unknown_object(the_repository
, &oids
[i
])))
314 if (write_ref_log_details
) {
315 msg
= xstrfmt("fetch from %s", write_ref_log_details
);
319 for (i
= 0; i
< targets
; i
++) {
322 strbuf_reset(&refname
);
323 strbuf_addf(&refname
, "refs/%s", write_ref
[i
]);
324 if (ref_transaction_update(transaction
, refname
.buf
,
326 msg
? msg
: "fetch (unknown)",
328 error("%s", err
.buf
);
332 if (ref_transaction_commit(transaction
, &err
)) {
333 error("%s", err
.buf
);
340 ref_transaction_free(transaction
);
343 strbuf_release(&err
);
344 strbuf_release(&refname
);
348 void walker_free(struct walker
*walker
)
350 walker
->cleanup(walker
);