Add more generated files to .gitignore
[git/trast.git] / upload-pack.c
blob878254d72f6448ff3a6cfb59650ef9a71d75ddde
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include "tag.h"
5 #include "object.h"
7 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
9 #define OUR_REF (1U << 1)
10 #define WANTED (1U << 2)
11 #define MAX_HAS 256
12 #define MAX_NEEDS 256
13 static int nr_has = 0, nr_needs = 0, nr_our_refs = 0;
14 static unsigned char has_sha1[MAX_HAS][20];
15 static unsigned char needs_sha1[MAX_NEEDS][20];
16 static unsigned int timeout = 0;
18 static void reset_timeout(void)
20 alarm(timeout);
23 static int strip(char *line, int len)
25 if (len && line[len-1] == '\n')
26 line[--len] = 0;
27 return len;
30 static void create_pack_file(void)
32 int fd[2];
33 pid_t pid;
34 int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
36 if (pipe(fd) < 0)
37 die("git-upload-pack: unable to create pipe");
38 pid = fork();
39 if (pid < 0)
40 die("git-upload-pack: unable to fork git-rev-list");
42 if (!pid) {
43 int i;
44 int args;
45 char **argv;
46 char *buf;
47 char **p;
49 if (create_full_pack)
50 args = 10;
51 else
52 args = nr_has + nr_needs + 5;
53 argv = xmalloc(args * sizeof(char *));
54 buf = xmalloc(args * 45);
55 p = argv;
57 dup2(fd[1], 1);
58 close(0);
59 close(fd[0]);
60 close(fd[1]);
61 *p++ = "git-rev-list";
62 *p++ = "--objects";
63 if (create_full_pack || MAX_NEEDS <= nr_needs)
64 *p++ = "--all";
65 else {
66 for (i = 0; i < nr_needs; i++) {
67 *p++ = buf;
68 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
69 buf += 41;
72 if (!create_full_pack)
73 for (i = 0; i < nr_has; i++) {
74 *p++ = buf;
75 *buf++ = '^';
76 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
77 buf += 41;
79 *p++ = NULL;
80 execvp("git-rev-list", argv);
81 die("git-upload-pack: unable to exec git-rev-list");
83 dup2(fd[0], 0);
84 close(fd[0]);
85 close(fd[1]);
86 execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL);
87 die("git-upload-pack: unable to exec git-pack-objects");
90 static int got_sha1(char *hex, unsigned char *sha1)
92 int nr;
93 if (get_sha1_hex(hex, sha1))
94 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
95 if (!has_sha1_file(sha1))
96 return 0;
97 nr = nr_has;
98 if (nr < MAX_HAS) {
99 memcpy(has_sha1[nr], sha1, 20);
100 nr_has = nr+1;
102 return 1;
105 static int get_common_commits(void)
107 static char line[1000];
108 unsigned char sha1[20];
109 int len;
111 for(;;) {
112 len = packet_read_line(0, line, sizeof(line));
113 reset_timeout();
115 if (!len) {
116 packet_write(1, "NAK\n");
117 continue;
119 len = strip(line, len);
120 if (!strncmp(line, "have ", 5)) {
121 if (got_sha1(line+5, sha1)) {
122 packet_write(1, "ACK %s\n", sha1_to_hex(sha1));
123 break;
125 continue;
127 if (!strcmp(line, "done")) {
128 packet_write(1, "NAK\n");
129 return -1;
131 die("git-upload-pack: expected SHA1 list, got '%s'", line);
134 for (;;) {
135 len = packet_read_line(0, line, sizeof(line));
136 reset_timeout();
137 if (!len)
138 continue;
139 len = strip(line, len);
140 if (!strncmp(line, "have ", 5)) {
141 got_sha1(line+5, sha1);
142 continue;
144 if (!strcmp(line, "done"))
145 break;
146 die("git-upload-pack: expected SHA1 list, got '%s'", line);
148 return 0;
151 static int receive_needs(void)
153 static char line[1000];
154 int len, needs;
156 needs = 0;
157 for (;;) {
158 struct object *o;
159 unsigned char dummy[20], *sha1_buf;
160 len = packet_read_line(0, line, sizeof(line));
161 reset_timeout();
162 if (!len)
163 return needs;
165 sha1_buf = dummy;
166 if (needs == MAX_NEEDS) {
167 fprintf(stderr,
168 "warning: supporting only a max of %d requests. "
169 "sending everything instead.\n",
170 MAX_NEEDS);
172 else if (needs < MAX_NEEDS)
173 sha1_buf = needs_sha1[needs];
175 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
176 die("git-upload-pack: protocol error, "
177 "expected to get sha, not '%s'", line);
179 /* We have sent all our refs already, and the other end
180 * should have chosen out of them; otherwise they are
181 * asking for nonsense.
183 * Hmph. We may later want to allow "want" line that
184 * asks for something like "master~10" (symbolic)...
185 * would it make sense? I don't know.
187 o = lookup_object(sha1_buf);
188 if (!o || !(o->flags & OUR_REF))
189 die("git-upload-pack: not our ref %s", line+5);
190 if (!(o->flags & WANTED)) {
191 o->flags |= WANTED;
192 needs++;
197 static int send_ref(const char *refname, const unsigned char *sha1)
199 struct object *o = parse_object(sha1);
201 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
202 if (!(o->flags & OUR_REF)) {
203 o->flags |= OUR_REF;
204 nr_our_refs++;
206 if (o->type == tag_type) {
207 o = deref_tag(o);
208 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
210 return 0;
213 static int upload_pack(void)
215 reset_timeout();
216 head_ref(send_ref);
217 for_each_ref(send_ref);
218 packet_flush(1);
219 nr_needs = receive_needs();
220 if (!nr_needs)
221 return 0;
222 get_common_commits();
223 create_pack_file();
224 return 0;
227 int main(int argc, char **argv)
229 const char *dir;
230 int i;
231 int strict = 0;
233 for (i = 1; i < argc; i++) {
234 char *arg = argv[i];
236 if (arg[0] != '-')
237 break;
238 if (!strcmp(arg, "--strict")) {
239 strict = 1;
240 continue;
242 if (!strncmp(arg, "--timeout=", 10)) {
243 timeout = atoi(arg+10);
244 continue;
246 if (!strcmp(arg, "--")) {
247 i++;
248 break;
252 if (i != argc-1)
253 usage(upload_pack_usage);
254 dir = argv[i];
256 /* chdir to the directory. If that fails, try appending ".git" */
257 if (chdir(dir) < 0) {
258 if (strict || chdir(mkpath("%s.git", dir)) < 0)
259 die("git-upload-pack unable to chdir to %s", dir);
261 if (!strict)
262 chdir(".git");
264 if (access("objects", X_OK) || access("refs", X_OK))
265 die("git-upload-pack: %s doesn't seem to be a git archive", dir);
267 putenv("GIT_DIR=.");
268 upload_pack();
269 return 0;