7 static const char upload_pack_usage
[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
10 #define MAX_NEEDS (256)
11 static int nr_has
= 0, nr_needs
= 0;
12 static unsigned char has_sha1
[MAX_HAS
][20];
13 static unsigned char needs_sha1
[MAX_NEEDS
][20];
14 static unsigned int timeout
= 0;
16 static void reset_timeout(void)
21 static int strip(char *line
, int len
)
23 if (len
&& line
[len
-1] == '\n')
28 static void create_pack_file(void)
34 die("git-upload-pack: unable to create pipe");
37 die("git-upload-pack: unable to fork git-rev-list");
46 if (MAX_NEEDS
<= nr_needs
)
49 args
= nr_has
+ nr_needs
+ 5;
50 argv
= xmalloc(args
* sizeof(char *));
51 buf
= xmalloc(args
* 45);
58 *p
++ = "git-rev-list";
60 if (MAX_NEEDS
<= nr_needs
)
63 for (i
= 0; i
< nr_needs
; i
++) {
65 memcpy(buf
, sha1_to_hex(needs_sha1
[i
]), 41);
69 for (i
= 0; i
< nr_has
; i
++) {
72 memcpy(buf
, sha1_to_hex(has_sha1
[i
]), 41);
76 execvp("git-rev-list", argv
);
77 die("git-upload-pack: unable to exec git-rev-list");
82 execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL
);
83 die("git-upload-pack: unable to exec git-pack-objects");
86 static int got_sha1(char *hex
, unsigned char *sha1
)
89 if (get_sha1_hex(hex
, sha1
))
90 die("git-upload-pack: expected SHA1 object, got '%s'", hex
);
91 if (!has_sha1_file(sha1
))
95 memcpy(has_sha1
[nr
], sha1
, 20);
101 static int get_common_commits(void)
103 static char line
[1000];
104 unsigned char sha1
[20];
108 len
= packet_read_line(0, line
, sizeof(line
));
112 packet_write(1, "NAK\n");
115 len
= strip(line
, len
);
116 if (!strncmp(line
, "have ", 5)) {
117 if (got_sha1(line
+5, sha1
)) {
118 packet_write(1, "ACK %s\n", sha1_to_hex(sha1
));
123 if (!strcmp(line
, "done")) {
124 packet_write(1, "NAK\n");
127 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
131 len
= packet_read_line(0, line
, sizeof(line
));
135 len
= strip(line
, len
);
136 if (!strncmp(line
, "have ", 5)) {
137 got_sha1(line
+5, sha1
);
140 if (!strcmp(line
, "done"))
142 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
147 static int receive_needs(void)
149 static char line
[1000];
154 unsigned char dummy
[20], *sha1_buf
;
155 len
= packet_read_line(0, line
, sizeof(line
));
161 if (needs
== MAX_NEEDS
) {
163 "warning: supporting only a max of %d requests. "
164 "sending everything instead.\n",
167 else if (needs
< MAX_NEEDS
)
168 sha1_buf
= needs_sha1
[needs
];
170 if (strncmp("want ", line
, 5) || get_sha1_hex(line
+5, sha1_buf
))
171 die("git-upload-pack: protocol error, "
172 "expected to get sha, not '%s'", line
);
177 static int send_ref(const char *refname
, const unsigned char *sha1
)
179 struct object
*o
= parse_object(sha1
);
181 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname
);
182 if (o
->type
== tag_type
) {
184 packet_write(1, "%s %s^{}\n", sha1_to_hex(o
->sha1
), refname
);
189 static int upload_pack(void)
193 for_each_ref(send_ref
);
195 nr_needs
= receive_needs();
198 get_common_commits();
203 int main(int argc
, char **argv
)
209 for (i
= 1; i
< argc
; i
++) {
214 if (!strcmp(arg
, "--strict")) {
218 if (!strncmp(arg
, "--timeout=", 10)) {
219 timeout
= atoi(arg
+10);
222 if (!strcmp(arg
, "--")) {
229 usage(upload_pack_usage
);
232 /* chdir to the directory. If that fails, try appending ".git" */
233 if (chdir(dir
) < 0) {
234 if (strict
|| chdir(mkpath("%s.git", dir
)) < 0)
235 die("git-upload-pack unable to chdir to %s", dir
);
240 if (access("objects", X_OK
) || access("refs", X_OK
))
241 die("git-upload-pack: %s doesn't seem to be a git archive", dir
);