remove unnecessary initializations
[git/git.git] / ssh-fetch.c
CommitLineData
f71a69ab
JH
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
6eb7ed54
DB
11#include "cache.h"
12#include "commit.h"
6eb7ed54 13#include "rsh.h"
215a7ad1 14#include "fetch.h"
c7c4bbe6 15#include "refs.h"
6eb7ed54
DB
16
17static int fd_in;
18static int fd_out;
19
96f1e58f 20static unsigned char remote_version;
dba385bb
DB
21static unsigned char local_version = 1;
22
635d37af 23static ssize_t force_write(int fd, void *buffer, size_t length)
70b9829e
DB
24{
25 ssize_t ret = 0;
26 while (ret < length) {
1d7f171c 27 ssize_t size = write(fd, (char *) buffer + ret, length - ret);
70b9829e
DB
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
96f1e58f 39static int prefetches;
70a0c6fe 40
96f1e58f 41static struct object_list *in_transit;
70a0c6fe
DB
42static struct object_list **end_of_transit = &in_transit;
43
1e8be59d
DB
44void prefetch(unsigned char *sha1)
45{
70b9829e 46 char type = 'o';
70a0c6fe
DB
47 struct object_list *node;
48 if (prefetches > 100) {
49 fetch(in_transit->item->sha1);
50 }
51 node = xmalloc(sizeof(struct object_list));
52 node->next = NULL;
53 node->item = lookup_unknown_object(sha1);
54 *end_of_transit = node;
55 end_of_transit = &node->next;
70b9829e
DB
56 force_write(fd_out, &type, 1);
57 force_write(fd_out, sha1, 20);
70a0c6fe 58 prefetches++;
1e8be59d
DB
59}
60
70b9829e 61static char conn_buf[4096];
96f1e58f 62static size_t conn_buf_posn;
70b9829e 63
4250a5e5 64int fetch(unsigned char *sha1)
6eb7ed54 65{
e78d9772 66 int ret;
dba385bb 67 signed char remote;
70a0c6fe
DB
68 struct object_list *temp;
69
70 if (memcmp(sha1, in_transit->item->sha1, 20)) {
a9486b02 71 /* we must have already fetched it to clean the queue */
70a0c6fe
DB
72 return has_sha1_file(sha1) ? 0 : -1;
73 }
74 prefetches--;
75 temp = in_transit;
76 in_transit = in_transit->next;
77 if (!in_transit)
78 end_of_transit = &in_transit;
79 free(temp);
70b9829e
DB
80
81 if (conn_buf_posn) {
82 remote = conn_buf[0];
83 memmove(conn_buf, conn_buf + 1, --conn_buf_posn);
84 } else {
85 if (read(fd_in, &remote, 1) < 1)
86 return -1;
87 }
a9486b02 88 /* fprintf(stderr, "Got %d\n", remote); */
dba385bb
DB
89 if (remote < 0)
90 return remote;
70b9829e 91 ret = write_sha1_from_fd(sha1, fd_in, conn_buf, 4096, &conn_buf_posn);
e78d9772
JH
92 if (!ret)
93 pull_say("got %s\n", sha1_to_hex(sha1));
94 return ret;
6eb7ed54
DB
95}
96
6da4016a 97static int get_version(void)
dba385bb
DB
98{
99 char type = 'v';
100 write(fd_out, &type, 1);
101 write(fd_out, &local_version, 1);
102 if (read(fd_in, &remote_version, 1) < 1) {
103 return error("Couldn't read version from remote end");
104 }
105 return 0;
106}
107
cd541a68
DB
108int fetch_ref(char *ref, unsigned char *sha1)
109{
c7c4bbe6
DB
110 signed char remote;
111 char type = 'r';
112 write(fd_out, &type, 1);
113 write(fd_out, ref, strlen(ref) + 1);
114 read(fd_in, &remote, 1);
115 if (remote < 0)
116 return remote;
117 read(fd_in, sha1, 20);
118 return 0;
cd541a68
DB
119}
120
f71a69ab
JH
121static const char ssh_fetch_usage[] =
122 MY_PROGRAM_NAME
cc41cd2e 123 " [-c] [-t] [-a] [-v] [--recover] [-w ref] commit-id url";
6eb7ed54
DB
124int main(int argc, char **argv)
125{
c6b69bdb 126 const char *write_ref = NULL;
6eb7ed54
DB
127 char *commit_id;
128 char *url;
129 int arg = 1;
c7c81b3a
JR
130 const char *prog;
131
132 prog = getenv("GIT_SSH_PUSH");
215a7ad1 133 if (!prog) prog = "git-ssh-upload";
6eb7ed54 134
19c45881 135 setup_ident();
5a327713 136 setup_git_directory();
d0740d92 137 git_config(git_default_config);
5a327713 138
6eb7ed54
DB
139 while (arg < argc && argv[arg][0] == '-') {
140 if (argv[arg][1] == 't') {
4250a5e5 141 get_tree = 1;
6eb7ed54 142 } else if (argv[arg][1] == 'c') {
4250a5e5 143 get_history = 1;
6eb7ed54 144 } else if (argv[arg][1] == 'a') {
4250a5e5
DB
145 get_all = 1;
146 get_tree = 1;
147 get_history = 1;
e78d9772
JH
148 } else if (argv[arg][1] == 'v') {
149 get_verbosely = 1;
c7c4bbe6
DB
150 } else if (argv[arg][1] == 'w') {
151 write_ref = argv[arg + 1];
152 arg++;
820eca68
DB
153 } else if (!strcmp(argv[arg], "--recover")) {
154 get_recover = 1;
6eb7ed54
DB
155 }
156 arg++;
157 }
158 if (argc < arg + 2) {
f71a69ab 159 usage(ssh_fetch_usage);
6eb7ed54
DB
160 return 1;
161 }
162 commit_id = argv[arg];
163 url = argv[arg + 1];
164
001d4a27 165 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
6eb7ed54
DB
166 return 1;
167
dba385bb
DB
168 if (get_version())
169 return 1;
170
4211e4d1 171 if (pull(1, &commit_id, &write_ref, url))
6eb7ed54
DB
172 return 1;
173
174 return 0;
175}