[PATCH] Move git_author_info and git_commiter_info to ident.c
[git/repo.git] / pull.c
blobed3078e3b27c62c07558fd94f339801cbd685593
1 #include "pull.h"
3 #include "cache.h"
4 #include "commit.h"
5 #include "tree.h"
6 #include "tag.h"
7 #include "blob.h"
8 #include "refs.h"
10 const char *write_ref = NULL;
12 const unsigned char *current_ref = NULL;
14 int get_tree = 0;
15 int get_history = 0;
16 int get_all = 0;
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) {
25 if (get_verbosely)
26 fprintf(stderr, fmt, hex);
29 static void report_missing(const char *what, const unsigned char *missing)
31 char missing_hex[41];
33 strcpy(missing_hex, sha1_to_hex(missing));;
34 fprintf(stderr,
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)
41 int status = 0;
43 if (!has_sha1_file(sha1)) {
44 status = fetch(sha1);
45 if (status && what)
46 report_missing(what, sha1);
48 return status;
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;
58 if (parse_tree(tree))
59 return -1;
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))
64 return -1;
65 if (entries->directory) {
66 if (process_tree(entries->item.tree->object.sha1))
67 return -1;
70 return 0;
73 static int process_commit(unsigned char *sha1)
75 struct commit *obj = lookup_commit(sha1);
77 if (make_sure_we_have_it(commitS, sha1))
78 return -1;
80 if (parse_commit(obj))
81 return -1;
83 if (get_tree) {
84 if (make_sure_we_have_it(treeS, obj->tree->object.sha1))
85 return -1;
86 if (process_tree(obj->tree->object.sha1))
87 return -1;
88 if (!get_all)
89 get_tree = 0;
91 if (get_history) {
92 struct commit_list *parents = obj->parents;
93 for (; parents; parents = parents->next) {
94 if (has_sha1_file(parents->item->object.sha1))
95 continue;
96 if (make_sure_we_have_it(NULL,
97 parents->item->object.sha1)) {
98 /* The server might not have it, and
99 * we don't mind.
101 continue;
103 if (process_commit(parents->item->object.sha1))
104 return -1;
105 memcpy(current_commit_sha1, sha1, 20);
108 return 0;
111 static int process_tag(unsigned char *sha1)
113 struct tag *obj = lookup_tag(sha1);
115 if (parse_tag(obj))
116 return -1;
117 return process_unknown(obj->tagged->sha1);
120 static int process_unknown(unsigned char *sha1)
122 struct object *obj;
123 if (make_sure_we_have_it("object", sha1))
124 return -1;
125 obj = parse_object(sha1);
126 if (!obj)
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)
133 return 0;
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))
143 return 0;
144 if (!check_ref_format(target)) {
145 if (!fetch_ref(target, sha1)) {
146 return 0;
149 return -1;
153 int pull(char *target)
155 unsigned char sha1[20];
156 int fd = -1;
158 if (write_ref && current_ref) {
159 fd = lock_ref_sha1(write_ref, current_ref);
160 if (fd < 0)
161 return -1;
164 if (interpret_target(target, sha1))
165 return error("Could not interpret %s as something to pull",
166 target);
167 if (process_unknown(sha1))
168 return -1;
170 if (write_ref) {
171 if (current_ref) {
172 write_ref_sha1(write_ref, fd, sha1);
173 } else {
174 write_ref_sha1_unlocked(write_ref, sha1);
177 return 0;