5 static const char send_pack_usage
[] = "git-send-pack [--exec=other] destination [heads]*";
6 static const char *exec
= "git-receive-pack";
8 static int path_match(const char *path
, int nr
, char **match
)
11 int pathlen
= strlen(path
);
13 for (i
= 0; i
< nr
; i
++) {
17 if (!len
|| len
> pathlen
)
19 if (memcmp(path
+ pathlen
- len
, s
, len
))
21 if (pathlen
> len
&& path
[pathlen
- len
- 1] != '/')
31 unsigned char old_sha1
[20];
32 unsigned char new_sha1
[20];
36 static void exec_pack_objects(void)
38 static char *args
[] = {
43 execvp("git-pack-objects", args
);
44 die("git-pack-objects exec failed (%s)", strerror(errno
));
47 static void exec_rev_list(struct ref
*refs
)
49 static char *args
[1000];
52 args
[i
++] = "git-rev-list"; /* 0 */
53 args
[i
++] = "--objects"; /* 1 */
55 char *buf
= malloc(100);
57 die("git-rev-list environment overflow");
59 snprintf(buf
, 50, "^%s", sha1_to_hex(refs
->old_sha1
));
62 snprintf(buf
, 50, "%s", sha1_to_hex(refs
->new_sha1
));
66 execvp("git-rev-list", args
);
67 die("git-rev-list exec failed (%s)", strerror(errno
));
70 static void rev_list(int fd
, struct ref
*refs
)
73 pid_t pack_objects_pid
;
75 if (pipe(pipe_fd
) < 0)
76 die("rev-list setup: pipe failed");
77 pack_objects_pid
= fork();
78 if (!pack_objects_pid
) {
85 die("pack-objects setup failed");
87 if (pack_objects_pid
< 0)
88 die("pack-objects fork failed");
96 static int pack_objects(int fd
, struct ref
*refs
)
100 rev_list_pid
= fork();
103 die("rev-list setup failed");
105 if (rev_list_pid
< 0)
106 die("rev-list fork failed");
108 * We don't wait for the rev-list pipeline in the parent:
109 * we end up waiting for the other end instead
113 static int read_ref(const char *ref
, unsigned char *sha1
)
116 static char pathname
[PATH_MAX
];
118 const char *git_dir
= gitenv(GIT_DIR_ENVIRONMENT
) ? : DEFAULT_GIT_DIR_ENVIRONMENT
;
120 snprintf(pathname
, sizeof(pathname
), "%s/%s", git_dir
, ref
);
121 fd
= open(pathname
, O_RDONLY
);
125 if (read(fd
, buffer
, sizeof(buffer
)) >= 40)
126 ret
= get_sha1_hex(buffer
, sha1
);
131 static int send_pack(int in
, int out
, int nr_match
, char **match
)
133 struct ref
*ref_list
= NULL
, **last_ref
= &ref_list
;
137 unsigned char old_sha1
[20];
138 unsigned char new_sha1
[20];
139 static char buffer
[1000];
143 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
146 if (buffer
[len
-1] == '\n')
149 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
150 die("protocol error: expected sha/ref, got '%s'", buffer
);
152 if (nr_match
&& !path_match(name
, nr_match
, match
))
154 if (read_ref(name
, new_sha1
) < 0)
155 return error("no such local reference '%s'", name
);
156 if (!has_sha1_file(old_sha1
))
157 return error("remote '%s' points to object I don't have", name
);
158 if (!memcmp(old_sha1
, new_sha1
, 20)) {
159 fprintf(stderr
, "'%s' unchanged\n", name
);
162 ref
= xmalloc(sizeof(*ref
) + len
- 40);
163 memcpy(ref
->old_sha1
, old_sha1
, 20);
164 memcpy(ref
->new_sha1
, new_sha1
, 20);
165 memcpy(ref
->name
, buffer
+ 41, len
- 40);
168 last_ref
= &ref
->next
;
171 for (ref
= ref_list
; ref
; ref
= ref
->next
) {
172 char old_hex
[60], *new_hex
;
173 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
174 new_hex
= sha1_to_hex(ref
->new_sha1
);
175 packet_write(out
, "%s %s %s", old_hex
, new_hex
, ref
->name
);
176 fprintf(stderr
, "'%s': updating from %s to %s\n", ref
->name
, old_hex
, new_hex
);
181 pack_objects(out
, ref_list
);
187 * First, make it shell-safe. We do this by just disallowing any
188 * special characters. Somebody who cares can do escaping and let
189 * through the rest. But since we're doing to feed this to ssh as
190 * a command line, we're going to be pretty damn anal for now.
192 static char *shell_safe(char *url
)
196 static const char flags
[256] = {
200 ['.'] = 1, ['/'] = 1,
201 ['-'] = 1, ['+'] = 1,
205 while ((c
= *n
++) != 0) {
207 die("I don't like '%c'. Sue me.", c
);
213 * Yeah, yeah, fixme. Need to pass in the heads etc.
215 static int setup_connection(int fd
[2], char *url
, char **heads
)
218 const char *host
, *path
;
223 url
= shell_safe(url
);
226 colon
= strchr(url
, ':');
232 snprintf(command
, sizeof(command
), "%s %s", exec
, path
);
233 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
234 die("unable to create pipe pair for communication");
237 dup2(pipefd
[1][0], 0);
238 dup2(pipefd
[0][1], 1);
244 execlp("ssh", "ssh", host
, command
, NULL
);
246 execlp("sh", "sh", "-c", command
, NULL
);
249 fd
[0] = pipefd
[0][0];
250 fd
[1] = pipefd
[1][1];
256 int main(int argc
, char **argv
)
265 for (i
= 1; i
< argc
; i
++) {
269 if (!strncmp(arg
, "--exec=", 7)) {
273 usage(send_pack_usage
);
277 nr_heads
= argc
- i
-1;
281 usage(send_pack_usage
);
282 pid
= setup_connection(fd
, dest
, heads
);
285 ret
= send_pack(fd
[0], fd
[1], nr_heads
, heads
);
288 waitpid(pid
, NULL
, 0);