7 static const char upload_pack_usage[] = "git-upload-pack <dir>";
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];
15 static int strip(char *line, int len)
17 if (len && line[len-1] == '\n')
22 static void create_pack_file(void)
28 die("git-upload-pack: unable to create pipe");
31 die("git-upload-pack: unable to fork git-rev-list");
40 if (MAX_NEEDS <= nr_needs)
43 args = nr_has + nr_needs + 5;
44 argv = xmalloc(args * sizeof(char *));
45 buf = xmalloc(args * 45);
52 *p++ = "git-rev-list";
54 if (MAX_NEEDS <= nr_needs)
57 for (i = 0; i < nr_needs; i++) {
59 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
63 for (i = 0; i < nr_has; i++) {
66 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
70 execvp("git-rev-list", argv);
71 die("git-upload-pack: unable to exec git-rev-list");
76 execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL);
77 die("git-upload-pack: unable to exec git-pack-objects");
80 static int got_sha1(char *hex, unsigned char *sha1)
83 if (get_sha1_hex(hex, sha1))
84 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
85 if (!has_sha1_file(sha1))
89 memcpy(has_sha1[nr], sha1, 20);
95 static int get_common_commits(void)
97 static char line[1000];
98 unsigned char sha1[20];
102 len = packet_read_line(0, line, sizeof(line));
105 packet_write(1, "NAK\n");
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));
116 if (!strcmp(line, "done")) {
117 packet_write(1, "NAK\n");
120 die("git-upload-pack: expected SHA1 list, got '%s'", line);
124 len = packet_read_line(0, line, sizeof(line));
127 len = strip(line, len);
128 if (!strncmp(line, "have ", 5)) {
129 got_sha1(line+5, sha1);
132 if (!strcmp(line, "done"))
134 die("git-upload-pack: expected SHA1 list, got '%s'", line);
139 static int receive_needs(void)
141 static char line[1000];
146 unsigned char dummy[20], *sha1_buf;
147 len = packet_read_line(0, line, sizeof(line));
152 if (needs == MAX_NEEDS) {
154 "warning: supporting only a max of %d requests. "
155 "sending everything instead.\n",
158 else if (needs < MAX_NEEDS)
159 sha1_buf = needs_sha1[needs];
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);
168 static int send_ref(const char *refname, const unsigned char *sha1)
170 struct object *o = parse_object(sha1);
172 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
173 if (o->type == tag_type) {
175 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
180 static int upload_pack(void)
183 for_each_ref(send_ref);
185 nr_needs = receive_needs();
188 get_common_commits();
193 int main(int argc, char **argv)
197 usage(upload_pack_usage);
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);
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);