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)
25 if (pipe(pipe_fd) < 0)
26 return error("send-pack: pipe failed");
29 return error("send-pack: unable to fork git-pack-objects");
32 * The child becomes pack-objects --revs; we feed
33 * the revision parameters to it via its stdin and
34 * let its stdout go back to the other end.
36 static const char *args[] = {
52 die("git-pack-objects exec failed (%s)", strerror(errno));
56 * We feed the pack-objects we just spawned with revision
57 * parameters by writing to the pipe.
65 if (!is_null_sha1(refs->old_sha1) &&
66 has_sha1_file(refs->old_sha1)) {
67 memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
70 if (!write_or_whine(pipe_fd[1], buf, 42,
71 "send-pack: send refs"))
74 if (!is_null_sha1(refs->new_sha1)) {
75 memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
77 if (!write_or_whine(pipe_fd[1], buf, 41,
78 "send-pack: send refs"))
87 pid_t waiting = waitpid(pid, &status, 0);
92 return error("waitpid failed (%s)", strerror(errno));
94 if ((waiting != pid) || WIFSIGNALED(status) ||
96 return error("pack-objects died with strange error");
97 code = WEXITSTATUS(status);
104 static void unmark_and_free(struct commit_list *list, unsigned int mark)
107 struct commit_list *temp = list;
108 temp->item->object.flags &= ~mark;
114 static int ref_newer(const unsigned char *new_sha1,
115 const unsigned char *old_sha1)
118 struct commit *old, *new;
119 struct commit_list *list, *used;
122 /* Both new and old must be commit-ish and new is descendant of
123 * old. Otherwise we require --force.
125 o = deref_tag(parse_object(old_sha1), NULL, 0);
126 if (!o || o->type != OBJ_COMMIT)
128 old = (struct commit *) o;
130 o = deref_tag(parse_object(new_sha1), NULL, 0);
131 if (!o || o->type != OBJ_COMMIT)
133 new = (struct commit *) o;
135 if (parse_commit(new) < 0)
139 commit_list_insert(new, &list);
141 new = pop_most_recent_commit(&list, 1);
142 commit_list_insert(new, &used);
148 unmark_and_free(list, 1);
149 unmark_and_free(used, 1);
153 static struct ref *local_refs, **local_tail;
154 static struct ref *remote_refs, **remote_tail;
156 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
159 int len = strlen(refname) + 1;
160 ref = xcalloc(1, sizeof(*ref) + len);
161 hashcpy(ref->new_sha1, sha1);
162 memcpy(ref->name, refname, len);
164 local_tail = &ref->next;
168 static void get_local_heads(void)
170 local_tail = &local_refs;
171 for_each_ref(one_local_ref, NULL);
174 static int receive_status(int in)
178 int len = packet_read_line(in, line, sizeof(line));
179 if (len < 10 || memcmp(line, "unpack ", 7)) {
180 fprintf(stderr, "did not receive status back\n");
183 if (memcmp(line, "unpack ok\n", 10)) {
188 len = packet_read_line(in, line, sizeof(line));
192 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
193 fprintf(stderr, "protocol error: %s\n", line);
197 if (!memcmp(line, "ok", 2))
205 static int send_pack(int in, int out, int nr_refspec, char **refspec)
210 int ask_for_status_report = 0;
211 int allow_deleting_refs = 0;
212 int expect_status_report = 0;
214 /* No funny business with the matcher */
215 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
218 /* Does the other end support the reporting? */
219 if (server_supports("report-status"))
220 ask_for_status_report = 1;
221 if (server_supports("delete-refs"))
222 allow_deleting_refs = 1;
226 remote_tail = &remote_refs;
227 if (match_refs(local_refs, remote_refs, &remote_tail,
228 nr_refspec, refspec, send_all))
232 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
237 * Finally, tell the other end!
240 for (ref = remote_refs; ref; ref = ref->next) {
241 char old_hex[60], *new_hex;
247 delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
248 if (delete_ref && !allow_deleting_refs) {
249 error("remote does not support deleting refs");
254 !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
256 fprintf(stderr, "'%s': up-to-date\n", ref->name);
260 /* This part determines what can overwrite what.
263 * (0) you can always use --force or +A:B notation to
264 * selectively force individual ref pairs.
266 * (1) if the old thing does not exist, it is OK.
268 * (2) if you do not have the old thing, you are not allowed
269 * to overwrite it; you would not know what you are losing
272 * (3) if both new and old are commit-ish, and new is a
273 * descendant of old, it is OK.
275 * (4) regardless of all of the above, removing :B is
281 !is_null_sha1(ref->old_sha1) &&
283 if (!has_sha1_file(ref->old_sha1) ||
284 !ref_newer(ref->peer_ref->new_sha1,
286 /* We do not have the remote ref, or
287 * we know that the remote ref is not
288 * an ancestor of what we are trying to
289 * push. Either way this can be losing
290 * commits at the remote end and likely
291 * we were not up to date to begin with.
293 error("remote '%s' is not a strict "
294 "subset of local ref '%s'. "
295 "maybe you are not up-to-date and "
296 "need to pull first?",
298 ref->peer_ref->name);
303 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
306 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
307 new_hex = sha1_to_hex(ref->new_sha1);
309 if (ask_for_status_report) {
310 packet_write(out, "%s %s %s%c%s",
311 old_hex, new_hex, ref->name, 0,
313 ask_for_status_report = 0;
314 expect_status_report = 1;
317 packet_write(out, "%s %s %s",
318 old_hex, new_hex, ref->name);
320 fprintf(stderr, "deleting '%s'\n", ref->name);
322 fprintf(stderr, "updating '%s'", ref->name);
323 if (strcmp(ref->name, ref->peer_ref->name))
324 fprintf(stderr, " using '%s'",
325 ref->peer_ref->name);
326 fprintf(stderr, "\n from %s\n to %s\n",
333 ret = pack_objects(out, remote_refs);
336 if (expect_status_report) {
337 if (receive_status(in))
341 if (!new_refs && ret == 0)
342 fprintf(stderr, "Everything up-to-date\n");
346 static void verify_remote_names(int nr_heads, char **heads)
350 for (i = 0; i < nr_heads; i++) {
351 const char *remote = strchr(heads[i], ':');
353 remote = remote ? (remote + 1) : heads[i];
354 switch (check_ref_format(remote)) {
356 case -2: /* ok but a single level -- that is fine for
361 die("remote part of refspec is not a valid name in %s",
366 int main(int argc, char **argv)
374 setup_git_directory();
375 git_config(git_default_config);
378 for (i = 1; i < argc; i++, argv++) {
382 if (!strncmp(arg, "--receive-pack=", 15)) {
383 receivepack = arg + 15;
386 if (!strncmp(arg, "--exec=", 7)) {
387 receivepack = arg + 7;
390 if (!strcmp(arg, "--all")) {
394 if (!strcmp(arg, "--force")) {
398 if (!strcmp(arg, "--verbose")) {
402 if (!strcmp(arg, "--thin")) {
406 usage(send_pack_usage);
417 usage(send_pack_usage);
418 if (heads && send_all)
419 usage(send_pack_usage);
420 verify_remote_names(nr_heads, heads);
422 pid = git_connect(fd, dest, receivepack);
425 ret = send_pack(fd[0], fd[1], nr_heads, heads);
428 ret |= finish_connect(pid);