Add revspec documentation for ':path', ':[0-3]:path' and git-describe
[git/jrn.git] / send-pack.c
blob5bb123a37696384c5413dac128529d1c1f679940
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;
17 static int is_zero_sha1(const unsigned char *sha1)
19 int i;
21 for (i = 0; i < 20; i++) {
22 if (*sha1++)
23 return 0;
25 return 1;
28 static void exec_pack_objects(void)
30 static const char *args[] = {
31 "pack-objects",
32 "--stdout",
33 NULL
35 execv_git_cmd(args);
36 die("git-pack-objects exec failed (%s)", strerror(errno));
39 static void exec_rev_list(struct ref *refs)
41 static const char *args[4];
42 int i = 0;
44 args[i++] = "rev-list"; /* 0 */
45 if (use_thin_pack) /* 1 */
46 args[i++] = "--objects-edge";
47 else
48 args[i++] = "--objects";
50 args[i++] = "--stdin";
52 args[i] = NULL;
53 execv_git_cmd(args);
54 die("git-rev-list exec failed (%s)", strerror(errno));
58 * Run "rev-list --stdin | pack-objects" pipe.
60 static void rev_list(int fd, struct ref *refs)
62 int pipe_fd[2];
63 pid_t pack_objects_pid;
65 if (pipe(pipe_fd) < 0)
66 die("rev-list setup: pipe failed");
67 pack_objects_pid = fork();
68 if (!pack_objects_pid) {
69 /* The child becomes pack-objects; reads from pipe
70 * and writes to the original fd
72 dup2(pipe_fd[0], 0);
73 dup2(fd, 1);
74 close(pipe_fd[0]);
75 close(pipe_fd[1]);
76 close(fd);
77 exec_pack_objects();
78 die("pack-objects setup failed");
80 if (pack_objects_pid < 0)
81 die("pack-objects fork failed");
83 /* We become rev-list --stdin; output goes to pipe. */
84 dup2(pipe_fd[1], 1);
85 close(pipe_fd[0]);
86 close(pipe_fd[1]);
87 close(fd);
88 exec_rev_list(refs);
92 * Create "rev-list --stdin | pack-objects" pipe and feed
93 * the refs into the pipeline.
95 static void rev_list_generate(int fd, struct ref *refs)
97 int pipe_fd[2];
98 pid_t rev_list_generate_pid;
100 if (pipe(pipe_fd) < 0)
101 die("rev-list-generate setup: pipe failed");
102 rev_list_generate_pid = fork();
103 if (!rev_list_generate_pid) {
104 /* The child becomes the "rev-list | pack-objects"
105 * pipeline. It takes input from us, and its output
106 * goes to fd.
108 dup2(pipe_fd[0], 0);
109 dup2(fd, 1);
110 close(pipe_fd[0]);
111 close(pipe_fd[1]);
112 close(fd);
113 rev_list(fd, refs);
114 die("rev-list setup failed");
116 if (rev_list_generate_pid < 0)
117 die("rev-list-generate fork failed");
119 /* We feed the rev parameters to them. We do not write into
120 * fd nor read from the pipe.
122 close(pipe_fd[0]);
123 close(fd);
124 while (refs) {
125 char buf[42];
127 if (!is_null_sha1(refs->old_sha1) &&
128 has_sha1_file(refs->old_sha1)) {
129 memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
130 buf[0] = '^';
131 buf[41] = '\n';
132 write(pipe_fd[1], buf, 42);
134 if (!is_null_sha1(refs->new_sha1)) {
135 memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
136 buf[40] = '\n';
137 write(pipe_fd[1], buf, 41);
139 refs = refs->next;
141 close(pipe_fd[1]);
142 // waitpid(rev_list_generate_pid);
143 exit(0);
147 * Make a pack stream and spit it out into file descriptor fd
149 static void pack_objects(int fd, struct ref *refs)
151 pid_t rev_list_pid;
153 rev_list_pid = fork();
154 if (!rev_list_pid) {
155 rev_list_generate(fd, refs);
156 die("rev-list setup failed");
158 if (rev_list_pid < 0)
159 die("rev-list fork failed");
161 * We don't wait for the rev-list pipeline in the parent:
162 * we end up waiting for the other end instead
166 static void unmark_and_free(struct commit_list *list, unsigned int mark)
168 while (list) {
169 struct commit_list *temp = list;
170 temp->item->object.flags &= ~mark;
171 list = temp->next;
172 free(temp);
176 static int ref_newer(const unsigned char *new_sha1,
177 const unsigned char *old_sha1)
179 struct object *o;
180 struct commit *old, *new;
181 struct commit_list *list, *used;
182 int found = 0;
184 /* Both new and old must be commit-ish and new is descendant of
185 * old. Otherwise we require --force.
187 o = deref_tag(parse_object(old_sha1), NULL, 0);
188 if (!o || o->type != OBJ_COMMIT)
189 return 0;
190 old = (struct commit *) o;
192 o = deref_tag(parse_object(new_sha1), NULL, 0);
193 if (!o || o->type != OBJ_COMMIT)
194 return 0;
195 new = (struct commit *) o;
197 if (parse_commit(new) < 0)
198 return 0;
200 used = list = NULL;
201 commit_list_insert(new, &list);
202 while (list) {
203 new = pop_most_recent_commit(&list, 1);
204 commit_list_insert(new, &used);
205 if (new == old) {
206 found = 1;
207 break;
210 unmark_and_free(list, 1);
211 unmark_and_free(used, 1);
212 return found;
215 static struct ref *local_refs, **local_tail;
216 static struct ref *remote_refs, **remote_tail;
218 static int one_local_ref(const char *refname, const unsigned char *sha1)
220 struct ref *ref;
221 int len = strlen(refname) + 1;
222 ref = xcalloc(1, sizeof(*ref) + len);
223 hashcpy(ref->new_sha1, sha1);
224 memcpy(ref->name, refname, len);
225 *local_tail = ref;
226 local_tail = &ref->next;
227 return 0;
230 static void get_local_heads(void)
232 local_tail = &local_refs;
233 for_each_ref(one_local_ref);
236 static int receive_status(int in)
238 char line[1000];
239 int ret = 0;
240 int len = packet_read_line(in, line, sizeof(line));
241 if (len < 10 || memcmp(line, "unpack ", 7)) {
242 fprintf(stderr, "did not receive status back\n");
243 return -1;
245 if (memcmp(line, "unpack ok\n", 10)) {
246 fputs(line, stderr);
247 ret = -1;
249 while (1) {
250 len = packet_read_line(in, line, sizeof(line));
251 if (!len)
252 break;
253 if (len < 3 ||
254 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
255 fprintf(stderr, "protocol error: %s\n", line);
256 ret = -1;
257 break;
259 if (!memcmp(line, "ok", 2))
260 continue;
261 fputs(line, stderr);
262 ret = -1;
264 return ret;
267 static int send_pack(int in, int out, int nr_refspec, char **refspec)
269 struct ref *ref;
270 int new_refs;
271 int ret = 0;
272 int ask_for_status_report = 0;
273 int expect_status_report = 0;
275 /* No funny business with the matcher */
276 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
277 get_local_heads();
279 /* Does the other end support the reporting? */
280 if (server_supports("report-status"))
281 ask_for_status_report = 1;
283 /* match them up */
284 if (!remote_tail)
285 remote_tail = &remote_refs;
286 if (match_refs(local_refs, remote_refs, &remote_tail,
287 nr_refspec, refspec, send_all))
288 return -1;
290 if (!remote_refs) {
291 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
292 return 0;
296 * Finally, tell the other end!
298 new_refs = 0;
299 for (ref = remote_refs; ref; ref = ref->next) {
300 char old_hex[60], *new_hex;
301 if (!ref->peer_ref)
302 continue;
303 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
304 if (verbose)
305 fprintf(stderr, "'%s': up-to-date\n", ref->name);
306 continue;
309 /* This part determines what can overwrite what.
310 * The rules are:
312 * (0) you can always use --force or +A:B notation to
313 * selectively force individual ref pairs.
315 * (1) if the old thing does not exist, it is OK.
317 * (2) if you do not have the old thing, you are not allowed
318 * to overwrite it; you would not know what you are losing
319 * otherwise.
321 * (3) if both new and old are commit-ish, and new is a
322 * descendant of old, it is OK.
325 if (!force_update &&
326 !is_zero_sha1(ref->old_sha1) &&
327 !ref->force) {
328 if (!has_sha1_file(ref->old_sha1) ||
329 !ref_newer(ref->peer_ref->new_sha1,
330 ref->old_sha1)) {
331 /* We do not have the remote ref, or
332 * we know that the remote ref is not
333 * an ancestor of what we are trying to
334 * push. Either way this can be losing
335 * commits at the remote end and likely
336 * we were not up to date to begin with.
338 error("remote '%s' is not a strict "
339 "subset of local ref '%s'. "
340 "maybe you are not up-to-date and "
341 "need to pull first?",
342 ref->name,
343 ref->peer_ref->name);
344 ret = -2;
345 continue;
348 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
349 if (is_zero_sha1(ref->new_sha1)) {
350 error("cannot happen anymore");
351 ret = -3;
352 continue;
354 new_refs++;
355 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
356 new_hex = sha1_to_hex(ref->new_sha1);
358 if (ask_for_status_report) {
359 packet_write(out, "%s %s %s%c%s",
360 old_hex, new_hex, ref->name, 0,
361 "report-status");
362 ask_for_status_report = 0;
363 expect_status_report = 1;
365 else
366 packet_write(out, "%s %s %s",
367 old_hex, new_hex, ref->name);
368 fprintf(stderr, "updating '%s'", ref->name);
369 if (strcmp(ref->name, ref->peer_ref->name))
370 fprintf(stderr, " using '%s'", ref->peer_ref->name);
371 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
374 packet_flush(out);
375 if (new_refs)
376 pack_objects(out, remote_refs);
377 close(out);
379 if (expect_status_report) {
380 if (receive_status(in))
381 ret = -4;
384 if (!new_refs && ret == 0)
385 fprintf(stderr, "Everything up-to-date\n");
386 return ret;
390 int main(int argc, char **argv)
392 int i, nr_heads = 0;
393 char *dest = NULL;
394 char **heads = NULL;
395 int fd[2], ret;
396 pid_t pid;
398 setup_git_directory();
399 git_config(git_default_config);
401 argv++;
402 for (i = 1; i < argc; i++, argv++) {
403 char *arg = *argv;
405 if (*arg == '-') {
406 if (!strncmp(arg, "--exec=", 7)) {
407 exec = arg + 7;
408 continue;
410 if (!strcmp(arg, "--all")) {
411 send_all = 1;
412 continue;
414 if (!strcmp(arg, "--force")) {
415 force_update = 1;
416 continue;
418 if (!strcmp(arg, "--verbose")) {
419 verbose = 1;
420 continue;
422 if (!strcmp(arg, "--thin")) {
423 use_thin_pack = 1;
424 continue;
426 usage(send_pack_usage);
428 if (!dest) {
429 dest = arg;
430 continue;
432 heads = argv;
433 nr_heads = argc - i;
434 break;
436 if (!dest)
437 usage(send_pack_usage);
438 if (heads && send_all)
439 usage(send_pack_usage);
440 pid = git_connect(fd, dest, exec);
441 if (pid < 0)
442 return 1;
443 ret = send_pack(fd[0], fd[1], nr_heads, heads);
444 close(fd[0]);
445 close(fd[1]);
446 ret |= finish_connect(pid);
447 return !!ret;