6 #include "run-command.h"
8 static const char send_pack_usage
[] =
9 "git-send-pack [--all] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
10 " --all and explicit <ref> specification are mutually exclusive.";
11 static const char *receivepack
= "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
)
23 * The child becomes pack-objects --revs; we feed
24 * the revision parameters to it via its stdin and
25 * let its stdout go back to the other end.
27 const char *args
[] = {
35 struct child_process po
;
39 memset(&po
, 0, sizeof(po
));
44 if (start_command(&po
))
45 die("git-pack-objects failed (%s)", strerror(errno
));
48 * We feed the pack-objects we just spawned with revision
49 * parameters by writing to the pipe.
54 if (!is_null_sha1(refs
->old_sha1
) &&
55 has_sha1_file(refs
->old_sha1
)) {
56 memcpy(buf
+ 1, sha1_to_hex(refs
->old_sha1
), 40);
59 if (!write_or_whine(po
.in
, buf
, 42,
60 "send-pack: send refs"))
63 if (!is_null_sha1(refs
->new_sha1
)) {
64 memcpy(buf
, sha1_to_hex(refs
->new_sha1
), 40);
66 if (!write_or_whine(po
.in
, buf
, 41,
67 "send-pack: send refs"))
73 if (finish_command(&po
))
74 return error("pack-objects died with strange error");
78 static void unmark_and_free(struct commit_list
*list
, unsigned int mark
)
81 struct commit_list
*temp
= list
;
82 temp
->item
->object
.flags
&= ~mark
;
88 static int ref_newer(const unsigned char *new_sha1
,
89 const unsigned char *old_sha1
)
92 struct commit
*old
, *new;
93 struct commit_list
*list
, *used
;
96 /* Both new and old must be commit-ish and new is descendant of
97 * old. Otherwise we require --force.
99 o
= deref_tag(parse_object(old_sha1
), NULL
, 0);
100 if (!o
|| o
->type
!= OBJ_COMMIT
)
102 old
= (struct commit
*) o
;
104 o
= deref_tag(parse_object(new_sha1
), NULL
, 0);
105 if (!o
|| o
->type
!= OBJ_COMMIT
)
107 new = (struct commit
*) o
;
109 if (parse_commit(new) < 0)
113 commit_list_insert(new, &list
);
115 new = pop_most_recent_commit(&list
, 1);
116 commit_list_insert(new, &used
);
122 unmark_and_free(list
, 1);
123 unmark_and_free(used
, 1);
127 static struct ref
*local_refs
, **local_tail
;
128 static struct ref
*remote_refs
, **remote_tail
;
130 static int one_local_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
133 int len
= strlen(refname
) + 1;
134 ref
= xcalloc(1, sizeof(*ref
) + len
);
135 hashcpy(ref
->new_sha1
, sha1
);
136 memcpy(ref
->name
, refname
, len
);
138 local_tail
= &ref
->next
;
142 static void get_local_heads(void)
144 local_tail
= &local_refs
;
145 for_each_ref(one_local_ref
, NULL
);
148 static int receive_status(int in
)
152 int len
= packet_read_line(in
, line
, sizeof(line
));
153 if (len
< 10 || memcmp(line
, "unpack ", 7)) {
154 fprintf(stderr
, "did not receive status back\n");
157 if (memcmp(line
, "unpack ok\n", 10)) {
162 len
= packet_read_line(in
, line
, sizeof(line
));
166 (memcmp(line
, "ok", 2) && memcmp(line
, "ng", 2))) {
167 fprintf(stderr
, "protocol error: %s\n", line
);
171 if (!memcmp(line
, "ok", 2))
179 static int send_pack(int in
, int out
, int nr_refspec
, char **refspec
)
184 int ask_for_status_report
= 0;
185 int allow_deleting_refs
= 0;
186 int expect_status_report
= 0;
188 /* No funny business with the matcher */
189 remote_tail
= get_remote_heads(in
, &remote_refs
, 0, NULL
, REF_NORMAL
);
192 /* Does the other end support the reporting? */
193 if (server_supports("report-status"))
194 ask_for_status_report
= 1;
195 if (server_supports("delete-refs"))
196 allow_deleting_refs
= 1;
200 remote_tail
= &remote_refs
;
201 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
202 nr_refspec
, refspec
, send_all
))
206 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
211 * Finally, tell the other end!
214 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
215 char old_hex
[60], *new_hex
;
221 delete_ref
= is_null_sha1(ref
->peer_ref
->new_sha1
);
222 if (delete_ref
&& !allow_deleting_refs
) {
223 error("remote does not support deleting refs");
228 !hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
230 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
234 /* This part determines what can overwrite what.
237 * (0) you can always use --force or +A:B notation to
238 * selectively force individual ref pairs.
240 * (1) if the old thing does not exist, it is OK.
242 * (2) if you do not have the old thing, you are not allowed
243 * to overwrite it; you would not know what you are losing
246 * (3) if both new and old are commit-ish, and new is a
247 * descendant of old, it is OK.
249 * (4) regardless of all of the above, removing :B is
255 !is_null_sha1(ref
->old_sha1
) &&
257 if (!has_sha1_file(ref
->old_sha1
) ||
258 !ref_newer(ref
->peer_ref
->new_sha1
,
260 /* We do not have the remote ref, or
261 * we know that the remote ref is not
262 * an ancestor of what we are trying to
263 * push. Either way this can be losing
264 * commits at the remote end and likely
265 * we were not up to date to begin with.
267 error("remote '%s' is not a strict "
268 "subset of local ref '%s'. "
269 "maybe you are not up-to-date and "
270 "need to pull first?",
272 ref
->peer_ref
->name
);
277 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
280 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
281 new_hex
= sha1_to_hex(ref
->new_sha1
);
283 if (ask_for_status_report
) {
284 packet_write(out
, "%s %s %s%c%s",
285 old_hex
, new_hex
, ref
->name
, 0,
287 ask_for_status_report
= 0;
288 expect_status_report
= 1;
291 packet_write(out
, "%s %s %s",
292 old_hex
, new_hex
, ref
->name
);
294 fprintf(stderr
, "deleting '%s'\n", ref
->name
);
296 fprintf(stderr
, "updating '%s'", ref
->name
);
297 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
298 fprintf(stderr
, " using '%s'",
299 ref
->peer_ref
->name
);
300 fprintf(stderr
, "\n from %s\n to %s\n",
307 ret
= pack_objects(out
, remote_refs
);
310 if (expect_status_report
) {
311 if (receive_status(in
))
315 if (!new_refs
&& ret
== 0)
316 fprintf(stderr
, "Everything up-to-date\n");
320 static void verify_remote_names(int nr_heads
, char **heads
)
324 for (i
= 0; i
< nr_heads
; i
++) {
325 const char *remote
= strchr(heads
[i
], ':');
327 remote
= remote
? (remote
+ 1) : heads
[i
];
328 switch (check_ref_format(remote
)) {
330 case -2: /* ok but a single level -- that is fine for
335 die("remote part of refspec is not a valid name in %s",
340 int main(int argc
, char **argv
)
348 setup_git_directory();
349 git_config(git_default_config
);
352 for (i
= 1; i
< argc
; i
++, argv
++) {
356 if (!prefixcmp(arg
, "--receive-pack=")) {
357 receivepack
= arg
+ 15;
360 if (!prefixcmp(arg
, "--exec=")) {
361 receivepack
= arg
+ 7;
364 if (!strcmp(arg
, "--all")) {
368 if (!strcmp(arg
, "--force")) {
372 if (!strcmp(arg
, "--verbose")) {
376 if (!strcmp(arg
, "--thin")) {
380 usage(send_pack_usage
);
391 usage(send_pack_usage
);
392 if (heads
&& send_all
)
393 usage(send_pack_usage
);
394 verify_remote_names(nr_heads
, heads
);
396 pid
= git_connect(fd
, dest
, receivepack
, verbose
? CONNECT_VERBOSE
: 0);
399 ret
= send_pack(fd
[0], fd
[1], nr_heads
, heads
);
402 ret
|= finish_connect(pid
);