Merge part of 'lt/rev-list' into 'fk/blame'
[alt-git.git] / ssh-fetch.c
blob4eb9e04829b5925ea07c764239f91e801e158c78
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 static int prefetches = 0;
41 static struct object_list *in_transit = NULL;
42 static struct object_list **end_of_transit = &in_transit;
44 void prefetch(unsigned char *sha1)
46 char type = 'o';
47 struct object_list *node;
48 if (prefetches > 100) {
49 fetch(in_transit->item->sha1);
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;
56 force_write(fd_out, &type, 1);
57 force_write(fd_out, sha1, 20);
58 prefetches++;
61 static char conn_buf[4096];
62 static size_t conn_buf_posn = 0;
64 int fetch(unsigned char *sha1)
66 int ret;
67 signed char remote;
68 struct object_list *temp;
70 if (memcmp(sha1, in_transit->item->sha1, 20)) {
71 // we must have already fetched it to clean the queue
72 return has_sha1_file(sha1) ? 0 : -1;
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);
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;
88 //fprintf(stderr, "Got %d\n", remote);
89 if (remote < 0)
90 return remote;
91 ret = write_sha1_from_fd(sha1, fd_in, conn_buf, 4096, &conn_buf_posn);
92 if (!ret)
93 pull_say("got %s\n", sha1_to_hex(sha1));
94 return ret;
97 static int get_version(void)
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");
105 return 0;
108 int fetch_ref(char *ref, unsigned char *sha1)
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;
121 static const char ssh_fetch_usage[] =
122 MY_PROGRAM_NAME
123 " [-c] [-t] [-a] [-v] [-d] [--recover] [-w ref] commit-id url";
124 int main(int argc, char **argv)
126 char *commit_id;
127 char *url;
128 int arg = 1;
129 const char *prog;
131 prog = getenv("GIT_SSH_PUSH");
132 if (!prog) prog = "git-ssh-upload";
134 setup_git_directory();
136 while (arg < argc && argv[arg][0] == '-') {
137 if (argv[arg][1] == 't') {
138 get_tree = 1;
139 } else if (argv[arg][1] == 'c') {
140 get_history = 1;
141 } else if (argv[arg][1] == 'a') {
142 get_all = 1;
143 get_tree = 1;
144 get_history = 1;
145 } else if (argv[arg][1] == 'v') {
146 get_verbosely = 1;
147 } else if (argv[arg][1] == 'w') {
148 write_ref = argv[arg + 1];
149 arg++;
150 } else if (!strcmp(argv[arg], "--recover")) {
151 get_recover = 1;
153 arg++;
155 if (argc < arg + 2) {
156 usage(ssh_fetch_usage);
157 return 1;
159 commit_id = argv[arg];
160 url = argv[arg + 1];
162 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
163 return 1;
165 if (get_version())
166 return 1;
168 if (pull(commit_id))
169 return 1;
171 return 0;