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 int use_thin_pack = 0;
18 static unsigned char has_sha1[MAX_HAS][20];
19 static unsigned char needs_sha1[MAX_NEEDS][20];
20 static unsigned int timeout = 0;
22 static void reset_timeout(void)
27 static int strip(char *line, int len)
29 if (len && line[len-1] == '\n')
34 static void create_pack_file(void)
38 int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
41 die("git-upload-pack: unable to create pipe");
44 die("git-upload-pack: unable to fork git-rev-list");
53 if (create_full_pack) {
55 use_thin_pack = 0; /* no point doing it */
58 args = nr_has + nr_needs + 5;
59 p = xmalloc(args * sizeof(char *));
60 argv = (const char **) p;
61 buf = xmalloc(args * 45);
68 *p++ = use_thin_pack ? "--objects-edge" : "--objects";
69 if (create_full_pack || MAX_NEEDS <= nr_needs)
72 for (i = 0; i < nr_needs; i++) {
74 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
78 if (!create_full_pack)
79 for (i = 0; i < nr_has; i++) {
82 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
87 die("git-upload-pack: unable to exec git-rev-list");
92 execl_git_cmd("pack-objects", "--stdout", NULL);
93 die("git-upload-pack: unable to exec git-pack-objects");
96 static int got_sha1(char *hex, unsigned char *sha1)
98 if (get_sha1_hex(hex, sha1))
99 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
100 if (!has_sha1_file(sha1))
102 if (nr_has < MAX_HAS) {
103 struct object *o = lookup_object(sha1);
104 if (!(o && o->parsed))
105 o = parse_object(sha1);
107 die("oops (%s)", sha1_to_hex(sha1));
108 if (o->type == commit_type) {
109 struct commit_list *parents;
110 if (o->flags & THEY_HAVE)
112 o->flags |= THEY_HAVE;
113 for (parents = ((struct commit*)o)->parents;
115 parents = parents->next)
116 parents->item->object.flags |= THEY_HAVE;
118 memcpy(has_sha1[nr_has++], sha1, 20);
123 static int get_common_commits(void)
125 static char line[1000];
126 unsigned char sha1[20], last_sha1[20];
129 track_object_refs = 0;
130 save_commit_buffer = 0;
133 len = packet_read_line(0, line, sizeof(line));
137 if (nr_has == 0 || multi_ack)
138 packet_write(1, "NAK\n");
141 len = strip(line, len);
142 if (!strncmp(line, "have ", 5)) {
143 if (got_sha1(line+5, sha1) &&
144 (multi_ack || nr_has == 1)) {
145 if (nr_has >= MAX_HAS)
147 packet_write(1, "ACK %s%s\n",
149 multi_ack ? " continue" : "");
151 memcpy(last_sha1, sha1, 20);
155 if (!strcmp(line, "done")) {
158 packet_write(1, "ACK %s\n",
159 sha1_to_hex(last_sha1));
162 packet_write(1, "NAK\n");
165 die("git-upload-pack: expected SHA1 list, got '%s'", line);
169 static int receive_needs(void)
171 static char line[1000];
177 unsigned char dummy[20], *sha1_buf;
178 len = packet_read_line(0, line, sizeof(line));
184 if (needs == MAX_NEEDS) {
186 "warning: supporting only a max of %d requests. "
187 "sending everything instead.\n",
190 else if (needs < MAX_NEEDS)
191 sha1_buf = needs_sha1[needs];
193 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
194 die("git-upload-pack: protocol error, "
195 "expected to get sha, not '%s'", line);
196 if (strstr(line+45, "multi_ack"))
198 if (strstr(line+45, "thin-pack"))
201 /* We have sent all our refs already, and the other end
202 * should have chosen out of them; otherwise they are
203 * asking for nonsense.
205 * Hmph. We may later want to allow "want" line that
206 * asks for something like "master~10" (symbolic)...
207 * would it make sense? I don't know.
209 o = lookup_object(sha1_buf);
210 if (!o || !(o->flags & OUR_REF))
211 die("git-upload-pack: not our ref %s", line+5);
212 if (!(o->flags & WANTED)) {
219 static int send_ref(const char *refname, const unsigned char *sha1)
221 static char *capabilities = "multi_ack thin-pack";
222 struct object *o = parse_object(sha1);
225 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
228 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
231 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
233 if (!(o->flags & OUR_REF)) {
237 if (o->type == tag_type) {
238 o = deref_tag(o, refname, 0);
239 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
244 static int upload_pack(void)
248 for_each_ref(send_ref);
250 nr_needs = receive_needs();
253 get_common_commits();
258 int main(int argc, char **argv)
264 for (i = 1; i < argc; i++) {
269 if (!strcmp(arg, "--strict")) {
273 if (!strncmp(arg, "--timeout=", 10)) {
274 timeout = atoi(arg+10);
277 if (!strcmp(arg, "--")) {
284 usage(upload_pack_usage);
287 if (!enter_repo(dir, strict))
288 die("'%s': unable to chdir or not a git archive", dir);