octopus: allow criss-cross and clarify the message when it rejects
[git/git.git] / upload-pack.c
CommitLineData
def88e9a
LT
1#include "cache.h"
2#include "refs.h"
3#include "pkt-line.h"
f6b42a81
JH
4#include "tag.h"
5#include "object.h"
f0243f26 6#include "commit.h"
def88e9a 7
960deccb 8static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
def88e9a 9
f0243f26 10#define THEY_HAVE (1U << 0)
565ebbf7
JH
11#define OUR_REF (1U << 1)
12#define WANTED (1U << 2)
6b32884a
JH
13#define MAX_HAS 256
14#define MAX_NEEDS 256
1bd8c8f0 15static int nr_has = 0, nr_needs = 0, multi_ack = 0, nr_our_refs = 0;
fb9040cc
LT
16static unsigned char has_sha1[MAX_HAS][20];
17static unsigned char needs_sha1[MAX_NEEDS][20];
960deccb
PA
18static unsigned int timeout = 0;
19
20static void reset_timeout(void)
21{
22 alarm(timeout);
23}
fb9040cc 24
75bfc6c2
LT
25static int strip(char *line, int len)
26{
27 if (len && line[len-1] == '\n')
28 line[--len] = 0;
29 return len;
30}
31
fb9040cc
LT
32static void create_pack_file(void)
33{
75bfc6c2
LT
34 int fd[2];
35 pid_t pid;
565ebbf7 36 int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
75bfc6c2
LT
37
38 if (pipe(fd) < 0)
39 die("git-upload-pack: unable to create pipe");
40 pid = fork();
41 if (pid < 0)
42 die("git-upload-pack: unable to fork git-rev-list");
43
44 if (!pid) {
45 int i;
e091eb93
JH
46 int args;
47 char **argv;
48 char *buf;
49 char **p;
50
565ebbf7
JH
51 if (create_full_pack)
52 args = 10;
e091eb93
JH
53 else
54 args = nr_has + nr_needs + 5;
55 argv = xmalloc(args * sizeof(char *));
56 buf = xmalloc(args * 45);
57 p = argv;
75bfc6c2
LT
58
59 dup2(fd[1], 1);
60 close(0);
61 close(fd[0]);
62 close(fd[1]);
63 *p++ = "git-rev-list";
64 *p++ = "--objects";
b5c367f7 65 if (create_full_pack || MAX_NEEDS <= nr_needs)
e091eb93
JH
66 *p++ = "--all";
67 else {
68 for (i = 0; i < nr_needs; i++) {
69 *p++ = buf;
70 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
71 buf += 41;
72 }
75bfc6c2 73 }
b5c367f7
JS
74 if (!create_full_pack)
75 for (i = 0; i < nr_has; i++) {
76 *p++ = buf;
77 *buf++ = '^';
78 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
79 buf += 41;
80 }
75bfc6c2
LT
81 *p++ = NULL;
82 execvp("git-rev-list", argv);
83 die("git-upload-pack: unable to exec git-rev-list");
84 }
85 dup2(fd[0], 0);
86 close(fd[0]);
87 close(fd[1]);
88 execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL);
89 die("git-upload-pack: unable to exec git-pack-objects");
fb9040cc
LT
90}
91
def88e9a
LT
92static int got_sha1(char *hex, unsigned char *sha1)
93{
94 if (get_sha1_hex(hex, sha1))
95 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
fb9040cc
LT
96 if (!has_sha1_file(sha1))
97 return 0;
f0243f26
JS
98 if (nr_has < MAX_HAS) {
99 struct object *o = lookup_object(sha1);
100 if (!(o && o->parsed))
101 o = parse_object(sha1);
102 if (!o)
103 die("oops (%s)", sha1_to_hex(sha1));
104 if (o->type == commit_type) {
105 struct commit_list *parents;
106 if (o->flags & THEY_HAVE)
107 return 0;
108 o->flags |= THEY_HAVE;
109 for (parents = ((struct commit*)o)->parents;
110 parents;
111 parents = parents->next)
112 parents->item->object.flags |= THEY_HAVE;
113 }
114 memcpy(has_sha1[nr_has++], sha1, 20);
fb9040cc
LT
115 }
116 return 1;
def88e9a
LT
117}
118
119static int get_common_commits(void)
120{
121 static char line[1000];
1bd8c8f0 122 unsigned char sha1[20], last_sha1[20];
def88e9a
LT
123 int len;
124
f0243f26
JS
125 track_object_refs = 0;
126 save_commit_buffer = 0;
127
def88e9a
LT
128 for(;;) {
129 len = packet_read_line(0, line, sizeof(line));
960deccb 130 reset_timeout();
def88e9a
LT
131
132 if (!len) {
1bd8c8f0
JS
133 if (nr_has == 0 || multi_ack)
134 packet_write(1, "NAK\n");
def88e9a
LT
135 continue;
136 }
75bfc6c2 137 len = strip(line, len);
def88e9a 138 if (!strncmp(line, "have ", 5)) {
1bd8c8f0
JS
139 if (got_sha1(line+5, sha1) &&
140 (multi_ack || nr_has == 1)) {
141 if (nr_has >= MAX_HAS)
142 multi_ack = 0;
143 packet_write(1, "ACK %s%s\n",
144 sha1_to_hex(sha1),
145 multi_ack ? " continue" : "");
146 if (multi_ack)
147 memcpy(last_sha1, sha1, 20);
af2d3aa4 148 }
def88e9a
LT
149 continue;
150 }
151 if (!strcmp(line, "done")) {
1bd8c8f0
JS
152 if (nr_has > 0) {
153 if (multi_ack)
154 packet_write(1, "ACK %s\n",
155 sha1_to_hex(last_sha1));
156 return 0;
157 }
def88e9a
LT
158 packet_write(1, "NAK\n");
159 return -1;
160 }
161 die("git-upload-pack: expected SHA1 list, got '%s'", line);
162 }
def88e9a
LT
163}
164
fb9040cc
LT
165static int receive_needs(void)
166{
167 static char line[1000];
168 int len, needs;
169
170 needs = 0;
171 for (;;) {
565ebbf7 172 struct object *o;
e091eb93 173 unsigned char dummy[20], *sha1_buf;
fb9040cc 174 len = packet_read_line(0, line, sizeof(line));
960deccb 175 reset_timeout();
fb9040cc
LT
176 if (!len)
177 return needs;
178
e091eb93
JH
179 sha1_buf = dummy;
180 if (needs == MAX_NEEDS) {
181 fprintf(stderr,
182 "warning: supporting only a max of %d requests. "
183 "sending everything instead.\n",
184 MAX_NEEDS);
185 }
186 else if (needs < MAX_NEEDS)
187 sha1_buf = needs_sha1[needs];
188
189 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
190 die("git-upload-pack: protocol error, "
191 "expected to get sha, not '%s'", line);
1bd8c8f0
JS
192 if (strstr(line+45, "multi_ack"))
193 multi_ack = 1;
565ebbf7
JH
194
195 /* We have sent all our refs already, and the other end
196 * should have chosen out of them; otherwise they are
197 * asking for nonsense.
198 *
199 * Hmph. We may later want to allow "want" line that
200 * asks for something like "master~10" (symbolic)...
201 * would it make sense? I don't know.
202 */
203 o = lookup_object(sha1_buf);
204 if (!o || !(o->flags & OUR_REF))
205 die("git-upload-pack: not our ref %s", line+5);
206 if (!(o->flags & WANTED)) {
207 o->flags |= WANTED;
208 needs++;
209 }
fb9040cc
LT
210 }
211}
212
def88e9a
LT
213static int send_ref(const char *refname, const unsigned char *sha1)
214{
1f5881bb 215 static char *capabilities = "multi_ack";
f6b42a81
JH
216 struct object *o = parse_object(sha1);
217
1f5881bb
JS
218 if (capabilities)
219 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
220 0, capabilities);
221 else
222 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
223 capabilities = NULL;
565ebbf7
JH
224 if (!(o->flags & OUR_REF)) {
225 o->flags |= OUR_REF;
226 nr_our_refs++;
227 }
f6b42a81 228 if (o->type == tag_type) {
9534f40b 229 o = deref_tag(o, refname, 0);
f6b42a81
JH
230 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
231 }
def88e9a
LT
232 return 0;
233}
234
235static int upload_pack(void)
236{
960deccb 237 reset_timeout();
723c31fe 238 head_ref(send_ref);
def88e9a
LT
239 for_each_ref(send_ref);
240 packet_flush(1);
fb9040cc
LT
241 nr_needs = receive_needs();
242 if (!nr_needs)
243 return 0;
def88e9a 244 get_common_commits();
fb9040cc 245 create_pack_file();
def88e9a
LT
246 return 0;
247}
248
249int main(int argc, char **argv)
250{
8d630132 251 char *dir;
960deccb
PA
252 int i;
253 int strict = 0;
254
255 for (i = 1; i < argc; i++) {
256 char *arg = argv[i];
257
258 if (arg[0] != '-')
259 break;
260 if (!strcmp(arg, "--strict")) {
261 strict = 1;
262 continue;
263 }
264 if (!strncmp(arg, "--timeout=", 10)) {
265 timeout = atoi(arg+10);
266 continue;
267 }
268 if (!strcmp(arg, "--")) {
269 i++;
270 break;
271 }
272 }
273
274 if (i != argc-1)
def88e9a 275 usage(upload_pack_usage);
960deccb 276 dir = argv[i];
113b9475 277
8d630132
AE
278 if (!enter_repo(dir, strict))
279 die("'%s': unable to chdir or not a git archive", dir);
960deccb 280
def88e9a
LT
281 upload_pack();
282 return 0;
283}