blob: 4eb9e04829b5925ea07c764239f91e801e158c78 [file] [log] [blame]
Junio C Hamanof71a69a2005-09-15 14:56:37 -07001#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
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070011#include "cache.h"
12#include "commit.h"
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070013#include "rsh.h"
Junio C Hamano215a7ad2005-09-07 17:26:23 -070014#include "fetch.h"
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040015#include "refs.h"
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070016
17static int fd_in;
18static int fd_out;
19
Daniel Barkalowdba385b2005-06-03 17:43:52 -040020static unsigned char remote_version = 0;
21static unsigned char local_version = 1;
22
Alecs King635d37a2005-08-04 11:35:37 +080023static ssize_t force_write(int fd, void *buffer, size_t length)
barkalow@iabervon.org70b98292005-08-02 19:46:29 -040024{
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
Daniel Barkalow70a0c6f2005-10-04 00:24:55 -040039static int prefetches = 0;
40
41static struct object_list *in_transit = NULL;
42static struct object_list **end_of_transit = &in_transit;
43
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -040044void prefetch(unsigned char *sha1)
45{
barkalow@iabervon.org70b98292005-08-02 19:46:29 -040046 char type = 'o';
Daniel Barkalow70a0c6f2005-10-04 00:24:55 -040047 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;
barkalow@iabervon.org70b98292005-08-02 19:46:29 -040056 force_write(fd_out, &type, 1);
57 force_write(fd_out, sha1, 20);
Daniel Barkalow70a0c6f2005-10-04 00:24:55 -040058 prefetches++;
barkalow@iabervon.org1e8be592005-08-02 19:46:10 -040059}
60
barkalow@iabervon.org70b98292005-08-02 19:46:29 -040061static char conn_buf[4096];
62static size_t conn_buf_posn = 0;
63
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070064int fetch(unsigned char *sha1)
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070065{
Junio C Hamanoe78d9772005-05-06 01:37:21 -070066 int ret;
Daniel Barkalowdba385b2005-06-03 17:43:52 -040067 signed char remote;
Daniel Barkalow70a0c6f2005-10-04 00:24:55 -040068 struct object_list *temp;
69
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;
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);
barkalow@iabervon.org70b98292005-08-02 19:46:29 -040080
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 }
88 //fprintf(stderr, "Got %d\n", remote);
Daniel Barkalowdba385b2005-06-03 17:43:52 -040089 if (remote < 0)
90 return remote;
barkalow@iabervon.org70b98292005-08-02 19:46:29 -040091 ret = write_sha1_from_fd(sha1, fd_in, conn_buf, 4096, &conn_buf_posn);
Junio C Hamanoe78d9772005-05-06 01:37:21 -070092 if (!ret)
93 pull_say("got %s\n", sha1_to_hex(sha1));
94 return ret;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070095}
96
Linus Torvalds6da40162005-07-03 10:10:45 -070097static int get_version(void)
Daniel Barkalowdba385b2005-06-03 17:43:52 -040098{
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
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400108int fetch_ref(char *ref, unsigned char *sha1)
109{
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -0400110 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;
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400119}
120
Junio C Hamanof71a69a2005-09-15 14:56:37 -0700121static const char ssh_fetch_usage[] =
122 MY_PROGRAM_NAME
123 " [-c] [-t] [-a] [-v] [-d] [--recover] [-w ref] commit-id url";
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700124int main(int argc, char **argv)
125{
126 char *commit_id;
127 char *url;
128 int arg = 1;
Jason Riedyc7c81b32005-08-23 13:34:07 -0700129 const char *prog;
130
131 prog = getenv("GIT_SSH_PUSH");
Junio C Hamano215a7ad2005-09-07 17:26:23 -0700132 if (!prog) prog = "git-ssh-upload";
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700133
Junio C Hamano5a327712005-11-26 00:47:59 -0800134 setup_git_directory();
135
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700136 while (arg < argc && argv[arg][0] == '-') {
137 if (argv[arg][1] == 't') {
Daniel Barkalow4250a5e2005-04-30 16:53:56 -0700138 get_tree = 1;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700139 } else if (argv[arg][1] == 'c') {
Daniel Barkalow4250a5e2005-04-30 16:53:56 -0700140 get_history = 1;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700141 } else if (argv[arg][1] == 'a') {
Daniel Barkalow4250a5e2005-04-30 16:53:56 -0700142 get_all = 1;
143 get_tree = 1;
144 get_history = 1;
Junio C Hamanoe78d9772005-05-06 01:37:21 -0700145 } else if (argv[arg][1] == 'v') {
146 get_verbosely = 1;
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -0400147 } else if (argv[arg][1] == 'w') {
148 write_ref = argv[arg + 1];
149 arg++;
Daniel Barkalow820eca62005-09-26 21:38:08 -0400150 } else if (!strcmp(argv[arg], "--recover")) {
151 get_recover = 1;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700152 }
153 arg++;
154 }
155 if (argc < arg + 2) {
Junio C Hamanof71a69a2005-09-15 14:56:37 -0700156 usage(ssh_fetch_usage);
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700157 return 1;
158 }
159 commit_id = argv[arg];
160 url = argv[arg + 1];
161
Linus Torvalds001d4a22005-06-07 14:23:46 -0700162 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700163 return 1;
164
Daniel Barkalowdba385b2005-06-03 17:43:52 -0400165 if (get_version())
166 return 1;
167
Daniel Barkalow4250a5e2005-04-30 16:53:56 -0700168 if (pull(commit_id))
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700169 return 1;
170
171 return 0;
172}