10 const char *write_ref
= NULL
;
12 const unsigned char *current_ref
= NULL
;
16 /* 1 means "get delta", 2 means "really check delta harder */
19 int get_verbosely
= 0;
20 static unsigned char current_commit_sha1
[20];
22 static const char commitS
[] = "commit";
23 static const char treeS
[] = "tree";
24 static const char blobS
[] = "blob";
26 void pull_say(const char *fmt
, const char *hex
) {
28 fprintf(stderr
, fmt
, hex
);
31 static void report_missing(const char *what
, const unsigned char *missing
)
35 strcpy(missing_hex
, sha1_to_hex(missing
));;
37 "Cannot obtain needed %s %s\nwhile processing commit %s.\n",
38 what
, missing_hex
, sha1_to_hex(current_commit_sha1
));
41 static int make_sure_we_have_it(const char *what
, unsigned char *sha1
)
45 if (!has_sha1_file(sha1
)) {
48 report_missing(what
, sha1
);
50 else if (get_delta
< 2)
54 unsigned char delta_sha1
[20];
55 status
= sha1_delta_base(sha1
, delta_sha1
);
57 status
= make_sure_we_have_it(what
, delta_sha1
);
62 static int process_unknown(unsigned char *sha1
);
64 static int process_tree(unsigned char *sha1
)
66 struct tree
*tree
= lookup_tree(sha1
);
67 struct tree_entry_list
*entries
;
72 for (entries
= tree
->entries
; entries
; entries
= entries
->next
) {
73 const char *what
= entries
->directory
? treeS
: blobS
;
74 if (make_sure_we_have_it(what
, entries
->item
.tree
->object
.sha1
))
76 if (entries
->directory
) {
77 if (process_tree(entries
->item
.tree
->object
.sha1
))
84 static int process_commit(unsigned char *sha1
)
86 struct commit
*obj
= lookup_commit(sha1
);
88 if (make_sure_we_have_it(commitS
, sha1
))
91 if (parse_commit(obj
))
95 if (make_sure_we_have_it(treeS
, obj
->tree
->object
.sha1
))
97 if (process_tree(obj
->tree
->object
.sha1
))
103 struct commit_list
*parents
= obj
->parents
;
104 for (; parents
; parents
= parents
->next
) {
105 if (has_sha1_file(parents
->item
->object
.sha1
))
107 if (make_sure_we_have_it(NULL
,
108 parents
->item
->object
.sha1
)) {
109 /* The server might not have it, and
114 if (process_commit(parents
->item
->object
.sha1
))
116 memcpy(current_commit_sha1
, sha1
, 20);
122 static int process_tag(unsigned char *sha1
)
124 struct tag
*obj
= lookup_tag(sha1
);
128 return process_unknown(obj
->tagged
->sha1
);
131 static int process_unknown(unsigned char *sha1
)
134 if (make_sure_we_have_it("object", sha1
))
136 obj
= parse_object(sha1
);
138 return error("Unable to parse object %s", sha1_to_hex(sha1
));
139 if (obj
->type
== commit_type
)
140 return process_commit(sha1
);
141 if (obj
->type
== tree_type
)
142 return process_tree(sha1
);
143 if (obj
->type
== blob_type
)
145 if (obj
->type
== tag_type
)
146 return process_tag(sha1
);
147 return error("Unable to determine requirement of type %s for %s",
148 obj
->type
, sha1_to_hex(sha1
));
151 static int interpret_target(char *target
, unsigned char *sha1
)
153 if (!get_sha1_hex(target
, sha1
))
155 if (!check_ref_format(target
)) {
156 if (!fetch_ref(target
, sha1
)) {
164 int pull(char *target
)
166 unsigned char sha1
[20];
169 if (write_ref
&& current_ref
) {
170 fd
= lock_ref_sha1(write_ref
, current_ref
);
175 if (interpret_target(target
, sha1
))
176 return error("Could not interpret %s as something to pull",
178 if (process_unknown(sha1
))
183 write_ref_sha1(write_ref
, fd
, sha1
);
185 write_ref_sha1_unlocked(write_ref
, sha1
);