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 char *args[] = {
36 die("git-pack-objects exec failed (%s)", strerror(errno));
39 static void exec_rev_list(struct ref *refs)
41 static char *args[1000];
44 args[i++] = "rev-list"; /* 0 */
45 if (use_thin_pack) /* 1 */
46 args[i++] = "--objects-edge";
48 args[i++] = "--objects";
50 char *buf = malloc(100);
52 die("git-rev-list environment overflow");
53 if (!is_zero_sha1(refs->old_sha1) &&
54 has_sha1_file(refs->old_sha1)) {
56 snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
59 if (!is_zero_sha1(refs->new_sha1)) {
61 snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
67 die("git-rev-list exec failed (%s)", strerror(errno));
70 static void rev_list(int fd, struct ref *refs)
73 pid_t pack_objects_pid;
75 if (pipe(pipe_fd) < 0)
76 die("rev-list setup: pipe failed");
77 pack_objects_pid = fork();
78 if (!pack_objects_pid) {
85 die("pack-objects setup failed");
87 if (pack_objects_pid < 0)
88 die("pack-objects fork failed");
96 static int pack_objects(int fd, struct ref *refs)
100 rev_list_pid = fork();
103 die("rev-list setup failed");
105 if (rev_list_pid < 0)
106 die("rev-list fork failed");
108 * We don't wait for the rev-list pipeline in the parent:
109 * we end up waiting for the other end instead
114 static void unmark_and_free(struct commit_list *list, unsigned int mark)
117 struct commit_list *temp = list;
118 temp->item->object.flags &= ~mark;
124 static int ref_newer(const unsigned char *new_sha1,
125 const unsigned char *old_sha1)
128 struct commit *old, *new;
129 struct commit_list *list, *used;
132 /* Both new and old must be commit-ish and new is descendant of
133 * old. Otherwise we require --force.
135 o = deref_tag(parse_object(old_sha1), NULL, 0);
136 if (!o || o->type != commit_type)
138 old = (struct commit *) o;
140 o = deref_tag(parse_object(new_sha1), NULL, 0);
141 if (!o || o->type != commit_type)
143 new = (struct commit *) o;
145 if (parse_commit(new) < 0)
149 commit_list_insert(new, &list);
151 new = pop_most_recent_commit(&list, 1);
152 commit_list_insert(new, &used);
158 unmark_and_free(list, 1);
159 unmark_and_free(used, 1);
163 static struct ref *local_refs, **local_tail;
164 static struct ref *remote_refs, **remote_tail;
166 static int one_local_ref(const char *refname, const unsigned char *sha1)
169 int len = strlen(refname) + 1;
170 ref = xcalloc(1, sizeof(*ref) + len);
171 memcpy(ref->new_sha1, sha1, 20);
172 memcpy(ref->name, refname, len);
174 local_tail = &ref->next;
178 static void get_local_heads(void)
180 local_tail = &local_refs;
181 for_each_ref(one_local_ref);
184 static int receive_status(int in)
188 int len = packet_read_line(in, line, sizeof(line));
189 if (len < 10 || memcmp(line, "unpack ", 7)) {
190 fprintf(stderr, "did not receive status back\n");
193 if (memcmp(line, "unpack ok\n", 10)) {
198 len = packet_read_line(in, line, sizeof(line));
202 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
203 fprintf(stderr, "protocol error: %s\n", line);
207 if (!memcmp(line, "ok", 2))
215 static int send_pack(int in, int out, int nr_refspec, char **refspec)
220 int ask_for_status_report = 0;
221 int expect_status_report = 0;
223 /* No funny business with the matcher */
224 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, 1);
227 /* Does the other end support the reporting? */
228 if (server_supports("report-status"))
229 ask_for_status_report = 1;
233 remote_tail = &remote_refs;
234 if (match_refs(local_refs, remote_refs, &remote_tail,
235 nr_refspec, refspec, send_all))
239 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
244 * Finally, tell the other end!
247 for (ref = remote_refs; ref; ref = ref->next) {
248 char old_hex[60], *new_hex;
251 if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
253 fprintf(stderr, "'%s': up-to-date\n", ref->name);
257 /* This part determines what can overwrite what.
260 * (0) you can always use --force or +A:B notation to
261 * selectively force individual ref pairs.
263 * (1) if the old thing does not exist, it is OK.
265 * (2) if you do not have the old thing, you are not allowed
266 * to overwrite it; you would not know what you are losing
269 * (3) if both new and old are commit-ish, and new is a
270 * descendant of old, it is OK.
274 !is_zero_sha1(ref->old_sha1) &&
276 if (!has_sha1_file(ref->old_sha1) ||
277 !ref_newer(ref->peer_ref->new_sha1,
279 /* We do not have the remote ref, or
280 * we know that the remote ref is not
281 * an ancestor of what we are trying to
282 * push. Either way this can be losing
283 * commits at the remote end and likely
284 * we were not up to date to begin with.
286 error("remote '%s' is not a strict "
287 "subset of local ref '%s'. "
288 "maybe you are not up-to-date and "
289 "need to pull first?",
291 ref->peer_ref->name);
296 memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
297 if (is_zero_sha1(ref->new_sha1)) {
298 error("cannot happen anymore");
303 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
304 new_hex = sha1_to_hex(ref->new_sha1);
306 if (ask_for_status_report) {
307 packet_write(out, "%s %s %s%c%s",
308 old_hex, new_hex, ref->name, 0,
310 ask_for_status_report = 0;
311 expect_status_report = 1;
314 packet_write(out, "%s %s %s",
315 old_hex, new_hex, ref->name);
316 fprintf(stderr, "updating '%s'", ref->name);
317 if (strcmp(ref->name, ref->peer_ref->name))
318 fprintf(stderr, " using '%s'", ref->peer_ref->name);
319 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
324 pack_objects(out, remote_refs);
327 if (expect_status_report) {
328 if (receive_status(in))
332 if (!new_refs && ret == 0)
333 fprintf(stderr, "Everything up-to-date\n");
338 int main(int argc, char **argv)
346 setup_git_directory();
348 for (i = 1; i < argc; i++, argv++) {
352 if (!strncmp(arg, "--exec=", 7)) {
356 if (!strcmp(arg, "--all")) {
360 if (!strcmp(arg, "--force")) {
364 if (!strcmp(arg, "--verbose")) {
368 if (!strcmp(arg, "--thin")) {
372 usage(send_pack_usage);
383 usage(send_pack_usage);
384 if (heads && send_all)
385 usage(send_pack_usage);
386 pid = git_connect(fd, dest, exec);
389 ret = send_pack(fd[0], fd[1], nr_heads, heads);