git-send-pack: actually send the object pack
[git.git] / send-pack.c
1 #include "cache.h"
2 #include "pkt-line.h"
3 #include <sys/wait.h>
4
5 static const char send_pack_usage[] = "git-send-pack [--exec=other] destination [heads]*";
6
7 static const char *exec = "git-receive-pack";
8
9 struct ref {
10         struct ref *next;
11         unsigned char old_sha1[20];
12         unsigned char new_sha1[20];
13         char name[0];
14 };
15
16 static void exec_pack_objects(void)
17 {
18         static char *args[] = {
19                 "git-pack-objects",
20                 "--stdout",
21                 NULL
22         };
23         execvp("git-pack-objects", args);
24         die("git-pack-objects exec failed (%s)", strerror(errno));
25 }
26
27 static void exec_rev_list(struct ref *refs)
28 {
29         static char *args[1000];
30         int i = 0;
31
32         args[i++] = "git-rev-list";     /* 0 */
33         args[i++] = "--objects";        /* 1 */
34         while (refs) {
35                 char *buf = malloc(100);
36                 if (i > 900)
37                         die("git-rev-list environment overflow");
38                 args[i++] = buf;
39                 snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
40                 buf += 50;
41                 args[i++] = buf;
42                 snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
43                 refs = refs->next;
44         }
45         args[i] = NULL;
46         execvp("git-rev-list", args);
47         die("git-rev-list exec failed (%s)", strerror(errno));
48 }
49
50 static void rev_list(int fd, struct ref *refs)
51 {
52         int pipe_fd[2];
53         pid_t pack_objects_pid;
54
55         if (pipe(pipe_fd) < 0)
56                 die("rev-list setup: pipe failed");
57         pack_objects_pid = fork();
58         if (!pack_objects_pid) {
59                 dup2(pipe_fd[0], 0);
60                 dup2(fd, 1);
61                 close(pipe_fd[0]);
62                 close(pipe_fd[1]);
63                 close(fd);
64                 exec_pack_objects();
65                 die("pack-objects setup failed");
66         }
67         if (pack_objects_pid < 0)
68                 die("pack-objects fork failed");
69         dup2(pipe_fd[1], 1);
70         close(pipe_fd[0]);
71         close(pipe_fd[1]);
72         close(fd);
73         exec_rev_list(refs);
74 }
75
76 static int pack_objects(int fd, struct ref *refs)
77 {
78         pid_t rev_list_pid;
79
80         rev_list_pid = fork();
81         if (!rev_list_pid) {
82                 rev_list(fd, refs);
83                 die("rev-list setup failed");
84         }
85         if (rev_list_pid < 0)
86                 die("rev-list fork failed");
87         /*
88          * We don't wait for the rev-list pipeline in the parent:
89          * we end up waiting for the other end instead
90          */
91 }
92
93 static int read_ref(const char *ref, unsigned char *sha1)
94 {
95         int fd, ret;
96         static char pathname[PATH_MAX];
97         char buffer[60];
98         const char *git_dir = gitenv(GIT_DIR_ENVIRONMENT) ? : DEFAULT_GIT_DIR_ENVIRONMENT;
99
100         snprintf(pathname, sizeof(pathname), "%s/%s", git_dir, ref);
101         fd = open(pathname, O_RDONLY);
102         if (fd < 0)
103                 return -1;
104         ret = -1;
105         if (read(fd, buffer, sizeof(buffer)) >= 40)
106                 ret = get_sha1_hex(buffer, sha1);
107         close(fd);
108         return ret;
109 }
110
111 static int send_pack(int in, int out)
112 {
113         struct ref *ref_list = NULL, **last_ref = &ref_list;
114         struct ref *ref;
115
116         for (;;) {
117                 unsigned char old_sha1[20];
118                 unsigned char new_sha1[20];
119                 static char buffer[1000];
120                 char *name;
121                 int len;
122
123                 len = packet_read_line(in, buffer, sizeof(buffer));
124                 if (!len)
125                         break;
126                 if (buffer[len-1] == '\n')
127                         buffer[--len] = 0;
128
129                 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
130                         die("protocol error: expected sha/ref, got '%s'", buffer);
131                 name = buffer + 41;
132                 if (read_ref(name, new_sha1) < 0)
133                         return error("no such local reference '%s'", name);
134                 if (!has_sha1_file(old_sha1))
135                         return error("remote '%s' points to object I don't have", name);
136                 if (!memcmp(old_sha1, new_sha1, 20)) {
137                         fprintf(stderr, "'%s' unchanged\n", name);
138                         continue;
139                 }
140                 ref = xmalloc(sizeof(*ref) + len - 40);
141                 memcpy(ref->old_sha1, old_sha1, 20);
142                 memcpy(ref->new_sha1, new_sha1, 20);
143                 memcpy(ref->name, buffer + 41, len - 40);
144                 ref->next = NULL;
145                 *last_ref = ref;
146                 last_ref = &ref->next;
147         }
148
149         for (ref = ref_list; ref; ref = ref->next) {
150                 char old_hex[60], *new_hex;
151                 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
152                 new_hex = sha1_to_hex(ref->new_sha1);
153                 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
154                 fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);
155         }
156         
157         packet_flush(out);
158         if (ref_list)
159                 pack_objects(out, ref_list);
160         close(out);
161         return 0;
162 }
163
164 /*
165  * First, make it shell-safe.  We do this by just disallowing any
166  * special characters. Somebody who cares can do escaping and let
167  * through the rest. But since we're doing to feed this to ssh as
168  * a command line, we're going to be pretty damn anal for now.
169  */
170 static char *shell_safe(char *url)
171 {
172         char *n = url;
173         unsigned char c;
174         static const char flags[256] = {
175                 ['0'...'9'] = 1,
176                 ['a'...'z'] = 1,
177                 ['A'...'Z'] = 1,
178                 ['.'] = 1, ['/'] = 1,
179                 ['-'] = 1, ['+'] = 1,
180                 [':'] = 1
181         };
182
183         while ((c = *n++) != 0) {
184                 if (flags[c] != 1)
185                         die("I don't like '%c'. Sue me.", c);
186         }
187         return url;
188 }
189
190 /*
191  * Yeah, yeah, fixme. Need to pass in the heads etc.
192  */
193 static int setup_connection(int fd[2], char *url, char **heads)
194 {
195         char command[1024];
196         const char *host, *path;
197         char *colon;
198         int pipefd[2][2];
199         pid_t pid;
200
201         url = shell_safe(url);
202         host = NULL;
203         path = url;
204         colon = strchr(url, ':');
205         if (colon) {
206                 *colon = 0;
207                 host = url;
208                 path = colon+1;
209         }
210         snprintf(command, sizeof(command), "%s %s", exec, path);
211         if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
212                 die("unable to create pipe pair for communication");
213         pid = fork();
214         if (!pid) {
215                 dup2(pipefd[1][0], 0);
216                 dup2(pipefd[0][1], 1);
217                 close(pipefd[0][0]);
218                 close(pipefd[0][1]);
219                 close(pipefd[1][0]);
220                 close(pipefd[1][1]);
221                 if (host)
222                         execlp("ssh", "ssh", host, command, NULL);
223                 else
224                         execlp("sh", "sh", "-c", command, NULL);
225                 die("exec failed");
226         }               
227         fd[0] = pipefd[0][0];
228         fd[1] = pipefd[1][1];
229         close(pipefd[0][1]);
230         close(pipefd[1][0]);
231         return pid;
232 }
233
234 int main(int argc, char **argv)
235 {
236         int i, nr_heads = 0;
237         char *dest = NULL;
238         char **heads = NULL;
239         int fd[2], ret;
240         pid_t pid;
241
242         argv++;
243         for (i = 1; i < argc; i++) {
244                 char *arg = *argv++;
245
246                 if (*arg == '-') {
247                         if (!strncmp(arg, "--exec=", 7)) {
248                                 exec = arg + 7;
249                                 continue;
250                         }
251                         usage(send_pack_usage);
252                 }
253                 dest = arg;
254                 heads = argv;
255                 nr_heads = argc - i -1;
256                 break;
257         }
258         if (!dest)
259                 usage(send_pack_usage);
260         pid = setup_connection(fd, dest, heads);
261         if (pid < 0)
262                 return 1;
263         ret = send_pack(fd[0], fd[1]);
264         close(fd[0]);
265         close(fd[1]);
266         waitpid(pid, NULL, 0);
267         return ret;
268 }