1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
7 #include "repository.h"
8 #include "object-store-ll.h"
12 #include "tree-walk.h"
18 static struct object_id current_commit_oid
;
20 void walker_say(struct walker
*walker
, const char *fmt
, ...)
22 if (walker
->get_verbosely
) {
25 vfprintf(stderr
, fmt
, ap
);
30 static void report_missing(const struct object
*obj
)
32 fprintf(stderr
, "Cannot obtain needed %s %s\n",
33 obj
->type
? type_name(obj
->type
): "object",
34 oid_to_hex(&obj
->oid
));
35 if (!is_null_oid(¤t_commit_oid
))
36 fprintf(stderr
, "while processing commit %s.\n",
37 oid_to_hex(¤t_commit_oid
));
40 static int process(struct walker
*walker
, struct object
*obj
);
42 static int process_tree(struct walker
*walker
, struct tree
*tree
)
44 struct tree_desc desc
;
45 struct name_entry entry
;
50 init_tree_desc(&desc
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
51 while (tree_entry(&desc
, &entry
)) {
52 struct object
*obj
= NULL
;
54 /* submodule commits are not stored in the superproject */
55 if (S_ISGITLINK(entry
.mode
))
57 if (S_ISDIR(entry
.mode
)) {
58 struct tree
*tree
= lookup_tree(the_repository
,
64 struct blob
*blob
= lookup_blob(the_repository
,
69 if (!obj
|| process(walker
, obj
))
72 free_tree_buffer(tree
);
76 /* Remember to update object flag allocation in object.h */
77 #define COMPLETE (1U << 0)
78 #define SEEN (1U << 1)
79 #define TO_SCAN (1U << 2)
81 static struct commit_list
*complete
= NULL
;
83 static int process_commit(struct walker
*walker
, struct commit
*commit
)
85 struct commit_list
*parents
;
87 if (repo_parse_commit(the_repository
, commit
))
90 while (complete
&& complete
->item
->date
>= commit
->date
) {
91 pop_most_recent_commit(&complete
, COMPLETE
);
94 if (commit
->object
.flags
& COMPLETE
)
97 oidcpy(¤t_commit_oid
, &commit
->object
.oid
);
99 walker_say(walker
, "walk %s\n", oid_to_hex(&commit
->object
.oid
));
101 if (process(walker
, &repo_get_commit_tree(the_repository
, commit
)->object
))
104 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
105 if (process(walker
, &parents
->item
->object
))
112 static int process_tag(struct walker
*walker
, struct tag
*tag
)
116 return process(walker
, tag
->tagged
);
119 static struct object_list
*process_queue
= NULL
;
120 static struct object_list
**process_queue_end
= &process_queue
;
122 static int process_object(struct walker
*walker
, struct object
*obj
)
124 if (obj
->type
== OBJ_COMMIT
) {
125 if (process_commit(walker
, (struct commit
*)obj
))
129 if (obj
->type
== OBJ_TREE
) {
130 if (process_tree(walker
, (struct tree
*)obj
))
134 if (obj
->type
== OBJ_BLOB
) {
137 if (obj
->type
== OBJ_TAG
) {
138 if (process_tag(walker
, (struct tag
*)obj
))
142 return error("Unable to determine requirements "
144 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
147 static int process(struct walker
*walker
, struct object
*obj
)
149 if (obj
->flags
& SEEN
)
153 if (repo_has_object_file(the_repository
, &obj
->oid
)) {
154 /* We already have it, so we should scan it now. */
155 obj
->flags
|= TO_SCAN
;
158 if (obj
->flags
& COMPLETE
)
160 walker
->prefetch(walker
, obj
->oid
.hash
);
163 object_list_insert(obj
, process_queue_end
);
164 process_queue_end
= &(*process_queue_end
)->next
;
168 static int loop(struct walker
*walker
)
170 struct object_list
*elem
;
171 struct progress
*progress
= NULL
;
174 if (walker
->get_progress
)
175 progress
= start_delayed_progress(_("Fetching objects"), 0);
177 while (process_queue
) {
178 struct object
*obj
= process_queue
->item
;
179 elem
= process_queue
;
180 process_queue
= elem
->next
;
183 process_queue_end
= &process_queue
;
185 /* If we are not scanning this object, we placed it in
186 * the queue because we needed to fetch it first.
188 if (! (obj
->flags
& TO_SCAN
)) {
189 if (walker
->fetch(walker
, obj
->oid
.hash
)) {
190 stop_progress(&progress
);
196 parse_object(the_repository
, &obj
->oid
);
197 if (process_object(walker
, obj
)) {
198 stop_progress(&progress
);
201 display_progress(progress
, ++nr
);
203 stop_progress(&progress
);
207 static int interpret_target(struct walker
*walker
, char *target
, struct object_id
*oid
)
209 if (!get_oid_hex(target
, oid
))
211 if (!check_refname_format(target
, 0)) {
212 struct ref
*ref
= alloc_ref(target
);
213 if (!walker
->fetch_ref(walker
, ref
)) {
214 oidcpy(oid
, &ref
->old_oid
);
223 static int mark_complete(const char *path UNUSED
,
224 const struct object_id
*oid
,
226 void *cb_data UNUSED
)
228 struct commit
*commit
= lookup_commit_reference_gently(the_repository
,
232 commit
->object
.flags
|= COMPLETE
;
233 commit_list_insert(commit
, &complete
);
238 int walker_targets_stdin(char ***target
, const char ***write_ref
)
240 int targets
= 0, targets_alloc
= 0;
241 struct strbuf buf
= STRBUF_INIT
;
242 *target
= NULL
; *write_ref
= NULL
;
247 if (strbuf_getline_lf(&buf
, stdin
) == EOF
)
250 rf_one
= strchr(tg_one
, '\t');
254 if (targets
>= targets_alloc
) {
255 targets_alloc
= targets_alloc
? targets_alloc
* 2 : 64;
256 REALLOC_ARRAY(*target
, targets_alloc
);
257 REALLOC_ARRAY(*write_ref
, targets_alloc
);
259 (*target
)[targets
] = xstrdup(tg_one
);
260 (*write_ref
)[targets
] = xstrdup_or_null(rf_one
);
263 strbuf_release(&buf
);
267 void walker_targets_free(int targets
, char **target
, const char **write_ref
)
270 free(target
[targets
]);
272 free((char *) write_ref
[targets
]);
276 int walker_fetch(struct walker
*walker
, int targets
, char **target
,
277 const char **write_ref
, const char *write_ref_log_details
)
279 struct strbuf refname
= STRBUF_INIT
;
280 struct strbuf err
= STRBUF_INIT
;
281 struct ref_transaction
*transaction
= NULL
;
282 struct object_id
*oids
;
286 save_commit_buffer
= 0;
288 ALLOC_ARRAY(oids
, targets
);
291 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
294 error("%s", err
.buf
);
299 if (!walker
->get_recover
) {
300 refs_for_each_ref(get_main_ref_store(the_repository
),
301 mark_complete
, NULL
);
302 commit_list_sort_by_date(&complete
);
305 for (i
= 0; i
< targets
; i
++) {
306 if (interpret_target(walker
, target
[i
], oids
+ i
)) {
307 error("Could not interpret response from server '%s' as something to pull", target
[i
]);
310 if (process(walker
, lookup_unknown_object(the_repository
, &oids
[i
])))
320 if (write_ref_log_details
) {
321 msg
= xstrfmt("fetch from %s", write_ref_log_details
);
325 for (i
= 0; i
< targets
; i
++) {
328 strbuf_reset(&refname
);
329 strbuf_addf(&refname
, "refs/%s", write_ref
[i
]);
330 if (ref_transaction_update(transaction
, refname
.buf
,
331 oids
+ i
, NULL
, NULL
, NULL
, 0,
332 msg
? msg
: "fetch (unknown)",
334 error("%s", err
.buf
);
338 if (ref_transaction_commit(transaction
, &err
)) {
339 error("%s", err
.buf
);
346 ref_transaction_free(transaction
);
349 strbuf_release(&err
);
350 strbuf_release(&refname
);
354 void walker_free(struct walker
*walker
)
356 walker
->cleanup(walker
);