Add comment on what send-pack still needs to do
[git/debian.git] / send-pack.c
blobca3eabbd7f69a6ce2fef93c4dadb7287aaea65cd
1 #include "cache.h"
2 #include "pkt-line.h"
3 #include <sys/wait.h>
5 static const char send_pack_usage[] = "git-send-pack [--exec=other] destination [heads]*";
7 static const char *exec = "git-receive-pack";
9 struct ref {
10 struct ref *next;
11 unsigned char old_sha1[20];
12 unsigned char new_sha1[20];
13 char name[0];
16 static struct ref *ref_list = NULL, **last_ref = &ref_list;
18 static int read_ref(const char *ref, unsigned char *sha1)
20 int fd, ret;
21 static char pathname[PATH_MAX];
22 char buffer[60];
23 const char *git_dir = gitenv(GIT_DIR_ENVIRONMENT) ? : DEFAULT_GIT_DIR_ENVIRONMENT;
25 snprintf(pathname, sizeof(pathname), "%s/%s", git_dir, ref);
26 fd = open(pathname, O_RDONLY);
27 if (fd < 0)
28 return -1;
29 ret = -1;
30 if (read(fd, buffer, sizeof(buffer)) >= 40)
31 ret = get_sha1_hex(buffer, sha1);
32 close(fd);
33 return ret;
36 static int send_pack(int in, int out)
38 struct ref *ref;
40 for (;;) {
41 unsigned char old_sha1[20];
42 unsigned char new_sha1[20];
43 static char buffer[1000];
44 char *name;
45 int len;
47 len = packet_read_line(in, buffer, sizeof(buffer));
48 if (!len)
49 break;
50 if (buffer[len-1] == '\n')
51 buffer[--len] = 0;
53 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
54 die("protocol error: expected sha/ref, got '%s'", buffer);
55 name = buffer + 41;
56 if (read_ref(name, new_sha1) < 0)
57 return error("no such local reference '%s'", name);
58 if (!has_sha1_file(old_sha1))
59 return error("remote '%s' points to object I don't have", name);
60 if (!memcmp(old_sha1, new_sha1, 20)) {
61 fprintf(stderr, "'%s' unchanged\n", name);
62 continue;
64 ref = xmalloc(sizeof(*ref) + len - 40);
65 memcpy(ref->old_sha1, old_sha1, 20);
66 memcpy(ref->new_sha1, new_sha1, 20);
67 memcpy(ref->name, buffer + 41, len - 40);
68 ref->next = NULL;
69 *last_ref = ref;
70 last_ref = &ref->next;
73 for (ref = ref_list; ref; ref = ref->next) {
74 char old_hex[60], *new_hex;
75 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
76 new_hex = sha1_to_hex(ref->new_sha1);
77 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
78 fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);
81 packet_flush(out);
83 * FIXME! Here we need to now send the pack-file to the "out" fd, using something
84 * like this:
86 * fork() +
87 * dup2(out, 1) +
88 * execve("/bin/sh git-rev-list --objects ..for-each-ref-list.. | "
89 * "git-pack-objects --stdout");
91 * but I'm too tired right now.
93 close(out);
94 return 0;
98 * First, make it shell-safe. We do this by just disallowing any
99 * special characters. Somebody who cares can do escaping and let
100 * through the rest. But since we're doing to feed this to ssh as
101 * a command line, we're going to be pretty damn anal for now.
103 static char *shell_safe(char *url)
105 char *n = url;
106 unsigned char c;
107 static const char flags[256] = {
108 ['0'...'9'] = 1,
109 ['a'...'z'] = 1,
110 ['A'...'Z'] = 1,
111 ['.'] = 1, ['/'] = 1,
112 ['-'] = 1, ['+'] = 1,
113 [':'] = 1
116 while ((c = *n++) != 0) {
117 if (flags[c] != 1)
118 die("I don't like '%c'. Sue me.", c);
120 return url;
124 * Yeah, yeah, fixme. Need to pass in the heads etc.
126 static int setup_connection(int fd[2], char *url, char **heads)
128 char command[1024];
129 const char *host, *path;
130 char *colon;
131 int pipefd[2][2];
132 pid_t pid;
134 url = shell_safe(url);
135 host = NULL;
136 path = url;
137 colon = strchr(url, ':');
138 if (colon) {
139 *colon = 0;
140 host = url;
141 path = colon+1;
143 snprintf(command, sizeof(command), "%s %s", exec, path);
144 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
145 die("unable to create pipe pair for communication");
146 pid = fork();
147 if (!pid) {
148 dup2(pipefd[1][0], 0);
149 dup2(pipefd[0][1], 1);
150 close(pipefd[0][0]);
151 close(pipefd[0][1]);
152 close(pipefd[1][0]);
153 close(pipefd[1][1]);
154 if (host)
155 execlp("ssh", "ssh", host, command, NULL);
156 else
157 execlp("sh", "sh", "-c", command, NULL);
158 die("exec failed");
160 fd[0] = pipefd[0][0];
161 fd[1] = pipefd[1][1];
162 close(pipefd[0][1]);
163 close(pipefd[1][0]);
164 return pid;
167 int main(int argc, char **argv)
169 int i, nr_heads = 0;
170 char *dest = NULL;
171 char **heads = NULL;
172 int fd[2], ret;
173 pid_t pid;
175 argv++;
176 for (i = 1; i < argc; i++) {
177 char *arg = *argv++;
179 if (*arg == '-') {
180 if (!strncmp(arg, "--exec=", 7)) {
181 exec = arg + 7;
182 continue;
184 usage(send_pack_usage);
186 dest = arg;
187 heads = argv;
188 nr_heads = argc - i -1;
189 break;
191 if (!dest)
192 usage(send_pack_usage);
193 pid = setup_connection(fd, dest, heads);
194 if (pid < 0)
195 return 1;
196 ret = send_pack(fd[0], fd[1]);
197 close(fd[0]);
198 close(fd[1]);
199 waitpid(pid, NULL, 0);
200 return ret;