git-fetch-script: use git-fetch-pack for local and ssh fetches.
[git] / fetch-pack.c
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include <sys/wait.h>
5
6 static const char fetch_pack_usage[] = "git-fetch-pack [host:]directory [heads]* < mycommitlist";
7 static const char *exec = "git-upload-pack";
8
9 static int get_ack(int fd, unsigned char *result_sha1)
10 {
11         static char line[1000];
12         int len = packet_read_line(fd, line, sizeof(line));
13
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;
23         }
24         die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
25 }
26
27 static int find_common(int fd[2], unsigned char *result_sha1, unsigned char *remote)
28 {
29         static char line[1000];
30         int count = 0, flushes = 0, retval;
31         FILE *revs;
32
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++;
48
49                         /*
50                          * We keep one window "ahead" of the other side, and
51                          * will wait for an ACK only on the next one
52                          */
53                         if (count == 32)
54                                 continue;
55                         if (get_ack(fd[0], result_sha1)) {
56                                 flushes = 0;
57                                 retval = 0;
58                                 break;
59                         }
60                         flushes--;
61                 }
62         }
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;
69         }
70         return retval;
71 }
72
73 static int get_old_sha1(const char *refname, unsigned char *sha1)
74 {
75         int fd, ret;
76
77         fd = open(git_path("%s", refname), O_RDONLY);
78         ret = -1;
79         if (fd >= 0) {
80                 char buffer[60];
81                 if (read(fd, buffer, sizeof(buffer)) >= 40)
82                         ret = get_sha1_hex(buffer, sha1);
83                 close(fd);
84         }
85         return ret;
86 }
87
88 static int check_ref(const char *refname, const unsigned char *sha1)
89 {
90         unsigned char mysha1[20];
91         char oldhex[41];
92
93         if (get_old_sha1(refname, mysha1) < 0)
94                 memset(mysha1, 0, 20);
95
96         if (!memcmp(sha1, mysha1, 20)) {
97                 fprintf(stderr, "%s: unchanged\n", refname);
98                 return 0;
99         }
100         
101         memcpy(oldhex, sha1_to_hex(mysha1), 41);
102         fprintf(stderr, "%s: %s (%s)\n", refname, sha1_to_hex(sha1), oldhex);
103         return 1;
104 }
105
106 static int get_remote_heads(int fd, int nr_match, char **match, unsigned char *result)
107 {
108         int count = 0;
109
110         for (;;) {
111                 static char line[1000];
112                 unsigned char sha1[20];
113                 char *refname;
114                 int len;
115
116                 len = packet_read_line(fd, line, sizeof(line));
117                 if (!len)
118                         break;
119                 if (line[len-1] == '\n')
120                         line[--len] = 0;
121                 if (len < 42 || get_sha1_hex(line, sha1))
122                         die("git-fetch-pack: protocol error - expected ref descriptor, got '%sä'", line);
123                 refname = line+41;
124                 if (nr_match && !path_match(refname, nr_match, match))
125                         continue;
126                 if (check_ref(refname, sha1)) {
127                         count++;
128                         memcpy(result, sha1, 20);
129                 }
130         }
131         return count;
132 }
133
134 static int fetch_pack(int fd[2], int nr_match, char **match)
135 {
136         unsigned char sha1[20], remote[20];
137         int heads, status;
138         pid_t pid;
139
140         heads = get_remote_heads(fd[0], nr_match, match, remote);
141         if (heads != 1) {
142                 packet_flush(fd[1]);
143                 die(heads ? "multiple remote heads" : "no matching remote head");
144         }
145         if (find_common(fd, sha1, remote) < 0)
146                 die("git-fetch-pack: no common commits");
147         pid = fork();
148         if (pid < 0)
149                 die("git-fetch-pack: unable to fork off git-unpack-objects");
150         if (!pid) {
151                 close(fd[1]);
152                 dup2(fd[0], 0);
153                 close(fd[0]);
154                 execlp("git-unpack-objects", "git-unpack-objects", NULL);
155                 die("git-unpack-objects exec failed");
156         }
157         close(fd[0]);
158         close(fd[1]);
159         while (waitpid(pid, &status, 0) < 0) {
160                 if (errno != EINTR)
161                         die("waiting for git-unpack-objects: %s", strerror(errno));
162         }
163         if (WIFEXITED(status)) {
164                 int code = WEXITSTATUS(status);
165                 if (code)
166                         die("git-unpack-objects died with error code %d", code);
167                 puts(sha1_to_hex(remote));
168                 return 0;
169         }
170         if (WIFSIGNALED(status)) {
171                 int sig = WTERMSIG(status);
172                 die("git-unpack-objects died of signal %d", sig);
173         }
174         die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status);
175 }
176
177 int main(int argc, char **argv)
178 {
179         int i, ret, nr_heads;
180         char *dest = NULL, **heads;
181         int fd[2];
182         pid_t pid;
183
184         nr_heads = 0;
185         heads = NULL;
186         for (i = 1; i < argc; i++) {
187                 char *arg = argv[i];
188
189                 if (*arg == '-') {
190                         /* Arguments go here */
191                         usage(fetch_pack_usage);
192                 }
193                 dest = arg;
194                 heads = argv + i + 1;
195                 nr_heads = argc - i - 1;
196                 break;
197         }
198         if (!dest)
199                 usage(fetch_pack_usage);
200         pid = git_connect(fd, dest, exec);
201         if (pid < 0)
202                 return 1;
203         ret = fetch_pack(fd, nr_heads, heads);
204         close(fd[0]);
205         close(fd[1]);
206         finish_connect(pid);
207         return ret;
208 }