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