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;
16 static int is_zero_sha1(const unsigned char *sha1)
20 for (i = 0; i < 20; i++) {
27 static void exec_pack_objects(void)
29 static char *args[] = {
35 die("git-pack-objects exec failed (%s)", strerror(errno));
38 static void exec_rev_list(struct ref *refs)
41 static char *args[1000];
44 args[i++] = "rev-list"; /* 0 */
45 args[i++] = "--objects"; /* 1 */
47 /* First send the ones we care about most */
48 for (ref = refs; ref; ref = ref->next) {
50 die("git-rev-list environment overflow");
51 if (!is_zero_sha1(ref->new_sha1)) {
52 char *buf = malloc(100);
54 snprintf(buf, 50, "%s", sha1_to_hex(ref->new_sha1));
56 if (!is_zero_sha1(ref->old_sha1) &&
57 has_sha1_file(ref->old_sha1)) {
59 snprintf(buf, 50, "^%s",
60 sha1_to_hex(ref->old_sha1));
65 /* Then a handful of the remainder
66 * NEEDSWORK: we would be better off if used the newer ones first.
68 for (ref = refs, j = i + 16;
69 i < 900 && i < j && ref;
71 if (is_zero_sha1(ref->new_sha1) &&
72 !is_zero_sha1(ref->old_sha1) &&
73 has_sha1_file(ref->old_sha1)) {
74 char *buf = malloc(42);
76 snprintf(buf, 42, "^%s", sha1_to_hex(ref->old_sha1));
81 die("git-rev-list exec failed (%s)", strerror(errno));
84 static void rev_list(int fd, struct ref *refs)
87 pid_t pack_objects_pid;
89 if (pipe(pipe_fd) < 0)
90 die("rev-list setup: pipe failed");
91 pack_objects_pid = fork();
92 if (!pack_objects_pid) {
99 die("pack-objects setup failed");
101 if (pack_objects_pid < 0)
102 die("pack-objects fork failed");
110 static int pack_objects(int fd, struct ref *refs)
114 rev_list_pid = fork();
117 die("rev-list setup failed");
119 if (rev_list_pid < 0)
120 die("rev-list fork failed");
122 * We don't wait for the rev-list pipeline in the parent:
123 * we end up waiting for the other end instead
128 static void unmark_and_free(struct commit_list *list, unsigned int mark)
131 struct commit_list *temp = list;
132 temp->item->object.flags &= ~mark;
138 static int ref_newer(const unsigned char *new_sha1,
139 const unsigned char *old_sha1)
142 struct commit *old, *new;
143 struct commit_list *list, *used;
146 /* Both new and old must be commit-ish and new is descendant of
147 * old. Otherwise we require --force.
149 o = deref_tag(parse_object(old_sha1), NULL, 0);
150 if (!o || o->type != commit_type)
152 old = (struct commit *) o;
154 o = deref_tag(parse_object(new_sha1), NULL, 0);
155 if (!o || o->type != commit_type)
157 new = (struct commit *) o;
159 if (parse_commit(new) < 0)
163 commit_list_insert(new, &list);
165 new = pop_most_recent_commit(&list, 1);
166 commit_list_insert(new, &used);
172 unmark_and_free(list, 1);
173 unmark_and_free(used, 1);
177 static struct ref *local_refs, **local_tail;
178 static struct ref *remote_refs, **remote_tail;
180 static int one_local_ref(const char *refname, const unsigned char *sha1)
183 int len = strlen(refname) + 1;
184 ref = xcalloc(1, sizeof(*ref) + len);
185 memcpy(ref->new_sha1, sha1, 20);
186 memcpy(ref->name, refname, len);
188 local_tail = &ref->next;
192 static void get_local_heads(void)
194 local_tail = &local_refs;
195 for_each_ref(one_local_ref);
198 static int receive_status(int in)
202 int len = packet_read_line(in, line, sizeof(line));
203 if (len < 10 || memcmp(line, "unpack ", 7)) {
204 fprintf(stderr, "did not receive status back\n");
207 if (memcmp(line, "unpack ok\n", 10)) {
212 len = packet_read_line(in, line, sizeof(line));
216 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
217 fprintf(stderr, "protocol error: %s\n", line);
221 if (!memcmp(line, "ok", 2))
229 static int send_pack(int in, int out, int nr_refspec, char **refspec)
234 int ask_for_status_report = 0;
235 int expect_status_report = 0;
237 /* No funny business with the matcher */
238 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, 1);
241 /* Does the other end support the reporting? */
242 if (server_supports("report-status"))
243 ask_for_status_report = 1;
247 remote_tail = &remote_refs;
248 if (match_refs(local_refs, remote_refs, &remote_tail,
249 nr_refspec, refspec, send_all))
253 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
258 * Finally, tell the other end!
261 for (ref = remote_refs; ref; ref = ref->next) {
262 char old_hex[60], *new_hex;
265 if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
267 fprintf(stderr, "'%s': up-to-date\n", ref->name);
271 /* This part determines what can overwrite what.
274 * (0) you can always use --force or +A:B notation to
275 * selectively force individual ref pairs.
277 * (1) if the old thing does not exist, it is OK.
279 * (2) if you do not have the old thing, you are not allowed
280 * to overwrite it; you would not know what you are losing
283 * (3) if both new and old are commit-ish, and new is a
284 * descendant of old, it is OK.
288 !is_zero_sha1(ref->old_sha1) &&
290 if (!has_sha1_file(ref->old_sha1) ||
291 !ref_newer(ref->peer_ref->new_sha1,
293 /* We do not have the remote ref, or
294 * we know that the remote ref is not
295 * an ancestor of what we are trying to
296 * push. Either way this can be losing
297 * commits at the remote end and likely
298 * we were not up to date to begin with.
300 error("remote '%s' is not a strict "
301 "subset of local ref '%s'. "
302 "maybe you are not up-to-date and "
303 "need to pull first?",
305 ref->peer_ref->name);
310 memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
311 if (is_zero_sha1(ref->new_sha1)) {
312 error("cannot happen anymore");
317 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
318 new_hex = sha1_to_hex(ref->new_sha1);
320 if (ask_for_status_report) {
321 packet_write(out, "%s %s %s%c%s",
322 old_hex, new_hex, ref->name, 0,
324 ask_for_status_report = 0;
325 expect_status_report = 1;
328 packet_write(out, "%s %s %s",
329 old_hex, new_hex, ref->name);
330 fprintf(stderr, "updating '%s'", ref->name);
331 if (strcmp(ref->name, ref->peer_ref->name))
332 fprintf(stderr, " using '%s'", ref->peer_ref->name);
333 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
338 pack_objects(out, remote_refs);
341 if (expect_status_report) {
342 if (receive_status(in))
346 if (!new_refs && ret == 0)
347 fprintf(stderr, "Everything up-to-date\n");
352 int main(int argc, char **argv)
360 setup_git_directory();
362 for (i = 1; i < argc; i++, argv++) {
366 if (!strncmp(arg, "--exec=", 7)) {
370 if (!strcmp(arg, "--all")) {
374 if (!strcmp(arg, "--force")) {
378 if (!strcmp(arg, "--verbose")) {
382 usage(send_pack_usage);
393 usage(send_pack_usage);
394 if (heads && send_all)
395 usage(send_pack_usage);
396 pid = git_connect(fd, dest, exec);
399 ret = send_pack(fd[0], fd[1], nr_heads, heads);