[PATCH] diffcore-break.c: various fixes.
[git/git.git] / pull.c
CommitLineData
4250a5e5
DB
1#include "pull.h"
2
3#include "cache.h"
4#include "commit.h"
5#include "tree.h"
6
7int get_tree = 0;
8int get_history = 0;
4a62b619 9int get_delta = 1;
4250a5e5 10int get_all = 0;
e78d9772 11int get_verbosely = 0;
b2d62f16 12static unsigned char current_commit_sha1[20];
4250a5e5 13
b2d62f16
JH
14static const char commitS[] = "commit";
15static const char treeS[] = "tree";
16static const char blobS[] = "blob";
17
e78d9772
JH
18void pull_say(const char *fmt, const char *hex) {
19 if (get_verbosely)
20 fprintf(stderr, fmt, hex);
21}
22
b2d62f16
JH
23static void report_missing(const char *what, const unsigned char *missing)
24{
25 char missing_hex[41];
26
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));
31}
32
33static int make_sure_we_have_it(const char *what, unsigned char *sha1)
ee4f439f 34{
b2d62f16 35 int status;
ee4f439f
JH
36 if (has_sha1_file(sha1))
37 return 0;
b2d62f16
JH
38 status = fetch(sha1);
39 if (status && what)
40 report_missing(what, sha1);
4a62b619
JH
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);
46 }
b2d62f16 47 return status;
ee4f439f
JH
48}
49
4250a5e5
DB
50static int process_tree(unsigned char *sha1)
51{
52 struct tree *tree = lookup_tree(sha1);
53 struct tree_entry_list *entries;
54
55 if (parse_tree(tree))
56 return -1;
57
58 for (entries = tree->entries; entries; entries = entries->next) {
b2d62f16
JH
59 const char *what = entries->directory ? treeS : blobS;
60 if (make_sure_we_have_it(what, entries->item.tree->object.sha1))
4250a5e5
DB
61 return -1;
62 if (entries->directory) {
63 if (process_tree(entries->item.tree->object.sha1))
64 return -1;
65 }
66 }
67 return 0;
68}
69
70static int process_commit(unsigned char *sha1)
71{
72 struct commit *obj = lookup_commit(sha1);
73
b2d62f16 74 if (make_sure_we_have_it(commitS, sha1))
4250a5e5
DB
75 return -1;
76
77 if (parse_commit(obj))
78 return -1;
79
80 if (get_tree) {
b2d62f16 81 if (make_sure_we_have_it(treeS, obj->tree->object.sha1))
4250a5e5
DB
82 return -1;
83 if (process_tree(obj->tree->object.sha1))
84 return -1;
85 if (!get_all)
86 get_tree = 0;
87 }
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;
b2d62f16
JH
93 if (make_sure_we_have_it(NULL,
94 parents->item->object.sha1)) {
4250a5e5
DB
95 /* The server might not have it, and
96 * we don't mind.
97 */
98 continue;
99 }
100 if (process_commit(parents->item->object.sha1))
101 return -1;
b2d62f16 102 memcpy(current_commit_sha1, sha1, 20);
4250a5e5
DB
103 }
104 }
105 return 0;
106}
107
108int pull(char *target)
109{
110 int retval;
111 unsigned char sha1[20];
112 retval = get_sha1_hex(target, sha1);
113 if (retval)
114 return retval;
b2d62f16 115 retval = make_sure_we_have_it(commitS, sha1);
4250a5e5
DB
116 if (retval)
117 return retval;
b2d62f16 118 memcpy(current_commit_sha1, sha1, 20);
4250a5e5
DB
119 return process_commit(sha1);
120}