9 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
11 #define THEY_HAVE (1U << 0)
12 #define OUR_REF (1U << 1)
13 #define WANTED (1U << 2)
16 static int nr_has = 0, nr_needs = 0, multi_ack = 0, nr_our_refs = 0;
17 static unsigned char has_sha1[MAX_HAS][20];
18 static unsigned char needs_sha1[MAX_NEEDS][20];
19 static unsigned int timeout = 0;
21 static void reset_timeout(void)
26 static int strip(char *line, int len)
28 if (len && line[len-1] == '\n')
33 static void create_pack_file(void)
37 int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
40 die("git-upload-pack: unable to create pipe");
43 die("git-upload-pack: unable to fork git-rev-list");
55 args = nr_has + nr_needs + 5;
56 argv = xmalloc(args * sizeof(char *));
57 buf = xmalloc(args * 45);
66 if (create_full_pack || MAX_NEEDS <= nr_needs)
69 for (i = 0; i < nr_needs; i++) {
71 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
75 if (!create_full_pack)
76 for (i = 0; i < nr_has; i++) {
79 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
84 die("git-upload-pack: unable to exec git-rev-list");
89 execl_git_cmd("pack-objects", "--stdout", NULL);
90 die("git-upload-pack: unable to exec git-pack-objects");
93 static int got_sha1(char *hex, unsigned char *sha1)
95 if (get_sha1_hex(hex, sha1))
96 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
97 if (!has_sha1_file(sha1))
99 if (nr_has < MAX_HAS) {
100 struct object *o = lookup_object(sha1);
101 if (!(o && o->parsed))
102 o = parse_object(sha1);
104 die("oops (%s)", sha1_to_hex(sha1));
105 if (o->type == commit_type) {
106 struct commit_list *parents;
107 if (o->flags & THEY_HAVE)
109 o->flags |= THEY_HAVE;
110 for (parents = ((struct commit*)o)->parents;
112 parents = parents->next)
113 parents->item->object.flags |= THEY_HAVE;
115 memcpy(has_sha1[nr_has++], sha1, 20);
120 static int get_common_commits(void)
122 static char line[1000];
123 unsigned char sha1[20], last_sha1[20];
126 track_object_refs = 0;
127 save_commit_buffer = 0;
130 len = packet_read_line(0, line, sizeof(line));
134 if (nr_has == 0 || multi_ack)
135 packet_write(1, "NAK\n");
138 len = strip(line, len);
139 if (!strncmp(line, "have ", 5)) {
140 if (got_sha1(line+5, sha1) &&
141 (multi_ack || nr_has == 1)) {
142 if (nr_has >= MAX_HAS)
144 packet_write(1, "ACK %s%s\n",
146 multi_ack ? " continue" : "");
148 memcpy(last_sha1, sha1, 20);
152 if (!strcmp(line, "done")) {
155 packet_write(1, "ACK %s\n",
156 sha1_to_hex(last_sha1));
159 packet_write(1, "NAK\n");
162 die("git-upload-pack: expected SHA1 list, got '%s'", line);
166 static int receive_needs(void)
168 static char line[1000];
174 unsigned char dummy[20], *sha1_buf;
175 len = packet_read_line(0, line, sizeof(line));
181 if (needs == MAX_NEEDS) {
183 "warning: supporting only a max of %d requests. "
184 "sending everything instead.\n",
187 else if (needs < MAX_NEEDS)
188 sha1_buf = needs_sha1[needs];
190 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
191 die("git-upload-pack: protocol error, "
192 "expected to get sha, not '%s'", line);
193 if (strstr(line+45, "multi_ack"))
196 /* We have sent all our refs already, and the other end
197 * should have chosen out of them; otherwise they are
198 * asking for nonsense.
200 * Hmph. We may later want to allow "want" line that
201 * asks for something like "master~10" (symbolic)...
202 * would it make sense? I don't know.
204 o = lookup_object(sha1_buf);
205 if (!o || !(o->flags & OUR_REF))
206 die("git-upload-pack: not our ref %s", line+5);
207 if (!(o->flags & WANTED)) {
214 static int send_ref(const char *refname, const unsigned char *sha1)
216 static char *capabilities = "multi_ack";
217 struct object *o = parse_object(sha1);
220 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
223 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
226 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
228 if (!(o->flags & OUR_REF)) {
232 if (o->type == tag_type) {
233 o = deref_tag(o, refname, 0);
234 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
239 static int upload_pack(void)
243 for_each_ref(send_ref);
245 nr_needs = receive_needs();
248 get_common_commits();
253 int main(int argc, char **argv)
259 for (i = 1; i < argc; i++) {
264 if (!strcmp(arg, "--strict")) {
268 if (!strncmp(arg, "--timeout=", 10)) {
269 timeout = atoi(arg+10);
272 if (!strcmp(arg, "--")) {
279 usage(upload_pack_usage);
282 if (!enter_repo(dir, strict))
283 die("'%s': unable to chdir or not a git archive", dir);