1#include "cache.h"
2#include "pkt-line.h"
3#include <sys/wait.h>
4
5static const char send_pack_usage[] = "git-send-pack [--exec=other] destination [heads]*";
6
7static const char *exec = "git-receive-pack";
8
9struct ref {
10 struct ref *next;
11 unsigned char old_sha1[20];
12 unsigned char new_sha1[20];
13 char name[0];
14};
15
16static struct ref *ref_list = NULL, **last_ref = &ref_list;
17
18static int read_ref(const char *ref, unsigned char *sha1)
19{
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;
24
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;
34}
35
36static int send_pack(int in, int out)
37{
38 struct ref *ref;
39
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;
46
47 len = packet_read_line(in, buffer, sizeof(buffer));
48 if (!len)
49 break;
50 if (buffer[len-1] == '\n')
51 buffer[--len] = 0;
52
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;
63 }
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;
71 }
72
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);
79 }
80
81 packet_flush(out);
82 close(out);
83 return 0;
84}
85
86/*
87 * First, make it shell-safe. We do this by just disallowing any
88 * special characters. Somebody who cares can do escaping and let
89 * through the rest. But since we're doing to feed this to ssh as
90 * a command line, we're going to be pretty damn anal for now.
91 */
92static char *shell_safe(char *url)
93{
94 char *n = url;
95 unsigned char c;
96 static const char flags[256] = {
97 ['0'...'9'] = 1,
98 ['a'...'z'] = 1,
99 ['A'...'Z'] = 1,
100 ['.'] = 1, ['/'] = 1,
101 ['-'] = 1, ['+'] = 1,
102 [':'] = 1
103 };
104
105 while ((c = *n++) != 0) {
106 if (flags[c] != 1)
107 die("I don't like '%c'. Sue me.", c);
108 }
109 return url;
110}
111
112/*
113 * Yeah, yeah, fixme. Need to pass in the heads etc.
114 */
115static int setup_connection(int fd[2], char *url, char **heads)
116{
117 char command[1024];
118 const char *host, *path;
119 char *colon;
120 int pipefd[2][2];
121 pid_t pid;
122
123 url = shell_safe(url);
124 host = NULL;
125 path = url;
126 colon = strchr(url, ':');
127 if (colon) {
128 *colon = 0;
129 host = url;
130 path = colon+1;
131 }
132 snprintf(command, sizeof(command), "%s %s", exec, path);
133 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
134 die("unable to create pipe pair for communication");
135 pid = fork();
136 if (!pid) {
137 dup2(pipefd[1][0], 0);
138 dup2(pipefd[0][1], 1);
139 close(pipefd[0][0]);
140 close(pipefd[0][1]);
141 close(pipefd[1][0]);
142 close(pipefd[1][1]);
143 if (host)
144 execlp("ssh", "ssh", host, command, NULL);
145 else
146 execlp("sh", "sh", "-c", command, NULL);
147 die("exec failed");
148 }
149 fd[0] = pipefd[0][0];
150 fd[1] = pipefd[1][1];
151 close(pipefd[0][1]);
152 close(pipefd[1][0]);
153 return pid;
154}
155
156int main(int argc, char **argv)
157{
158 int i, nr_heads = 0;
159 char *dest = NULL;
160 char **heads = NULL;
161 int fd[2], ret;
162 pid_t pid;
163
164 argv++;
165 for (i = 1; i < argc; i++) {
166 char *arg = *argv++;
167
168 if (*arg == '-') {
169 if (!strncmp(arg, "--exec=", 7)) {
170 exec = arg + 7;
171 continue;
172 }
173 usage(send_pack_usage);
174 }
175 dest = arg;
176 heads = argv;
177 nr_heads = argc - i -1;
178 break;
179 }
180 if (!dest)
181 usage(send_pack_usage);
182 pid = setup_connection(fd, dest, heads);
183 if (pid < 0)
184 return 1;
185 ret = send_pack(fd[0], fd[1]);
186 close(fd[0]);
187 close(fd[1]);
188 waitpid(pid, NULL, 0);
189 return ret;
190}