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 struct ref
*ref_list
= NULL
, **last_ref
= &ref_list
;
18 static int read_ref(const char *ref
, unsigned char *sha1
)
21 static char pathname
[PATH_MAX
];
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
);
30 if (read(fd
, buffer
, sizeof(buffer
)) >= 40)
31 ret
= get_sha1_hex(buffer
, sha1
);
36 static int send_pack(int in
, int out
)
41 unsigned char old_sha1
[20];
42 unsigned char new_sha1
[20];
43 static char buffer
[1000];
47 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
50 if (buffer
[len
-1] == '\n')
53 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
54 die("protocol error: expected sha/ref, got '%s'", buffer
);
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
);
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);
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
);
83 * FIXME! Here we need to now send the pack-file to the "out" fd, using something
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.
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
)
107 static const char flags
[256] = {
111 ['.'] = 1, ['/'] = 1,
112 ['-'] = 1, ['+'] = 1,
116 while ((c
= *n
++) != 0) {
118 die("I don't like '%c'. Sue me.", c
);
124 * Yeah, yeah, fixme. Need to pass in the heads etc.
126 static int setup_connection(int fd
[2], char *url
, char **heads
)
129 const char *host
, *path
;
134 url
= shell_safe(url
);
137 colon
= strchr(url
, ':');
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");
148 dup2(pipefd
[1][0], 0);
149 dup2(pipefd
[0][1], 1);
155 execlp("ssh", "ssh", host
, command
, NULL
);
157 execlp("sh", "sh", "-c", command
, NULL
);
160 fd
[0] = pipefd
[0][0];
161 fd
[1] = pipefd
[1][1];
167 int main(int argc
, char **argv
)
176 for (i
= 1; i
< argc
; i
++) {
180 if (!strncmp(arg
, "--exec=", 7)) {
184 usage(send_pack_usage
);
188 nr_heads
= argc
- i
-1;
192 usage(send_pack_usage
);
193 pid
= setup_connection(fd
, dest
, heads
);
196 ret
= send_pack(fd
[0], fd
[1]);
199 waitpid(pid
, NULL
, 0);