5 static const char upload_pack_usage[] = "git-upload-pack <dir>";
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 void create_pack_file(void)
18 * git-rev-list --objects needs_sha1 --not has_sha1 |
19 * git-pack-objects --stdout
25 static int got_sha1(char *hex, unsigned char *sha1)
28 if (get_sha1_hex(hex, sha1))
29 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
30 if (!has_sha1_file(sha1))
34 memcpy(has_sha1[nr], sha1, 20);
40 static int get_common_commits(void)
42 static char line[1000];
43 unsigned char sha1[20];
47 len = packet_read_line(0, line, sizeof(line));
50 packet_write(1, "NAK\n");
53 if (line[len-1] == '\n')
55 if (!strncmp(line, "have ", 5)) {
56 if (got_sha1(line+5, sha1)) {
57 packet_write(1, "ACK %s\n", sha1_to_hex(sha1));
62 if (!strcmp(line, "done")) {
63 packet_write(1, "NAK\n");
66 die("git-upload-pack: expected SHA1 list, got '%s'", line);
70 len = packet_read_line(0, line, sizeof(line));
73 if (!strncmp(line, "have ", 5)) {
74 got_sha1(line+5, sha1);
77 if (!strcmp(line, "done"))
79 die("git-upload-pack: expected SHA1 list, got '%s'", line);
84 static int receive_needs(void)
86 static char line[1000];
91 len = packet_read_line(0, line, sizeof(line));
96 * This is purely theoretical right now: git-fetch-pack only
97 * ever asks for a single HEAD
99 if (needs >= MAX_NEEDS)
100 die("I'm only doing a max of %d requests", MAX_NEEDS);
101 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, needs_sha1[needs]))
102 die("git-upload-pack: protocol error, expected to get sha, not '%s'", line);
107 static int send_ref(const char *refname, const unsigned char *sha1)
109 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
113 static int upload_pack(void)
115 for_each_ref(send_ref);
117 nr_needs = receive_needs();
120 get_common_commits();
125 int main(int argc, char **argv)
129 usage(upload_pack_usage);
132 die("git-upload-pack unable to chdir to %s", dir);
134 if (access("objects", X_OK) || access("refs", X_OK))
135 die("git-upload-pack: %s doesn't seem to be a git archive", dir);
136 setenv("GIT_DIR", ".", 1);