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