10 static unsigned char current_commit_sha1
[20];
12 void walker_say(struct walker
*walker
, const char *fmt
, const char *hex
)
14 if (walker
->get_verbosely
)
15 fprintf(stderr
, fmt
, hex
);
18 static void report_missing(const struct object
*obj
)
20 fprintf(stderr
, "Cannot obtain needed %s %s\n",
21 obj
->type
? typename(obj
->type
): "object",
22 oid_to_hex(&obj
->oid
));
23 if (!is_null_sha1(current_commit_sha1
))
24 fprintf(stderr
, "while processing commit %s.\n",
25 sha1_to_hex(current_commit_sha1
));
28 static int process(struct walker
*walker
, struct object
*obj
);
30 static int process_tree(struct walker
*walker
, struct tree
*tree
)
32 struct tree_desc desc
;
33 struct name_entry entry
;
38 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
39 while (tree_entry(&desc
, &entry
)) {
40 struct object
*obj
= NULL
;
42 /* submodule commits are not stored in the superproject */
43 if (S_ISGITLINK(entry
.mode
))
45 if (S_ISDIR(entry
.mode
)) {
46 struct tree
*tree
= lookup_tree(entry
.sha1
);
51 struct blob
*blob
= lookup_blob(entry
.sha1
);
55 if (!obj
|| process(walker
, obj
))
58 free_tree_buffer(tree
);
62 /* Remember to update object flag allocation in object.h */
63 #define COMPLETE (1U << 0)
64 #define SEEN (1U << 1)
65 #define TO_SCAN (1U << 2)
67 static struct commit_list
*complete
= NULL
;
69 static int process_commit(struct walker
*walker
, struct commit
*commit
)
71 if (parse_commit(commit
))
74 while (complete
&& complete
->item
->date
>= commit
->date
) {
75 pop_most_recent_commit(&complete
, COMPLETE
);
78 if (commit
->object
.flags
& COMPLETE
)
81 hashcpy(current_commit_sha1
, commit
->object
.oid
.hash
);
83 walker_say(walker
, "walk %s\n", oid_to_hex(&commit
->object
.oid
));
85 if (walker
->get_tree
) {
86 if (process(walker
, &commit
->tree
->object
))
91 if (walker
->get_history
) {
92 struct commit_list
*parents
= commit
->parents
;
93 for (; parents
; parents
= parents
->next
) {
94 if (process(walker
, &parents
->item
->object
))
101 static int process_tag(struct walker
*walker
, struct tag
*tag
)
105 return process(walker
, tag
->tagged
);
108 static struct object_list
*process_queue
= NULL
;
109 static struct object_list
**process_queue_end
= &process_queue
;
111 static int process_object(struct walker
*walker
, struct object
*obj
)
113 if (obj
->type
== OBJ_COMMIT
) {
114 if (process_commit(walker
, (struct commit
*)obj
))
118 if (obj
->type
== OBJ_TREE
) {
119 if (process_tree(walker
, (struct tree
*)obj
))
123 if (obj
->type
== OBJ_BLOB
) {
126 if (obj
->type
== OBJ_TAG
) {
127 if (process_tag(walker
, (struct tag
*)obj
))
131 return error("Unable to determine requirements "
133 typename(obj
->type
), oid_to_hex(&obj
->oid
));
136 static int process(struct walker
*walker
, struct object
*obj
)
138 if (obj
->flags
& SEEN
)
142 if (has_object_file(&obj
->oid
)) {
143 /* We already have it, so we should scan it now. */
144 obj
->flags
|= TO_SCAN
;
147 if (obj
->flags
& COMPLETE
)
149 walker
->prefetch(walker
, obj
->oid
.hash
);
152 object_list_insert(obj
, process_queue_end
);
153 process_queue_end
= &(*process_queue_end
)->next
;
157 static int loop(struct walker
*walker
)
159 struct object_list
*elem
;
161 while (process_queue
) {
162 struct object
*obj
= process_queue
->item
;
163 elem
= process_queue
;
164 process_queue
= elem
->next
;
167 process_queue_end
= &process_queue
;
169 /* If we are not scanning this object, we placed it in
170 * the queue because we needed to fetch it first.
172 if (! (obj
->flags
& TO_SCAN
)) {
173 if (walker
->fetch(walker
, obj
->oid
.hash
)) {
179 parse_object(obj
->oid
.hash
);
180 if (process_object(walker
, obj
))
186 static int interpret_target(struct walker
*walker
, char *target
, unsigned char *sha1
)
188 if (!get_sha1_hex(target
, sha1
))
190 if (!check_refname_format(target
, 0)) {
191 struct ref
*ref
= alloc_ref(target
);
192 if (!walker
->fetch_ref(walker
, ref
)) {
193 hashcpy(sha1
, ref
->old_oid
.hash
);
202 static int mark_complete(const char *path
, const struct object_id
*oid
,
203 int flag
, void *cb_data
)
205 struct commit
*commit
= lookup_commit_reference_gently(oid
->hash
, 1);
208 commit
->object
.flags
|= COMPLETE
;
209 commit_list_insert(commit
, &complete
);
214 int walker_targets_stdin(char ***target
, const char ***write_ref
)
216 int targets
= 0, targets_alloc
= 0;
217 struct strbuf buf
= STRBUF_INIT
;
218 *target
= NULL
; *write_ref
= NULL
;
223 if (strbuf_getline_lf(&buf
, stdin
) == EOF
)
226 rf_one
= strchr(tg_one
, '\t');
230 if (targets
>= targets_alloc
) {
231 targets_alloc
= targets_alloc
? targets_alloc
* 2 : 64;
232 REALLOC_ARRAY(*target
, targets_alloc
);
233 REALLOC_ARRAY(*write_ref
, targets_alloc
);
235 (*target
)[targets
] = xstrdup(tg_one
);
236 (*write_ref
)[targets
] = xstrdup_or_null(rf_one
);
239 strbuf_release(&buf
);
243 void walker_targets_free(int targets
, char **target
, const char **write_ref
)
246 free(target
[targets
]);
248 free((char *) write_ref
[targets
]);
252 int walker_fetch(struct walker
*walker
, int targets
, char **target
,
253 const char **write_ref
, const char *write_ref_log_details
)
255 struct strbuf refname
= STRBUF_INIT
;
256 struct strbuf err
= STRBUF_INIT
;
257 struct ref_transaction
*transaction
= NULL
;
258 unsigned char *sha1
= xmalloc(targets
* 20);
262 save_commit_buffer
= 0;
265 transaction
= ref_transaction_begin(&err
);
267 error("%s", err
.buf
);
272 if (!walker
->get_recover
) {
273 for_each_ref(mark_complete
, NULL
);
274 commit_list_sort_by_date(&complete
);
277 for (i
= 0; i
< targets
; i
++) {
278 if (interpret_target(walker
, target
[i
], &sha1
[20 * i
])) {
279 error("Could not interpret response from server '%s' as something to pull", target
[i
]);
282 if (process(walker
, lookup_unknown_object(&sha1
[20 * i
])))
292 if (write_ref_log_details
) {
293 msg
= xstrfmt("fetch from %s", write_ref_log_details
);
297 for (i
= 0; i
< targets
; i
++) {
300 strbuf_reset(&refname
);
301 strbuf_addf(&refname
, "refs/%s", write_ref
[i
]);
302 if (ref_transaction_update(transaction
, refname
.buf
,
303 &sha1
[20 * i
], NULL
, 0,
304 msg
? msg
: "fetch (unknown)",
306 error("%s", err
.buf
);
310 if (ref_transaction_commit(transaction
, &err
)) {
311 error("%s", err
.buf
);
318 ref_transaction_free(transaction
);
321 strbuf_release(&err
);
322 strbuf_release(&refname
);
326 void walker_free(struct walker
*walker
)
328 walker
->cleanup(walker
);