Merge with git://repo.or.cz/git.git#master
[git/mingw.git] / send-pack.c
blob597130f41a27ef3f048ba5a951af0441bd585011
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "refs.h"
5 #include "pkt-line.h"
6 #include "exec_cmd.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";
12 static int verbose;
13 static int send_all;
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)
22 int pipe_fd[2];
23 int pack_fd[2] = { -1, fd };
24 pid_t pid;
27 * The child becomes pack-objects --revs; we feed
28 * the revision parameters to it via its stdin and
29 * let its stdout go back to the other end.
31 static const char *args[] = {
32 "pack-objects",
33 "--all-progress",
34 "--revs",
35 "--stdout",
36 NULL,
37 NULL,
39 if (use_thin_pack)
40 args[4] = "--thin";
42 if (pipe(pipe_fd) < 0)
43 return error("send-pack: pipe failed");
44 pid = spawnv_git_cmd(args, pipe_fd, pack_fd);
45 if (pid < 0)
46 return error("send-pack: unable to fork git-pack-objects");
49 * We feed the pack-objects we just spawned with revision
50 * parameters by writing to the pipe.
53 while (refs) {
54 char buf[42];
56 if (!is_null_sha1(refs->old_sha1) &&
57 has_sha1_file(refs->old_sha1)) {
58 memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
59 buf[0] = '^';
60 buf[41] = '\n';
61 if (!write_or_whine(pipe_fd[1], buf, 42,
62 "send-pack: send refs"))
63 break;
65 if (!is_null_sha1(refs->new_sha1)) {
66 memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
67 buf[40] = '\n';
68 if (!write_or_whine(pipe_fd[1], buf, 41,
69 "send-pack: send refs"))
70 break;
72 refs = refs->next;
74 close(pipe_fd[1]);
76 for (;;) {
77 int status, code;
78 pid_t waiting = waitpid(pid, &status, 0);
80 if (waiting < 0) {
81 if (errno == EINTR)
82 continue;
83 return error("waitpid failed (%s)", strerror(errno));
85 if ((waiting != pid) || WIFSIGNALED(status) ||
86 !WIFEXITED(status))
87 return error("pack-objects died with strange error");
88 code = WEXITSTATUS(status);
89 if (code)
90 return -code;
91 return 0;
95 static void unmark_and_free(struct commit_list *list, unsigned int mark)
97 while (list) {
98 struct commit_list *temp = list;
99 temp->item->object.flags &= ~mark;
100 list = temp->next;
101 free(temp);
105 static int ref_newer(const unsigned char *new_sha1,
106 const unsigned char *old_sha1)
108 struct object *o;
109 struct commit *old, *new;
110 struct commit_list *list, *used;
111 int found = 0;
113 /* Both new and old must be commit-ish and new is descendant of
114 * old. Otherwise we require --force.
116 o = deref_tag(parse_object(old_sha1), NULL, 0);
117 if (!o || o->type != OBJ_COMMIT)
118 return 0;
119 old = (struct commit *) o;
121 o = deref_tag(parse_object(new_sha1), NULL, 0);
122 if (!o || o->type != OBJ_COMMIT)
123 return 0;
124 new = (struct commit *) o;
126 if (parse_commit(new) < 0)
127 return 0;
129 used = list = NULL;
130 commit_list_insert(new, &list);
131 while (list) {
132 new = pop_most_recent_commit(&list, 1);
133 commit_list_insert(new, &used);
134 if (new == old) {
135 found = 1;
136 break;
139 unmark_and_free(list, 1);
140 unmark_and_free(used, 1);
141 return found;
144 static struct ref *local_refs, **local_tail;
145 static struct ref *remote_refs, **remote_tail;
147 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
149 struct ref *ref;
150 int len = strlen(refname) + 1;
151 ref = xcalloc(1, sizeof(*ref) + len);
152 hashcpy(ref->new_sha1, sha1);
153 memcpy(ref->name, refname, len);
154 *local_tail = ref;
155 local_tail = &ref->next;
156 return 0;
159 static void get_local_heads(void)
161 local_tail = &local_refs;
162 for_each_ref(one_local_ref, NULL);
165 static int receive_status(int in)
167 char line[1000];
168 int ret = 0;
169 int len = packet_read_line(in, line, sizeof(line));
170 if (len < 10 || memcmp(line, "unpack ", 7)) {
171 fprintf(stderr, "did not receive status back\n");
172 return -1;
174 if (memcmp(line, "unpack ok\n", 10)) {
175 fputs(line, stderr);
176 ret = -1;
178 while (1) {
179 len = packet_read_line(in, line, sizeof(line));
180 if (!len)
181 break;
182 if (len < 3 ||
183 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
184 fprintf(stderr, "protocol error: %s\n", line);
185 ret = -1;
186 break;
188 if (!memcmp(line, "ok", 2))
189 continue;
190 fputs(line, stderr);
191 ret = -1;
193 return ret;
196 static int send_pack(int in, int out, int nr_refspec, char **refspec)
198 struct ref *ref;
199 int new_refs;
200 int ret = 0;
201 int ask_for_status_report = 0;
202 int allow_deleting_refs = 0;
203 int expect_status_report = 0;
205 /* No funny business with the matcher */
206 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
207 get_local_heads();
209 /* Does the other end support the reporting? */
210 if (server_supports("report-status"))
211 ask_for_status_report = 1;
212 if (server_supports("delete-refs"))
213 allow_deleting_refs = 1;
215 /* match them up */
216 if (!remote_tail)
217 remote_tail = &remote_refs;
218 if (match_refs(local_refs, remote_refs, &remote_tail,
219 nr_refspec, refspec, send_all))
220 return -1;
222 if (!remote_refs) {
223 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
224 return 0;
228 * Finally, tell the other end!
230 new_refs = 0;
231 for (ref = remote_refs; ref; ref = ref->next) {
232 char old_hex[60], *new_hex;
233 int delete_ref;
235 if (!ref->peer_ref)
236 continue;
238 delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
239 if (delete_ref && !allow_deleting_refs) {
240 error("remote does not support deleting refs");
241 ret = -2;
242 continue;
244 if (!delete_ref &&
245 !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
246 if (verbose)
247 fprintf(stderr, "'%s': up-to-date\n", ref->name);
248 continue;
251 /* This part determines what can overwrite what.
252 * The rules are:
254 * (0) you can always use --force or +A:B notation to
255 * selectively force individual ref pairs.
257 * (1) if the old thing does not exist, it is OK.
259 * (2) if you do not have the old thing, you are not allowed
260 * to overwrite it; you would not know what you are losing
261 * otherwise.
263 * (3) if both new and old are commit-ish, and new is a
264 * descendant of old, it is OK.
266 * (4) regardless of all of the above, removing :B is
267 * always allowed.
270 if (!force_update &&
271 !delete_ref &&
272 !is_null_sha1(ref->old_sha1) &&
273 !ref->force) {
274 if (!has_sha1_file(ref->old_sha1) ||
275 !ref_newer(ref->peer_ref->new_sha1,
276 ref->old_sha1)) {
277 /* We do not have the remote ref, or
278 * we know that the remote ref is not
279 * an ancestor of what we are trying to
280 * push. Either way this can be losing
281 * commits at the remote end and likely
282 * we were not up to date to begin with.
284 error("remote '%s' is not a strict "
285 "subset of local ref '%s'. "
286 "maybe you are not up-to-date and "
287 "need to pull first?",
288 ref->name,
289 ref->peer_ref->name);
290 ret = -2;
291 continue;
294 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
295 if (!delete_ref)
296 new_refs++;
297 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
298 new_hex = sha1_to_hex(ref->new_sha1);
300 if (ask_for_status_report) {
301 packet_write(out, "%s %s %s%c%s",
302 old_hex, new_hex, ref->name, 0,
303 "report-status");
304 ask_for_status_report = 0;
305 expect_status_report = 1;
307 else
308 packet_write(out, "%s %s %s",
309 old_hex, new_hex, ref->name);
310 if (delete_ref)
311 fprintf(stderr, "deleting '%s'\n", ref->name);
312 else {
313 fprintf(stderr, "updating '%s'", ref->name);
314 if (strcmp(ref->name, ref->peer_ref->name))
315 fprintf(stderr, " using '%s'",
316 ref->peer_ref->name);
317 fprintf(stderr, "\n from %s\n to %s\n",
318 old_hex, new_hex);
322 packet_flush(out);
323 if (new_refs)
324 ret = pack_objects(out, remote_refs);
325 close(out);
327 if (expect_status_report) {
328 if (receive_status(in))
329 ret = -4;
332 if (!new_refs && ret == 0)
333 fprintf(stderr, "Everything up-to-date\n");
334 return ret;
337 static void verify_remote_names(int nr_heads, char **heads)
339 int i;
341 for (i = 0; i < nr_heads; i++) {
342 const char *remote = strchr(heads[i], ':');
344 remote = remote ? (remote + 1) : heads[i];
345 switch (check_ref_format(remote)) {
346 case 0: /* ok */
347 case -2: /* ok but a single level -- that is fine for
348 * a match pattern.
350 continue;
352 die("remote part of refspec is not a valid name in %s",
353 heads[i]);
357 int main(int argc, char **argv)
359 int i, nr_heads = 0;
360 char *dest = NULL;
361 char **heads = NULL;
362 int fd[2], ret;
363 pid_t pid;
365 setup_git_directory();
366 git_config(git_default_config);
368 argv++;
369 for (i = 1; i < argc; i++, argv++) {
370 char *arg = *argv;
372 if (*arg == '-') {
373 if (!prefixcmp(arg, "--receive-pack=")) {
374 receivepack = arg + 15;
375 continue;
377 if (!prefixcmp(arg, "--exec=")) {
378 receivepack = arg + 7;
379 continue;
381 if (!strcmp(arg, "--all")) {
382 send_all = 1;
383 continue;
385 if (!strcmp(arg, "--force")) {
386 force_update = 1;
387 continue;
389 if (!strcmp(arg, "--verbose")) {
390 verbose = 1;
391 continue;
393 if (!strcmp(arg, "--thin")) {
394 use_thin_pack = 1;
395 continue;
397 usage(send_pack_usage);
399 if (!dest) {
400 dest = arg;
401 continue;
403 heads = argv;
404 nr_heads = argc - i;
405 break;
407 if (!dest)
408 usage(send_pack_usage);
409 if (heads && send_all)
410 usage(send_pack_usage);
411 verify_remote_names(nr_heads, heads);
413 pid = git_connect(fd, dest, receivepack);
414 if (pid < 0)
415 return 1;
416 ret = send_pack(fd[0], fd[1], nr_heads, heads);
417 close(fd[0]);
418 close(fd[1]);
419 ret |= finish_connect(pid);
420 return !!ret;