Do not fall back on vi on dumb terminals.
[git/git-svn.git] / upload-pack.c
blobd1980556ca5b9c62a76129907cc511cd5c0c6c83
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include "tag.h"
5 #include "object.h"
6 #include "commit.h"
7 #include "exec_cmd.h"
9 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
11 #define THEY_HAVE (1U << 0)
12 #define OUR_REF (1U << 1)
13 #define WANTED (1U << 2)
14 #define MAX_HAS 256
15 #define MAX_NEEDS 256
16 static int nr_has = 0, nr_needs = 0, multi_ack = 0, nr_our_refs = 0;
17 static unsigned char has_sha1[MAX_HAS][20];
18 static unsigned char needs_sha1[MAX_NEEDS][20];
19 static unsigned int timeout = 0;
21 static void reset_timeout(void)
23 alarm(timeout);
26 static int strip(char *line, int len)
28 if (len && line[len-1] == '\n')
29 line[--len] = 0;
30 return len;
33 static void create_pack_file(void)
35 int fd[2];
36 pid_t pid;
37 int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
39 if (pipe(fd) < 0)
40 die("git-upload-pack: unable to create pipe");
41 pid = fork();
42 if (pid < 0)
43 die("git-upload-pack: unable to fork git-rev-list");
45 if (!pid) {
46 int i;
47 int args;
48 char **argv;
49 char *buf;
50 char **p;
52 if (create_full_pack)
53 args = 10;
54 else
55 args = nr_has + nr_needs + 5;
56 argv = xmalloc(args * sizeof(char *));
57 buf = xmalloc(args * 45);
58 p = argv;
60 dup2(fd[1], 1);
61 close(0);
62 close(fd[0]);
63 close(fd[1]);
64 *p++ = "rev-list";
65 *p++ = "--objects";
66 if (create_full_pack || MAX_NEEDS <= nr_needs)
67 *p++ = "--all";
68 else {
69 for (i = 0; i < nr_needs; i++) {
70 *p++ = buf;
71 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
72 buf += 41;
75 if (!create_full_pack)
76 for (i = 0; i < nr_has; i++) {
77 *p++ = buf;
78 *buf++ = '^';
79 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
80 buf += 41;
82 *p++ = NULL;
83 execv_git_cmd(argv);
84 die("git-upload-pack: unable to exec git-rev-list");
86 dup2(fd[0], 0);
87 close(fd[0]);
88 close(fd[1]);
89 execl_git_cmd("pack-objects", "--stdout", NULL);
90 die("git-upload-pack: unable to exec git-pack-objects");
93 static int got_sha1(char *hex, unsigned char *sha1)
95 if (get_sha1_hex(hex, sha1))
96 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
97 if (!has_sha1_file(sha1))
98 return 0;
99 if (nr_has < MAX_HAS) {
100 struct object *o = lookup_object(sha1);
101 if (!(o && o->parsed))
102 o = parse_object(sha1);
103 if (!o)
104 die("oops (%s)", sha1_to_hex(sha1));
105 if (o->type == commit_type) {
106 struct commit_list *parents;
107 if (o->flags & THEY_HAVE)
108 return 0;
109 o->flags |= THEY_HAVE;
110 for (parents = ((struct commit*)o)->parents;
111 parents;
112 parents = parents->next)
113 parents->item->object.flags |= THEY_HAVE;
115 memcpy(has_sha1[nr_has++], sha1, 20);
117 return 1;
120 static int get_common_commits(void)
122 static char line[1000];
123 unsigned char sha1[20], last_sha1[20];
124 int len;
126 track_object_refs = 0;
127 save_commit_buffer = 0;
129 for(;;) {
130 len = packet_read_line(0, line, sizeof(line));
131 reset_timeout();
133 if (!len) {
134 if (nr_has == 0 || multi_ack)
135 packet_write(1, "NAK\n");
136 continue;
138 len = strip(line, len);
139 if (!strncmp(line, "have ", 5)) {
140 if (got_sha1(line+5, sha1) &&
141 (multi_ack || nr_has == 1)) {
142 if (nr_has >= MAX_HAS)
143 multi_ack = 0;
144 packet_write(1, "ACK %s%s\n",
145 sha1_to_hex(sha1),
146 multi_ack ? " continue" : "");
147 if (multi_ack)
148 memcpy(last_sha1, sha1, 20);
150 continue;
152 if (!strcmp(line, "done")) {
153 if (nr_has > 0) {
154 if (multi_ack)
155 packet_write(1, "ACK %s\n",
156 sha1_to_hex(last_sha1));
157 return 0;
159 packet_write(1, "NAK\n");
160 return -1;
162 die("git-upload-pack: expected SHA1 list, got '%s'", line);
166 static int receive_needs(void)
168 static char line[1000];
169 int len, needs;
171 needs = 0;
172 for (;;) {
173 struct object *o;
174 unsigned char dummy[20], *sha1_buf;
175 len = packet_read_line(0, line, sizeof(line));
176 reset_timeout();
177 if (!len)
178 return needs;
180 sha1_buf = dummy;
181 if (needs == MAX_NEEDS) {
182 fprintf(stderr,
183 "warning: supporting only a max of %d requests. "
184 "sending everything instead.\n",
185 MAX_NEEDS);
187 else if (needs < MAX_NEEDS)
188 sha1_buf = needs_sha1[needs];
190 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
191 die("git-upload-pack: protocol error, "
192 "expected to get sha, not '%s'", line);
193 if (strstr(line+45, "multi_ack"))
194 multi_ack = 1;
196 /* We have sent all our refs already, and the other end
197 * should have chosen out of them; otherwise they are
198 * asking for nonsense.
200 * Hmph. We may later want to allow "want" line that
201 * asks for something like "master~10" (symbolic)...
202 * would it make sense? I don't know.
204 o = lookup_object(sha1_buf);
205 if (!o || !(o->flags & OUR_REF))
206 die("git-upload-pack: not our ref %s", line+5);
207 if (!(o->flags & WANTED)) {
208 o->flags |= WANTED;
209 needs++;
214 static int send_ref(const char *refname, const unsigned char *sha1)
216 static char *capabilities = "multi_ack";
217 struct object *o = parse_object(sha1);
219 if (capabilities)
220 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
221 0, capabilities);
222 else
223 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
224 capabilities = NULL;
225 if (!(o->flags & OUR_REF)) {
226 o->flags |= OUR_REF;
227 nr_our_refs++;
229 if (o->type == tag_type) {
230 o = deref_tag(o, refname, 0);
231 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
233 return 0;
236 static int upload_pack(void)
238 reset_timeout();
239 head_ref(send_ref);
240 for_each_ref(send_ref);
241 packet_flush(1);
242 nr_needs = receive_needs();
243 if (!nr_needs)
244 return 0;
245 get_common_commits();
246 create_pack_file();
247 return 0;
250 int main(int argc, char **argv)
252 char *dir;
253 int i;
254 int strict = 0;
256 for (i = 1; i < argc; i++) {
257 char *arg = argv[i];
259 if (arg[0] != '-')
260 break;
261 if (!strcmp(arg, "--strict")) {
262 strict = 1;
263 continue;
265 if (!strncmp(arg, "--timeout=", 10)) {
266 timeout = atoi(arg+10);
267 continue;
269 if (!strcmp(arg, "--")) {
270 i++;
271 break;
275 if (i != argc-1)
276 usage(upload_pack_usage);
277 dir = argv[i];
279 if (!enter_repo(dir, strict))
280 die("'%s': unable to chdir or not a git archive", dir);
282 upload_pack();
283 return 0;