git-svn: fail on rebase if we are unable to find a ref to rebase against
[git/dscho.git] / ssh-fetch.c
blobbdf51a7a1437efd49fb5c642ab9294c7f43698de
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;
21 static unsigned char local_version = 1;
23 static int prefetches;
25 static struct object_list *in_transit;
26 static struct object_list **end_of_transit = &in_transit;
28 void prefetch(unsigned char *sha1)
30 char type = 'o';
31 struct object_list *node;
32 if (prefetches > 100) {
33 fetch(in_transit->item->sha1);
35 node = xmalloc(sizeof(struct object_list));
36 node->next = NULL;
37 node->item = lookup_unknown_object(sha1);
38 *end_of_transit = node;
39 end_of_transit = &node->next;
40 /* XXX: what if these writes fail? */
41 write_in_full(fd_out, &type, 1);
42 write_in_full(fd_out, sha1, 20);
43 prefetches++;
46 static char conn_buf[4096];
47 static size_t conn_buf_posn;
49 int fetch(unsigned char *sha1)
51 int ret;
52 signed char remote;
53 struct object_list *temp;
55 if (hashcmp(sha1, in_transit->item->sha1)) {
56 /* we must have already fetched it to clean the queue */
57 return has_sha1_file(sha1) ? 0 : -1;
59 prefetches--;
60 temp = in_transit;
61 in_transit = in_transit->next;
62 if (!in_transit)
63 end_of_transit = &in_transit;
64 free(temp);
66 if (conn_buf_posn) {
67 remote = conn_buf[0];
68 memmove(conn_buf, conn_buf + 1, --conn_buf_posn);
69 } else {
70 if (xread(fd_in, &remote, 1) < 1)
71 return -1;
73 /* fprintf(stderr, "Got %d\n", remote); */
74 if (remote < 0)
75 return remote;
76 ret = write_sha1_from_fd(sha1, fd_in, conn_buf, 4096, &conn_buf_posn);
77 if (!ret)
78 pull_say("got %s\n", sha1_to_hex(sha1));
79 return ret;
82 static int get_version(void)
84 char type = 'v';
85 if (write_in_full(fd_out, &type, 1) != 1 ||
86 write_in_full(fd_out, &local_version, 1)) {
87 return error("Couldn't request version from remote end");
89 if (xread(fd_in, &remote_version, 1) < 1) {
90 return error("Couldn't read version from remote end");
92 return 0;
95 int fetch_ref(char *ref, unsigned char *sha1)
97 signed char remote;
98 char type = 'r';
99 int length = strlen(ref) + 1;
100 if (write_in_full(fd_out, &type, 1) != 1 ||
101 write_in_full(fd_out, ref, length) != length)
102 return -1;
104 if (read_in_full(fd_in, &remote, 1) != 1)
105 return -1;
106 if (remote < 0)
107 return remote;
108 if (read_in_full(fd_in, sha1, 20) != 20)
109 return -1;
110 return 0;
113 static const char ssh_fetch_usage[] =
114 MY_PROGRAM_NAME
115 " [-c] [-t] [-a] [-v] [--recover] [-w ref] commit-id url";
116 int main(int argc, char **argv)
118 const char *write_ref = NULL;
119 char *commit_id;
120 char *url;
121 int arg = 1;
122 const char *prog;
124 prog = getenv("GIT_SSH_PUSH");
125 if (!prog) prog = "git-ssh-upload";
127 setup_git_directory();
128 git_config(git_default_config);
130 while (arg < argc && argv[arg][0] == '-') {
131 if (argv[arg][1] == 't') {
132 get_tree = 1;
133 } else if (argv[arg][1] == 'c') {
134 get_history = 1;
135 } else if (argv[arg][1] == 'a') {
136 get_all = 1;
137 get_tree = 1;
138 get_history = 1;
139 } else if (argv[arg][1] == 'v') {
140 get_verbosely = 1;
141 } else if (argv[arg][1] == 'w') {
142 write_ref = argv[arg + 1];
143 arg++;
144 } else if (!strcmp(argv[arg], "--recover")) {
145 get_recover = 1;
147 arg++;
149 if (argc < arg + 2) {
150 usage(ssh_fetch_usage);
151 return 1;
153 commit_id = argv[arg];
154 url = argv[arg + 1];
156 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
157 return 1;
159 if (get_version())
160 return 1;
162 if (pull(1, &commit_id, &write_ref, url))
163 return 1;
165 return 0;