Remove multi-head support from fetch-pack
[alt-git.git] / fetch-pack.c
blob96ce7f5d5abd9faf66a189e583161e5860f582ef
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include <sys/wait.h>
6 static const char fetch_pack_usage[] = "git-fetch-pack [host:]directory [heads]* < mycommitlist";
7 static const char *exec = "git-upload-pack";
9 static int get_ack(int fd, unsigned char *result_sha1)
11 static char line[1000];
12 int len = packet_read_line(fd, line, sizeof(line));
14 if (!len)
15 die("git-fetch-pack: expected ACK/NAK, got EOF");
16 if (line[len-1] == '\n')
17 line[--len] = 0;
18 if (!strcmp(line, "NAK"))
19 return 0;
20 if (!strncmp(line, "ACK ", 3)) {
21 if (!get_sha1_hex(line+4, result_sha1))
22 return 1;
24 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
27 static int find_common(int fd[2], unsigned char *result_sha1, unsigned char *remote)
29 static char line[1000];
30 int count = 0, flushes = 0, retval;
31 FILE *revs;
33 revs = popen("git-rev-list $(git-rev-parse --all)", "r");
34 if (!revs)
35 die("unable to run 'git-rev-list'");
36 packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
37 packet_flush(fd[1]);
38 flushes = 1;
39 retval = -1;
40 while (fgets(line, sizeof(line), revs) != NULL) {
41 unsigned char sha1[20];
42 if (get_sha1_hex(line, sha1))
43 die("git-fetch-pack: expected object name, got crud");
44 packet_write(fd[1], "have %s\n", sha1_to_hex(sha1));
45 if (!(31 & ++count)) {
46 packet_flush(fd[1]);
47 flushes++;
50 * We keep one window "ahead" of the other side, and
51 * will wait for an ACK only on the next one
53 if (count == 32)
54 continue;
55 if (get_ack(fd[0], result_sha1)) {
56 flushes = 0;
57 retval = 0;
58 break;
60 flushes--;
63 pclose(revs);
64 packet_write(fd[1], "done\n");
65 while (flushes) {
66 flushes--;
67 if (get_ack(fd[0], result_sha1))
68 return 0;
70 return retval;
73 static int get_remote_heads(int fd, int nr_match, char **match, unsigned char *result)
75 int count = 0;
77 for (;;) {
78 static char line[1000];
79 unsigned char sha1[20];
80 char *refname;
81 int len;
83 len = packet_read_line(fd, line, sizeof(line));
84 if (!len)
85 break;
86 if (line[len-1] == '\n')
87 line[--len] = 0;
88 if (len < 42 || get_sha1_hex(line, sha1))
89 die("git-fetch-pack: protocol error - expected ref descriptor, got '%s¤'", line);
90 refname = line+41;
91 if (nr_match && !path_match(refname, nr_match, match))
92 continue;
93 count++;
94 memcpy(result, sha1, 20);
96 return count;
99 static int fetch_pack(int fd[2], int nr_match, char **match)
101 unsigned char sha1[20], remote[20];
102 int heads, status;
103 pid_t pid;
105 heads = get_remote_heads(fd[0], nr_match, match, remote);
106 if (heads != 1) {
107 packet_flush(fd[1]);
108 die(heads ? "multiple remote heads" : "no matching remote head");
110 if (find_common(fd, sha1, remote) < 0)
111 die("git-fetch-pack: no common commits");
112 pid = fork();
113 if (pid < 0)
114 die("git-fetch-pack: unable to fork off git-unpack-objects");
115 if (!pid) {
116 close(fd[1]);
117 dup2(fd[0], 0);
118 close(fd[0]);
119 execlp("git-unpack-objects", "git-unpack-objects", NULL);
120 die("git-unpack-objects exec failed");
122 close(fd[0]);
123 close(fd[1]);
124 while (waitpid(pid, &status, 0) < 0) {
125 if (errno != EINTR)
126 die("waiting for git-unpack-objects: %s", strerror(errno));
128 if (WIFEXITED(status)) {
129 int code = WEXITSTATUS(status);
130 if (code)
131 die("git-unpack-objects died with error code %d", code);
132 puts(sha1_to_hex(remote));
133 return 0;
135 if (WIFSIGNALED(status)) {
136 int sig = WTERMSIG(status);
137 die("git-unpack-objects died of signal %d", sig);
139 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status);
142 int main(int argc, char **argv)
144 int i, ret, nr_heads;
145 char *dest = NULL, **heads;
146 int fd[2];
147 pid_t pid;
149 nr_heads = 0;
150 heads = NULL;
151 for (i = 1; i < argc; i++) {
152 char *arg = argv[i];
154 if (*arg == '-') {
155 /* Arguments go here */
156 usage(fetch_pack_usage);
158 dest = arg;
159 heads = argv + i + 1;
160 nr_heads = argc - i - 1;
161 break;
163 if (!dest)
164 usage(fetch_pack_usage);
165 pid = git_connect(fd, dest, exec);
166 if (pid < 0)
167 return 1;
168 ret = fetch_pack(fd, nr_heads, heads);
169 close(fd[0]);
170 close(fd[1]);
171 finish_connect(pid);
172 return ret;