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