5 static const char upload_pack_usage[] = "git-upload-pack <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];
13 static int strip(char *line, int len)
15 if (len && line[len-1] == '\n')
20 static void create_pack_file(void)
26 die("git-upload-pack: unable to create pipe");
29 die("git-upload-pack: unable to fork git-rev-list");
38 if (MAX_NEEDS <= nr_needs)
41 args = nr_has + nr_needs + 5;
42 argv = xmalloc(args * sizeof(char *));
43 buf = xmalloc(args * 45);
50 *p++ = "git-rev-list";
52 if (MAX_NEEDS <= nr_needs)
55 for (i = 0; i < nr_needs; i++) {
57 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
61 for (i = 0; i < nr_has; i++) {
64 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
68 execvp("git-rev-list", argv);
69 die("git-upload-pack: unable to exec git-rev-list");
74 execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL);
75 die("git-upload-pack: unable to exec git-pack-objects");
78 static int got_sha1(char *hex, unsigned char *sha1)
81 if (get_sha1_hex(hex, sha1))
82 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
83 if (!has_sha1_file(sha1))
87 memcpy(has_sha1[nr], sha1, 20);
93 static int get_common_commits(void)
95 static char line[1000];
96 unsigned char sha1[20];
100 len = packet_read_line(0, line, sizeof(line));
103 packet_write(1, "NAK\n");
106 len = strip(line, len);
107 if (!strncmp(line, "have ", 5)) {
108 if (got_sha1(line+5, sha1)) {
109 packet_write(1, "ACK %s\n", sha1_to_hex(sha1));
114 if (!strcmp(line, "done")) {
115 packet_write(1, "NAK\n");
118 die("git-upload-pack: expected SHA1 list, got '%s'", line);
122 len = packet_read_line(0, line, sizeof(line));
125 len = strip(line, len);
126 if (!strncmp(line, "have ", 5)) {
127 got_sha1(line+5, sha1);
130 if (!strcmp(line, "done"))
132 die("git-upload-pack: expected SHA1 list, got '%s'", line);
137 static int receive_needs(void)
139 static char line[1000];
144 unsigned char dummy[20], *sha1_buf;
145 len = packet_read_line(0, line, sizeof(line));
150 if (needs == MAX_NEEDS) {
152 "warning: supporting only a max of %d requests. "
153 "sending everything instead.\n",
156 else if (needs < MAX_NEEDS)
157 sha1_buf = needs_sha1[needs];
159 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
160 die("git-upload-pack: protocol error, "
161 "expected to get sha, not '%s'", line);
166 static int send_ref(const char *refname, const unsigned char *sha1)
168 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
172 static int upload_pack(void)
175 for_each_ref(send_ref);
177 nr_needs = receive_needs();
180 get_common_commits();
185 int main(int argc, char **argv)
189 usage(upload_pack_usage);
192 /* chdir to the directory. If that fails, try appending ".git" */
193 if (chdir(dir) < 0) {
194 if (chdir(mkpath("%s.git", dir)) < 0)
195 die("git-upload-pack unable to chdir to %s", dir);
198 if (access("objects", X_OK) || access("refs", X_OK))
199 die("git-upload-pack: %s doesn't seem to be a git archive", dir);