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
);
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
)) {
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 char *referent UNUSED
,
225 const struct object_id
*oid
,
227 void *cb_data UNUSED
)
229 struct commit
*commit
= lookup_commit_reference_gently(the_repository
,
233 commit
->object
.flags
|= COMPLETE
;
234 commit_list_insert(commit
, &complete
);
239 int walker_targets_stdin(char ***target
, const char ***write_ref
)
241 int targets
= 0, targets_alloc
= 0;
242 struct strbuf buf
= STRBUF_INIT
;
243 *target
= NULL
; *write_ref
= NULL
;
248 if (strbuf_getline_lf(&buf
, stdin
) == EOF
)
251 rf_one
= strchr(tg_one
, '\t');
255 if (targets
>= targets_alloc
) {
256 targets_alloc
= targets_alloc
? targets_alloc
* 2 : 64;
257 REALLOC_ARRAY(*target
, targets_alloc
);
258 REALLOC_ARRAY(*write_ref
, targets_alloc
);
260 (*target
)[targets
] = xstrdup(tg_one
);
261 (*write_ref
)[targets
] = xstrdup_or_null(rf_one
);
264 strbuf_release(&buf
);
268 void walker_targets_free(int targets
, char **target
, const char **write_ref
)
271 free(target
[targets
]);
273 free((char *) write_ref
[targets
]);
277 int walker_fetch(struct walker
*walker
, int targets
, char **target
,
278 const char **write_ref
, const char *write_ref_log_details
)
280 struct strbuf refname
= STRBUF_INIT
;
281 struct strbuf err
= STRBUF_INIT
;
282 struct ref_transaction
*transaction
= NULL
;
283 struct object_id
*oids
;
287 save_commit_buffer
= 0;
289 ALLOC_ARRAY(oids
, targets
);
292 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
295 error("%s", err
.buf
);
300 if (!walker
->get_recover
) {
301 refs_for_each_ref(get_main_ref_store(the_repository
),
302 mark_complete
, NULL
);
303 commit_list_sort_by_date(&complete
);
306 for (i
= 0; i
< targets
; i
++) {
307 if (interpret_target(walker
, target
[i
], oids
+ i
)) {
308 error("Could not interpret response from server '%s' as something to pull", target
[i
]);
311 if (process(walker
, lookup_unknown_object(the_repository
, &oids
[i
])))
321 if (write_ref_log_details
) {
322 msg
= xstrfmt("fetch from %s", write_ref_log_details
);
326 for (i
= 0; i
< targets
; i
++) {
329 strbuf_reset(&refname
);
330 strbuf_addf(&refname
, "refs/%s", write_ref
[i
]);
331 if (ref_transaction_update(transaction
, refname
.buf
,
332 oids
+ i
, NULL
, NULL
, NULL
, 0,
333 msg
? msg
: "fetch (unknown)",
335 error("%s", err
.buf
);
339 if (ref_transaction_commit(transaction
, &err
)) {
340 error("%s", err
.buf
);
347 ref_transaction_free(transaction
);
350 strbuf_release(&err
);
351 strbuf_release(&refname
);
355 void walker_free(struct walker
*walker
)
357 walker
->cleanup(walker
);