8 static const char upload_pack_usage
[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
10 #define THEY_HAVE (1U << 0)
11 #define OUR_REF (1U << 1)
12 #define WANTED (1U << 2)
15 static int nr_has
= 0, nr_needs
= 0, nr_our_refs
= 0;
16 static unsigned char has_sha1
[MAX_HAS
][20];
17 static unsigned char needs_sha1
[MAX_NEEDS
][20];
18 static unsigned int timeout
= 0;
20 static void reset_timeout(void)
25 static int strip(char *line
, int len
)
27 if (len
&& line
[len
-1] == '\n')
32 static void create_pack_file(void)
36 int create_full_pack
= (nr_our_refs
== nr_needs
&& !nr_has
);
39 die("git-upload-pack: unable to create pipe");
42 die("git-upload-pack: unable to fork git-rev-list");
54 args
= nr_has
+ nr_needs
+ 5;
55 argv
= xmalloc(args
* sizeof(char *));
56 buf
= xmalloc(args
* 45);
63 *p
++ = "git-rev-list";
65 if (create_full_pack
|| MAX_NEEDS
<= nr_needs
)
68 for (i
= 0; i
< nr_needs
; i
++) {
70 memcpy(buf
, sha1_to_hex(needs_sha1
[i
]), 41);
74 if (!create_full_pack
)
75 for (i
= 0; i
< nr_has
; i
++) {
78 memcpy(buf
, sha1_to_hex(has_sha1
[i
]), 41);
82 execvp("git-rev-list", argv
);
83 die("git-upload-pack: unable to exec git-rev-list");
88 execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL
);
89 die("git-upload-pack: unable to exec git-pack-objects");
92 static int got_sha1(char *hex
, unsigned char *sha1
)
94 if (get_sha1_hex(hex
, sha1
))
95 die("git-upload-pack: expected SHA1 object, got '%s'", hex
);
96 if (!has_sha1_file(sha1
))
98 if (nr_has
< MAX_HAS
) {
99 struct object
*o
= lookup_object(sha1
);
100 if (!(o
&& o
->parsed
))
101 o
= parse_object(sha1
);
103 die("oops (%s)", sha1_to_hex(sha1
));
104 if (o
->type
== commit_type
) {
105 struct commit_list
*parents
;
106 if (o
->flags
& THEY_HAVE
)
108 o
->flags
|= THEY_HAVE
;
109 for (parents
= ((struct commit
*)o
)->parents
;
111 parents
= parents
->next
)
112 parents
->item
->object
.flags
|= THEY_HAVE
;
114 memcpy(has_sha1
[nr_has
++], sha1
, 20);
119 static int get_common_commits(void)
121 static char line
[1000];
122 unsigned char sha1
[20];
125 track_object_refs
= 0;
126 save_commit_buffer
= 0;
129 len
= packet_read_line(0, line
, sizeof(line
));
133 packet_write(1, "NAK\n");
136 len
= strip(line
, len
);
137 if (!strncmp(line
, "have ", 5)) {
138 if (got_sha1(line
+5, sha1
)) {
139 packet_write(1, "ACK %s\n", sha1_to_hex(sha1
));
144 if (!strcmp(line
, "done")) {
145 packet_write(1, "NAK\n");
148 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
152 len
= packet_read_line(0, line
, sizeof(line
));
156 len
= strip(line
, len
);
157 if (!strncmp(line
, "have ", 5)) {
158 got_sha1(line
+5, sha1
);
161 if (!strcmp(line
, "done"))
163 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
168 static int receive_needs(void)
170 static char line
[1000];
176 unsigned char dummy
[20], *sha1_buf
;
177 len
= packet_read_line(0, line
, sizeof(line
));
183 if (needs
== MAX_NEEDS
) {
185 "warning: supporting only a max of %d requests. "
186 "sending everything instead.\n",
189 else if (needs
< MAX_NEEDS
)
190 sha1_buf
= needs_sha1
[needs
];
192 if (strncmp("want ", line
, 5) || get_sha1_hex(line
+5, sha1_buf
))
193 die("git-upload-pack: protocol error, "
194 "expected to get sha, not '%s'", line
);
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 struct object
*o
= parse_object(sha1
);
218 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname
);
219 if (!(o
->flags
& OUR_REF
)) {
223 if (o
->type
== tag_type
) {
225 packet_write(1, "%s %s^{}\n", sha1_to_hex(o
->sha1
), refname
);
230 static int upload_pack(void)
234 for_each_ref(send_ref
);
236 nr_needs
= receive_needs();
239 get_common_commits();
244 int main(int argc
, char **argv
)
250 for (i
= 1; i
< argc
; i
++) {
255 if (!strcmp(arg
, "--strict")) {
259 if (!strncmp(arg
, "--timeout=", 10)) {
260 timeout
= atoi(arg
+10);
263 if (!strcmp(arg
, "--")) {
270 usage(upload_pack_usage
);
273 /* chdir to the directory. If that fails, try appending ".git" */
274 if (chdir(dir
) < 0) {
275 if (strict
|| chdir(mkpath("%s.git", dir
)) < 0)
276 die("git-upload-pack unable to chdir to %s", dir
);
281 if (access("objects", X_OK
) || access("refs", X_OK
))
282 die("git-upload-pack: %s doesn't seem to be a git archive", dir
);