1#include "cache.h"
2#include "commit.h"
3#include "rsh.h"
4#include "pull.h"
5#include "refs.h"
67
static int fd_in;
8static int fd_out;
910
static unsigned char remote_version = 0;
11static unsigned char local_version = 1;
1213
int fetch(unsigned char *sha1)
14{
15int ret;
16signed char remote;
17char type = 'o';
18if (has_sha1_file(sha1))
19return 0;
20write(fd_out, &type, 1);
21write(fd_out, sha1, 20);
22if (read(fd_in, &remote, 1) < 1)
23return -1;
24if (remote < 0)
25return remote;
26ret = write_sha1_from_fd(sha1, fd_in);
27if (!ret)
28pull_say("got %s\n", sha1_to_hex(sha1));
29return ret;
30}
3132
int get_version(void)
33{
34char type = 'v';
35write(fd_out, &type, 1);
36write(fd_out, &local_version, 1);
37if (read(fd_in, &remote_version, 1) < 1) {
38return error("Couldn't read version from remote end");
39}
40return 0;
41}
4243
int fetch_ref(char *ref, unsigned char *sha1)
44{
45signed char remote;
46char type = 'r';
47write(fd_out, &type, 1);
48write(fd_out, ref, strlen(ref) + 1);
49read(fd_in, &remote, 1);
50if (remote < 0)
51return remote;
52read(fd_in, sha1, 20);
53return 0;
54}
5556
int main(int argc, char **argv)
57{
58char *commit_id;
59char *url;
60int arg = 1;
61const char *prog = getenv("GIT_SSH_PUSH") ? : "git-ssh-push";
6263
while (arg < argc && argv[arg][0] == '-') {
64if (argv[arg][1] == 't') {
65get_tree = 1;
66} else if (argv[arg][1] == 'c') {
67get_history = 1;
68} else if (argv[arg][1] == 'd') {
69get_delta = 0;
70} else if (!strcmp(argv[arg], "--recover")) {
71get_delta = 2;
72} else if (argv[arg][1] == 'a') {
73get_all = 1;
74get_tree = 1;
75get_history = 1;
76} else if (argv[arg][1] == 'v') {
77get_verbosely = 1;
78} else if (argv[arg][1] == 'w') {
79write_ref = argv[arg + 1];
80arg++;
81}
82arg++;
83}
84if (argc < arg + 2) {
85usage("git-ssh-pull [-c] [-t] [-a] [-v] [-d] [--recover] [-w ref] commit-id url");
86return 1;
87}
88commit_id = argv[arg];
89url = argv[arg + 1];
9091
if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
92return 1;
9394
if (get_version())
95return 1;
9697
if (pull(commit_id))
98return 1;
99100
return 0;
101}