]> git.ipfire.org Git - thirdparty/git.git/blame - send-pack.c
instaweb: load Apache mime and dir modules if they are needed
[thirdparty/git.git] / send-pack.c
CommitLineData
61221472 1#include "cache.h"
2a9c3fe8 2#include "commit.h"
37fde874 3#include "tag.h"
584c6cc9 4#include "refs.h"
f3a3214e 5#include "pkt-line.h"
77cb17e9 6#include "exec_cmd.h"
61221472 7
2a245013 8static const char send_pack_usage[] =
0bc3cdfc
JH
9"git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n"
10" --all and explicit <head> specification are mutually exclusive.";
61221472 11static const char *exec = "git-receive-pack";
96f1e58f
DR
12static int verbose;
13static int send_all;
14static int force_update;
15static int use_thin_pack;
61221472 16
c727fe2a 17/*
0ae5f98c 18 * Make a pack stream and spit it out into file descriptor fd
c727fe2a 19 */
0ae5f98c 20static int pack_objects(int fd, struct ref *refs)
c727fe2a
AW
21{
22 int pipe_fd[2];
0ae5f98c 23 pid_t pid;
c727fe2a
AW
24
25 if (pipe(pipe_fd) < 0)
0ae5f98c
JH
26 return error("send-pack: pipe failed");
27 pid = fork();
28 if (!pid) {
29 /*
30 * The child becomes pack-objects --revs; we feed
31 * the revision parameters to it via its stdin and
32 * let its stdout go back to the other end.
c727fe2a 33 */
0ae5f98c
JH
34 static const char *args[] = {
35 "pack-objects",
36 "--all-progress",
37 "--revs",
38 "--stdout",
39 NULL,
40 NULL,
41 };
42 if (use_thin_pack)
43 args[4] = "--thin";
c727fe2a
AW
44 dup2(pipe_fd[0], 0);
45 dup2(fd, 1);
46 close(pipe_fd[0]);
47 close(pipe_fd[1]);
48 close(fd);
0ae5f98c
JH
49 execv_git_cmd(args);
50 die("git-pack-objects exec failed (%s)", strerror(errno));
c727fe2a 51 }
c727fe2a 52
0ae5f98c
JH
53 /*
54 * We feed the pack-objects we just spawned with revision
55 * parameters by writing to the pipe.
c727fe2a
AW
56 */
57 close(pipe_fd[0]);
58 close(fd);
0ae5f98c 59
c727fe2a
AW
60 while (refs) {
61 char buf[42];
62
63 if (!is_null_sha1(refs->old_sha1) &&
64 has_sha1_file(refs->old_sha1)) {
65 memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
66 buf[0] = '^';
67 buf[41] = '\n';
68 write(pipe_fd[1], buf, 42);
69 }
70 if (!is_null_sha1(refs->new_sha1)) {
71 memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
72 buf[40] = '\n';
73 write(pipe_fd[1], buf, 41);
74 }
75 refs = refs->next;
76 }
77 close(pipe_fd[1]);
c727fe2a 78
0ae5f98c
JH
79 for (;;) {
80 int status, code;
81 pid_t waiting = waitpid(pid, &status, 0);
94fdb7aa 82
0ae5f98c
JH
83 if (waiting < 0) {
84 if (errno == EINTR)
85 continue;
86 return error("waitpid failed (%s)", strerror(errno));
87 }
88 if ((waiting != pid) || WIFSIGNALED(status) ||
89 !WIFEXITED(status))
90 return error("pack-objects died with strange error");
91 code = WEXITSTATUS(status);
92 if (code)
93 return -code;
94 return 0;
94fdb7aa 95 }
94fdb7aa 96}
e4b5c7ff 97
51b0fca0
JH
98static void unmark_and_free(struct commit_list *list, unsigned int mark)
99{
100 while (list) {
101 struct commit_list *temp = list;
102 temp->item->object.flags &= ~mark;
103 list = temp->next;
104 free(temp);
105 }
106}
107
37fde874
JH
108static int ref_newer(const unsigned char *new_sha1,
109 const unsigned char *old_sha1)
584c6cc9 110{
37fde874
JH
111 struct object *o;
112 struct commit *old, *new;
51b0fca0
JH
113 struct commit_list *list, *used;
114 int found = 0;
2a9c3fe8 115
37fde874
JH
116 /* Both new and old must be commit-ish and new is descendant of
117 * old. Otherwise we require --force.
118 */
9534f40b 119 o = deref_tag(parse_object(old_sha1), NULL, 0);
1974632c 120 if (!o || o->type != OBJ_COMMIT)
584c6cc9 121 return 0;
37fde874
JH
122 old = (struct commit *) o;
123
9534f40b 124 o = deref_tag(parse_object(new_sha1), NULL, 0);
1974632c 125 if (!o || o->type != OBJ_COMMIT)
2a9c3fe8 126 return 0;
37fde874
JH
127 new = (struct commit *) o;
128
2a9c3fe8
LT
129 if (parse_commit(new) < 0)
130 return 0;
51b0fca0
JH
131
132 used = list = NULL;
2a9c3fe8 133 commit_list_insert(new, &list);
bdf25142
LT
134 while (list) {
135 new = pop_most_recent_commit(&list, 1);
51b0fca0
JH
136 commit_list_insert(new, &used);
137 if (new == old) {
138 found = 1;
139 break;
140 }
2a9c3fe8 141 }
51b0fca0
JH
142 unmark_and_free(list, 1);
143 unmark_and_free(used, 1);
144 return found;
584c6cc9
LT
145}
146
f88395ac
JH
147static struct ref *local_refs, **local_tail;
148static struct ref *remote_refs, **remote_tail;
584c6cc9 149
8da19775 150static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
584c6cc9
LT
151{
152 struct ref *ref;
f88395ac
JH
153 int len = strlen(refname) + 1;
154 ref = xcalloc(1, sizeof(*ref) + len);
e702496e 155 hashcpy(ref->new_sha1, sha1);
584c6cc9 156 memcpy(ref->name, refname, len);
f88395ac
JH
157 *local_tail = ref;
158 local_tail = &ref->next;
584c6cc9
LT
159 return 0;
160}
161
f88395ac
JH
162static void get_local_heads(void)
163{
164 local_tail = &local_refs;
cb5d709f 165 for_each_ref(one_local_ref, NULL);
f88395ac
JH
166}
167
cfee10a7
JH
168static int receive_status(int in)
169{
170 char line[1000];
171 int ret = 0;
172 int len = packet_read_line(in, line, sizeof(line));
173 if (len < 10 || memcmp(line, "unpack ", 7)) {
174 fprintf(stderr, "did not receive status back\n");
175 return -1;
176 }
177 if (memcmp(line, "unpack ok\n", 10)) {
178 fputs(line, stderr);
179 ret = -1;
180 }
181 while (1) {
182 len = packet_read_line(in, line, sizeof(line));
183 if (!len)
184 break;
185 if (len < 3 ||
186 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
187 fprintf(stderr, "protocol error: %s\n", line);
188 ret = -1;
189 break;
190 }
191 if (!memcmp(line, "ok", 2))
192 continue;
193 fputs(line, stderr);
194 ret = -1;
195 }
196 return ret;
197}
198
f88395ac 199static int send_pack(int in, int out, int nr_refspec, char **refspec)
61221472 200{
7f8e9828 201 struct ref *ref;
584c6cc9 202 int new_refs;
ed24928e 203 int ret = 0;
cfee10a7 204 int ask_for_status_report = 0;
d4f694ba 205 int allow_deleting_refs = 0;
cfee10a7 206 int expect_status_report = 0;
7f8e9828 207
f88395ac 208 /* No funny business with the matcher */
2718ff09 209 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
f88395ac 210 get_local_heads();
584c6cc9 211
cfee10a7
JH
212 /* Does the other end support the reporting? */
213 if (server_supports("report-status"))
214 ask_for_status_report = 1;
d4f694ba
JH
215 if (server_supports("delete-refs"))
216 allow_deleting_refs = 1;
cfee10a7 217
f88395ac
JH
218 /* match them up */
219 if (!remote_tail)
220 remote_tail = &remote_refs;
221 if (match_refs(local_refs, remote_refs, &remote_tail,
222 nr_refspec, refspec, send_all))
223 return -1;
4c353e89
DB
224
225 if (!remote_refs) {
226 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
227 return 0;
228 }
229
584c6cc9 230 /*
f88395ac 231 * Finally, tell the other end!
584c6cc9 232 */
f88395ac
JH
233 new_refs = 0;
234 for (ref = remote_refs; ref; ref = ref->next) {
235 char old_hex[60], *new_hex;
d4f694ba
JH
236 int delete_ref;
237
f88395ac 238 if (!ref->peer_ref)
7f8e9828 239 continue;
d4f694ba
JH
240
241 delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
242 if (delete_ref && !allow_deleting_refs) {
243 error("remote does not support deleting refs");
244 ret = -2;
245 continue;
246 }
247 if (!delete_ref &&
248 !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
41f93a2c
LT
249 if (verbose)
250 fprintf(stderr, "'%s': up-to-date\n", ref->name);
37fde874
JH
251 continue;
252 }
253
254 /* This part determines what can overwrite what.
255 * The rules are:
256 *
ff27adf3
JH
257 * (0) you can always use --force or +A:B notation to
258 * selectively force individual ref pairs.
37fde874
JH
259 *
260 * (1) if the old thing does not exist, it is OK.
261 *
262 * (2) if you do not have the old thing, you are not allowed
263 * to overwrite it; you would not know what you are losing
264 * otherwise.
265 *
266 * (3) if both new and old are commit-ish, and new is a
267 * descendant of old, it is OK.
d4f694ba
JH
268 *
269 * (4) regardless of all of the above, removing :B is
270 * always allowed.
37fde874
JH
271 */
272
ff27adf3 273 if (!force_update &&
d4f694ba 274 !delete_ref &&
4b4ee90e 275 !is_null_sha1(ref->old_sha1) &&
ff27adf3 276 !ref->force) {
69310a34
JH
277 if (!has_sha1_file(ref->old_sha1) ||
278 !ref_newer(ref->peer_ref->new_sha1,
f88395ac 279 ref->old_sha1)) {
69310a34
JH
280 /* We do not have the remote ref, or
281 * we know that the remote ref is not
282 * an ancestor of what we are trying to
283 * push. Either way this can be losing
284 * commits at the remote end and likely
285 * we were not up to date to begin with.
286 */
287 error("remote '%s' is not a strict "
288 "subset of local ref '%s'. "
289 "maybe you are not up-to-date and "
290 "need to pull first?",
291 ref->name,
f88395ac 292 ref->peer_ref->name);
ed24928e 293 ret = -2;
f88395ac
JH
294 continue;
295 }
e4b5c7ff 296 }
e702496e 297 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
d4f694ba
JH
298 if (!delete_ref)
299 new_refs++;
7f8e9828
LT
300 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
301 new_hex = sha1_to_hex(ref->new_sha1);
cfee10a7
JH
302
303 if (ask_for_status_report) {
304 packet_write(out, "%s %s %s%c%s",
305 old_hex, new_hex, ref->name, 0,
306 "report-status");
307 ask_for_status_report = 0;
308 expect_status_report = 1;
309 }
310 else
311 packet_write(out, "%s %s %s",
312 old_hex, new_hex, ref->name);
d4f694ba
JH
313 if (delete_ref)
314 fprintf(stderr, "deleting '%s'\n", ref->name);
315 else {
316 fprintf(stderr, "updating '%s'", ref->name);
317 if (strcmp(ref->name, ref->peer_ref->name))
318 fprintf(stderr, " using '%s'",
319 ref->peer_ref->name);
320 fprintf(stderr, "\n from %s\n to %s\n",
321 old_hex, new_hex);
322 }
61221472 323 }
f88395ac 324
f3a3214e 325 packet_flush(out);
584c6cc9 326 if (new_refs)
0ae5f98c 327 ret = pack_objects(out, remote_refs);
61221472 328 close(out);
cfee10a7
JH
329
330 if (expect_status_report) {
331 if (receive_status(in))
332 ret = -4;
333 }
334
335 if (!new_refs && ret == 0)
336 fprintf(stderr, "Everything up-to-date\n");
ed24928e 337 return ret;
61221472
LT
338}
339
37adac76
JH
340static void verify_remote_names(int nr_heads, char **heads)
341{
342 int i;
343
344 for (i = 0; i < nr_heads; i++) {
345 const char *remote = strchr(heads[i], ':');
346
347 remote = remote ? (remote + 1) : heads[i];
348 switch (check_ref_format(remote)) {
349 case 0: /* ok */
350 case -2: /* ok but a single level -- that is fine for
351 * a match pattern.
352 */
353 continue;
354 }
355 die("remote part of refspec is not a valid name in %s",
356 heads[i]);
357 }
358}
f88395ac 359
61221472
LT
360int main(int argc, char **argv)
361{
362 int i, nr_heads = 0;
363 char *dest = NULL;
364 char **heads = NULL;
7f8e9828
LT
365 int fd[2], ret;
366 pid_t pid;
61221472 367
5a327713 368 setup_git_directory();
84a9b58c
JH
369 git_config(git_default_config);
370
61221472 371 argv++;
d089391c
LT
372 for (i = 1; i < argc; i++, argv++) {
373 char *arg = *argv;
61221472
LT
374
375 if (*arg == '-') {
376 if (!strncmp(arg, "--exec=", 7)) {
377 exec = arg + 7;
378 continue;
379 }
d089391c
LT
380 if (!strcmp(arg, "--all")) {
381 send_all = 1;
382 continue;
383 }
2a9c3fe8
LT
384 if (!strcmp(arg, "--force")) {
385 force_update = 1;
386 continue;
387 }
41f93a2c
LT
388 if (!strcmp(arg, "--verbose")) {
389 verbose = 1;
390 continue;
391 }
2245be3e
JH
392 if (!strcmp(arg, "--thin")) {
393 use_thin_pack = 1;
394 continue;
395 }
61221472
LT
396 usage(send_pack_usage);
397 }
d089391c
LT
398 if (!dest) {
399 dest = arg;
400 continue;
401 }
61221472 402 heads = argv;
d089391c 403 nr_heads = argc - i;
61221472
LT
404 break;
405 }
406 if (!dest)
407 usage(send_pack_usage);
0bc3cdfc
JH
408 if (heads && send_all)
409 usage(send_pack_usage);
37adac76
JH
410 verify_remote_names(nr_heads, heads);
411
f7192598 412 pid = git_connect(fd, dest, exec);
7f8e9828 413 if (pid < 0)
61221472 414 return 1;
d0efc8a7 415 ret = send_pack(fd[0], fd[1], nr_heads, heads);
7f8e9828
LT
416 close(fd[0]);
417 close(fd[1]);
8a5dbef8
FBH
418 ret |= finish_connect(pid);
419 return !!ret;
61221472 420}