diff 'rename' format change.
[git/mjg.git] / pull.c
blobf4f1d8fcd38aa70a971c82e08fc08942f76f19bc
1 #include "pull.h"
3 #include "cache.h"
4 #include "commit.h"
5 #include "tree.h"
7 int get_tree = 0;
8 int get_history = 0;
9 /* 1 means "get delta", 2 means "really check delta harder */
10 int get_delta = 1;
11 int get_all = 0;
12 int get_verbosely = 0;
13 static unsigned char current_commit_sha1[20];
15 static const char commitS[] = "commit";
16 static const char treeS[] = "tree";
17 static const char blobS[] = "blob";
19 void pull_say(const char *fmt, const char *hex) {
20 if (get_verbosely)
21 fprintf(stderr, fmt, hex);
24 static void report_missing(const char *what, const unsigned char *missing)
26 char missing_hex[41];
28 strcpy(missing_hex, sha1_to_hex(missing));;
29 fprintf(stderr,
30 "Cannot obtain needed %s %s\nwhile processing commit %s.\n",
31 what, missing_hex, sha1_to_hex(current_commit_sha1));
34 static int make_sure_we_have_it(const char *what, unsigned char *sha1)
36 int status = 0;
38 if (!has_sha1_file(sha1)) {
39 status = fetch(sha1);
40 if (status && what)
41 report_missing(what, sha1);
43 else if (get_delta < 2)
44 return 0;
46 if (get_delta) {
47 char delta_sha1[20];
48 status = sha1_delta_base(sha1, delta_sha1);
49 if (0 < status)
50 status = make_sure_we_have_it(what, delta_sha1);
52 return status;
55 static int process_tree(unsigned char *sha1)
57 struct tree *tree = lookup_tree(sha1);
58 struct tree_entry_list *entries;
60 if (parse_tree(tree))
61 return -1;
63 for (entries = tree->entries; entries; entries = entries->next) {
64 const char *what = entries->directory ? treeS : blobS;
65 if (make_sure_we_have_it(what, entries->item.tree->object.sha1))
66 return -1;
67 if (entries->directory) {
68 if (process_tree(entries->item.tree->object.sha1))
69 return -1;
72 return 0;
75 static int process_commit(unsigned char *sha1)
77 struct commit *obj = lookup_commit(sha1);
79 if (make_sure_we_have_it(commitS, sha1))
80 return -1;
82 if (parse_commit(obj))
83 return -1;
85 if (get_tree) {
86 if (make_sure_we_have_it(treeS, obj->tree->object.sha1))
87 return -1;
88 if (process_tree(obj->tree->object.sha1))
89 return -1;
90 if (!get_all)
91 get_tree = 0;
93 if (get_history) {
94 struct commit_list *parents = obj->parents;
95 for (; parents; parents = parents->next) {
96 if (has_sha1_file(parents->item->object.sha1))
97 continue;
98 if (make_sure_we_have_it(NULL,
99 parents->item->object.sha1)) {
100 /* The server might not have it, and
101 * we don't mind.
103 continue;
105 if (process_commit(parents->item->object.sha1))
106 return -1;
107 memcpy(current_commit_sha1, sha1, 20);
110 return 0;
113 int pull(char *target)
115 int retval;
116 unsigned char sha1[20];
117 retval = get_sha1_hex(target, sha1);
118 if (retval)
119 return retval;
120 retval = make_sure_we_have_it(commitS, sha1);
121 if (retval)
122 return retval;
123 memcpy(current_commit_sha1, sha1, 20);
124 return process_commit(sha1);