8 static const char send_pack_usage[] =
9 "git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n"
10 " --all and explicit <head> specification are mutually exclusive.";
11 static const char *exec = "git-receive-pack";
12 static int verbose = 0;
13 static int send_all = 0;
14 static int force_update = 0;
15 static int use_thin_pack = 0;
17 static int is_zero_sha1(const unsigned char *sha1)
21 for (i = 0; i < 20; i++) {
28 static void exec_pack_objects(void)
30 static const char *args[] = {
36 die("git-pack-objects exec failed (%s)", strerror(errno));
39 static void exec_rev_list(struct ref *refs)
42 static const char *args[1000];
45 args[i++] = "rev-list"; /* 0 */
46 if (use_thin_pack) /* 1 */
47 args[i++] = "--objects-edge";
49 args[i++] = "--objects";
51 /* First send the ones we care about most */
52 for (ref = refs; ref; ref = ref->next) {
54 die("git-rev-list environment overflow");
55 if (!is_zero_sha1(ref->new_sha1)) {
56 char *buf = malloc(100);
58 snprintf(buf, 50, "%s", sha1_to_hex(ref->new_sha1));
60 if (!is_zero_sha1(ref->old_sha1) &&
61 has_sha1_file(ref->old_sha1)) {
63 snprintf(buf, 50, "^%s",
64 sha1_to_hex(ref->old_sha1));
69 /* Then a handful of the remainder
70 * NEEDSWORK: we would be better off if used the newer ones first.
72 for (ref = refs, j = i + 16;
73 i < 900 && i < j && ref;
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);
80 snprintf(buf, 42, "^%s", sha1_to_hex(ref->old_sha1));
85 die("git-rev-list exec failed (%s)", strerror(errno));
88 static void rev_list(int fd, struct ref *refs)
91 pid_t pack_objects_pid;
93 if (pipe(pipe_fd) < 0)
94 die("rev-list setup: pipe failed");
95 pack_objects_pid = fork();
96 if (!pack_objects_pid) {
103 die("pack-objects setup failed");
105 if (pack_objects_pid < 0)
106 die("pack-objects fork failed");
114 static int pack_objects(int fd, struct ref *refs)
118 rev_list_pid = fork();
121 die("rev-list setup failed");
123 if (rev_list_pid < 0)
124 die("rev-list fork failed");
126 * We don't wait for the rev-list pipeline in the parent:
127 * we end up waiting for the other end instead
132 static void unmark_and_free(struct commit_list *list, unsigned int mark)
135 struct commit_list *temp = list;
136 temp->item->object.flags &= ~mark;
142 static int ref_newer(const unsigned char *new_sha1,
143 const unsigned char *old_sha1)
146 struct commit *old, *new;
147 struct commit_list *list, *used;
150 /* Both new and old must be commit-ish and new is descendant of
151 * old. Otherwise we require --force.
153 o = deref_tag(parse_object(old_sha1), NULL, 0);
154 if (!o || o->type != commit_type)
156 old = (struct commit *) o;
158 o = deref_tag(parse_object(new_sha1), NULL, 0);
159 if (!o || o->type != commit_type)
161 new = (struct commit *) o;
163 if (parse_commit(new) < 0)
167 commit_list_insert(new, &list);
169 new = pop_most_recent_commit(&list, 1);
170 commit_list_insert(new, &used);
176 unmark_and_free(list, 1);
177 unmark_and_free(used, 1);
181 static struct ref *local_refs, **local_tail;
182 static struct ref *remote_refs, **remote_tail;
184 static int one_local_ref(const char *refname, const unsigned char *sha1)
187 int len = strlen(refname) + 1;
188 ref = xcalloc(1, sizeof(*ref) + len);
189 memcpy(ref->new_sha1, sha1, 20);
190 memcpy(ref->name, refname, len);
192 local_tail = &ref->next;
196 static void get_local_heads(void)
198 local_tail = &local_refs;
199 for_each_ref(one_local_ref);
202 static int receive_status(int in)
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");
211 if (memcmp(line, "unpack ok\n", 10)) {
216 len = packet_read_line(in, line, sizeof(line));
220 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
221 fprintf(stderr, "protocol error: %s\n", line);
225 if (!memcmp(line, "ok", 2))
233 static int send_pack(int in, int out, int nr_refspec, char **refspec)
238 int ask_for_status_report = 0;
239 int expect_status_report = 0;
241 /* No funny business with the matcher */
242 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, 1);
245 /* Does the other end support the reporting? */
246 if (server_supports("report-status"))
247 ask_for_status_report = 1;
251 remote_tail = &remote_refs;
252 if (match_refs(local_refs, remote_refs, &remote_tail,
253 nr_refspec, refspec, send_all))
257 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
262 * Finally, tell the other end!
265 for (ref = remote_refs; ref; ref = ref->next) {
266 char old_hex[60], *new_hex;
269 if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
271 fprintf(stderr, "'%s': up-to-date\n", ref->name);
275 /* This part determines what can overwrite what.
278 * (0) you can always use --force or +A:B notation to
279 * selectively force individual ref pairs.
281 * (1) if the old thing does not exist, it is OK.
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
287 * (3) if both new and old are commit-ish, and new is a
288 * descendant of old, it is OK.
292 !is_zero_sha1(ref->old_sha1) &&
294 if (!has_sha1_file(ref->old_sha1) ||
295 !ref_newer(ref->peer_ref->new_sha1,
297 /* 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.
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?",
309 ref->peer_ref->name);
314 memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
315 if (is_zero_sha1(ref->new_sha1)) {
316 error("cannot happen anymore");
321 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
322 new_hex = sha1_to_hex(ref->new_sha1);
324 if (ask_for_status_report) {
325 packet_write(out, "%s %s %s%c%s",
326 old_hex, new_hex, ref->name, 0,
328 ask_for_status_report = 0;
329 expect_status_report = 1;
332 packet_write(out, "%s %s %s",
333 old_hex, new_hex, ref->name);
334 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);
342 pack_objects(out, remote_refs);
345 if (expect_status_report) {
346 if (receive_status(in))
350 if (!new_refs && ret == 0)
351 fprintf(stderr, "Everything up-to-date\n");
356 int main(int argc, char **argv)
364 setup_git_directory();
366 for (i = 1; i < argc; i++, argv++) {
370 if (!strncmp(arg, "--exec=", 7)) {
374 if (!strcmp(arg, "--all")) {
378 if (!strcmp(arg, "--force")) {
382 if (!strcmp(arg, "--verbose")) {
386 if (!strcmp(arg, "--thin")) {
390 usage(send_pack_usage);
401 usage(send_pack_usage);
402 if (heads && send_all)
403 usage(send_pack_usage);
404 pid = git_connect(fd, dest, exec);
407 ret = send_pack(fd[0], fd[1], nr_heads, heads);