3 #include "repository.h"
4 #include "object-store.h"
13 static struct object_id current_commit_oid
;
15 void walker_say(struct walker
*walker
, const char *fmt
, ...)
17 if (walker
->get_verbosely
) {
20 vfprintf(stderr
, fmt
, ap
);
25 static void report_missing(const struct object
*obj
)
27 fprintf(stderr
, "Cannot obtain needed %s %s\n",
28 obj
->type
? type_name(obj
->type
): "object",
29 oid_to_hex(&obj
->oid
));
30 if (!is_null_oid(¤t_commit_oid
))
31 fprintf(stderr
, "while processing commit %s.\n",
32 oid_to_hex(¤t_commit_oid
));
35 static int process(struct walker
*walker
, struct object
*obj
);
37 static int process_tree(struct walker
*walker
, struct tree
*tree
)
39 struct tree_desc desc
;
40 struct name_entry entry
;
45 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
46 while (tree_entry(&desc
, &entry
)) {
47 struct object
*obj
= NULL
;
49 /* submodule commits are not stored in the superproject */
50 if (S_ISGITLINK(entry
.mode
))
52 if (S_ISDIR(entry
.mode
)) {
53 struct tree
*tree
= lookup_tree(the_repository
,
59 struct blob
*blob
= lookup_blob(the_repository
,
64 if (!obj
|| process(walker
, obj
))
67 free_tree_buffer(tree
);
71 /* Remember to update object flag allocation in object.h */
72 #define COMPLETE (1U << 0)
73 #define SEEN (1U << 1)
74 #define TO_SCAN (1U << 2)
76 static struct commit_list
*complete
= NULL
;
78 static int process_commit(struct walker
*walker
, struct commit
*commit
)
80 struct commit_list
*parents
;
82 if (parse_commit(commit
))
85 while (complete
&& complete
->item
->date
>= commit
->date
) {
86 pop_most_recent_commit(&complete
, COMPLETE
);
89 if (commit
->object
.flags
& COMPLETE
)
92 oidcpy(¤t_commit_oid
, &commit
->object
.oid
);
94 walker_say(walker
, "walk %s\n", oid_to_hex(&commit
->object
.oid
));
96 if (process(walker
, &get_commit_tree(commit
)->object
))
99 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
100 if (process(walker
, &parents
->item
->object
))
107 static int process_tag(struct walker
*walker
, struct tag
*tag
)
111 return process(walker
, tag
->tagged
);
114 static struct object_list
*process_queue
= NULL
;
115 static struct object_list
**process_queue_end
= &process_queue
;
117 static int process_object(struct walker
*walker
, struct object
*obj
)
119 if (obj
->type
== OBJ_COMMIT
) {
120 if (process_commit(walker
, (struct commit
*)obj
))
124 if (obj
->type
== OBJ_TREE
) {
125 if (process_tree(walker
, (struct tree
*)obj
))
129 if (obj
->type
== OBJ_BLOB
) {
132 if (obj
->type
== OBJ_TAG
) {
133 if (process_tag(walker
, (struct tag
*)obj
))
137 return error("Unable to determine requirements "
139 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
142 static int process(struct walker
*walker
, struct object
*obj
)
144 if (obj
->flags
& SEEN
)
148 if (has_object_file(&obj
->oid
)) {
149 /* We already have it, so we should scan it now. */
150 obj
->flags
|= TO_SCAN
;
153 if (obj
->flags
& COMPLETE
)
155 walker
->prefetch(walker
, obj
->oid
.hash
);
158 object_list_insert(obj
, process_queue_end
);
159 process_queue_end
= &(*process_queue_end
)->next
;
163 static int loop(struct walker
*walker
)
165 struct object_list
*elem
;
166 struct progress
*progress
= NULL
;
169 if (walker
->get_progress
)
170 progress
= start_delayed_progress(_("Fetching objects"), 0);
172 while (process_queue
) {
173 struct object
*obj
= process_queue
->item
;
174 elem
= process_queue
;
175 process_queue
= elem
->next
;
178 process_queue_end
= &process_queue
;
180 /* If we are not scanning this object, we placed it in
181 * the queue because we needed to fetch it first.
183 if (! (obj
->flags
& TO_SCAN
)) {
184 if (walker
->fetch(walker
, obj
->oid
.hash
)) {
185 stop_progress(&progress
);
191 parse_object(the_repository
, &obj
->oid
);
192 if (process_object(walker
, obj
)) {
193 stop_progress(&progress
);
196 display_progress(progress
, ++nr
);
198 stop_progress(&progress
);
202 static int interpret_target(struct walker
*walker
, char *target
, struct object_id
*oid
)
204 if (!get_oid_hex(target
, oid
))
206 if (!check_refname_format(target
, 0)) {
207 struct ref
*ref
= alloc_ref(target
);
208 if (!walker
->fetch_ref(walker
, ref
)) {
209 oidcpy(oid
, &ref
->old_oid
);
218 static int mark_complete(const char *path
, const struct object_id
*oid
,
219 int flag
, void *cb_data
)
221 struct commit
*commit
= lookup_commit_reference_gently(the_repository
,
225 commit
->object
.flags
|= COMPLETE
;
226 commit_list_insert(commit
, &complete
);
231 int walker_targets_stdin(char ***target
, const char ***write_ref
)
233 int targets
= 0, targets_alloc
= 0;
234 struct strbuf buf
= STRBUF_INIT
;
235 *target
= NULL
; *write_ref
= NULL
;
240 if (strbuf_getline_lf(&buf
, stdin
) == EOF
)
243 rf_one
= strchr(tg_one
, '\t');
247 if (targets
>= targets_alloc
) {
248 targets_alloc
= targets_alloc
? targets_alloc
* 2 : 64;
249 REALLOC_ARRAY(*target
, targets_alloc
);
250 REALLOC_ARRAY(*write_ref
, targets_alloc
);
252 (*target
)[targets
] = xstrdup(tg_one
);
253 (*write_ref
)[targets
] = xstrdup_or_null(rf_one
);
256 strbuf_release(&buf
);
260 void walker_targets_free(int targets
, char **target
, const char **write_ref
)
263 free(target
[targets
]);
265 free((char *) write_ref
[targets
]);
269 int walker_fetch(struct walker
*walker
, int targets
, char **target
,
270 const char **write_ref
, const char *write_ref_log_details
)
272 struct strbuf refname
= STRBUF_INIT
;
273 struct strbuf err
= STRBUF_INIT
;
274 struct ref_transaction
*transaction
= NULL
;
275 struct object_id
*oids
;
279 save_commit_buffer
= 0;
281 ALLOC_ARRAY(oids
, targets
);
284 transaction
= ref_transaction_begin(&err
);
286 error("%s", err
.buf
);
291 if (!walker
->get_recover
) {
292 for_each_ref(mark_complete
, NULL
);
293 commit_list_sort_by_date(&complete
);
296 for (i
= 0; i
< targets
; i
++) {
297 if (interpret_target(walker
, target
[i
], oids
+ i
)) {
298 error("Could not interpret response from server '%s' as something to pull", target
[i
]);
301 if (process(walker
, lookup_unknown_object(&oids
[i
])))
311 if (write_ref_log_details
) {
312 msg
= xstrfmt("fetch from %s", write_ref_log_details
);
316 for (i
= 0; i
< targets
; i
++) {
319 strbuf_reset(&refname
);
320 strbuf_addf(&refname
, "refs/%s", write_ref
[i
]);
321 if (ref_transaction_update(transaction
, refname
.buf
,
323 msg
? msg
: "fetch (unknown)",
325 error("%s", err
.buf
);
329 if (ref_transaction_commit(transaction
, &err
)) {
330 error("%s", err
.buf
);
337 ref_transaction_free(transaction
);
340 strbuf_release(&err
);
341 strbuf_release(&refname
);
345 void walker_free(struct walker
*walker
)
347 walker
->cleanup(walker
);