blob: c8ffc8d537d68e8296d8112582fd76e5148a9f98 [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;
Junio C Hamano2245be32006-02-19 15:03:49 -080015static int use_thin_pack = 0;
Linus Torvalds61221472005-06-29 19:09:05 -070016
Linus Torvalds584c6cc2005-07-08 13:58:40 -070017static 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
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070028static void exec_pack_objects(void)
29{
Junio C Hamano9201c702006-03-05 02:47:29 -080030 static const char *args[] = {
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050031 "pack-objects",
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070032 "--stdout",
33 NULL
34 };
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050035 execv_git_cmd(args);
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070036 die("git-pack-objects exec failed (%s)", strerror(errno));
37}
38
39static void exec_rev_list(struct ref *refs)
40{
Junio C Hamano797656e2006-02-21 18:59:37 -080041 struct ref *ref;
Junio C Hamano9201c702006-03-05 02:47:29 -080042 static const char *args[1000];
Junio C Hamano797656e2006-02-21 18:59:37 -080043 int i = 0, j;
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070044
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050045 args[i++] = "rev-list"; /* 0 */
Junio C Hamano2245be32006-02-19 15:03:49 -080046 if (use_thin_pack) /* 1 */
47 args[i++] = "--objects-edge";
48 else
49 args[i++] = "--objects";
Junio C Hamano797656e2006-02-21 18:59:37 -080050
51 /* First send the ones we care about most */
52 for (ref = refs; ref; ref = ref->next) {
53 if (900 < i)
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070054 die("git-rev-list environment overflow");
Junio C Hamano797656e2006-02-21 18:59:37 -080055 if (!is_zero_sha1(ref->new_sha1)) {
56 char *buf = malloc(100);
Linus Torvalds584c6cc2005-07-08 13:58:40 -070057 args[i++] = buf;
Junio C Hamano797656e2006-02-21 18:59:37 -080058 snprintf(buf, 50, "%s", sha1_to_hex(ref->new_sha1));
Linus Torvalds584c6cc2005-07-08 13:58:40 -070059 buf += 50;
Junio C Hamano797656e2006-02-21 18:59:37 -080060 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 }
Linus Torvalds584c6cc2005-07-08 13:58:40 -070066 }
Junio C Hamano797656e2006-02-21 18:59:37 -080067 }
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);
Linus Torvalds584c6cc2005-07-08 13:58:40 -070079 args[i++] = buf;
Junio C Hamano797656e2006-02-21 18:59:37 -080080 snprintf(buf, 42, "^%s", sha1_to_hex(ref->old_sha1));
Linus Torvalds584c6cc2005-07-08 13:58:40 -070081 }
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070082 }
83 args[i] = NULL;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050084 execv_git_cmd(args);
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070085 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
114static int pack_objects(int fd, struct ref *refs)
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 */
Linus Torvalds7ec4e602005-07-03 10:00:01 -0700129 return 0;
Linus Torvalds94fdb7a2005-06-30 10:17:39 -0700130}
Linus Torvaldse4b5c7f2005-06-29 22:31:41 -0700131
Junio C Hamano51b0fca2005-08-05 23:05:33 -0700132static void unmark_and_free(struct commit_list *list, unsigned int mark)
133{
134 while (list) {
135 struct commit_list *temp = list;
136 temp->item->object.flags &= ~mark;
137 list = temp->next;
138 free(temp);
139 }
140}
141
Junio C Hamano37fde872005-08-05 00:47:56 -0700142static int ref_newer(const unsigned char *new_sha1,
143 const unsigned char *old_sha1)
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700144{
Junio C Hamano37fde872005-08-05 00:47:56 -0700145 struct object *o;
146 struct commit *old, *new;
Junio C Hamano51b0fca2005-08-05 23:05:33 -0700147 struct commit_list *list, *used;
148 int found = 0;
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -0400149
Junio C Hamano37fde872005-08-05 00:47:56 -0700150 /* Both new and old must be commit-ish and new is descendant of
151 * old. Otherwise we require --force.
152 */
Junio C Hamano9534f402005-11-02 15:19:13 -0800153 o = deref_tag(parse_object(old_sha1), NULL, 0);
Junio C Hamano37fde872005-08-05 00:47:56 -0700154 if (!o || o->type != commit_type)
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700155 return 0;
Junio C Hamano37fde872005-08-05 00:47:56 -0700156 old = (struct commit *) o;
157
Junio C Hamano9534f402005-11-02 15:19:13 -0800158 o = deref_tag(parse_object(new_sha1), NULL, 0);
Junio C Hamano37fde872005-08-05 00:47:56 -0700159 if (!o || o->type != commit_type)
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -0400160 return 0;
Junio C Hamano37fde872005-08-05 00:47:56 -0700161 new = (struct commit *) o;
162
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -0400163 if (parse_commit(new) < 0)
164 return 0;
Junio C Hamano51b0fca2005-08-05 23:05:33 -0700165
166 used = list = NULL;
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -0400167 commit_list_insert(new, &list);
Linus Torvaldsbdf25142005-07-26 20:04:22 -0700168 while (list) {
169 new = pop_most_recent_commit(&list, 1);
Junio C Hamano51b0fca2005-08-05 23:05:33 -0700170 commit_list_insert(new, &used);
171 if (new == old) {
172 found = 1;
173 break;
174 }
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -0400175 }
Junio C Hamano51b0fca2005-08-05 23:05:33 -0700176 unmark_and_free(list, 1);
177 unmark_and_free(used, 1);
178 return found;
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700179}
180
Junio C Hamanof88395a2005-08-03 16:35:29 -0700181static struct ref *local_refs, **local_tail;
182static struct ref *remote_refs, **remote_tail;
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700183
Junio C Hamanof88395a2005-08-03 16:35:29 -0700184static int one_local_ref(const char *refname, const unsigned char *sha1)
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700185{
186 struct ref *ref;
Junio C Hamanof88395a2005-08-03 16:35:29 -0700187 int len = strlen(refname) + 1;
188 ref = xcalloc(1, sizeof(*ref) + len);
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700189 memcpy(ref->new_sha1, sha1, 20);
190 memcpy(ref->name, refname, len);
Junio C Hamanof88395a2005-08-03 16:35:29 -0700191 *local_tail = ref;
192 local_tail = &ref->next;
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700193 return 0;
194}
195
Junio C Hamanof88395a2005-08-03 16:35:29 -0700196static void get_local_heads(void)
Linus Torvalds61221472005-06-29 19:09:05 -0700197{
Junio C Hamanof88395a2005-08-03 16:35:29 -0700198 local_tail = &local_refs;
199 for_each_ref(one_local_ref);
200}
201
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800202static int receive_status(int in)
203{
204 char line[1000];
205 int ret = 0;
206 int len = packet_read_line(in, line, sizeof(line));
207 if (len < 10 || memcmp(line, "unpack ", 7)) {
208 fprintf(stderr, "did not receive status back\n");
209 return -1;
210 }
211 if (memcmp(line, "unpack ok\n", 10)) {
212 fputs(line, stderr);
213 ret = -1;
214 }
215 while (1) {
216 len = packet_read_line(in, line, sizeof(line));
217 if (!len)
218 break;
219 if (len < 3 ||
220 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
221 fprintf(stderr, "protocol error: %s\n", line);
222 ret = -1;
223 break;
224 }
225 if (!memcmp(line, "ok", 2))
226 continue;
227 fputs(line, stderr);
228 ret = -1;
229 }
230 return ret;
231}
232
Junio C Hamanof88395a2005-08-03 16:35:29 -0700233static int send_pack(int in, int out, int nr_refspec, char **refspec)
234{
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700235 struct ref *ref;
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700236 int new_refs;
Petr Baudised249282005-12-14 01:45:40 +0100237 int ret = 0;
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800238 int ask_for_status_report = 0;
239 int expect_status_report = 0;
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700240
Junio C Hamanof88395a2005-08-03 16:35:29 -0700241 /* No funny business with the matcher */
Junio C Hamano1a7141f2005-10-13 18:57:40 -0700242 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, 1);
Junio C Hamanof88395a2005-08-03 16:35:29 -0700243 get_local_heads();
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700244
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800245 /* Does the other end support the reporting? */
246 if (server_supports("report-status"))
247 ask_for_status_report = 1;
248
Junio C Hamanof88395a2005-08-03 16:35:29 -0700249 /* match them up */
250 if (!remote_tail)
251 remote_tail = &remote_refs;
252 if (match_refs(local_refs, remote_refs, &remote_tail,
253 nr_refspec, refspec, send_all))
254 return -1;
Daniel Barkalow4c353e82005-12-04 11:59:37 -0500255
256 if (!remote_refs) {
257 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
258 return 0;
259 }
260
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700261 /*
262 * Finally, tell the other end!
263 */
264 new_refs = 0;
Junio C Hamanof88395a2005-08-03 16:35:29 -0700265 for (ref = remote_refs; ref; ref = ref->next) {
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700266 char old_hex[60], *new_hex;
Junio C Hamanof88395a2005-08-03 16:35:29 -0700267 if (!ref->peer_ref)
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700268 continue;
Junio C Hamano37fde872005-08-05 00:47:56 -0700269 if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
Linus Torvalds41f93a22005-12-20 18:13:02 -0800270 if (verbose)
271 fprintf(stderr, "'%s': up-to-date\n", ref->name);
Junio C Hamano37fde872005-08-05 00:47:56 -0700272 continue;
273 }
274
275 /* This part determines what can overwrite what.
276 * The rules are:
277 *
Junio C Hamanoff27adf2005-08-24 00:40:14 -0700278 * (0) you can always use --force or +A:B notation to
279 * selectively force individual ref pairs.
Junio C Hamano37fde872005-08-05 00:47:56 -0700280 *
281 * (1) if the old thing does not exist, it is OK.
282 *
283 * (2) if you do not have the old thing, you are not allowed
284 * to overwrite it; you would not know what you are losing
285 * otherwise.
286 *
287 * (3) if both new and old are commit-ish, and new is a
288 * descendant of old, it is OK.
289 */
290
Junio C Hamanoff27adf2005-08-24 00:40:14 -0700291 if (!force_update &&
292 !is_zero_sha1(ref->old_sha1) &&
293 !ref->force) {
Junio C Hamano69310a32005-12-22 12:39:39 -0800294 if (!has_sha1_file(ref->old_sha1) ||
295 !ref_newer(ref->peer_ref->new_sha1,
Junio C Hamanof88395a2005-08-03 16:35:29 -0700296 ref->old_sha1)) {
Junio C Hamano69310a32005-12-22 12:39:39 -0800297 /* We do not have the remote ref, or
298 * we know that the remote ref is not
299 * an ancestor of what we are trying to
300 * push. Either way this can be losing
301 * commits at the remote end and likely
302 * we were not up to date to begin with.
303 */
304 error("remote '%s' is not a strict "
305 "subset of local ref '%s'. "
306 "maybe you are not up-to-date and "
307 "need to pull first?",
308 ref->name,
Junio C Hamanof88395a2005-08-03 16:35:29 -0700309 ref->peer_ref->name);
Petr Baudised249282005-12-14 01:45:40 +0100310 ret = -2;
Junio C Hamanof88395a2005-08-03 16:35:29 -0700311 continue;
312 }
313 }
Junio C Hamanof88395a2005-08-03 16:35:29 -0700314 memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
315 if (is_zero_sha1(ref->new_sha1)) {
316 error("cannot happen anymore");
Petr Baudised249282005-12-14 01:45:40 +0100317 ret = -3;
Junio C Hamanof88395a2005-08-03 16:35:29 -0700318 continue;
319 }
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700320 new_refs++;
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700321 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
322 new_hex = sha1_to_hex(ref->new_sha1);
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800323
324 if (ask_for_status_report) {
325 packet_write(out, "%s %s %s%c%s",
326 old_hex, new_hex, ref->name, 0,
327 "report-status");
328 ask_for_status_report = 0;
329 expect_status_report = 1;
330 }
331 else
332 packet_write(out, "%s %s %s",
333 old_hex, new_hex, ref->name);
Junio C Hamanof88395a2005-08-03 16:35:29 -0700334 fprintf(stderr, "updating '%s'", ref->name);
335 if (strcmp(ref->name, ref->peer_ref->name))
336 fprintf(stderr, " using '%s'", ref->peer_ref->name);
337 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700338 }
Junio C Hamanof88395a2005-08-03 16:35:29 -0700339
Linus Torvaldsf3a32142005-06-29 20:50:15 -0700340 packet_flush(out);
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700341 if (new_refs)
Junio C Hamanof88395a2005-08-03 16:35:29 -0700342 pack_objects(out, remote_refs);
Linus Torvalds61221472005-06-29 19:09:05 -0700343 close(out);
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800344
345 if (expect_status_report) {
346 if (receive_status(in))
347 ret = -4;
348 }
349
350 if (!new_refs && ret == 0)
351 fprintf(stderr, "Everything up-to-date\n");
Petr Baudised249282005-12-14 01:45:40 +0100352 return ret;
Linus Torvalds61221472005-06-29 19:09:05 -0700353}
354
Junio C Hamanof88395a2005-08-03 16:35:29 -0700355
Linus Torvalds61221472005-06-29 19:09:05 -0700356int main(int argc, char **argv)
357{
358 int i, nr_heads = 0;
359 char *dest = NULL;
360 char **heads = NULL;
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700361 int fd[2], ret;
362 pid_t pid;
Linus Torvalds61221472005-06-29 19:09:05 -0700363
Junio C Hamano5a327712005-11-26 00:47:59 -0800364 setup_git_directory();
Linus Torvalds61221472005-06-29 19:09:05 -0700365 argv++;
Linus Torvaldsd0893912005-07-16 13:26:33 -0700366 for (i = 1; i < argc; i++, argv++) {
367 char *arg = *argv;
Linus Torvalds61221472005-06-29 19:09:05 -0700368
369 if (*arg == '-') {
370 if (!strncmp(arg, "--exec=", 7)) {
371 exec = arg + 7;
372 continue;
373 }
Linus Torvaldsd0893912005-07-16 13:26:33 -0700374 if (!strcmp(arg, "--all")) {
375 send_all = 1;
376 continue;
377 }
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -0400378 if (!strcmp(arg, "--force")) {
379 force_update = 1;
380 continue;
381 }
Linus Torvalds41f93a22005-12-20 18:13:02 -0800382 if (!strcmp(arg, "--verbose")) {
383 verbose = 1;
384 continue;
385 }
Junio C Hamano2245be32006-02-19 15:03:49 -0800386 if (!strcmp(arg, "--thin")) {
387 use_thin_pack = 1;
388 continue;
389 }
Linus Torvalds61221472005-06-29 19:09:05 -0700390 usage(send_pack_usage);
391 }
Linus Torvaldsd0893912005-07-16 13:26:33 -0700392 if (!dest) {
393 dest = arg;
394 continue;
395 }
Linus Torvalds61221472005-06-29 19:09:05 -0700396 heads = argv;
Linus Torvaldsd0893912005-07-16 13:26:33 -0700397 nr_heads = argc - i;
Linus Torvalds61221472005-06-29 19:09:05 -0700398 break;
399 }
400 if (!dest)
401 usage(send_pack_usage);
Junio C Hamano0bc3cdf2005-08-02 12:20:27 -0700402 if (heads && send_all)
403 usage(send_pack_usage);
Linus Torvaldsf7192592005-07-04 11:57:58 -0700404 pid = git_connect(fd, dest, exec);
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700405 if (pid < 0)
Linus Torvalds61221472005-06-29 19:09:05 -0700406 return 1;
Linus Torvaldsd0efc8a2005-06-30 12:28:24 -0700407 ret = send_pack(fd[0], fd[1], nr_heads, heads);
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700408 close(fd[0]);
409 close(fd[1]);
Linus Torvaldsf7192592005-07-04 11:57:58 -0700410 finish_connect(pid);
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700411 return ret;
Linus Torvalds61221472005-06-29 19:09:05 -0700412}