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 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");
33 int args
= nr_has
+ nr_needs
+ 5;
34 char **argv
= xmalloc(args
* sizeof(char *));
35 char *buf
= xmalloc(args
* 45);
42 *p
++ = "git-rev-list";
44 for (i
= 0; i
< nr_needs
; i
++) {
46 memcpy(buf
, sha1_to_hex(needs_sha1
[i
]), 41);
49 for (i
= 0; i
< nr_has
; i
++) {
52 memcpy(buf
, sha1_to_hex(has_sha1
[i
]), 41);
56 execvp("git-rev-list", argv
);
57 die("git-upload-pack: unable to exec git-rev-list");
62 execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL
);
63 die("git-upload-pack: unable to exec git-pack-objects");
66 static int got_sha1(char *hex
, unsigned char *sha1
)
69 if (get_sha1_hex(hex
, sha1
))
70 die("git-upload-pack: expected SHA1 object, got '%s'", hex
);
71 if (!has_sha1_file(sha1
))
75 memcpy(has_sha1
[nr
], sha1
, 20);
81 static int get_common_commits(void)
83 static char line
[1000];
84 unsigned char sha1
[20];
88 len
= packet_read_line(0, line
, sizeof(line
));
91 packet_write(1, "NAK\n");
94 len
= strip(line
, len
);
95 if (!strncmp(line
, "have ", 5)) {
96 if (got_sha1(line
+5, sha1
)) {
97 packet_write(1, "ACK %s\n", sha1_to_hex(sha1
));
102 if (!strcmp(line
, "done")) {
103 packet_write(1, "NAK\n");
106 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
110 len
= packet_read_line(0, line
, sizeof(line
));
113 len
= strip(line
, len
);
114 if (!strncmp(line
, "have ", 5)) {
115 got_sha1(line
+5, sha1
);
118 if (!strcmp(line
, "done"))
120 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
125 static int receive_needs(void)
127 static char line
[1000];
132 len
= packet_read_line(0, line
, sizeof(line
));
137 * This is purely theoretical right now: git-fetch-pack only
138 * ever asks for a single HEAD
140 if (needs
>= MAX_NEEDS
)
141 die("I'm only doing a max of %d requests", MAX_NEEDS
);
142 if (strncmp("want ", line
, 5) || get_sha1_hex(line
+5, needs_sha1
[needs
]))
143 die("git-upload-pack: protocol error, expected to get sha, not '%s'", line
);
148 static int send_ref(const char *refname
, const unsigned char *sha1
)
150 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname
);
154 static int upload_pack(void)
157 for_each_ref(send_ref
);
159 nr_needs
= receive_needs();
162 get_common_commits();
167 int main(int argc
, char **argv
)
171 usage(upload_pack_usage
);
174 die("git-upload-pack unable to chdir to %s", dir
);
176 if (access("objects", X_OK
) || access("refs", X_OK
))
177 die("git-upload-pack: %s doesn't seem to be a git archive", dir
);
178 setenv("GIT_DIR", ".", 1);