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
;
18 * Make a pack stream and spit it out into file descriptor fd
20 static int pack_objects(int fd
, struct ref
*refs
)
25 if (pipe(pipe_fd
) < 0)
26 return error("send-pack: pipe failed");
30 * The child becomes pack-objects --revs; we feed
31 * the revision parameters to it via its stdin and
32 * let its stdout go back to the other end.
34 static const char *args
[] = {
50 die("git-pack-objects exec failed (%s)", strerror(errno
));
54 * We feed the pack-objects we just spawned with revision
55 * parameters by writing to the pipe.
63 if (!is_null_sha1(refs
->old_sha1
) &&
64 has_sha1_file(refs
->old_sha1
)) {
65 memcpy(buf
+ 1, sha1_to_hex(refs
->old_sha1
), 40);
68 if (!write_in_full(pipe_fd
[1], buf
, 42,
69 "send-pack: send refs"))
72 if (!is_null_sha1(refs
->new_sha1
)) {
73 memcpy(buf
, sha1_to_hex(refs
->new_sha1
), 40);
75 if (!write_in_full(pipe_fd
[1], buf
, 41,
76 "send-pack: send refs"))
85 pid_t waiting
= waitpid(pid
, &status
, 0);
90 return error("waitpid failed (%s)", strerror(errno
));
92 if ((waiting
!= pid
) || WIFSIGNALED(status
) ||
94 return error("pack-objects died with strange error");
95 code
= WEXITSTATUS(status
);
102 static void unmark_and_free(struct commit_list
*list
, unsigned int mark
)
105 struct commit_list
*temp
= list
;
106 temp
->item
->object
.flags
&= ~mark
;
112 static int ref_newer(const unsigned char *new_sha1
,
113 const unsigned char *old_sha1
)
116 struct commit
*old
, *new;
117 struct commit_list
*list
, *used
;
120 /* Both new and old must be commit-ish and new is descendant of
121 * old. Otherwise we require --force.
123 o
= deref_tag(parse_object(old_sha1
), NULL
, 0);
124 if (!o
|| o
->type
!= OBJ_COMMIT
)
126 old
= (struct commit
*) o
;
128 o
= deref_tag(parse_object(new_sha1
), NULL
, 0);
129 if (!o
|| o
->type
!= OBJ_COMMIT
)
131 new = (struct commit
*) o
;
133 if (parse_commit(new) < 0)
137 commit_list_insert(new, &list
);
139 new = pop_most_recent_commit(&list
, 1);
140 commit_list_insert(new, &used
);
146 unmark_and_free(list
, 1);
147 unmark_and_free(used
, 1);
151 static struct ref
*local_refs
, **local_tail
;
152 static struct ref
*remote_refs
, **remote_tail
;
154 static int one_local_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
157 int len
= strlen(refname
) + 1;
158 ref
= xcalloc(1, sizeof(*ref
) + len
);
159 hashcpy(ref
->new_sha1
, sha1
);
160 memcpy(ref
->name
, refname
, len
);
162 local_tail
= &ref
->next
;
166 static void get_local_heads(void)
168 local_tail
= &local_refs
;
169 for_each_ref(one_local_ref
, NULL
);
172 static int receive_status(int in
)
176 int len
= packet_read_line(in
, line
, sizeof(line
));
177 if (len
< 10 || memcmp(line
, "unpack ", 7)) {
178 fprintf(stderr
, "did not receive status back\n");
181 if (memcmp(line
, "unpack ok\n", 10)) {
186 len
= packet_read_line(in
, line
, sizeof(line
));
190 (memcmp(line
, "ok", 2) && memcmp(line
, "ng", 2))) {
191 fprintf(stderr
, "protocol error: %s\n", line
);
195 if (!memcmp(line
, "ok", 2))
203 static int send_pack(int in
, int out
, int nr_refspec
, char **refspec
)
208 int ask_for_status_report
= 0;
209 int allow_deleting_refs
= 0;
210 int expect_status_report
= 0;
212 /* No funny business with the matcher */
213 remote_tail
= get_remote_heads(in
, &remote_refs
, 0, NULL
, REF_NORMAL
);
216 /* Does the other end support the reporting? */
217 if (server_supports("report-status"))
218 ask_for_status_report
= 1;
219 if (server_supports("delete-refs"))
220 allow_deleting_refs
= 1;
224 remote_tail
= &remote_refs
;
225 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
226 nr_refspec
, refspec
, send_all
))
230 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
235 * Finally, tell the other end!
238 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
239 char old_hex
[60], *new_hex
;
245 delete_ref
= is_null_sha1(ref
->peer_ref
->new_sha1
);
246 if (delete_ref
&& !allow_deleting_refs
) {
247 error("remote does not support deleting refs");
252 !hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
254 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
258 /* This part determines what can overwrite what.
261 * (0) you can always use --force or +A:B notation to
262 * selectively force individual ref pairs.
264 * (1) if the old thing does not exist, it is OK.
266 * (2) if you do not have the old thing, you are not allowed
267 * to overwrite it; you would not know what you are losing
270 * (3) if both new and old are commit-ish, and new is a
271 * descendant of old, it is OK.
273 * (4) regardless of all of the above, removing :B is
279 !is_null_sha1(ref
->old_sha1
) &&
281 if (!has_sha1_file(ref
->old_sha1
) ||
282 !ref_newer(ref
->peer_ref
->new_sha1
,
284 /* We do not have the remote ref, or
285 * we know that the remote ref is not
286 * an ancestor of what we are trying to
287 * push. Either way this can be losing
288 * commits at the remote end and likely
289 * we were not up to date to begin with.
291 error("remote '%s' is not a strict "
292 "subset of local ref '%s'. "
293 "maybe you are not up-to-date and "
294 "need to pull first?",
296 ref
->peer_ref
->name
);
301 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
304 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
305 new_hex
= sha1_to_hex(ref
->new_sha1
);
307 if (ask_for_status_report
) {
308 packet_write(out
, "%s %s %s%c%s",
309 old_hex
, new_hex
, ref
->name
, 0,
311 ask_for_status_report
= 0;
312 expect_status_report
= 1;
315 packet_write(out
, "%s %s %s",
316 old_hex
, new_hex
, ref
->name
);
318 fprintf(stderr
, "deleting '%s'\n", ref
->name
);
320 fprintf(stderr
, "updating '%s'", ref
->name
);
321 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
322 fprintf(stderr
, " using '%s'",
323 ref
->peer_ref
->name
);
324 fprintf(stderr
, "\n from %s\n to %s\n",
331 ret
= pack_objects(out
, remote_refs
);
334 if (expect_status_report
) {
335 if (receive_status(in
))
339 if (!new_refs
&& ret
== 0)
340 fprintf(stderr
, "Everything up-to-date\n");
344 static void verify_remote_names(int nr_heads
, char **heads
)
348 for (i
= 0; i
< nr_heads
; i
++) {
349 const char *remote
= strchr(heads
[i
], ':');
351 remote
= remote
? (remote
+ 1) : heads
[i
];
352 switch (check_ref_format(remote
)) {
354 case -2: /* ok but a single level -- that is fine for
359 die("remote part of refspec is not a valid name in %s",
364 int main(int argc
, char **argv
)
372 setup_git_directory();
373 git_config(git_default_config
);
376 for (i
= 1; i
< argc
; i
++, argv
++) {
380 if (!strncmp(arg
, "--exec=", 7)) {
384 if (!strcmp(arg
, "--all")) {
388 if (!strcmp(arg
, "--force")) {
392 if (!strcmp(arg
, "--verbose")) {
396 if (!strcmp(arg
, "--thin")) {
400 usage(send_pack_usage
);
411 usage(send_pack_usage
);
412 if (heads
&& send_all
)
413 usage(send_pack_usage
);
414 verify_remote_names(nr_heads
, heads
);
416 pid
= git_connect(fd
, dest
, exec
);
419 ret
= send_pack(fd
[0], fd
[1], nr_heads
, heads
);
422 ret
|= finish_connect(pid
);