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