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