Do a cross-project merge of Paul Mackerras' gitk visualizer
[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 int fetch(unsigned char *sha1)
14 {
15         int ret;
16         signed char remote;
17         char type = 'o';
18         if (has_sha1_file(sha1))
19                 return 0;
20         write(fd_out, &type, 1);
21         write(fd_out, sha1, 20);
22         if (read(fd_in, &remote, 1) < 1)
23                 return -1;
24         if (remote < 0)
25                 return remote;
26         ret = write_sha1_from_fd(sha1, fd_in);
27         if (!ret)
28                 pull_say("got %s\n", sha1_to_hex(sha1));
29         return ret;
30 }
31
32 int get_version(void)
33 {
34         char type = 'v';
35         write(fd_out, &type, 1);
36         write(fd_out, &local_version, 1);
37         if (read(fd_in, &remote_version, 1) < 1) {
38                 return error("Couldn't read version from remote end");
39         }
40         return 0;
41 }
42
43 int fetch_ref(char *ref, unsigned char *sha1)
44 {
45         signed char remote;
46         char type = 'r';
47         write(fd_out, &type, 1);
48         write(fd_out, ref, strlen(ref) + 1);
49         read(fd_in, &remote, 1);
50         if (remote < 0)
51                 return remote;
52         read(fd_in, sha1, 20);
53         return 0;
54 }
55
56 int main(int argc, char **argv)
57 {
58         char *commit_id;
59         char *url;
60         int arg = 1;
61         const char *prog = getenv("GIT_SSH_PUSH") ? : "git-ssh-push";
62
63         while (arg < argc && argv[arg][0] == '-') {
64                 if (argv[arg][1] == 't') {
65                         get_tree = 1;
66                 } else if (argv[arg][1] == 'c') {
67                         get_history = 1;
68                 } else if (argv[arg][1] == 'd') {
69                         get_delta = 0;
70                 } else if (!strcmp(argv[arg], "--recover")) {
71                         get_delta = 2;
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 }