1 #include "git-compat-util.h"
5 #include "repository.h"
6 #include "object-store.h"
15 static struct object_id current_commit_oid
;
17 void walker_say(struct walker
*walker
, const char *fmt
, ...)
19 if (walker
->get_verbosely
) {
22 vfprintf(stderr
, fmt
, ap
);
27 static void report_missing(const struct object
*obj
)
29 fprintf(stderr
, "Cannot obtain needed %s %s\n",
30 obj
->type
? type_name(obj
->type
): "object",
31 oid_to_hex(&obj
->oid
));
32 if (!is_null_oid(¤t_commit_oid
))
33 fprintf(stderr
, "while processing commit %s.\n",
34 oid_to_hex(¤t_commit_oid
));
37 static int process(struct walker
*walker
, struct object
*obj
);
39 static int process_tree(struct walker
*walker
, struct tree
*tree
)
41 struct tree_desc desc
;
42 struct name_entry entry
;
47 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
48 while (tree_entry(&desc
, &entry
)) {
49 struct object
*obj
= NULL
;
51 /* submodule commits are not stored in the superproject */
52 if (S_ISGITLINK(entry
.mode
))
54 if (S_ISDIR(entry
.mode
)) {
55 struct tree
*tree
= lookup_tree(the_repository
,
61 struct blob
*blob
= lookup_blob(the_repository
,
66 if (!obj
|| process(walker
, obj
))
69 free_tree_buffer(tree
);
73 /* Remember to update object flag allocation in object.h */
74 #define COMPLETE (1U << 0)
75 #define SEEN (1U << 1)
76 #define TO_SCAN (1U << 2)
78 static struct commit_list
*complete
= NULL
;
80 static int process_commit(struct walker
*walker
, struct commit
*commit
)
82 struct commit_list
*parents
;
84 if (repo_parse_commit(the_repository
, commit
))
87 while (complete
&& complete
->item
->date
>= commit
->date
) {
88 pop_most_recent_commit(&complete
, COMPLETE
);
91 if (commit
->object
.flags
& COMPLETE
)
94 oidcpy(¤t_commit_oid
, &commit
->object
.oid
);
96 walker_say(walker
, "walk %s\n", oid_to_hex(&commit
->object
.oid
));
98 if (process(walker
, &repo_get_commit_tree(the_repository
, commit
)->object
))
101 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
102 if (process(walker
, &parents
->item
->object
))
109 static int process_tag(struct walker
*walker
, struct tag
*tag
)
113 return process(walker
, tag
->tagged
);
116 static struct object_list
*process_queue
= NULL
;
117 static struct object_list
**process_queue_end
= &process_queue
;
119 static int process_object(struct walker
*walker
, struct object
*obj
)
121 if (obj
->type
== OBJ_COMMIT
) {
122 if (process_commit(walker
, (struct commit
*)obj
))
126 if (obj
->type
== OBJ_TREE
) {
127 if (process_tree(walker
, (struct tree
*)obj
))
131 if (obj
->type
== OBJ_BLOB
) {
134 if (obj
->type
== OBJ_TAG
) {
135 if (process_tag(walker
, (struct tag
*)obj
))
139 return error("Unable to determine requirements "
141 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
144 static int process(struct walker
*walker
, struct object
*obj
)
146 if (obj
->flags
& SEEN
)
150 if (repo_has_object_file(the_repository
, &obj
->oid
)) {
151 /* We already have it, so we should scan it now. */
152 obj
->flags
|= TO_SCAN
;
155 if (obj
->flags
& COMPLETE
)
157 walker
->prefetch(walker
, obj
->oid
.hash
);
160 object_list_insert(obj
, process_queue_end
);
161 process_queue_end
= &(*process_queue_end
)->next
;
165 static int loop(struct walker
*walker
)
167 struct object_list
*elem
;
168 struct progress
*progress
= NULL
;
171 if (walker
->get_progress
)
172 progress
= start_delayed_progress(_("Fetching objects"), 0);
174 while (process_queue
) {
175 struct object
*obj
= process_queue
->item
;
176 elem
= process_queue
;
177 process_queue
= elem
->next
;
180 process_queue_end
= &process_queue
;
182 /* If we are not scanning this object, we placed it in
183 * the queue because we needed to fetch it first.
185 if (! (obj
->flags
& TO_SCAN
)) {
186 if (walker
->fetch(walker
, obj
->oid
.hash
)) {
187 stop_progress(&progress
);
193 parse_object(the_repository
, &obj
->oid
);
194 if (process_object(walker
, obj
)) {
195 stop_progress(&progress
);
198 display_progress(progress
, ++nr
);
200 stop_progress(&progress
);
204 static int interpret_target(struct walker
*walker
, char *target
, struct object_id
*oid
)
206 if (!get_oid_hex(target
, oid
))
208 if (!check_refname_format(target
, 0)) {
209 struct ref
*ref
= alloc_ref(target
);
210 if (!walker
->fetch_ref(walker
, ref
)) {
211 oidcpy(oid
, &ref
->old_oid
);
220 static int mark_complete(const char *path UNUSED
,
221 const struct object_id
*oid
,
223 void *cb_data UNUSED
)
225 struct commit
*commit
= lookup_commit_reference_gently(the_repository
,
229 commit
->object
.flags
|= COMPLETE
;
230 commit_list_insert(commit
, &complete
);
235 int walker_targets_stdin(char ***target
, const char ***write_ref
)
237 int targets
= 0, targets_alloc
= 0;
238 struct strbuf buf
= STRBUF_INIT
;
239 *target
= NULL
; *write_ref
= NULL
;
244 if (strbuf_getline_lf(&buf
, stdin
) == EOF
)
247 rf_one
= strchr(tg_one
, '\t');
251 if (targets
>= targets_alloc
) {
252 targets_alloc
= targets_alloc
? targets_alloc
* 2 : 64;
253 REALLOC_ARRAY(*target
, targets_alloc
);
254 REALLOC_ARRAY(*write_ref
, targets_alloc
);
256 (*target
)[targets
] = xstrdup(tg_one
);
257 (*write_ref
)[targets
] = xstrdup_or_null(rf_one
);
260 strbuf_release(&buf
);
264 void walker_targets_free(int targets
, char **target
, const char **write_ref
)
267 free(target
[targets
]);
269 free((char *) write_ref
[targets
]);
273 int walker_fetch(struct walker
*walker
, int targets
, char **target
,
274 const char **write_ref
, const char *write_ref_log_details
)
276 struct strbuf refname
= STRBUF_INIT
;
277 struct strbuf err
= STRBUF_INIT
;
278 struct ref_transaction
*transaction
= NULL
;
279 struct object_id
*oids
;
283 save_commit_buffer
= 0;
285 ALLOC_ARRAY(oids
, targets
);
288 transaction
= ref_transaction_begin(&err
);
290 error("%s", err
.buf
);
295 if (!walker
->get_recover
) {
296 for_each_ref(mark_complete
, NULL
);
297 commit_list_sort_by_date(&complete
);
300 for (i
= 0; i
< targets
; i
++) {
301 if (interpret_target(walker
, target
[i
], oids
+ i
)) {
302 error("Could not interpret response from server '%s' as something to pull", target
[i
]);
305 if (process(walker
, lookup_unknown_object(the_repository
, &oids
[i
])))
315 if (write_ref_log_details
) {
316 msg
= xstrfmt("fetch from %s", write_ref_log_details
);
320 for (i
= 0; i
< targets
; i
++) {
323 strbuf_reset(&refname
);
324 strbuf_addf(&refname
, "refs/%s", write_ref
[i
]);
325 if (ref_transaction_update(transaction
, refname
.buf
,
327 msg
? msg
: "fetch (unknown)",
329 error("%s", err
.buf
);
333 if (ref_transaction_commit(transaction
, &err
)) {
334 error("%s", err
.buf
);
341 ref_transaction_free(transaction
);
344 strbuf_release(&err
);
345 strbuf_release(&refname
);
349 void walker_free(struct walker
*walker
)
351 walker
->cleanup(walker
);