send-pack: tell pack-objects to use its internal rev-list.
[git.git] / send-pack.c
blobeaa6efbc0c844de78510d3c65a3bd6a83d9cdcef
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] [--exec=git-receive-pack] <remote> [<head>...]\n"
10 " --all and explicit <head> specification are mutually exclusive.";
11 static const char *exec = "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 pid_t pid;
25 if (pipe(pipe_fd) < 0)
26 return error("send-pack: pipe failed");
27 pid = fork();
28 if (!pid) {
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[] = {
35 "pack-objects",
36 "--all-progress",
37 "--revs",
38 "--stdout",
39 NULL,
40 NULL,
42 if (use_thin_pack)
43 args[4] = "--thin";
44 dup2(pipe_fd[0], 0);
45 dup2(fd, 1);
46 close(pipe_fd[0]);
47 close(pipe_fd[1]);
48 close(fd);
49 execv_git_cmd(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.
57 close(pipe_fd[0]);
58 close(fd);
60 while (refs) {
61 char buf[42];
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);
66 buf[0] = '^';
67 buf[41] = '\n';
68 write(pipe_fd[1], buf, 42);
70 if (!is_null_sha1(refs->new_sha1)) {
71 memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
72 buf[40] = '\n';
73 write(pipe_fd[1], buf, 41);
75 refs = refs->next;
77 close(pipe_fd[1]);
79 for (;;) {
80 int status, code;
81 pid_t waiting = waitpid(pid, &status, 0);
83 if (waiting < 0) {
84 if (errno == EINTR)
85 continue;
86 return error("waitpid failed (%s)", strerror(errno));
88 if ((waiting != pid) || WIFSIGNALED(status) ||
89 !WIFEXITED(status))
90 return error("pack-objects died with strange error");
91 code = WEXITSTATUS(status);
92 if (code)
93 return -code;
94 return 0;
98 static void unmark_and_free(struct commit_list *list, unsigned int mark)
100 while (list) {
101 struct commit_list *temp = list;
102 temp->item->object.flags &= ~mark;
103 list = temp->next;
104 free(temp);
108 static int ref_newer(const unsigned char *new_sha1,
109 const unsigned char *old_sha1)
111 struct object *o;
112 struct commit *old, *new;
113 struct commit_list *list, *used;
114 int found = 0;
116 /* Both new and old must be commit-ish and new is descendant of
117 * old. Otherwise we require --force.
119 o = deref_tag(parse_object(old_sha1), NULL, 0);
120 if (!o || o->type != OBJ_COMMIT)
121 return 0;
122 old = (struct commit *) o;
124 o = deref_tag(parse_object(new_sha1), NULL, 0);
125 if (!o || o->type != OBJ_COMMIT)
126 return 0;
127 new = (struct commit *) o;
129 if (parse_commit(new) < 0)
130 return 0;
132 used = list = NULL;
133 commit_list_insert(new, &list);
134 while (list) {
135 new = pop_most_recent_commit(&list, 1);
136 commit_list_insert(new, &used);
137 if (new == old) {
138 found = 1;
139 break;
142 unmark_and_free(list, 1);
143 unmark_and_free(used, 1);
144 return found;
147 static struct ref *local_refs, **local_tail;
148 static struct ref *remote_refs, **remote_tail;
150 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
152 struct ref *ref;
153 int len = strlen(refname) + 1;
154 ref = xcalloc(1, sizeof(*ref) + len);
155 hashcpy(ref->new_sha1, sha1);
156 memcpy(ref->name, refname, len);
157 *local_tail = ref;
158 local_tail = &ref->next;
159 return 0;
162 static void get_local_heads(void)
164 local_tail = &local_refs;
165 for_each_ref(one_local_ref, NULL);
168 static int receive_status(int in)
170 char line[1000];
171 int ret = 0;
172 int len = packet_read_line(in, line, sizeof(line));
173 if (len < 10 || memcmp(line, "unpack ", 7)) {
174 fprintf(stderr, "did not receive status back\n");
175 return -1;
177 if (memcmp(line, "unpack ok\n", 10)) {
178 fputs(line, stderr);
179 ret = -1;
181 while (1) {
182 len = packet_read_line(in, line, sizeof(line));
183 if (!len)
184 break;
185 if (len < 3 ||
186 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
187 fprintf(stderr, "protocol error: %s\n", line);
188 ret = -1;
189 break;
191 if (!memcmp(line, "ok", 2))
192 continue;
193 fputs(line, stderr);
194 ret = -1;
196 return ret;
199 static int send_pack(int in, int out, int nr_refspec, char **refspec)
201 struct ref *ref;
202 int new_refs;
203 int ret = 0;
204 int ask_for_status_report = 0;
205 int allow_deleting_refs = 0;
206 int expect_status_report = 0;
208 /* No funny business with the matcher */
209 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
210 get_local_heads();
212 /* Does the other end support the reporting? */
213 if (server_supports("report-status"))
214 ask_for_status_report = 1;
215 if (server_supports("delete-refs"))
216 allow_deleting_refs = 1;
218 /* match them up */
219 if (!remote_tail)
220 remote_tail = &remote_refs;
221 if (match_refs(local_refs, remote_refs, &remote_tail,
222 nr_refspec, refspec, send_all))
223 return -1;
225 if (!remote_refs) {
226 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
227 return 0;
231 * Finally, tell the other end!
233 new_refs = 0;
234 for (ref = remote_refs; ref; ref = ref->next) {
235 char old_hex[60], *new_hex;
236 int delete_ref;
238 if (!ref->peer_ref)
239 continue;
241 delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
242 if (delete_ref && !allow_deleting_refs) {
243 error("remote does not support deleting refs");
244 ret = -2;
245 continue;
247 if (!delete_ref &&
248 !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
249 if (verbose)
250 fprintf(stderr, "'%s': up-to-date\n", ref->name);
251 continue;
254 /* This part determines what can overwrite what.
255 * The rules are:
257 * (0) you can always use --force or +A:B notation to
258 * selectively force individual ref pairs.
260 * (1) if the old thing does not exist, it is OK.
262 * (2) if you do not have the old thing, you are not allowed
263 * to overwrite it; you would not know what you are losing
264 * otherwise.
266 * (3) if both new and old are commit-ish, and new is a
267 * descendant of old, it is OK.
269 * (4) regardless of all of the above, removing :B is
270 * always allowed.
273 if (!force_update &&
274 !delete_ref &&
275 !is_null_sha1(ref->old_sha1) &&
276 !ref->force) {
277 if (!has_sha1_file(ref->old_sha1) ||
278 !ref_newer(ref->peer_ref->new_sha1,
279 ref->old_sha1)) {
280 /* We do not have the remote ref, or
281 * we know that the remote ref is not
282 * an ancestor of what we are trying to
283 * push. Either way this can be losing
284 * commits at the remote end and likely
285 * we were not up to date to begin with.
287 error("remote '%s' is not a strict "
288 "subset of local ref '%s'. "
289 "maybe you are not up-to-date and "
290 "need to pull first?",
291 ref->name,
292 ref->peer_ref->name);
293 ret = -2;
294 continue;
297 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
298 if (!delete_ref)
299 new_refs++;
300 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
301 new_hex = sha1_to_hex(ref->new_sha1);
303 if (ask_for_status_report) {
304 packet_write(out, "%s %s %s%c%s",
305 old_hex, new_hex, ref->name, 0,
306 "report-status");
307 ask_for_status_report = 0;
308 expect_status_report = 1;
310 else
311 packet_write(out, "%s %s %s",
312 old_hex, new_hex, ref->name);
313 if (delete_ref)
314 fprintf(stderr, "deleting '%s'\n", ref->name);
315 else {
316 fprintf(stderr, "updating '%s'", ref->name);
317 if (strcmp(ref->name, ref->peer_ref->name))
318 fprintf(stderr, " using '%s'",
319 ref->peer_ref->name);
320 fprintf(stderr, "\n from %s\n to %s\n",
321 old_hex, new_hex);
325 packet_flush(out);
326 if (new_refs)
327 ret = pack_objects(out, remote_refs);
328 close(out);
330 if (expect_status_report) {
331 if (receive_status(in))
332 ret = -4;
335 if (!new_refs && ret == 0)
336 fprintf(stderr, "Everything up-to-date\n");
337 return ret;
340 static void verify_remote_names(int nr_heads, char **heads)
342 int i;
344 for (i = 0; i < nr_heads; i++) {
345 const char *remote = strchr(heads[i], ':');
347 remote = remote ? (remote + 1) : heads[i];
348 switch (check_ref_format(remote)) {
349 case 0: /* ok */
350 case -2: /* ok but a single level -- that is fine for
351 * a match pattern.
353 continue;
355 die("remote part of refspec is not a valid name in %s",
356 heads[i]);
360 int main(int argc, char **argv)
362 int i, nr_heads = 0;
363 char *dest = NULL;
364 char **heads = NULL;
365 int fd[2], ret;
366 pid_t pid;
368 setup_git_directory();
369 git_config(git_default_config);
371 argv++;
372 for (i = 1; i < argc; i++, argv++) {
373 char *arg = *argv;
375 if (*arg == '-') {
376 if (!strncmp(arg, "--exec=", 7)) {
377 exec = arg + 7;
378 continue;
380 if (!strcmp(arg, "--all")) {
381 send_all = 1;
382 continue;
384 if (!strcmp(arg, "--force")) {
385 force_update = 1;
386 continue;
388 if (!strcmp(arg, "--verbose")) {
389 verbose = 1;
390 continue;
392 if (!strcmp(arg, "--thin")) {
393 use_thin_pack = 1;
394 continue;
396 usage(send_pack_usage);
398 if (!dest) {
399 dest = arg;
400 continue;
402 heads = argv;
403 nr_heads = argc - i;
404 break;
406 if (!dest)
407 usage(send_pack_usage);
408 if (heads && send_all)
409 usage(send_pack_usage);
410 verify_remote_names(nr_heads, heads);
412 pid = git_connect(fd, dest, exec);
413 if (pid < 0)
414 return 1;
415 ret = send_pack(fd[0], fd[1], nr_heads, heads);
416 close(fd[0]);
417 close(fd[1]);
418 ret |= finish_connect(pid);
419 return !!ret;