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
))
65 #define COMPLETE (1U << 0)
66 #define SEEN (1U << 1)
67 #define TO_SCAN (1U << 2)
69 static struct commit_list
*complete
= NULL
;
71 static int process_commit(struct walker
*walker
, struct commit
*commit
)
73 if (parse_commit(commit
))
76 while (complete
&& complete
->item
->date
>= commit
->date
) {
77 pop_most_recent_commit(&complete
, COMPLETE
);
80 if (commit
->object
.flags
& COMPLETE
)
83 hashcpy(current_commit_sha1
, commit
->object
.sha1
);
85 walker_say(walker
, "walk %s\n", sha1_to_hex(commit
->object
.sha1
));
87 if (walker
->get_tree
) {
88 if (process(walker
, &commit
->tree
->object
))
93 if (walker
->get_history
) {
94 struct commit_list
*parents
= commit
->parents
;
95 for (; parents
; parents
= parents
->next
) {
96 if (process(walker
, &parents
->item
->object
))
103 static int process_tag(struct walker
*walker
, struct tag
*tag
)
107 return process(walker
, tag
->tagged
);
110 static struct object_list
*process_queue
= NULL
;
111 static struct object_list
**process_queue_end
= &process_queue
;
113 static int process_object(struct walker
*walker
, struct object
*obj
)
115 if (obj
->type
== OBJ_COMMIT
) {
116 if (process_commit(walker
, (struct commit
*)obj
))
120 if (obj
->type
== OBJ_TREE
) {
121 if (process_tree(walker
, (struct tree
*)obj
))
125 if (obj
->type
== OBJ_BLOB
) {
128 if (obj
->type
== OBJ_TAG
) {
129 if (process_tag(walker
, (struct tag
*)obj
))
133 return error("Unable to determine requirements "
135 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
138 static int process(struct walker
*walker
, struct object
*obj
)
140 if (obj
->flags
& SEEN
)
144 if (has_sha1_file(obj
->sha1
)) {
145 /* We already have it, so we should scan it now. */
146 obj
->flags
|= TO_SCAN
;
149 if (obj
->flags
& COMPLETE
)
151 walker
->prefetch(walker
, obj
->sha1
);
154 object_list_insert(obj
, process_queue_end
);
155 process_queue_end
= &(*process_queue_end
)->next
;
159 static int loop(struct walker
*walker
)
161 struct object_list
*elem
;
163 while (process_queue
) {
164 struct object
*obj
= process_queue
->item
;
165 elem
= process_queue
;
166 process_queue
= elem
->next
;
169 process_queue_end
= &process_queue
;
171 /* If we are not scanning this object, we placed it in
172 * the queue because we needed to fetch it first.
174 if (! (obj
->flags
& TO_SCAN
)) {
175 if (walker
->fetch(walker
, obj
->sha1
)) {
181 parse_object(obj
->sha1
);
182 if (process_object(walker
, obj
))
188 static int interpret_target(struct walker
*walker
, char *target
, unsigned char *sha1
)
190 if (!get_sha1_hex(target
, sha1
))
192 if (!check_ref_format(target
)) {
193 struct ref
*ref
= alloc_ref(strlen(target
));
194 strcpy(ref
->name
, target
);
195 if (!walker
->fetch_ref(walker
, ref
)) {
196 hashcpy(sha1
, ref
->old_sha1
);
205 static int mark_complete(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
207 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
209 commit
->object
.flags
|= COMPLETE
;
210 insert_by_date(commit
, &complete
);
215 int walker_targets_stdin(char ***target
, const char ***write_ref
)
217 int targets
= 0, targets_alloc
= 0;
219 *target
= NULL
; *write_ref
= NULL
;
220 strbuf_init(&buf
, 0);
225 if (strbuf_getline(&buf
, stdin
, '\n') == EOF
)
228 rf_one
= strchr(tg_one
, '\t');
232 if (targets
>= targets_alloc
) {
233 targets_alloc
= targets_alloc
? targets_alloc
* 2 : 64;
234 *target
= xrealloc(*target
, targets_alloc
* sizeof(**target
));
235 *write_ref
= xrealloc(*write_ref
, targets_alloc
* sizeof(**write_ref
));
237 (*target
)[targets
] = xstrdup(tg_one
);
238 (*write_ref
)[targets
] = rf_one
? xstrdup(rf_one
) : NULL
;
241 strbuf_release(&buf
);
245 void walker_targets_free(int targets
, char **target
, const char **write_ref
)
248 free(target
[targets
]);
249 if (write_ref
&& write_ref
[targets
])
250 free((char *) write_ref
[targets
]);
254 int walker_fetch(struct walker
*walker
, int targets
, char **target
,
255 const char **write_ref
, const char *write_ref_log_details
)
257 struct ref_lock
**lock
= xcalloc(targets
, sizeof(struct ref_lock
*));
258 unsigned char *sha1
= xmalloc(targets
* 20);
263 save_commit_buffer
= 0;
265 for (i
= 0; i
< targets
; i
++) {
266 if (!write_ref
|| !write_ref
[i
])
269 lock
[i
] = lock_ref_sha1(write_ref
[i
], NULL
);
271 error("Can't lock ref %s", write_ref
[i
]);
272 goto unlock_and_fail
;
276 if (!walker
->get_recover
)
277 for_each_ref(mark_complete
, NULL
);
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
]);
282 goto unlock_and_fail
;
284 if (process(walker
, lookup_unknown_object(&sha1
[20 * i
])))
285 goto unlock_and_fail
;
289 goto unlock_and_fail
;
291 if (write_ref_log_details
) {
292 msg
= xmalloc(strlen(write_ref_log_details
) + 12);
293 sprintf(msg
, "fetch from %s", write_ref_log_details
);
297 for (i
= 0; i
< targets
; i
++) {
298 if (!write_ref
|| !write_ref
[i
])
300 ret
= write_ref_sha1(lock
[i
], &sha1
[20 * i
], msg
? msg
: "fetch (unknown)");
303 goto unlock_and_fail
;
310 for (i
= 0; i
< targets
; i
++)
317 void walker_free(struct walker
*walker
)
319 walker
->cleanup(walker
);