8 static const char upload_pack_usage
[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
10 #define THEY_HAVE (1U << 0)
13 static int nr_has
= 0, nr_needs
= 0, multi_ack
= 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)
36 die("git-upload-pack: unable to create pipe");
39 die("git-upload-pack: unable to fork git-rev-list");
48 if (MAX_NEEDS
<= nr_needs
)
51 args
= nr_has
+ nr_needs
+ 5;
52 argv
= xmalloc(args
* sizeof(char *));
53 buf
= xmalloc(args
* 45);
60 *p
++ = "git-rev-list";
62 if (MAX_NEEDS
<= nr_needs
)
65 for (i
= 0; i
< nr_needs
; i
++) {
67 memcpy(buf
, sha1_to_hex(needs_sha1
[i
]), 41);
71 for (i
= 0; i
< nr_has
; i
++) {
74 memcpy(buf
, sha1_to_hex(has_sha1
[i
]), 41);
78 execvp("git-rev-list", argv
);
79 die("git-upload-pack: unable to exec git-rev-list");
84 execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL
);
85 die("git-upload-pack: unable to exec git-pack-objects");
88 static int got_sha1(char *hex
, unsigned char *sha1
)
90 if (get_sha1_hex(hex
, sha1
))
91 die("git-upload-pack: expected SHA1 object, got '%s'", hex
);
92 if (!has_sha1_file(sha1
))
94 if (nr_has
< MAX_HAS
) {
95 struct object
*o
= lookup_object(sha1
);
96 if (!o
|| (!o
->parsed
&& !parse_object(sha1
)))
97 die("oops (%s)", sha1_to_hex(sha1
));
98 if (o
->type
== commit_type
) {
99 struct commit_list
*parents
;
100 if (o
->flags
& THEY_HAVE
)
102 o
->flags
|= THEY_HAVE
;
103 for (parents
= ((struct commit
*)o
)->parents
;
105 parents
= parents
->next
)
106 parents
->item
->object
.flags
|= THEY_HAVE
;
108 memcpy(has_sha1
[nr_has
++], sha1
, 20);
113 static int get_common_commits(void)
115 static char line
[1000];
116 unsigned char sha1
[20];
119 track_object_refs
= 0;
120 save_commit_buffer
= 0;
123 len
= packet_read_line(0, line
, sizeof(line
));
127 if (multi_ack
|| nr_has
== 0)
128 packet_write(1, "NAK\n");
131 len
= strip(line
, len
);
132 if (!strncmp(line
, "have ", 5)) {
133 if (got_sha1(line
+5, sha1
) &&
134 (multi_ack
|| nr_has
== 1))
135 packet_write(1, "ACK %s%s\n",
137 multi_ack
&& nr_has
< MAX_HAS
?
141 if (!strcmp(line
, "done")) {
144 packet_write(1, "NAK\n");
147 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
151 static int receive_needs(void)
153 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 if (strstr(line
+45, "multi_ack"))
185 static int send_ref(const char *refname
, const unsigned char *sha1
)
187 struct object
*o
= parse_object(sha1
);
189 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname
);
190 if (o
->type
== tag_type
) {
192 packet_write(1, "%s %s^{}\n", sha1_to_hex(o
->sha1
), refname
);
197 static int upload_pack(void)
201 for_each_ref(send_ref
);
203 nr_needs
= receive_needs();
206 get_common_commits();
211 int main(int argc
, char **argv
)
217 for (i
= 1; i
< argc
; i
++) {
222 if (!strcmp(arg
, "--strict")) {
226 if (!strncmp(arg
, "--timeout=", 10)) {
227 timeout
= atoi(arg
+10);
230 if (!strcmp(arg
, "--")) {
237 usage(upload_pack_usage
);
240 /* chdir to the directory. If that fails, try appending ".git" */
241 if (chdir(dir
) < 0) {
242 if (strict
|| chdir(mkpath("%s.git", dir
)) < 0)
243 die("git-upload-pack unable to chdir to %s", dir
);
248 if (access("objects", X_OK
) || access("refs", X_OK
))
249 die("git-upload-pack: %s doesn't seem to be a git archive", dir
);