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"
78
static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
910
#define THEY_HAVE (1U << 0)
11#define MAX_HAS 256
12#define MAX_NEEDS 256
13static int nr_has = 0, nr_needs = 0, multi_ack = 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;
3435
if (pipe(fd) < 0)
36die("git-upload-pack: unable to create pipe");
37pid = fork();
38if (pid < 0)
39die("git-upload-pack: unable to fork git-rev-list");
4041
if (!pid) {
42int i;
43int args;
44char **argv;
45char *buf;
46char **p;
4748
if (MAX_NEEDS <= nr_needs)
49args = nr_has + 10;
50else
51args = nr_has + nr_needs + 5;
52argv = xmalloc(args * sizeof(char *));
53buf = xmalloc(args * 45);
54p = argv;
5556
dup2(fd[1], 1);
57close(0);
58close(fd[0]);
59close(fd[1]);
60*p++ = "git-rev-list";
61*p++ = "--objects";
62if (MAX_NEEDS <= nr_needs)
63*p++ = "--all";
64else {
65for (i = 0; i < nr_needs; i++) {
66*p++ = buf;
67memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
68buf += 41;
69}
70}
71for (i = 0; i < nr_has; i++) {
72*p++ = buf;
73*buf++ = '^';
74memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
75buf += 41;
76}
77*p++ = NULL;
78execvp("git-rev-list", argv);
79die("git-upload-pack: unable to exec git-rev-list");
80}
81dup2(fd[0], 0);
82close(fd[0]);
83close(fd[1]);
84execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL);
85die("git-upload-pack: unable to exec git-pack-objects");
86}
8788
static int got_sha1(char *hex, unsigned char *sha1)
89{
90if (get_sha1_hex(hex, sha1))
91die("git-upload-pack: expected SHA1 object, got '%s'", hex);
92if (!has_sha1_file(sha1))
93return 0;
94if (nr_has < MAX_HAS) {
95struct object *o = lookup_object(sha1);
96if (!(o && o->parsed))
97o = parse_object(sha1);
98if (!o)
99die("oops (%s)", sha1_to_hex(sha1));
100if (o->type == commit_type) {
101struct commit_list *parents;
102if (o->flags & THEY_HAVE)
103return 0;
104o->flags |= THEY_HAVE;
105for (parents = ((struct commit*)o)->parents;
106parents;
107parents = parents->next)
108parents->item->object.flags |= THEY_HAVE;
109}
110memcpy(has_sha1[nr_has++], sha1, 20);
111}
112return 1;
113}
114115
static int get_common_commits(void)
116{
117static char line[1000];
118unsigned char sha1[20];
119int len;
120121
track_object_refs = 0;
122save_commit_buffer = 0;
123124
for(;;) {
125len = packet_read_line(0, line, sizeof(line));
126reset_timeout();
127128
if (!len) {
129if (multi_ack || nr_has == 0)
130packet_write(1, "NAK\n");
131continue;
132}
133len = strip(line, len);
134if (!strncmp(line, "have ", 5)) {
135if (got_sha1(line+5, sha1) &&
136(multi_ack || nr_has == 1))
137packet_write(1, "ACK %s%s\n",
138sha1_to_hex(sha1),
139multi_ack && nr_has < MAX_HAS ?
140" continue" : "");
141continue;
142}
143if (!strcmp(line, "done")) {
144if (nr_has > 0)
145return 0;
146packet_write(1, "NAK\n");
147return -1;
148}
149die("git-upload-pack: expected SHA1 list, got '%s'", line);
150}
151}
152153
static int receive_needs(void)
154{
155static char line[1000];
156int len, needs;
157158
needs = 0;
159for (;;) {
160unsigned char dummy[20], *sha1_buf;
161len = packet_read_line(0, line, sizeof(line));
162reset_timeout();
163if (!len)
164return needs;
165166
sha1_buf = dummy;
167if (needs == MAX_NEEDS) {
168fprintf(stderr,
169"warning: supporting only a max of %d requests. "
170"sending everything instead.\n",
171MAX_NEEDS);
172}
173else if (needs < MAX_NEEDS)
174sha1_buf = needs_sha1[needs];
175176
if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
177die("git-upload-pack: protocol error, "
178"expected to get sha, not '%s'", line);
179180
if (strstr(line+45, "multi_ack"))
181multi_ack = 1;
182183
needs++;
184}
185}
186187
static int send_ref(const char *refname, const unsigned char *sha1)
188{
189struct object *o = parse_object(sha1);
190191
packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
192if (o->type == tag_type) {
193o = deref_tag(o);
194packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
195}
196return 0;
197}
198199
static int upload_pack(void)
200{
201reset_timeout();
202head_ref(send_ref);
203for_each_ref(send_ref);
204packet_flush(1);
205nr_needs = receive_needs();
206if (!nr_needs)
207return 0;
208get_common_commits();
209create_pack_file();
210return 0;
211}
212213
int main(int argc, char **argv)
214{
215const char *dir;
216int i;
217int strict = 0;
218219
for (i = 1; i < argc; i++) {
220char *arg = argv[i];
221222
if (arg[0] != '-')
223break;
224if (!strcmp(arg, "--strict")) {
225strict = 1;
226continue;
227}
228if (!strncmp(arg, "--timeout=", 10)) {
229timeout = atoi(arg+10);
230continue;
231}
232if (!strcmp(arg, "--")) {
233i++;
234break;
235}
236}
237238
if (i != argc-1)
239usage(upload_pack_usage);
240dir = argv[i];
241242
/* chdir to the directory. If that fails, try appending ".git" */
243if (chdir(dir) < 0) {
244if (strict || chdir(mkpath("%s.git", dir)) < 0)
245die("git-upload-pack unable to chdir to %s", dir);
246}
247if (!strict)
248chdir(".git");
249250
if (access("objects", X_OK) || access("refs", X_OK))
251die("git-upload-pack: %s doesn't seem to be a git archive", dir);
252253
putenv("GIT_DIR=.");
254upload_pack();
255return 0;
256}