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 && !parse_object(sha1)))
97die("oops (%s)", sha1_to_hex(sha1));
98if (o->type == commit_type) {
99struct commit_list *parents;
100if (o->flags & THEY_HAVE)
101return 0;
102o->flags |= THEY_HAVE;
103for (parents = ((struct commit*)o)->parents;
104parents;
105parents = parents->next)
106parents->item->object.flags |= THEY_HAVE;
107}
108memcpy(has_sha1[nr_has++], sha1, 20);
109}
110return 1;
111}
112113
static int get_common_commits(void)
114{
115static char line[1000];
116unsigned char sha1[20];
117int len;
118119
track_object_refs = 0;
120save_commit_buffer = 0;
121122
for(;;) {
123len = packet_read_line(0, line, sizeof(line));
124reset_timeout();
125126
if (!len) {
127if (multi_ack || nr_has == 0)
128packet_write(1, "NAK\n");
129continue;
130}
131len = strip(line, len);
132if (!strncmp(line, "have ", 5)) {
133if (got_sha1(line+5, sha1) &&
134(multi_ack || nr_has == 1))
135packet_write(1, "ACK %s%s\n",
136sha1_to_hex(sha1),
137multi_ack && nr_has < MAX_HAS ?
138" continue" : "");
139continue;
140}
141if (!strcmp(line, "done")) {
142if (nr_has > 0)
143return 0;
144packet_write(1, "NAK\n");
145return -1;
146}
147die("git-upload-pack: expected SHA1 list, got '%s'", line);
148}
149}
150151
static int receive_needs(void)
152{
153static char line[1000];
154int len, needs;
155156
needs = 0;
157for (;;) {
158unsigned char dummy[20], *sha1_buf;
159len = packet_read_line(0, line, sizeof(line));
160reset_timeout();
161if (!len)
162return needs;
163164
sha1_buf = dummy;
165if (needs == MAX_NEEDS) {
166fprintf(stderr,
167"warning: supporting only a max of %d requests. "
168"sending everything instead.\n",
169MAX_NEEDS);
170}
171else if (needs < MAX_NEEDS)
172sha1_buf = needs_sha1[needs];
173174
if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
175die("git-upload-pack: protocol error, "
176"expected to get sha, not '%s'", line);
177178
if (strstr(line+45, "multi_ack"))
179multi_ack = 1;
180181
needs++;
182}
183}
184185
static int send_ref(const char *refname, const unsigned char *sha1)
186{
187struct object *o = parse_object(sha1);
188189
packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
190if (o->type == tag_type) {
191o = deref_tag(o);
192packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
193}
194return 0;
195}
196197
static int upload_pack(void)
198{
199reset_timeout();
200head_ref(send_ref);
201for_each_ref(send_ref);
202packet_flush(1);
203nr_needs = receive_needs();
204if (!nr_needs)
205return 0;
206get_common_commits();
207create_pack_file();
208return 0;
209}
210211
int main(int argc, char **argv)
212{
213const char *dir;
214int i;
215int strict = 0;
216217
for (i = 1; i < argc; i++) {
218char *arg = argv[i];
219220
if (arg[0] != '-')
221break;
222if (!strcmp(arg, "--strict")) {
223strict = 1;
224continue;
225}
226if (!strncmp(arg, "--timeout=", 10)) {
227timeout = atoi(arg+10);
228continue;
229}
230if (!strcmp(arg, "--")) {
231i++;
232break;
233}
234}
235236
if (i != argc-1)
237usage(upload_pack_usage);
238dir = argv[i];
239240
/* chdir to the directory. If that fails, try appending ".git" */
241if (chdir(dir) < 0) {
242if (strict || chdir(mkpath("%s.git", dir)) < 0)
243die("git-upload-pack unable to chdir to %s", dir);
244}
245if (!strict)
246chdir(".git");
247248
if (access("objects", X_OK) || access("refs", X_OK))
249die("git-upload-pack: %s doesn't seem to be a git archive", dir);
250251
putenv("GIT_DIR=.");
252upload_pack();
253return 0;
254}