5 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
8 #define MAX_NEEDS (256)
9 static int nr_has = 0, nr_needs = 0;
10 static unsigned char has_sha1[MAX_HAS][20];
11 static unsigned char needs_sha1[MAX_NEEDS][20];
12 static unsigned int timeout = 0;
14 static void reset_timeout(void)
19 static int strip(char *line, int len)
21 if (len && line[len-1] == '\n')
26 static void create_pack_file(void)
32 die("git-upload-pack: unable to create pipe");
35 die("git-upload-pack: unable to fork git-rev-list");
44 if (MAX_NEEDS <= nr_needs)
47 args = nr_has + nr_needs + 5;
48 argv = xmalloc(args * sizeof(char *));
49 buf = xmalloc(args * 45);
56 *p++ = "git-rev-list";
58 if (MAX_NEEDS <= nr_needs)
61 for (i = 0; i < nr_needs; i++) {
63 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
67 for (i = 0; i < nr_has; i++) {
70 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
74 execvp("git-rev-list", argv);
75 die("git-upload-pack: unable to exec git-rev-list");
80 execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL);
81 die("git-upload-pack: unable to exec git-pack-objects");
84 static int got_sha1(char *hex, unsigned char *sha1)
87 if (get_sha1_hex(hex, sha1))
88 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
89 if (!has_sha1_file(sha1))
93 memcpy(has_sha1[nr], sha1, 20);
99 static int get_common_commits(void)
101 static char line[1000];
102 unsigned char sha1[20];
106 len = packet_read_line(0, line, sizeof(line));
110 packet_write(1, "NAK\n");
113 len = strip(line, len);
114 if (!strncmp(line, "have ", 5)) {
115 if (got_sha1(line+5, sha1)) {
116 packet_write(1, "ACK %s\n", sha1_to_hex(sha1));
121 if (!strcmp(line, "done")) {
122 packet_write(1, "NAK\n");
125 die("git-upload-pack: expected SHA1 list, got '%s'", line);
129 len = packet_read_line(0, line, sizeof(line));
133 len = strip(line, len);
134 if (!strncmp(line, "have ", 5)) {
135 got_sha1(line+5, sha1);
138 if (!strcmp(line, "done"))
140 die("git-upload-pack: expected SHA1 list, got '%s'", line);
145 static int receive_needs(void)
147 static char line[1000];
152 unsigned char dummy[20], *sha1_buf;
153 len = packet_read_line(0, line, sizeof(line));
159 if (needs == MAX_NEEDS) {
161 "warning: supporting only a max of %d requests. "
162 "sending everything instead.\n",
165 else if (needs < MAX_NEEDS)
166 sha1_buf = needs_sha1[needs];
168 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
169 die("git-upload-pack: protocol error, "
170 "expected to get sha, not '%s'", line);
175 static int send_ref(const char *refname, const unsigned char *sha1)
177 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
181 static int upload_pack(void)
185 for_each_ref(send_ref);
187 nr_needs = receive_needs();
190 get_common_commits();
195 int main(int argc, char **argv)
201 for (i = 1; i < argc; i++) {
206 if (!strcmp(arg, "--strict")) {
210 if (!strncmp(arg, "--timeout=", 10)) {
211 timeout = atoi(arg+10);
214 if (!strcmp(arg, "--")) {
221 usage(upload_pack_usage);
224 /* chdir to the directory. If that fails, try appending ".git" */
225 if (chdir(dir) < 0) {
226 if (strict || chdir(mkpath("%s.git", dir)) < 0)
227 die("git-upload-pack unable to chdir to %s", dir);
232 if (access("objects", X_OK) || access("refs", X_OK))
233 die("git-upload-pack: %s doesn't seem to be a git archive", dir);