7 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
9 #define OUR_REF (1U << 1)
10 #define WANTED (1U << 2)
13 static int nr_has = 0, nr_needs = 0, nr_our_refs = 0;
14 static unsigned char has_sha1[MAX_HAS][20];
15 static unsigned char needs_sha1[MAX_NEEDS][20];
16 static unsigned int timeout = 0;
18 static void reset_timeout(void)
23 static int strip(char *line, int len)
25 if (len && line[len-1] == '\n')
30 static void create_pack_file(void)
34 int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
37 die("git-upload-pack: unable to create pipe");
40 die("git-upload-pack: unable to fork git-rev-list");
52 args = nr_has + nr_needs + 5;
53 argv = xmalloc(args * sizeof(char *));
54 buf = xmalloc(args * 45);
61 *p++ = "git-rev-list";
63 if (MAX_NEEDS <= nr_needs)
66 for (i = 0; i < nr_needs; i++) {
68 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
72 for (i = 0; i < nr_has; i++) {
75 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
79 execvp("git-rev-list", argv);
80 die("git-upload-pack: unable to exec git-rev-list");
85 execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL);
86 die("git-upload-pack: unable to exec git-pack-objects");
89 static int got_sha1(char *hex, unsigned char *sha1)
92 if (get_sha1_hex(hex, sha1))
93 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
94 if (!has_sha1_file(sha1))
98 memcpy(has_sha1[nr], sha1, 20);
104 static int get_common_commits(void)
106 static char line[1000];
107 unsigned char sha1[20];
111 len = packet_read_line(0, line, sizeof(line));
115 packet_write(1, "NAK\n");
118 len = strip(line, len);
119 if (!strncmp(line, "have ", 5)) {
120 if (got_sha1(line+5, sha1)) {
121 packet_write(1, "ACK %s\n", sha1_to_hex(sha1));
126 if (!strcmp(line, "done")) {
127 packet_write(1, "NAK\n");
130 die("git-upload-pack: expected SHA1 list, got '%s'", line);
134 len = packet_read_line(0, line, sizeof(line));
138 len = strip(line, len);
139 if (!strncmp(line, "have ", 5)) {
140 got_sha1(line+5, sha1);
143 if (!strcmp(line, "done"))
145 die("git-upload-pack: expected SHA1 list, got '%s'", line);
150 static int receive_needs(void)
152 static char line[1000];
158 unsigned char dummy[20], *sha1_buf;
159 len = packet_read_line(0, line, sizeof(line));
165 if (needs == MAX_NEEDS) {
167 "warning: supporting only a max of %d requests. "
168 "sending everything instead.\n",
171 else if (needs < MAX_NEEDS)
172 sha1_buf = needs_sha1[needs];
174 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
175 die("git-upload-pack: protocol error, "
176 "expected to get sha, not '%s'", line);
178 /* We have sent all our refs already, and the other end
179 * should have chosen out of them; otherwise they are
180 * asking for nonsense.
182 * Hmph. We may later want to allow "want" line that
183 * asks for something like "master~10" (symbolic)...
184 * would it make sense? I don't know.
186 o = lookup_object(sha1_buf);
187 if (!o || !(o->flags & OUR_REF))
188 die("git-upload-pack: not our ref %s", line+5);
189 if (!(o->flags & WANTED)) {
196 static int send_ref(const char *refname, const unsigned char *sha1)
198 struct object *o = parse_object(sha1);
200 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
201 if (!(o->flags & OUR_REF)) {
205 if (o->type == tag_type) {
207 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
212 static int upload_pack(void)
216 for_each_ref(send_ref);
218 nr_needs = receive_needs();
221 get_common_commits();
226 int main(int argc, char **argv)
232 for (i = 1; i < argc; i++) {
237 if (!strcmp(arg, "--strict")) {
241 if (!strncmp(arg, "--timeout=", 10)) {
242 timeout = atoi(arg+10);
245 if (!strcmp(arg, "--")) {
252 usage(upload_pack_usage);
255 /* chdir to the directory. If that fails, try appending ".git" */
256 if (chdir(dir) < 0) {
257 if (strict || chdir(mkpath("%s.git", dir)) < 0)
258 die("git-upload-pack unable to chdir to %s", dir);
263 if (access("objects", X_OK) || access("refs", X_OK))
264 die("git-upload-pack: %s doesn't seem to be a git archive", dir);