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
)
40 static char *args
[1000];
43 args
[i
++] = "rev-list"; /* 0 */
44 args
[i
++] = "--objects"; /* 1 */
46 char *buf
= malloc(100);
48 die("git-rev-list environment overflow");
49 if (!is_zero_sha1(refs
->old_sha1
) &&
50 has_sha1_file(refs
->old_sha1
)) {
52 snprintf(buf
, 50, "^%s", sha1_to_hex(refs
->old_sha1
));
55 if (!is_zero_sha1(refs
->new_sha1
)) {
57 snprintf(buf
, 50, "%s", sha1_to_hex(refs
->new_sha1
));
63 die("git-rev-list exec failed (%s)", strerror(errno
));
66 static void rev_list(int fd
, struct ref
*refs
)
69 pid_t pack_objects_pid
;
71 if (pipe(pipe_fd
) < 0)
72 die("rev-list setup: pipe failed");
73 pack_objects_pid
= fork();
74 if (!pack_objects_pid
) {
81 die("pack-objects setup failed");
83 if (pack_objects_pid
< 0)
84 die("pack-objects fork failed");
92 static int pack_objects(int fd
, struct ref
*refs
)
96 rev_list_pid
= fork();
99 die("rev-list setup failed");
101 if (rev_list_pid
< 0)
102 die("rev-list fork failed");
104 * We don't wait for the rev-list pipeline in the parent:
105 * we end up waiting for the other end instead
110 static void unmark_and_free(struct commit_list
*list
, unsigned int mark
)
113 struct commit_list
*temp
= list
;
114 temp
->item
->object
.flags
&= ~mark
;
120 static int ref_newer(const unsigned char *new_sha1
,
121 const unsigned char *old_sha1
)
124 struct commit
*old
, *new;
125 struct commit_list
*list
, *used
;
128 /* Both new and old must be commit-ish and new is descendant of
129 * old. Otherwise we require --force.
131 o
= deref_tag(parse_object(old_sha1
), NULL
, 0);
132 if (!o
|| o
->type
!= commit_type
)
134 old
= (struct commit
*) o
;
136 o
= deref_tag(parse_object(new_sha1
), NULL
, 0);
137 if (!o
|| o
->type
!= commit_type
)
139 new = (struct commit
*) o
;
141 if (parse_commit(new) < 0)
145 commit_list_insert(new, &list
);
147 new = pop_most_recent_commit(&list
, 1);
148 commit_list_insert(new, &used
);
154 unmark_and_free(list
, 1);
155 unmark_and_free(used
, 1);
159 static struct ref
*local_refs
, **local_tail
;
160 static struct ref
*remote_refs
, **remote_tail
;
162 static int one_local_ref(const char *refname
, const unsigned char *sha1
)
165 int len
= strlen(refname
) + 1;
166 ref
= xcalloc(1, sizeof(*ref
) + len
);
167 memcpy(ref
->new_sha1
, sha1
, 20);
168 memcpy(ref
->name
, refname
, len
);
170 local_tail
= &ref
->next
;
174 static void get_local_heads(void)
176 local_tail
= &local_refs
;
177 for_each_ref(one_local_ref
);
180 static int receive_status(int in
)
184 int len
= packet_read_line(in
, line
, sizeof(line
));
185 if (len
< 10 || memcmp(line
, "unpack ", 7)) {
186 fprintf(stderr
, "did not receive status back\n");
189 if (memcmp(line
, "unpack ok\n", 10)) {
194 len
= packet_read_line(in
, line
, sizeof(line
));
198 (memcmp(line
, "ok", 2) && memcmp(line
, "ng", 2))) {
199 fprintf(stderr
, "protocol error: %s\n", line
);
203 if (!memcmp(line
, "ok", 2))
211 static int send_pack(int in
, int out
, int nr_refspec
, char **refspec
)
216 int ask_for_status_report
= 0;
217 int expect_status_report
= 0;
219 /* No funny business with the matcher */
220 remote_tail
= get_remote_heads(in
, &remote_refs
, 0, NULL
, 1);
223 /* Does the other end support the reporting? */
224 if (server_supports("report-status"))
225 ask_for_status_report
= 1;
229 remote_tail
= &remote_refs
;
230 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
231 nr_refspec
, refspec
, send_all
))
235 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
240 * Finally, tell the other end!
243 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
244 char old_hex
[60], *new_hex
;
247 if (!memcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
, 20)) {
249 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
253 /* This part determines what can overwrite what.
256 * (0) you can always use --force or +A:B notation to
257 * selectively force individual ref pairs.
259 * (1) if the old thing does not exist, it is OK.
261 * (2) if you do not have the old thing, you are not allowed
262 * to overwrite it; you would not know what you are losing
265 * (3) if both new and old are commit-ish, and new is a
266 * descendant of old, it is OK.
270 !is_zero_sha1(ref
->old_sha1
) &&
272 if (!has_sha1_file(ref
->old_sha1
) ||
273 !ref_newer(ref
->peer_ref
->new_sha1
,
275 /* We do not have the remote ref, or
276 * we know that the remote ref is not
277 * an ancestor of what we are trying to
278 * push. Either way this can be losing
279 * commits at the remote end and likely
280 * we were not up to date to begin with.
282 error("remote '%s' is not a strict "
283 "subset of local ref '%s'. "
284 "maybe you are not up-to-date and "
285 "need to pull first?",
287 ref
->peer_ref
->name
);
292 memcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
, 20);
293 if (is_zero_sha1(ref
->new_sha1
)) {
294 error("cannot happen anymore");
299 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
300 new_hex
= sha1_to_hex(ref
->new_sha1
);
302 if (ask_for_status_report
) {
303 packet_write(out
, "%s %s %s%c%s",
304 old_hex
, new_hex
, ref
->name
, 0,
306 ask_for_status_report
= 0;
307 expect_status_report
= 1;
310 packet_write(out
, "%s %s %s",
311 old_hex
, new_hex
, ref
->name
);
312 fprintf(stderr
, "updating '%s'", ref
->name
);
313 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
314 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
315 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
320 pack_objects(out
, remote_refs
);
323 if (expect_status_report
) {
324 if (receive_status(in
))
328 if (!new_refs
&& ret
== 0)
329 fprintf(stderr
, "Everything up-to-date\n");
334 int main(int argc
, char **argv
)
342 setup_git_directory();
344 for (i
= 1; i
< argc
; i
++, argv
++) {
348 if (!strncmp(arg
, "--exec=", 7)) {
352 if (!strcmp(arg
, "--all")) {
356 if (!strcmp(arg
, "--force")) {
360 if (!strcmp(arg
, "--verbose")) {
364 usage(send_pack_usage
);
375 usage(send_pack_usage
);
376 if (heads
&& send_all
)
377 usage(send_pack_usage
);
378 pid
= git_connect(fd
, dest
, exec
);
381 ret
= send_pack(fd
[0], fd
[1], nr_heads
, heads
);