8 const char *write_ref
= NULL
;
10 const unsigned char *current_ref
= NULL
;
14 /* 1 means "get delta", 2 means "really check delta harder */
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
);
48 else if (get_delta
< 2)
53 status
= sha1_delta_base(sha1
, delta_sha1
);
55 status
= make_sure_we_have_it(what
, delta_sha1
);
60 static int process_tree(unsigned char *sha1
)
62 struct tree
*tree
= lookup_tree(sha1
);
63 struct tree_entry_list
*entries
;
68 for (entries
= tree
->entries
; entries
; entries
= entries
->next
) {
69 const char *what
= entries
->directory
? treeS
: blobS
;
70 if (make_sure_we_have_it(what
, entries
->item
.tree
->object
.sha1
))
72 if (entries
->directory
) {
73 if (process_tree(entries
->item
.tree
->object
.sha1
))
80 static int process_commit(unsigned char *sha1
)
82 struct commit
*obj
= lookup_commit(sha1
);
84 if (make_sure_we_have_it(commitS
, sha1
))
87 if (parse_commit(obj
))
91 if (make_sure_we_have_it(treeS
, obj
->tree
->object
.sha1
))
93 if (process_tree(obj
->tree
->object
.sha1
))
99 struct commit_list
*parents
= obj
->parents
;
100 for (; parents
; parents
= parents
->next
) {
101 if (has_sha1_file(parents
->item
->object
.sha1
))
103 if (make_sure_we_have_it(NULL
,
104 parents
->item
->object
.sha1
)) {
105 /* The server might not have it, and
110 if (process_commit(parents
->item
->object
.sha1
))
112 memcpy(current_commit_sha1
, sha1
, 20);
118 static int interpret_target(char *target
, unsigned char *sha1
)
120 if (!get_sha1_hex(target
, sha1
))
122 if (!check_ref_format(target
)) {
123 if (!fetch_ref(target
, sha1
)) {
131 int pull(char *target
)
133 unsigned char sha1
[20];
136 if (write_ref
&& current_ref
) {
137 fd
= lock_ref_sha1(write_ref
, current_ref
);
142 if (interpret_target(target
, sha1
))
143 return error("Could not interpret %s as something to pull",
145 if (process_commit(sha1
))
150 write_ref_sha1(write_ref
, fd
, sha1
);
152 write_ref_sha1_unlocked(write_ref
, sha1
);