blob: 990be3f1a338a34025afa4acbe25f4c67ea6ce30 [file] [log] [blame]
Linus Torvalds61221472005-06-29 19:09:05 -07001#include "cache.h"
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -04002#include "commit.h"
Junio C Hamano37fde872005-08-05 00:47:56 -07003#include "tag.h"
Linus Torvalds584c6cc2005-07-08 13:58:40 -07004#include "refs.h"
Linus Torvaldsf3a32142005-06-29 20:50:15 -07005#include "pkt-line.h"
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05006#include "exec_cmd.h"
Linus Torvalds61221472005-06-29 19:09:05 -07007
Junio C Hamano2a245012005-07-14 00:10:05 -07008static const char send_pack_usage[] =
Junio C Hamano0bc3cdf2005-08-02 12:20:27 -07009"git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n"
10" --all and explicit <head> specification are mutually exclusive.";
Linus Torvalds61221472005-06-29 19:09:05 -070011static const char *exec = "git-receive-pack";
Linus Torvalds41f93a22005-12-20 18:13:02 -080012static int verbose = 0;
Linus Torvaldsd0893912005-07-16 13:26:33 -070013static int send_all = 0;
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -040014static int force_update = 0;
Linus Torvalds61221472005-06-29 19:09:05 -070015
Linus Torvalds584c6cc2005-07-08 13:58:40 -070016static 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
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070027static void exec_pack_objects(void)
28{
29 static char *args[] = {
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050030 "pack-objects",
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070031 "--stdout",
32 NULL
33 };
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050034 execv_git_cmd(args);
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070035 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
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050043 args[i++] = "rev-list"; /* 0 */
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070044 args[i++] = "--objects"; /* 1 */
45 while (refs) {
46 char *buf = malloc(100);
47 if (i > 900)
48 die("git-rev-list environment overflow");
Junio C Hamano40b64d42005-08-03 12:41:12 -070049 if (!is_zero_sha1(refs->old_sha1) &&
50 has_sha1_file(refs->old_sha1)) {
Linus Torvalds584c6cc2005-07-08 13:58:40 -070051 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 }
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070059 refs = refs->next;
60 }
61 args[i] = NULL;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050062 execv_git_cmd(args);
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070063 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 */
Linus Torvalds7ec4e602005-07-03 10:00:01 -0700107 return 0;
Linus Torvalds94fdb7a2005-06-30 10:17:39 -0700108}
Linus Torvaldse4b5c7f2005-06-29 22:31:41 -0700109
Junio C Hamano51b0fca2005-08-05 23:05:33 -0700110static 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
Junio C Hamano37fde872005-08-05 00:47:56 -0700120static int ref_newer(const unsigned char *new_sha1,
121 const unsigned char *old_sha1)
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700122{
Junio C Hamano37fde872005-08-05 00:47:56 -0700123 struct object *o;
124 struct commit *old, *new;
Junio C Hamano51b0fca2005-08-05 23:05:33 -0700125 struct commit_list *list, *used;
126 int found = 0;
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -0400127
Junio C Hamano37fde872005-08-05 00:47:56 -0700128 /* Both new and old must be commit-ish and new is descendant of
129 * old. Otherwise we require --force.
130 */
Junio C Hamano9534f402005-11-02 15:19:13 -0800131 o = deref_tag(parse_object(old_sha1), NULL, 0);
Junio C Hamano37fde872005-08-05 00:47:56 -0700132 if (!o || o->type != commit_type)
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700133 return 0;
Junio C Hamano37fde872005-08-05 00:47:56 -0700134 old = (struct commit *) o;
135
Junio C Hamano9534f402005-11-02 15:19:13 -0800136 o = deref_tag(parse_object(new_sha1), NULL, 0);
Junio C Hamano37fde872005-08-05 00:47:56 -0700137 if (!o || o->type != commit_type)
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -0400138 return 0;
Junio C Hamano37fde872005-08-05 00:47:56 -0700139 new = (struct commit *) o;
140
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -0400141 if (parse_commit(new) < 0)
142 return 0;
Junio C Hamano51b0fca2005-08-05 23:05:33 -0700143
144 used = list = NULL;
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -0400145 commit_list_insert(new, &list);
Linus Torvaldsbdf25142005-07-26 20:04:22 -0700146 while (list) {
147 new = pop_most_recent_commit(&list, 1);
Junio C Hamano51b0fca2005-08-05 23:05:33 -0700148 commit_list_insert(new, &used);
149 if (new == old) {
150 found = 1;
151 break;
152 }
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -0400153 }
Junio C Hamano51b0fca2005-08-05 23:05:33 -0700154 unmark_and_free(list, 1);
155 unmark_and_free(used, 1);
156 return found;
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700157}
158
Junio C Hamanof88395a2005-08-03 16:35:29 -0700159static struct ref *local_refs, **local_tail;
160static struct ref *remote_refs, **remote_tail;
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700161
Junio C Hamanof88395a2005-08-03 16:35:29 -0700162static int one_local_ref(const char *refname, const unsigned char *sha1)
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700163{
164 struct ref *ref;
Junio C Hamanof88395a2005-08-03 16:35:29 -0700165 int len = strlen(refname) + 1;
166 ref = xcalloc(1, sizeof(*ref) + len);
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700167 memcpy(ref->new_sha1, sha1, 20);
168 memcpy(ref->name, refname, len);
Junio C Hamanof88395a2005-08-03 16:35:29 -0700169 *local_tail = ref;
170 local_tail = &ref->next;
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700171 return 0;
172}
173
Junio C Hamanof88395a2005-08-03 16:35:29 -0700174static void get_local_heads(void)
Linus Torvalds61221472005-06-29 19:09:05 -0700175{
Junio C Hamanof88395a2005-08-03 16:35:29 -0700176 local_tail = &local_refs;
177 for_each_ref(one_local_ref);
178}
179
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800180static 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
Junio C Hamanof88395a2005-08-03 16:35:29 -0700211static int send_pack(int in, int out, int nr_refspec, char **refspec)
212{
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700213 struct ref *ref;
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700214 int new_refs;
Petr Baudised249282005-12-14 01:45:40 +0100215 int ret = 0;
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800216 int ask_for_status_report = 0;
217 int expect_status_report = 0;
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700218
Junio C Hamanof88395a2005-08-03 16:35:29 -0700219 /* No funny business with the matcher */
Junio C Hamano1a7141f2005-10-13 18:57:40 -0700220 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, 1);
Junio C Hamanof88395a2005-08-03 16:35:29 -0700221 get_local_heads();
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700222
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800223 /* Does the other end support the reporting? */
224 if (server_supports("report-status"))
225 ask_for_status_report = 1;
226
Junio C Hamanof88395a2005-08-03 16:35:29 -0700227 /* 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;
Daniel Barkalow4c353e82005-12-04 11:59:37 -0500233
234 if (!remote_refs) {
235 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
236 return 0;
237 }
238
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700239 /*
240 * Finally, tell the other end!
241 */
242 new_refs = 0;
Junio C Hamanof88395a2005-08-03 16:35:29 -0700243 for (ref = remote_refs; ref; ref = ref->next) {
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700244 char old_hex[60], *new_hex;
Junio C Hamanof88395a2005-08-03 16:35:29 -0700245 if (!ref->peer_ref)
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700246 continue;
Junio C Hamano37fde872005-08-05 00:47:56 -0700247 if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
Linus Torvalds41f93a22005-12-20 18:13:02 -0800248 if (verbose)
249 fprintf(stderr, "'%s': up-to-date\n", ref->name);
Junio C Hamano37fde872005-08-05 00:47:56 -0700250 continue;
251 }
252
253 /* This part determines what can overwrite what.
254 * The rules are:
255 *
Junio C Hamanoff27adf2005-08-24 00:40:14 -0700256 * (0) you can always use --force or +A:B notation to
257 * selectively force individual ref pairs.
Junio C Hamano37fde872005-08-05 00:47:56 -0700258 *
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
Junio C Hamanoff27adf2005-08-24 00:40:14 -0700269 if (!force_update &&
270 !is_zero_sha1(ref->old_sha1) &&
271 !ref->force) {
Junio C Hamano69310a32005-12-22 12:39:39 -0800272 if (!has_sha1_file(ref->old_sha1) ||
273 !ref_newer(ref->peer_ref->new_sha1,
Junio C Hamanof88395a2005-08-03 16:35:29 -0700274 ref->old_sha1)) {
Junio C Hamano69310a32005-12-22 12:39:39 -0800275 /* 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,
Junio C Hamanof88395a2005-08-03 16:35:29 -0700287 ref->peer_ref->name);
Petr Baudised249282005-12-14 01:45:40 +0100288 ret = -2;
Junio C Hamanof88395a2005-08-03 16:35:29 -0700289 continue;
290 }
291 }
Junio C Hamanof88395a2005-08-03 16:35:29 -0700292 memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
293 if (is_zero_sha1(ref->new_sha1)) {
294 error("cannot happen anymore");
Petr Baudised249282005-12-14 01:45:40 +0100295 ret = -3;
Junio C Hamanof88395a2005-08-03 16:35:29 -0700296 continue;
297 }
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700298 new_refs++;
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700299 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
300 new_hex = sha1_to_hex(ref->new_sha1);
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800301
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);
Junio C Hamanof88395a2005-08-03 16:35:29 -0700312 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);
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700316 }
Junio C Hamanof88395a2005-08-03 16:35:29 -0700317
Linus Torvaldsf3a32142005-06-29 20:50:15 -0700318 packet_flush(out);
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700319 if (new_refs)
Junio C Hamanof88395a2005-08-03 16:35:29 -0700320 pack_objects(out, remote_refs);
Linus Torvalds61221472005-06-29 19:09:05 -0700321 close(out);
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800322
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");
Petr Baudised249282005-12-14 01:45:40 +0100330 return ret;
Linus Torvalds61221472005-06-29 19:09:05 -0700331}
332
Junio C Hamanof88395a2005-08-03 16:35:29 -0700333
Linus Torvalds61221472005-06-29 19:09:05 -0700334int main(int argc, char **argv)
335{
336 int i, nr_heads = 0;
337 char *dest = NULL;
338 char **heads = NULL;
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700339 int fd[2], ret;
340 pid_t pid;
Linus Torvalds61221472005-06-29 19:09:05 -0700341
Junio C Hamano5a327712005-11-26 00:47:59 -0800342 setup_git_directory();
Linus Torvalds61221472005-06-29 19:09:05 -0700343 argv++;
Linus Torvaldsd0893912005-07-16 13:26:33 -0700344 for (i = 1; i < argc; i++, argv++) {
345 char *arg = *argv;
Linus Torvalds61221472005-06-29 19:09:05 -0700346
347 if (*arg == '-') {
348 if (!strncmp(arg, "--exec=", 7)) {
349 exec = arg + 7;
350 continue;
351 }
Linus Torvaldsd0893912005-07-16 13:26:33 -0700352 if (!strcmp(arg, "--all")) {
353 send_all = 1;
354 continue;
355 }
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -0400356 if (!strcmp(arg, "--force")) {
357 force_update = 1;
358 continue;
359 }
Linus Torvalds41f93a22005-12-20 18:13:02 -0800360 if (!strcmp(arg, "--verbose")) {
361 verbose = 1;
362 continue;
363 }
Linus Torvalds61221472005-06-29 19:09:05 -0700364 usage(send_pack_usage);
365 }
Linus Torvaldsd0893912005-07-16 13:26:33 -0700366 if (!dest) {
367 dest = arg;
368 continue;
369 }
Linus Torvalds61221472005-06-29 19:09:05 -0700370 heads = argv;
Linus Torvaldsd0893912005-07-16 13:26:33 -0700371 nr_heads = argc - i;
Linus Torvalds61221472005-06-29 19:09:05 -0700372 break;
373 }
374 if (!dest)
375 usage(send_pack_usage);
Junio C Hamano0bc3cdf2005-08-02 12:20:27 -0700376 if (heads && send_all)
377 usage(send_pack_usage);
Linus Torvaldsf7192592005-07-04 11:57:58 -0700378 pid = git_connect(fd, dest, exec);
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700379 if (pid < 0)
Linus Torvalds61221472005-06-29 19:09:05 -0700380 return 1;
Linus Torvaldsd0efc8a2005-06-30 12:28:24 -0700381 ret = send_pack(fd[0], fd[1], nr_heads, heads);
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700382 close(fd[0]);
383 close(fd[1]);
Linus Torvaldsf7192592005-07-04 11:57:58 -0700384 finish_connect(pid);
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700385 return ret;
Linus Torvalds61221472005-06-29 19:09:05 -0700386}