[PATCH] diffcore-break.c: various fixes.
[git.git] / pull.c
blobcd77738ac62be17e7382bc3b368e686f11f7098d
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 int get_delta = 1;
10 int get_all = 0;
11 int get_verbosely = 0;
12 static unsigned char current_commit_sha1[20];
14 static const char commitS[] = "commit";
15 static const char treeS[] = "tree";
16 static const char blobS[] = "blob";
18 void pull_say(const char *fmt, const char *hex) {
19 if (get_verbosely)
20 fprintf(stderr, fmt, hex);
23 static void report_missing(const char *what, const unsigned char *missing)
25 char missing_hex[41];
27 strcpy(missing_hex, sha1_to_hex(missing));;
28 fprintf(stderr,
29 "Cannot obtain needed %s %s\nwhile processing commit %s.\n",
30 what, missing_hex, sha1_to_hex(current_commit_sha1));
33 static int make_sure_we_have_it(const char *what, unsigned char *sha1)
35 int status;
36 if (has_sha1_file(sha1))
37 return 0;
38 status = fetch(sha1);
39 if (status && what)
40 report_missing(what, sha1);
41 if (get_delta) {
42 char delta_sha1[20];
43 status = sha1_delta_base(sha1, delta_sha1);
44 if (0 < status)
45 status = make_sure_we_have_it(what, delta_sha1);
47 return status;
50 static int process_tree(unsigned char *sha1)
52 struct tree *tree = lookup_tree(sha1);
53 struct tree_entry_list *entries;
55 if (parse_tree(tree))
56 return -1;
58 for (entries = tree->entries; entries; entries = entries->next) {
59 const char *what = entries->directory ? treeS : blobS;
60 if (make_sure_we_have_it(what, entries->item.tree->object.sha1))
61 return -1;
62 if (entries->directory) {
63 if (process_tree(entries->item.tree->object.sha1))
64 return -1;
67 return 0;
70 static int process_commit(unsigned char *sha1)
72 struct commit *obj = lookup_commit(sha1);
74 if (make_sure_we_have_it(commitS, sha1))
75 return -1;
77 if (parse_commit(obj))
78 return -1;
80 if (get_tree) {
81 if (make_sure_we_have_it(treeS, obj->tree->object.sha1))
82 return -1;
83 if (process_tree(obj->tree->object.sha1))
84 return -1;
85 if (!get_all)
86 get_tree = 0;
88 if (get_history) {
89 struct commit_list *parents = obj->parents;
90 for (; parents; parents = parents->next) {
91 if (has_sha1_file(parents->item->object.sha1))
92 continue;
93 if (make_sure_we_have_it(NULL,
94 parents->item->object.sha1)) {
95 /* The server might not have it, and
96 * we don't mind.
98 continue;
100 if (process_commit(parents->item->object.sha1))
101 return -1;
102 memcpy(current_commit_sha1, sha1, 20);
105 return 0;
108 int pull(char *target)
110 int retval;
111 unsigned char sha1[20];
112 retval = get_sha1_hex(target, sha1);
113 if (retval)
114 return retval;
115 retval = make_sure_we_have_it(commitS, sha1);
116 if (retval)
117 return retval;
118 memcpy(current_commit_sha1, sha1, 20);
119 return process_commit(sha1);