Parallelize the build
[alt-git.git] / ssh-fetch.c
blob683a1e4a01f250043324ae49fe61cc33470ceb9c
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
11 #include "cache.h"
12 #include "commit.h"
13 #include "rsh.h"
14 #include "fetch.h"
15 #include "refs.h"
17 static int fd_in;
18 static int fd_out;
20 static unsigned char remote_version = 0;
21 static unsigned char local_version = 1;
23 static ssize_t force_write(int fd, void *buffer, size_t length)
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;
31 if (size == 0) {
32 return ret;
34 ret += size;
36 return ret;
39 void prefetch(unsigned char *sha1)
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);
47 static char conn_buf[4096];
48 static size_t conn_buf_posn = 0;
50 int fetch(unsigned char *sha1)
52 int ret;
53 signed char remote;
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;
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;
71 static int get_version(void)
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");
79 return 0;
82 int fetch_ref(char *ref, unsigned char *sha1)
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;
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)
100 char *commit_id;
101 char *url;
102 int arg = 1;
103 const char *prog;
105 prog = getenv("GIT_SSH_PUSH");
106 if (!prog) prog = "git-ssh-upload";
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++;
123 arg++;
125 if (argc < arg + 2) {
126 usage(ssh_fetch_usage);
127 return 1;
129 commit_id = argv[arg];
130 url = argv[arg + 1];
132 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
133 return 1;
135 if (get_version())
136 return 1;
138 if (pull(commit_id))
139 return 1;
141 return 0;