1#include "cache.h"
2#include "refs.h"
3#include "pkt-line.h"
4#include "tag.h"
5#include "object.h"
67
static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
89
#define OUR_REF (1U << 1)
10#define WANTED (1U << 2)
11#define MAX_HAS 256
12#define MAX_NEEDS 256
13static int nr_has = 0, nr_needs = 0, nr_our_refs = 0;
14static unsigned char has_sha1[MAX_HAS][20];
15static unsigned char needs_sha1[MAX_NEEDS][20];
16static unsigned int timeout = 0;
1718
static void reset_timeout(void)
19{
20alarm(timeout);
21}
2223
static int strip(char *line, int len)
24{
25if (len && line[len-1] == '\n')
26line[--len] = 0;
27return len;
28}
2930
static void create_pack_file(void)
31{
32int fd[2];
33pid_t pid;
34int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
3536
if (pipe(fd) < 0)
37die("git-upload-pack: unable to create pipe");
38pid = fork();
39if (pid < 0)
40die("git-upload-pack: unable to fork git-rev-list");
4142
if (!pid) {
43int i;
44int args;
45char **argv;
46char *buf;
47char **p;
4849
if (create_full_pack)
50args = 10;
51else
52args = nr_has + nr_needs + 5;
53argv = xmalloc(args * sizeof(char *));
54buf = xmalloc(args * 45);
55p = argv;
5657
dup2(fd[1], 1);
58close(0);
59close(fd[0]);
60close(fd[1]);
61*p++ = "git-rev-list";
62*p++ = "--objects";
63if (create_full_pack || MAX_NEEDS <= nr_needs)
64*p++ = "--all";
65else {
66for (i = 0; i < nr_needs; i++) {
67*p++ = buf;
68memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
69buf += 41;
70}
71}
72if (!create_full_pack)
73for (i = 0; i < nr_has; i++) {
74*p++ = buf;
75*buf++ = '^';
76memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
77buf += 41;
78}
79*p++ = NULL;
80execvp("git-rev-list", argv);
81die("git-upload-pack: unable to exec git-rev-list");
82}
83dup2(fd[0], 0);
84close(fd[0]);
85close(fd[1]);
86execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL);
87die("git-upload-pack: unable to exec git-pack-objects");
88}
8990
static int got_sha1(char *hex, unsigned char *sha1)
91{
92int nr;
93if (get_sha1_hex(hex, sha1))
94die("git-upload-pack: expected SHA1 object, got '%s'", hex);
95if (!has_sha1_file(sha1))
96return 0;
97nr = nr_has;
98if (nr < MAX_HAS) {
99memcpy(has_sha1[nr], sha1, 20);
100nr_has = nr+1;
101}
102return 1;
103}
104105
static int get_common_commits(void)
106{
107static char line[1000];
108unsigned char sha1[20];
109int len;
110111
for(;;) {
112len = packet_read_line(0, line, sizeof(line));
113reset_timeout();
114115
if (!len) {
116packet_write(1, "NAK\n");
117continue;
118}
119len = strip(line, len);
120if (!strncmp(line, "have ", 5)) {
121if (got_sha1(line+5, sha1)) {
122packet_write(1, "ACK %s\n", sha1_to_hex(sha1));
123break;
124}
125continue;
126}
127if (!strcmp(line, "done")) {
128packet_write(1, "NAK\n");
129return -1;
130}
131die("git-upload-pack: expected SHA1 list, got '%s'", line);
132}
133134
for (;;) {
135len = packet_read_line(0, line, sizeof(line));
136reset_timeout();
137if (!len)
138continue;
139len = strip(line, len);
140if (!strncmp(line, "have ", 5)) {
141got_sha1(line+5, sha1);
142continue;
143}
144if (!strcmp(line, "done"))
145break;
146die("git-upload-pack: expected SHA1 list, got '%s'", line);
147}
148return 0;
149}
150151
static int receive_needs(void)
152{
153static char line[1000];
154int len, needs;
155156
needs = 0;
157for (;;) {
158struct object *o;
159unsigned char dummy[20], *sha1_buf;
160len = packet_read_line(0, line, sizeof(line));
161reset_timeout();
162if (!len)
163return needs;
164165
sha1_buf = dummy;
166if (needs == MAX_NEEDS) {
167fprintf(stderr,
168"warning: supporting only a max of %d requests. "
169"sending everything instead.\n",
170MAX_NEEDS);
171}
172else if (needs < MAX_NEEDS)
173sha1_buf = needs_sha1[needs];
174175
if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
176die("git-upload-pack: protocol error, "
177"expected to get sha, not '%s'", line);
178179
/* 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.
182*
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.
186*/
187o = lookup_object(sha1_buf);
188if (!o || !(o->flags & OUR_REF))
189die("git-upload-pack: not our ref %s", line+5);
190if (!(o->flags & WANTED)) {
191o->flags |= WANTED;
192needs++;
193}
194}
195}
196197
static int send_ref(const char *refname, const unsigned char *sha1)
198{
199struct object *o = parse_object(sha1);
200201
packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
202if (!(o->flags & OUR_REF)) {
203o->flags |= OUR_REF;
204nr_our_refs++;
205}
206if (o->type == tag_type) {
207o = deref_tag(o);
208packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
209}
210return 0;
211}
212213
static int upload_pack(void)
214{
215reset_timeout();
216head_ref(send_ref);
217for_each_ref(send_ref);
218packet_flush(1);
219nr_needs = receive_needs();
220if (!nr_needs)
221return 0;
222get_common_commits();
223create_pack_file();
224return 0;
225}
226227
int main(int argc, char **argv)
228{
229const char *dir;
230int i;
231int strict = 0;
232233
for (i = 1; i < argc; i++) {
234char *arg = argv[i];
235236
if (arg[0] != '-')
237break;
238if (!strcmp(arg, "--strict")) {
239strict = 1;
240continue;
241}
242if (!strncmp(arg, "--timeout=", 10)) {
243timeout = atoi(arg+10);
244continue;
245}
246if (!strcmp(arg, "--")) {
247i++;
248break;
249}
250}
251252
if (i != argc-1)
253usage(upload_pack_usage);
254dir = argv[i];
255256
/* chdir to the directory. If that fails, try appending ".git" */
257if (chdir(dir) < 0) {
258if (strict || chdir(mkpath("%s.git", dir)) < 0)
259die("git-upload-pack unable to chdir to %s", dir);
260}
261if (!strict)
262chdir(".git");
263264
if (access("objects", X_OK) || access("refs", X_OK))
265die("git-upload-pack: %s doesn't seem to be a git archive", dir);
266267
putenv("GIT_DIR=.");
268upload_pack();
269return 0;
270}