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