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 static const char commitS
[] = "commit";
21 static const char treeS
[] = "tree";
22 static const char blobS
[] = "blob";
24 void pull_say(const char *fmt
, const char *hex
) {
26 fprintf(stderr
, fmt
, hex
);
29 static void report_missing(const char *what
, const unsigned char *missing
)
33 strcpy(missing_hex
, sha1_to_hex(missing
));;
35 "Cannot obtain needed %s %s\nwhile processing commit %s.\n",
36 what
, missing_hex
, sha1_to_hex(current_commit_sha1
));
39 static int make_sure_we_have_it(const char *what
, unsigned char *sha1
)
43 if (!has_sha1_file(sha1
)) {
46 report_missing(what
, sha1
);
51 static int process_unknown(unsigned char *sha1
);
53 static int process_tree(unsigned char *sha1
)
55 struct tree
*tree
= lookup_tree(sha1
);
56 struct tree_entry_list
*entries
;
61 for (entries
= tree
->entries
; entries
; entries
= entries
->next
) {
62 const char *what
= entries
->directory
? treeS
: blobS
;
63 if (make_sure_we_have_it(what
, entries
->item
.tree
->object
.sha1
))
65 if (entries
->directory
) {
66 if (process_tree(entries
->item
.tree
->object
.sha1
))
73 static int process_commit(unsigned char *sha1
)
75 struct commit
*obj
= lookup_commit(sha1
);
77 if (make_sure_we_have_it(commitS
, sha1
))
80 if (parse_commit(obj
))
84 if (make_sure_we_have_it(treeS
, obj
->tree
->object
.sha1
))
86 if (process_tree(obj
->tree
->object
.sha1
))
92 struct commit_list
*parents
= obj
->parents
;
93 for (; parents
; parents
= parents
->next
) {
94 if (has_sha1_file(parents
->item
->object
.sha1
))
96 if (make_sure_we_have_it(NULL
,
97 parents
->item
->object
.sha1
)) {
98 /* The server might not have it, and
103 if (process_commit(parents
->item
->object
.sha1
))
105 memcpy(current_commit_sha1
, sha1
, 20);
111 static int process_tag(unsigned char *sha1
)
113 struct tag
*obj
= lookup_tag(sha1
);
117 return process_unknown(obj
->tagged
->sha1
);
120 static int process_unknown(unsigned char *sha1
)
123 if (make_sure_we_have_it("object", sha1
))
125 obj
= parse_object(sha1
);
127 return error("Unable to parse object %s", sha1_to_hex(sha1
));
128 if (obj
->type
== commit_type
)
129 return process_commit(sha1
);
130 if (obj
->type
== tree_type
)
131 return process_tree(sha1
);
132 if (obj
->type
== blob_type
)
134 if (obj
->type
== tag_type
)
135 return process_tag(sha1
);
136 return error("Unable to determine requirement of type %s for %s",
137 obj
->type
, sha1_to_hex(sha1
));
140 static int interpret_target(char *target
, unsigned char *sha1
)
142 if (!get_sha1_hex(target
, sha1
))
144 if (!check_ref_format(target
)) {
145 if (!fetch_ref(target
, sha1
)) {
153 int pull(char *target
)
155 unsigned char sha1
[20];
158 if (write_ref
&& current_ref
) {
159 fd
= lock_ref_sha1(write_ref
, current_ref
);
164 if (interpret_target(target
, sha1
))
165 return error("Could not interpret %s as something to pull",
167 if (process_unknown(sha1
))
172 write_ref_sha1(write_ref
, fd
, sha1
);
174 write_ref_sha1_unlocked(write_ref
, sha1
);