Diff: -l<num> to limit rename/copy detection.
[git] / ssh-fetch.c
1 #ifndef COUNTERPART_ENV_NAME
2 #define COUNTERPART_ENV_NAME "GIT_SSH_UPLOAD"
3 #endif
4 #ifndef COUNTERPART_PROGRAM_NAME
5 #define COUNTERPART_PROGRAM_NAME "git-ssh-upload"
6 #endif
7 #ifndef MY_PROGRAM_NAME
8 #define MY_PROGRAM_NAME "git-ssh-fetch"
9 #endif
10
11 #include "cache.h"
12 #include "commit.h"
13 #include "rsh.h"
14 #include "fetch.h"
15 #include "refs.h"
16
17 static int fd_in;
18 static int fd_out;
19
20 static unsigned char remote_version = 0;
21 static unsigned char local_version = 1;
22
23 static ssize_t force_write(int fd, void *buffer, size_t length)
24 {
25         ssize_t ret = 0;
26         while (ret < length) {
27                 ssize_t size = write(fd, buffer + ret, length - ret);
28                 if (size < 0) {
29                         return size;
30                 }
31                 if (size == 0) {
32                         return ret;
33                 }
34                 ret += size;
35         }
36         return ret;
37 }
38
39 void prefetch(unsigned char *sha1)
40 {
41         char type = 'o';
42         force_write(fd_out, &type, 1);
43         force_write(fd_out, sha1, 20);
44         //memcpy(requested + 20 * prefetches++, sha1, 20);
45 }
46
47 static char conn_buf[4096];
48 static size_t conn_buf_posn = 0;
49
50 int fetch(unsigned char *sha1)
51 {
52         int ret;
53         signed char remote;
54
55         if (conn_buf_posn) {
56                 remote = conn_buf[0];
57                 memmove(conn_buf, conn_buf + 1, --conn_buf_posn);
58         } else {
59                 if (read(fd_in, &remote, 1) < 1)
60                         return -1;
61         }
62         //fprintf(stderr, "Got %d\n", remote);
63         if (remote < 0)
64                 return remote;
65         ret = write_sha1_from_fd(sha1, fd_in, conn_buf, 4096, &conn_buf_posn);
66         if (!ret)
67                 pull_say("got %s\n", sha1_to_hex(sha1));
68         return ret;
69 }
70
71 static int get_version(void)
72 {
73         char type = 'v';
74         write(fd_out, &type, 1);
75         write(fd_out, &local_version, 1);
76         if (read(fd_in, &remote_version, 1) < 1) {
77                 return error("Couldn't read version from remote end");
78         }
79         return 0;
80 }
81
82 int fetch_ref(char *ref, unsigned char *sha1)
83 {
84         signed char remote;
85         char type = 'r';
86         write(fd_out, &type, 1);
87         write(fd_out, ref, strlen(ref) + 1);
88         read(fd_in, &remote, 1);
89         if (remote < 0)
90                 return remote;
91         read(fd_in, sha1, 20);
92         return 0;
93 }
94
95 static const char ssh_fetch_usage[] =
96   MY_PROGRAM_NAME
97   " [-c] [-t] [-a] [-v] [-d] [--recover] [-w ref] commit-id url";
98 int main(int argc, char **argv)
99 {
100         char *commit_id;
101         char *url;
102         int arg = 1;
103         const char *prog;
104
105         prog = getenv("GIT_SSH_PUSH");
106         if (!prog) prog = "git-ssh-upload";
107
108         while (arg < argc && argv[arg][0] == '-') {
109                 if (argv[arg][1] == 't') {
110                         get_tree = 1;
111                 } else if (argv[arg][1] == 'c') {
112                         get_history = 1;
113                 } else if (argv[arg][1] == 'a') {
114                         get_all = 1;
115                         get_tree = 1;
116                         get_history = 1;
117                 } else if (argv[arg][1] == 'v') {
118                         get_verbosely = 1;
119                 } else if (argv[arg][1] == 'w') {
120                         write_ref = argv[arg + 1];
121                         arg++;
122                 }
123                 arg++;
124         }
125         if (argc < arg + 2) {
126                 usage(ssh_fetch_usage);
127                 return 1;
128         }
129         commit_id = argv[arg];
130         url = argv[arg + 1];
131
132         if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
133                 return 1;
134
135         if (get_version())
136                 return 1;
137
138         if (pull(commit_id))
139                 return 1;
140
141         return 0;
142 }