4 static const char send_pack_usage[] = "git-send-pack [--exec=other] destination [heads]*";
6 static const char *exec = "git-receive-pack";
10 unsigned char old_sha1[20];
11 unsigned char new_sha1[20];
15 static struct ref *ref_list = NULL;
17 static int read_ref(const char *ref, unsigned char *sha1)
20 static char pathname[PATH_MAX];
22 const char *git_dir = gitenv(GIT_DIR_ENVIRONMENT) ? : DEFAULT_GIT_DIR_ENVIRONMENT;
24 snprintf(pathname, sizeof(pathname), "%s/%s", git_dir, ref);
25 fd = open(pathname, O_RDONLY);
29 if (read(fd, buffer, sizeof(buffer)) >= 40)
30 ret = get_sha1_hex(buffer, sha1);
35 static int send_pack(int in, int out)
38 unsigned char old_sha1[20];
39 unsigned char new_sha1[20];
40 static char buffer[1000];
45 len = packet_read_line(in, buffer, sizeof(buffer));
48 if (buffer[len-1] == '\n')
51 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
52 die("protocol error: expected sha/ref, got '%s'", buffer);
54 if (read_ref(name, new_sha1) < 0) {
55 fprintf(stderr, "no such local reference '%s'\n", name);
58 if (!has_sha1_file(old_sha1)) {
59 fprintf(stderr, "remote '%s' points to object I don't have\n", name);
62 if (!memcmp(old_sha1, new_sha1, 20)) {
63 fprintf(stderr, "'%s' unchanged\n", name);
66 strcpy(new_hex, sha1_to_hex(new_sha1));
67 fprintf(stderr, "%s: updating from %s to %s\n", name, sha1_to_hex(old_sha1), new_hex);
69 n = xmalloc(sizeof(*n) + len - 40);
70 memcpy(n->old_sha1, old_sha1, 20);
71 memcpy(n->new_sha1, new_sha1, 20);
72 memcpy(n->name, buffer + 41, len - 40);
82 * First, make it shell-safe. We do this by just disallowing any
83 * special characters. Somebody who cares can do escaping and let
84 * through the rest. But since we're doing to feed this to ssh as
85 * a command line, we're going to be pretty damn anal for now.
87 static char *shell_safe(char *url)
91 static const char flags[256] = {
100 while ((c = *n++) != 0) {
102 die("I don't like '%c'. Sue me.", c);
108 * Yeah, yeah, fixme. Need to pass in the heads etc.
110 static int setup_connection(int fd[2], char *url, char **heads)
113 const char *host, *path;
117 url = shell_safe(url);
120 colon = strchr(url, ':');
126 snprintf(command, sizeof(command), "%s %s", exec, path);
127 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
128 die("unable to create pipe pair for communication");
130 dup2(pipefd[1][0], 0);
131 dup2(pipefd[0][1], 1);
137 execlp("ssh", "ssh", host, command, NULL);
139 execlp("sh", "sh", "-c", command, NULL);
142 fd[0] = pipefd[0][0];
143 fd[1] = pipefd[1][1];
149 int main(int argc, char **argv)
157 for (i = 1; i < argc; i++) {
161 if (!strncmp(arg, "--exec=", 7)) {
165 usage(send_pack_usage);
169 nr_heads = argc - i -1;
173 usage(send_pack_usage);
174 if (setup_connection(fd, dest, heads))
176 return send_pack(fd[0], fd[1]);