5 static const char send_pack_usage
[] = "git-send-pack [--exec=other] destination [heads]*";
7 static const char *exec
= "git-receive-pack";
11 unsigned char old_sha1
[20];
12 unsigned char new_sha1
[20];
16 static void exec_pack_objects(void)
18 static char *args
[] = {
23 execvp("git-pack-objects", args
);
24 die("git-pack-objects exec failed (%s)", strerror(errno
));
27 static void exec_rev_list(struct ref
*refs
)
29 static char *args
[1000];
32 args
[i
++] = "git-rev-list"; /* 0 */
33 args
[i
++] = "--objects"; /* 1 */
35 char *buf
= malloc(100);
37 die("git-rev-list environment overflow");
39 snprintf(buf
, 50, "^%s", sha1_to_hex(refs
->old_sha1
));
42 snprintf(buf
, 50, "%s", sha1_to_hex(refs
->new_sha1
));
46 execvp("git-rev-list", args
);
47 die("git-rev-list exec failed (%s)", strerror(errno
));
50 static void rev_list(int fd
, struct ref
*refs
)
53 pid_t pack_objects_pid
;
55 if (pipe(pipe_fd
) < 0)
56 die("rev-list setup: pipe failed");
57 pack_objects_pid
= fork();
58 if (!pack_objects_pid
) {
65 die("pack-objects setup failed");
67 if (pack_objects_pid
< 0)
68 die("pack-objects fork failed");
76 static int pack_objects(int fd
, struct ref
*refs
)
80 rev_list_pid
= fork();
83 die("rev-list setup failed");
86 die("rev-list fork failed");
88 * We don't wait for the rev-list pipeline in the parent:
89 * we end up waiting for the other end instead
93 static int read_ref(const char *ref
, unsigned char *sha1
)
96 static char pathname
[PATH_MAX
];
98 const char *git_dir
= gitenv(GIT_DIR_ENVIRONMENT
) ? : DEFAULT_GIT_DIR_ENVIRONMENT
;
100 snprintf(pathname
, sizeof(pathname
), "%s/%s", git_dir
, ref
);
101 fd
= open(pathname
, O_RDONLY
);
105 if (read(fd
, buffer
, sizeof(buffer
)) >= 40)
106 ret
= get_sha1_hex(buffer
, sha1
);
111 static int send_pack(int in
, int out
)
113 struct ref
*ref_list
= NULL
, **last_ref
= &ref_list
;
117 unsigned char old_sha1
[20];
118 unsigned char new_sha1
[20];
119 static char buffer
[1000];
123 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
126 if (buffer
[len
-1] == '\n')
129 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
130 die("protocol error: expected sha/ref, got '%s'", buffer
);
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
);
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);
146 last_ref
= &ref
->next
;
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
);
159 pack_objects(out
, ref_list
);
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.
170 static char *shell_safe(char *url
)
174 static const char flags
[256] = {
178 ['.'] = 1, ['/'] = 1,
179 ['-'] = 1, ['+'] = 1,
183 while ((c
= *n
++) != 0) {
185 die("I don't like '%c'. Sue me.", c
);
191 * Yeah, yeah, fixme. Need to pass in the heads etc.
193 static int setup_connection(int fd
[2], char *url
, char **heads
)
196 const char *host
, *path
;
201 url
= shell_safe(url
);
204 colon
= strchr(url
, ':');
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");
215 dup2(pipefd
[1][0], 0);
216 dup2(pipefd
[0][1], 1);
222 execlp("ssh", "ssh", host
, command
, NULL
);
224 execlp("sh", "sh", "-c", command
, NULL
);
227 fd
[0] = pipefd
[0][0];
228 fd
[1] = pipefd
[1][1];
234 int main(int argc
, char **argv
)
243 for (i
= 1; i
< argc
; i
++) {
247 if (!strncmp(arg
, "--exec=", 7)) {
251 usage(send_pack_usage
);
255 nr_heads
= argc
- i
-1;
259 usage(send_pack_usage
);
260 pid
= setup_connection(fd
, dest
, heads
);
263 ret
= send_pack(fd
[0], fd
[1]);
266 waitpid(pid
, NULL
, 0);