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