7 static const char send_pack_usage[] =
8 "git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n"
9 " --all and explicit <head> specification are mutually exclusive.";
10 static const char *exec = "git-receive-pack";
11 static int send_all = 0;
12 static int force_update = 0;
14 static int is_zero_sha1(const unsigned char *sha1)
18 for (i = 0; i < 20; i++) {
25 static void exec_pack_objects(void)
27 static char *args[] = {
32 execvp("git-pack-objects", args);
33 die("git-pack-objects exec failed (%s)", strerror(errno));
36 static void exec_rev_list(struct ref *refs)
38 static char *args[1000];
41 args[i++] = "git-rev-list"; /* 0 */
42 args[i++] = "--objects"; /* 1 */
44 char *buf = malloc(100);
46 die("git-rev-list environment overflow");
47 if (!is_zero_sha1(refs->old_sha1) &&
48 has_sha1_file(refs->old_sha1)) {
50 snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
53 if (!is_zero_sha1(refs->new_sha1)) {
55 snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
60 execvp("git-rev-list", args);
61 die("git-rev-list exec failed (%s)", strerror(errno));
64 static void rev_list(int fd, struct ref *refs)
67 pid_t pack_objects_pid;
69 if (pipe(pipe_fd) < 0)
70 die("rev-list setup: pipe failed");
71 pack_objects_pid = fork();
72 if (!pack_objects_pid) {
79 die("pack-objects setup failed");
81 if (pack_objects_pid < 0)
82 die("pack-objects fork failed");
90 static int pack_objects(int fd, struct ref *refs)
94 rev_list_pid = fork();
97 die("rev-list setup failed");
100 die("rev-list fork failed");
102 * We don't wait for the rev-list pipeline in the parent:
103 * we end up waiting for the other end instead
108 static int ref_newer(const unsigned char *new_sha1,
109 const unsigned char *old_sha1)
112 struct commit *old, *new;
113 struct commit_list *list;
115 /* Both new and old must be commit-ish and new is descendant of
116 * old. Otherwise we require --force.
118 o = deref_tag(parse_object(old_sha1));
119 if (!o || o->type != commit_type)
121 old = (struct commit *) o;
123 o = deref_tag(parse_object(new_sha1));
124 if (!o || o->type != commit_type)
126 new = (struct commit *) o;
128 if (parse_commit(new) < 0)
131 commit_list_insert(new, &list);
133 new = pop_most_recent_commit(&list, 1);
140 static struct ref *local_refs, **local_tail;
141 static struct ref *remote_refs, **remote_tail;
143 static int one_local_ref(const char *refname, const unsigned char *sha1)
146 int len = strlen(refname) + 1;
147 ref = xcalloc(1, sizeof(*ref) + len);
148 memcpy(ref->new_sha1, sha1, 20);
149 memcpy(ref->name, refname, len);
151 local_tail = &ref->next;
155 static void get_local_heads(void)
157 local_tail = &local_refs;
158 for_each_ref(one_local_ref);
161 static int send_pack(int in, int out, int nr_refspec, char **refspec)
166 /* No funny business with the matcher */
167 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL);
172 remote_tail = &remote_refs;
173 if (match_refs(local_refs, remote_refs, &remote_tail,
174 nr_refspec, refspec, send_all))
177 * Finally, tell the other end!
180 for (ref = remote_refs; ref; ref = ref->next) {
181 char old_hex[60], *new_hex;
184 if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
185 fprintf(stderr, "'%s': up-to-date\n", ref->name);
189 /* This part determines what can overwrite what.
192 * (0) you can always use --force.
194 * (1) if the old thing does not exist, it is OK.
196 * (2) if you do not have the old thing, you are not allowed
197 * to overwrite it; you would not know what you are losing
200 * (3) if both new and old are commit-ish, and new is a
201 * descendant of old, it is OK.
204 if (!force_update && !is_zero_sha1(ref->old_sha1)) {
205 if (!has_sha1_file(ref->old_sha1)) {
206 error("remote '%s' object %s does not "
208 ref->name, sha1_to_hex(ref->old_sha1));
211 /* We assume that local is fsck-clean. Otherwise
212 * you _could_ have a old tag which points at
213 * something you do not have which may or may not
216 if (!ref_newer(ref->peer_ref->new_sha1,
218 error("remote ref '%s' is not a strict "
219 "subset of local ref '%s'.", ref->name,
220 ref->peer_ref->name);
224 memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
225 if (is_zero_sha1(ref->new_sha1)) {
226 error("cannot happen anymore");
230 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
231 new_hex = sha1_to_hex(ref->new_sha1);
232 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
233 fprintf(stderr, "updating '%s'", ref->name);
234 if (strcmp(ref->name, ref->peer_ref->name))
235 fprintf(stderr, " using '%s'", ref->peer_ref->name);
236 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
241 pack_objects(out, remote_refs);
247 int main(int argc, char **argv)
256 for (i = 1; i < argc; i++, argv++) {
260 if (!strncmp(arg, "--exec=", 7)) {
264 if (!strcmp(arg, "--all")) {
268 if (!strcmp(arg, "--force")) {
272 usage(send_pack_usage);
283 usage(send_pack_usage);
284 if (heads && send_all)
285 usage(send_pack_usage);
286 pid = git_connect(fd, dest, exec);
289 ret = send_pack(fd[0], fd[1], nr_heads, heads);