1 #include "git-compat-util.h"
5 #include "repository.h"
6 #include "object-store-ll.h"
10 #include "tree-walk.h"
16 static struct object_id current_commit_oid
;
18 void walker_say(struct walker
*walker
, const char *fmt
, ...)
20 if (walker
->get_verbosely
) {
23 vfprintf(stderr
, fmt
, ap
);
28 static void report_missing(const struct object
*obj
)
30 fprintf(stderr
, "Cannot obtain needed %s %s\n",
31 obj
->type
? type_name(obj
->type
): "object",
32 oid_to_hex(&obj
->oid
));
33 if (!is_null_oid(¤t_commit_oid
))
34 fprintf(stderr
, "while processing commit %s.\n",
35 oid_to_hex(¤t_commit_oid
));
38 static int process(struct walker
*walker
, struct object
*obj
);
40 static int process_tree(struct walker
*walker
, struct tree
*tree
)
42 struct tree_desc desc
;
43 struct name_entry entry
;
48 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
49 while (tree_entry(&desc
, &entry
)) {
50 struct object
*obj
= NULL
;
52 /* submodule commits are not stored in the superproject */
53 if (S_ISGITLINK(entry
.mode
))
55 if (S_ISDIR(entry
.mode
)) {
56 struct tree
*tree
= lookup_tree(the_repository
,
62 struct blob
*blob
= lookup_blob(the_repository
,
67 if (!obj
|| process(walker
, obj
))
70 free_tree_buffer(tree
);
74 /* Remember to update object flag allocation in object.h */
75 #define COMPLETE (1U << 0)
76 #define SEEN (1U << 1)
77 #define TO_SCAN (1U << 2)
79 static struct commit_list
*complete
= NULL
;
81 static int process_commit(struct walker
*walker
, struct commit
*commit
)
83 struct commit_list
*parents
;
85 if (repo_parse_commit(the_repository
, commit
))
88 while (complete
&& complete
->item
->date
>= commit
->date
) {
89 pop_most_recent_commit(&complete
, COMPLETE
);
92 if (commit
->object
.flags
& COMPLETE
)
95 oidcpy(¤t_commit_oid
, &commit
->object
.oid
);
97 walker_say(walker
, "walk %s\n", oid_to_hex(&commit
->object
.oid
));
99 if (process(walker
, &repo_get_commit_tree(the_repository
, commit
)->object
))
102 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
103 if (process(walker
, &parents
->item
->object
))
110 static int process_tag(struct walker
*walker
, struct tag
*tag
)
114 return process(walker
, tag
->tagged
);
117 static struct object_list
*process_queue
= NULL
;
118 static struct object_list
**process_queue_end
= &process_queue
;
120 static int process_object(struct walker
*walker
, struct object
*obj
)
122 if (obj
->type
== OBJ_COMMIT
) {
123 if (process_commit(walker
, (struct commit
*)obj
))
127 if (obj
->type
== OBJ_TREE
) {
128 if (process_tree(walker
, (struct tree
*)obj
))
132 if (obj
->type
== OBJ_BLOB
) {
135 if (obj
->type
== OBJ_TAG
) {
136 if (process_tag(walker
, (struct tag
*)obj
))
140 return error("Unable to determine requirements "
142 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
145 static int process(struct walker
*walker
, struct object
*obj
)
147 if (obj
->flags
& SEEN
)
151 if (repo_has_object_file(the_repository
, &obj
->oid
)) {
152 /* We already have it, so we should scan it now. */
153 obj
->flags
|= TO_SCAN
;
156 if (obj
->flags
& COMPLETE
)
158 walker
->prefetch(walker
, obj
->oid
.hash
);
161 object_list_insert(obj
, process_queue_end
);
162 process_queue_end
= &(*process_queue_end
)->next
;
166 static int loop(struct walker
*walker
)
168 struct object_list
*elem
;
169 struct progress
*progress
= NULL
;
172 if (walker
->get_progress
)
173 progress
= start_delayed_progress(_("Fetching objects"), 0);
175 while (process_queue
) {
176 struct object
*obj
= process_queue
->item
;
177 elem
= process_queue
;
178 process_queue
= elem
->next
;
181 process_queue_end
= &process_queue
;
183 /* If we are not scanning this object, we placed it in
184 * the queue because we needed to fetch it first.
186 if (! (obj
->flags
& TO_SCAN
)) {
187 if (walker
->fetch(walker
, obj
->oid
.hash
)) {
188 stop_progress(&progress
);
194 parse_object(the_repository
, &obj
->oid
);
195 if (process_object(walker
, obj
)) {
196 stop_progress(&progress
);
199 display_progress(progress
, ++nr
);
201 stop_progress(&progress
);
205 static int interpret_target(struct walker
*walker
, char *target
, struct object_id
*oid
)
207 if (!get_oid_hex(target
, oid
))
209 if (!check_refname_format(target
, 0)) {
210 struct ref
*ref
= alloc_ref(target
);
211 if (!walker
->fetch_ref(walker
, ref
)) {
212 oidcpy(oid
, &ref
->old_oid
);
221 static int mark_complete(const char *path UNUSED
,
222 const struct object_id
*oid
,
224 void *cb_data UNUSED
)
226 struct commit
*commit
= lookup_commit_reference_gently(the_repository
,
230 commit
->object
.flags
|= COMPLETE
;
231 commit_list_insert(commit
, &complete
);
236 int walker_targets_stdin(char ***target
, const char ***write_ref
)
238 int targets
= 0, targets_alloc
= 0;
239 struct strbuf buf
= STRBUF_INIT
;
240 *target
= NULL
; *write_ref
= NULL
;
245 if (strbuf_getline_lf(&buf
, stdin
) == EOF
)
248 rf_one
= strchr(tg_one
, '\t');
252 if (targets
>= targets_alloc
) {
253 targets_alloc
= targets_alloc
? targets_alloc
* 2 : 64;
254 REALLOC_ARRAY(*target
, targets_alloc
);
255 REALLOC_ARRAY(*write_ref
, targets_alloc
);
257 (*target
)[targets
] = xstrdup(tg_one
);
258 (*write_ref
)[targets
] = xstrdup_or_null(rf_one
);
261 strbuf_release(&buf
);
265 void walker_targets_free(int targets
, char **target
, const char **write_ref
)
268 free(target
[targets
]);
270 free((char *) write_ref
[targets
]);
274 int walker_fetch(struct walker
*walker
, int targets
, char **target
,
275 const char **write_ref
, const char *write_ref_log_details
)
277 struct strbuf refname
= STRBUF_INIT
;
278 struct strbuf err
= STRBUF_INIT
;
279 struct ref_transaction
*transaction
= NULL
;
280 struct object_id
*oids
;
284 save_commit_buffer
= 0;
286 ALLOC_ARRAY(oids
, targets
);
289 transaction
= ref_transaction_begin(&err
);
291 error("%s", err
.buf
);
296 if (!walker
->get_recover
) {
297 for_each_ref(mark_complete
, NULL
);
298 commit_list_sort_by_date(&complete
);
301 for (i
= 0; i
< targets
; i
++) {
302 if (interpret_target(walker
, target
[i
], oids
+ i
)) {
303 error("Could not interpret response from server '%s' as something to pull", target
[i
]);
306 if (process(walker
, lookup_unknown_object(the_repository
, &oids
[i
])))
316 if (write_ref_log_details
) {
317 msg
= xstrfmt("fetch from %s", write_ref_log_details
);
321 for (i
= 0; i
< targets
; i
++) {
324 strbuf_reset(&refname
);
325 strbuf_addf(&refname
, "refs/%s", write_ref
[i
]);
326 if (ref_transaction_update(transaction
, refname
.buf
,
328 msg
? msg
: "fetch (unknown)",
330 error("%s", err
.buf
);
334 if (ref_transaction_commit(transaction
, &err
)) {
335 error("%s", err
.buf
);
342 ref_transaction_free(transaction
);
345 strbuf_release(&err
);
346 strbuf_release(&refname
);
350 void walker_free(struct walker
*walker
)
352 walker
->cleanup(walker
);