10 const char *write_ref
= NULL
;
12 const unsigned char *current_ref
= NULL
;
17 int get_verbosely
= 0;
18 static unsigned char current_commit_sha1
[20];
20 void pull_say(const char *fmt
, const char *hex
)
23 fprintf(stderr
, fmt
, hex
);
26 static void report_missing(const char *what
, const unsigned char *missing
)
30 strcpy(missing_hex
, sha1_to_hex(missing
));;
32 "Cannot obtain needed %s %s\nwhile processing commit %s.\n",
33 what
, missing_hex
, sha1_to_hex(current_commit_sha1
));
36 static int make_sure_we_have_it(const char *what
, unsigned char *sha1
)
40 if (!has_sha1_file(sha1
)) {
43 report_missing(what
, sha1
);
48 static int process(unsigned char *sha1
, const char *type
);
50 static int process_tree(struct tree
*tree
)
52 struct tree_entry_list
*entries
;
57 for (entries
= tree
->entries
; entries
; entries
= entries
->next
) {
58 if (process(entries
->item
.any
->sha1
,
59 entries
->directory
? tree_type
: blob_type
))
66 static struct commit_list
*complete
= NULL
;
68 static int process_commit(struct commit
*commit
)
70 if (parse_commit(commit
))
73 while (complete
&& complete
->item
->date
>= commit
->date
) {
74 pop_most_recent_commit(&complete
, COMPLETE
);
78 if (commit
->object
.flags
& COMPLETE
)
81 memcpy(current_commit_sha1
, commit
->object
.sha1
, 20);
84 if (process(commit
->tree
->object
.sha1
, tree_type
))
90 struct commit_list
*parents
= commit
->parents
;
91 for (; parents
; parents
= parents
->next
) {
92 if (process(parents
->item
->object
.sha1
,
100 static int process_tag(struct tag
*tag
)
104 return process(tag
->tagged
->sha1
, NULL
);
107 static struct object_list
*process_queue
= NULL
;
108 static struct object_list
**process_queue_end
= &process_queue
;
110 static int process_object(struct object
*obj
)
112 if (obj
->type
== commit_type
) {
113 if (process_commit((struct commit
*)obj
))
117 if (obj
->type
== tree_type
) {
118 if (process_tree((struct tree
*)obj
))
122 if (obj
->type
== blob_type
) {
125 if (obj
->type
== tag_type
) {
126 if (process_tag((struct tag
*)obj
))
130 return error("Unable to determine requirements "
132 obj
->type
, sha1_to_hex(obj
->sha1
));
135 static int process(unsigned char *sha1
, const char *type
)
137 struct object
*obj
= lookup_object_type(sha1
, type
);
139 if (has_sha1_file(sha1
)) {
141 /* We already have it, so we should scan it now. */
142 return process_object(obj
);
144 if (object_list_contains(process_queue
, obj
))
146 object_list_insert(obj
, process_queue_end
);
147 process_queue_end
= &(*process_queue_end
)->next
;
149 //fprintf(stderr, "prefetch %s\n", sha1_to_hex(sha1));
155 static int loop(void)
157 while (process_queue
) {
158 struct object
*obj
= process_queue
->item
;
160 fprintf(stderr, "%d objects to pull\n",
161 object_list_length(process_queue));
163 process_queue
= process_queue
->next
;
165 process_queue_end
= &process_queue
;
167 //fprintf(stderr, "fetch %s\n", sha1_to_hex(obj->sha1));
169 if (make_sure_we_have_it(obj
->type
? obj
->type
: "object",
173 parse_object(obj
->sha1
);
174 if (process_object(obj
))
180 static int interpret_target(char *target
, unsigned char *sha1
)
182 if (!get_sha1_hex(target
, sha1
))
184 if (!check_ref_format(target
)) {
185 if (!fetch_ref(target
, sha1
)) {
192 static int mark_complete(const char *path
, const unsigned char *sha1
)
194 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
196 commit
->object
.flags
|= COMPLETE
;
197 insert_by_date(commit
, &complete
);
202 int pull(char *target
)
204 unsigned char sha1
[20];
207 save_commit_buffer
= 0;
208 if (write_ref
&& current_ref
) {
209 fd
= lock_ref_sha1(write_ref
, current_ref
);
214 for_each_ref(mark_complete
);
216 if (interpret_target(target
, sha1
))
217 return error("Could not interpret %s as something to pull",
219 if (process(sha1
, NULL
))
226 write_ref_sha1(write_ref
, fd
, sha1
);
228 write_ref_sha1_unlocked(write_ref
, sha1
);