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
)
21 strcpy(missing_hex
, sha1_to_hex(obj
->sha1
));
22 fprintf(stderr
, "Cannot obtain needed %s %s\n",
23 obj
->type
? typename(obj
->type
): "object", missing_hex
);
24 if (!is_null_sha1(current_commit_sha1
))
25 fprintf(stderr
, "while processing commit %s.\n",
26 sha1_to_hex(current_commit_sha1
));
29 static int process(struct walker
*walker
, struct object
*obj
);
31 static int process_tree(struct walker
*walker
, struct tree
*tree
)
33 struct tree_desc desc
;
34 struct name_entry entry
;
39 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
40 while (tree_entry(&desc
, &entry
)) {
41 struct object
*obj
= NULL
;
43 /* submodule commits are not stored in the superproject */
44 if (S_ISGITLINK(entry
.mode
))
46 if (S_ISDIR(entry
.mode
)) {
47 struct tree
*tree
= lookup_tree(entry
.sha1
);
52 struct blob
*blob
= lookup_blob(entry
.sha1
);
56 if (!obj
|| process(walker
, obj
))
59 free_tree_buffer(tree
);
63 /* Remember to update object flag allocation in object.h */
64 #define COMPLETE (1U << 0)
65 #define SEEN (1U << 1)
66 #define TO_SCAN (1U << 2)
68 static struct commit_list
*complete
= NULL
;
70 static int process_commit(struct walker
*walker
, struct commit
*commit
)
72 if (parse_commit(commit
))
75 while (complete
&& complete
->item
->date
>= commit
->date
) {
76 pop_most_recent_commit(&complete
, COMPLETE
);
79 if (commit
->object
.flags
& COMPLETE
)
82 hashcpy(current_commit_sha1
, commit
->object
.sha1
);
84 walker_say(walker
, "walk %s\n", sha1_to_hex(commit
->object
.sha1
));
86 if (walker
->get_tree
) {
87 if (process(walker
, &commit
->tree
->object
))
92 if (walker
->get_history
) {
93 struct commit_list
*parents
= commit
->parents
;
94 for (; parents
; parents
= parents
->next
) {
95 if (process(walker
, &parents
->item
->object
))
102 static int process_tag(struct walker
*walker
, struct tag
*tag
)
106 return process(walker
, tag
->tagged
);
109 static struct object_list
*process_queue
= NULL
;
110 static struct object_list
**process_queue_end
= &process_queue
;
112 static int process_object(struct walker
*walker
, struct object
*obj
)
114 if (obj
->type
== OBJ_COMMIT
) {
115 if (process_commit(walker
, (struct commit
*)obj
))
119 if (obj
->type
== OBJ_TREE
) {
120 if (process_tree(walker
, (struct tree
*)obj
))
124 if (obj
->type
== OBJ_BLOB
) {
127 if (obj
->type
== OBJ_TAG
) {
128 if (process_tag(walker
, (struct tag
*)obj
))
132 return error("Unable to determine requirements "
134 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
137 static int process(struct walker
*walker
, struct object
*obj
)
139 if (obj
->flags
& SEEN
)
143 if (has_sha1_file(obj
->sha1
)) {
144 /* We already have it, so we should scan it now. */
145 obj
->flags
|= TO_SCAN
;
148 if (obj
->flags
& COMPLETE
)
150 walker
->prefetch(walker
, obj
->sha1
);
153 object_list_insert(obj
, process_queue_end
);
154 process_queue_end
= &(*process_queue_end
)->next
;
158 static int loop(struct walker
*walker
)
160 struct object_list
*elem
;
162 while (process_queue
) {
163 struct object
*obj
= process_queue
->item
;
164 elem
= process_queue
;
165 process_queue
= elem
->next
;
168 process_queue_end
= &process_queue
;
170 /* If we are not scanning this object, we placed it in
171 * the queue because we needed to fetch it first.
173 if (! (obj
->flags
& TO_SCAN
)) {
174 if (walker
->fetch(walker
, obj
->sha1
)) {
180 parse_object(obj
->sha1
);
181 if (process_object(walker
, obj
))
187 static int interpret_target(struct walker
*walker
, char *target
, unsigned char *sha1
)
189 if (!get_sha1_hex(target
, sha1
))
191 if (!check_refname_format(target
, 0)) {
192 struct ref
*ref
= alloc_ref(target
);
193 if (!walker
->fetch_ref(walker
, ref
)) {
194 hashcpy(sha1
, ref
->old_sha1
);
203 static int mark_complete(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
205 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
207 commit
->object
.flags
|= COMPLETE
;
208 commit_list_insert(commit
, &complete
);
213 int walker_targets_stdin(char ***target
, const char ***write_ref
)
215 int targets
= 0, targets_alloc
= 0;
216 struct strbuf buf
= STRBUF_INIT
;
217 *target
= NULL
; *write_ref
= NULL
;
222 if (strbuf_getline(&buf
, stdin
, '\n') == EOF
)
225 rf_one
= strchr(tg_one
, '\t');
229 if (targets
>= targets_alloc
) {
230 targets_alloc
= targets_alloc
? targets_alloc
* 2 : 64;
231 REALLOC_ARRAY(*target
, targets_alloc
);
232 REALLOC_ARRAY(*write_ref
, targets_alloc
);
234 (*target
)[targets
] = xstrdup(tg_one
);
235 (*write_ref
)[targets
] = xstrdup_or_null(rf_one
);
238 strbuf_release(&buf
);
242 void walker_targets_free(int targets
, char **target
, const char **write_ref
)
245 free(target
[targets
]);
247 free((char *) write_ref
[targets
]);
251 int walker_fetch(struct walker
*walker
, int targets
, char **target
,
252 const char **write_ref
, const char *write_ref_log_details
)
254 struct strbuf refname
= STRBUF_INIT
;
255 struct strbuf err
= STRBUF_INIT
;
256 struct ref_transaction
*transaction
= NULL
;
257 unsigned char *sha1
= xmalloc(targets
* 20);
261 save_commit_buffer
= 0;
264 transaction
= ref_transaction_begin(&err
);
266 error("%s", err
.buf
);
271 if (!walker
->get_recover
) {
272 struct each_ref_fn_sha1_adapter wrapped_mark_complete
=
273 {mark_complete
, NULL
};
275 for_each_ref(each_ref_fn_adapter
, &wrapped_mark_complete
);
276 commit_list_sort_by_date(&complete
);
279 for (i
= 0; i
< targets
; i
++) {
280 if (interpret_target(walker
, target
[i
], &sha1
[20 * i
])) {
281 error("Could not interpret response from server '%s' as something to pull", target
[i
]);
284 if (process(walker
, lookup_unknown_object(&sha1
[20 * i
])))
294 if (write_ref_log_details
) {
295 msg
= xstrfmt("fetch from %s", write_ref_log_details
);
299 for (i
= 0; i
< targets
; i
++) {
302 strbuf_reset(&refname
);
303 strbuf_addf(&refname
, "refs/%s", write_ref
[i
]);
304 if (ref_transaction_update(transaction
, refname
.buf
,
305 &sha1
[20 * i
], NULL
, 0,
306 msg
? msg
: "fetch (unknown)",
308 error("%s", err
.buf
);
312 if (ref_transaction_commit(transaction
, &err
)) {
313 error("%s", err
.buf
);
320 ref_transaction_free(transaction
);
323 strbuf_release(&err
);
324 strbuf_release(&refname
);
328 void walker_free(struct walker
*walker
)
330 walker
->cleanup(walker
);