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";
14 static int force_update
;
15 static int use_thin_pack
;
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 void 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
131 static void unmark_and_free(struct commit_list
*list
, unsigned int mark
)
134 struct commit_list
*temp
= list
;
135 temp
->item
->object
.flags
&= ~mark
;
141 static int ref_newer(const unsigned char *new_sha1
,
142 const unsigned char *old_sha1
)
145 struct commit
*old
, *new;
146 struct commit_list
*list
, *used
;
149 /* Both new and old must be commit-ish and new is descendant of
150 * old. Otherwise we require --force.
152 o
= deref_tag(parse_object(old_sha1
), NULL
, 0);
153 if (!o
|| o
->type
!= OBJ_COMMIT
)
155 old
= (struct commit
*) o
;
157 o
= deref_tag(parse_object(new_sha1
), NULL
, 0);
158 if (!o
|| o
->type
!= OBJ_COMMIT
)
160 new = (struct commit
*) o
;
162 if (parse_commit(new) < 0)
166 commit_list_insert(new, &list
);
168 new = pop_most_recent_commit(&list
, 1);
169 commit_list_insert(new, &used
);
175 unmark_and_free(list
, 1);
176 unmark_and_free(used
, 1);
180 static struct ref
*local_refs
, **local_tail
;
181 static struct ref
*remote_refs
, **remote_tail
;
183 static int one_local_ref(const char *refname
, const unsigned char *sha1
)
186 int len
= strlen(refname
) + 1;
187 ref
= xcalloc(1, sizeof(*ref
) + len
);
188 memcpy(ref
->new_sha1
, sha1
, 20);
189 memcpy(ref
->name
, refname
, len
);
191 local_tail
= &ref
->next
;
195 static void get_local_heads(void)
197 local_tail
= &local_refs
;
198 for_each_ref(one_local_ref
);
201 static int receive_status(int in
)
205 int len
= packet_read_line(in
, line
, sizeof(line
));
206 if (len
< 10 || memcmp(line
, "unpack ", 7)) {
207 fprintf(stderr
, "did not receive status back\n");
210 if (memcmp(line
, "unpack ok\n", 10)) {
215 len
= packet_read_line(in
, line
, sizeof(line
));
219 (memcmp(line
, "ok", 2) && memcmp(line
, "ng", 2))) {
220 fprintf(stderr
, "protocol error: %s\n", line
);
224 if (!memcmp(line
, "ok", 2))
232 static int send_pack(int in
, int out
, int nr_refspec
, char **refspec
)
237 int ask_for_status_report
= 0;
238 int expect_status_report
= 0;
240 /* No funny business with the matcher */
241 remote_tail
= get_remote_heads(in
, &remote_refs
, 0, NULL
, REF_NORMAL
);
244 /* Does the other end support the reporting? */
245 if (server_supports("report-status"))
246 ask_for_status_report
= 1;
250 remote_tail
= &remote_refs
;
251 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
252 nr_refspec
, refspec
, send_all
))
256 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
261 * Finally, tell the other end!
264 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
265 char old_hex
[60], *new_hex
;
268 if (!hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
270 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
274 /* This part determines what can overwrite what.
277 * (0) you can always use --force or +A:B notation to
278 * selectively force individual ref pairs.
280 * (1) if the old thing does not exist, it is OK.
282 * (2) if you do not have the old thing, you are not allowed
283 * to overwrite it; you would not know what you are losing
286 * (3) if both new and old are commit-ish, and new is a
287 * descendant of old, it is OK.
291 !is_zero_sha1(ref
->old_sha1
) &&
293 if (!has_sha1_file(ref
->old_sha1
) ||
294 !ref_newer(ref
->peer_ref
->new_sha1
,
296 /* We do not have the remote ref, or
297 * we know that the remote ref is not
298 * an ancestor of what we are trying to
299 * push. Either way this can be losing
300 * commits at the remote end and likely
301 * we were not up to date to begin with.
303 error("remote '%s' is not a strict "
304 "subset of local ref '%s'. "
305 "maybe you are not up-to-date and "
306 "need to pull first?",
308 ref
->peer_ref
->name
);
313 memcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
, 20);
314 if (is_zero_sha1(ref
->new_sha1
)) {
315 error("cannot happen anymore");
320 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
321 new_hex
= sha1_to_hex(ref
->new_sha1
);
323 if (ask_for_status_report
) {
324 packet_write(out
, "%s %s %s%c%s",
325 old_hex
, new_hex
, ref
->name
, 0,
327 ask_for_status_report
= 0;
328 expect_status_report
= 1;
331 packet_write(out
, "%s %s %s",
332 old_hex
, new_hex
, ref
->name
);
333 fprintf(stderr
, "updating '%s'", ref
->name
);
334 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
335 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
336 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
341 pack_objects(out
, remote_refs
);
344 if (expect_status_report
) {
345 if (receive_status(in
))
349 if (!new_refs
&& ret
== 0)
350 fprintf(stderr
, "Everything up-to-date\n");
355 int main(int argc
, char **argv
)
363 setup_git_directory();
364 git_config(git_default_config
);
367 for (i
= 1; i
< argc
; i
++, argv
++) {
371 if (!strncmp(arg
, "--exec=", 7)) {
375 if (!strcmp(arg
, "--all")) {
379 if (!strcmp(arg
, "--force")) {
383 if (!strcmp(arg
, "--verbose")) {
387 if (!strcmp(arg
, "--thin")) {
391 usage(send_pack_usage
);
402 usage(send_pack_usage
);
403 if (heads
&& send_all
)
404 usage(send_pack_usage
);
405 pid
= git_connect(fd
, dest
, exec
);
408 ret
= send_pack(fd
[0], fd
[1], nr_heads
, heads
);