upload-pack: fix thinko in common-commit finder code.
[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"
794f9fe7 6#include "commit.h"
def88e9a 7
960deccb 8static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
def88e9a 9
794f9fe7 10#define THEY_HAVE (1U << 0)
6b32884a
JH
11#define MAX_HAS 256
12#define MAX_NEEDS 256
0f8fdc39 13static int nr_has = 0, nr_needs = 0, multi_ack = 0;
fb9040cc
LT
14static unsigned char has_sha1[MAX_HAS][20];
15static unsigned char needs_sha1[MAX_NEEDS][20];
960deccb
PA
16static unsigned int timeout = 0;
17
18static void reset_timeout(void)
19{
20 alarm(timeout);
21}
fb9040cc 22
75bfc6c2
LT
23static int strip(char *line, int len)
24{
25 if (len && line[len-1] == '\n')
26 line[--len] = 0;
27 return len;
28}
29
fb9040cc
LT
30static void create_pack_file(void)
31{
75bfc6c2
LT
32 int fd[2];
33 pid_t pid;
34
35 if (pipe(fd) < 0)
36 die("git-upload-pack: unable to create pipe");
37 pid = fork();
38 if (pid < 0)
39 die("git-upload-pack: unable to fork git-rev-list");
40
41 if (!pid) {
42 int i;
e091eb93
JH
43 int args;
44 char **argv;
45 char *buf;
46 char **p;
47
48 if (MAX_NEEDS <= nr_needs)
49 args = nr_has + 10;
50 else
51 args = nr_has + nr_needs + 5;
52 argv = xmalloc(args * sizeof(char *));
53 buf = xmalloc(args * 45);
54 p = argv;
75bfc6c2
LT
55
56 dup2(fd[1], 1);
57 close(0);
58 close(fd[0]);
59 close(fd[1]);
60 *p++ = "git-rev-list";
61 *p++ = "--objects";
e091eb93
JH
62 if (MAX_NEEDS <= nr_needs)
63 *p++ = "--all";
64 else {
65 for (i = 0; i < nr_needs; i++) {
66 *p++ = buf;
67 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
68 buf += 41;
69 }
75bfc6c2
LT
70 }
71 for (i = 0; i < nr_has; i++) {
72 *p++ = buf;
73 *buf++ = '^';
74 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
75 buf += 41;
76 }
77 *p++ = NULL;
78 execvp("git-rev-list", argv);
79 die("git-upload-pack: unable to exec git-rev-list");
80 }
81 dup2(fd[0], 0);
82 close(fd[0]);
83 close(fd[1]);
84 execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL);
85 die("git-upload-pack: unable to exec git-pack-objects");
fb9040cc
LT
86}
87
def88e9a
LT
88static int got_sha1(char *hex, unsigned char *sha1)
89{
90 if (get_sha1_hex(hex, sha1))
91 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
fb9040cc
LT
92 if (!has_sha1_file(sha1))
93 return 0;
794f9fe7
JS
94 if (nr_has < MAX_HAS) {
95 struct object *o = lookup_object(sha1);
7efc8e43
JH
96 if (!(o && o->parsed))
97 o = parse_object(sha1);
98 if (!o)
794f9fe7
JS
99 die("oops (%s)", sha1_to_hex(sha1));
100 if (o->type == commit_type) {
101 struct commit_list *parents;
102 if (o->flags & THEY_HAVE)
103 return 0;
104 o->flags |= THEY_HAVE;
105 for (parents = ((struct commit*)o)->parents;
106 parents;
107 parents = parents->next)
108 parents->item->object.flags |= THEY_HAVE;
109 }
110 memcpy(has_sha1[nr_has++], sha1, 20);
fb9040cc
LT
111 }
112 return 1;
def88e9a
LT
113}
114
115static int get_common_commits(void)
116{
117 static char line[1000];
118 unsigned char sha1[20];
119 int len;
120
794f9fe7
JS
121 track_object_refs = 0;
122 save_commit_buffer = 0;
123
def88e9a
LT
124 for(;;) {
125 len = packet_read_line(0, line, sizeof(line));
960deccb 126 reset_timeout();
def88e9a
LT
127
128 if (!len) {
0f8fdc39
JS
129 if (multi_ack || nr_has == 0)
130 packet_write(1, "NAK\n");
def88e9a
LT
131 continue;
132 }
75bfc6c2 133 len = strip(line, len);
def88e9a 134 if (!strncmp(line, "have ", 5)) {
0f8fdc39
JS
135 if (got_sha1(line+5, sha1) &&
136 (multi_ack || nr_has == 1))
137 packet_write(1, "ACK %s%s\n",
138 sha1_to_hex(sha1),
139 multi_ack && nr_has < MAX_HAS ?
140 " continue" : "");
def88e9a
LT
141 continue;
142 }
143 if (!strcmp(line, "done")) {
0f8fdc39
JS
144 if (nr_has > 0)
145 return 0;
def88e9a
LT
146 packet_write(1, "NAK\n");
147 return -1;
148 }
149 die("git-upload-pack: expected SHA1 list, got '%s'", line);
150 }
def88e9a
LT
151}
152
fb9040cc
LT
153static int receive_needs(void)
154{
155 static char line[1000];
156 int len, needs;
157
158 needs = 0;
159 for (;;) {
e091eb93 160 unsigned char dummy[20], *sha1_buf;
fb9040cc 161 len = packet_read_line(0, line, sizeof(line));
960deccb 162 reset_timeout();
fb9040cc
LT
163 if (!len)
164 return needs;
165
e091eb93
JH
166 sha1_buf = dummy;
167 if (needs == MAX_NEEDS) {
168 fprintf(stderr,
169 "warning: supporting only a max of %d requests. "
170 "sending everything instead.\n",
171 MAX_NEEDS);
172 }
173 else if (needs < MAX_NEEDS)
174 sha1_buf = needs_sha1[needs];
175
176 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
177 die("git-upload-pack: protocol error, "
178 "expected to get sha, not '%s'", line);
0f8fdc39
JS
179
180 if (strstr(line+45, "multi_ack"))
181 multi_ack = 1;
182
fb9040cc
LT
183 needs++;
184 }
185}
186
def88e9a
LT
187static int send_ref(const char *refname, const unsigned char *sha1)
188{
f6b42a81
JH
189 struct object *o = parse_object(sha1);
190
def88e9a 191 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
f6b42a81
JH
192 if (o->type == tag_type) {
193 o = deref_tag(o);
194 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
195 }
def88e9a
LT
196 return 0;
197}
198
199static int upload_pack(void)
200{
960deccb 201 reset_timeout();
723c31fe 202 head_ref(send_ref);
def88e9a
LT
203 for_each_ref(send_ref);
204 packet_flush(1);
fb9040cc
LT
205 nr_needs = receive_needs();
206 if (!nr_needs)
207 return 0;
def88e9a 208 get_common_commits();
fb9040cc 209 create_pack_file();
def88e9a
LT
210 return 0;
211}
212
213int main(int argc, char **argv)
214{
215 const char *dir;
960deccb
PA
216 int i;
217 int strict = 0;
218
219 for (i = 1; i < argc; i++) {
220 char *arg = argv[i];
221
222 if (arg[0] != '-')
223 break;
224 if (!strcmp(arg, "--strict")) {
225 strict = 1;
226 continue;
227 }
228 if (!strncmp(arg, "--timeout=", 10)) {
229 timeout = atoi(arg+10);
230 continue;
231 }
232 if (!strcmp(arg, "--")) {
233 i++;
234 break;
235 }
236 }
237
238 if (i != argc-1)
def88e9a 239 usage(upload_pack_usage);
960deccb 240 dir = argv[i];
113b9475
LT
241
242 /* chdir to the directory. If that fails, try appending ".git" */
243 if (chdir(dir) < 0) {
960deccb 244 if (strict || chdir(mkpath("%s.git", dir)) < 0)
113b9475
LT
245 die("git-upload-pack unable to chdir to %s", dir);
246 }
960deccb
PA
247 if (!strict)
248 chdir(".git");
249
def88e9a
LT
250 if (access("objects", X_OK) || access("refs", X_OK))
251 die("git-upload-pack: %s doesn't seem to be a git archive", dir);
960deccb 252
e72a7d45 253 putenv("GIT_DIR=.");
def88e9a
LT
254 upload_pack();
255 return 0;
256}