blob: 27484126dd899cd9a6d4a14d2f41a60fc3a36a52 [file] [log] [blame]
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -07001#include "cache.h"
2#include "commit.h"
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -07003#include "rsh.h"
Daniel Barkalow4250a5e2005-04-30 16:53:56 -07004#include "pull.h"
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -04005#include "refs.h"
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -07006
7static int fd_in;
8static int fd_out;
9
Daniel Barkalowdba385b2005-06-03 17:43:52 -040010static unsigned char remote_version = 0;
11static unsigned char local_version = 1;
12
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070013int fetch(unsigned char *sha1)
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070014{
Junio C Hamanoe78d9772005-05-06 01:37:21 -070015 int ret;
Daniel Barkalowdba385b2005-06-03 17:43:52 -040016 signed char remote;
17 char type = 'o';
18 if (has_sha1_file(sha1))
19 return 0;
20 write(fd_out, &type, 1);
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070021 write(fd_out, sha1, 20);
Daniel Barkalowdba385b2005-06-03 17:43:52 -040022 if (read(fd_in, &remote, 1) < 1)
23 return -1;
24 if (remote < 0)
25 return remote;
Junio C Hamanoe78d9772005-05-06 01:37:21 -070026 ret = write_sha1_from_fd(sha1, fd_in);
27 if (!ret)
28 pull_say("got %s\n", sha1_to_hex(sha1));
29 return ret;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070030}
31
Daniel Barkalowdba385b2005-06-03 17:43:52 -040032int get_version(void)
33{
34 char type = 'v';
35 write(fd_out, &type, 1);
36 write(fd_out, &local_version, 1);
37 if (read(fd_in, &remote_version, 1) < 1) {
38 return error("Couldn't read version from remote end");
39 }
40 return 0;
41}
42
Daniel Barkalowcd541a62005-06-06 16:38:26 -040043int fetch_ref(char *ref, unsigned char *sha1)
44{
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040045 signed char remote;
46 char type = 'r';
47 write(fd_out, &type, 1);
48 write(fd_out, ref, strlen(ref) + 1);
49 read(fd_in, &remote, 1);
50 if (remote < 0)
51 return remote;
52 read(fd_in, sha1, 20);
53 return 0;
Daniel Barkalowcd541a62005-06-06 16:38:26 -040054}
55
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070056int main(int argc, char **argv)
57{
58 char *commit_id;
59 char *url;
60 int arg = 1;
Linus Torvalds001d4a22005-06-07 14:23:46 -070061 const char *prog = getenv("GIT_SSH_PUSH") ? : "git-ssh-push";
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070062
63 while (arg < argc && argv[arg][0] == '-') {
64 if (argv[arg][1] == 't') {
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070065 get_tree = 1;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070066 } else if (argv[arg][1] == 'c') {
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070067 get_history = 1;
Junio C Hamano4a62b612005-06-02 15:19:00 -070068 } else if (argv[arg][1] == 'd') {
69 get_delta = 0;
Junio C Hamanoa48e1d62005-06-04 23:11:38 -070070 } else if (!strcmp(argv[arg], "--recover")) {
71 get_delta = 2;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070072 } else if (argv[arg][1] == 'a') {
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070073 get_all = 1;
74 get_tree = 1;
75 get_history = 1;
Junio C Hamanoe78d9772005-05-06 01:37:21 -070076 } else if (argv[arg][1] == 'v') {
77 get_verbosely = 1;
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040078 } else if (argv[arg][1] == 'w') {
79 write_ref = argv[arg + 1];
80 arg++;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070081 }
82 arg++;
83 }
84 if (argc < arg + 2) {
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040085 usage("git-ssh-pull [-c] [-t] [-a] [-v] [-d] [--recover] [-w ref] commit-id url");
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070086 return 1;
87 }
88 commit_id = argv[arg];
89 url = argv[arg + 1];
90
Linus Torvalds001d4a22005-06-07 14:23:46 -070091 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070092 return 1;
93
Daniel Barkalowdba385b2005-06-03 17:43:52 -040094 if (get_version())
95 return 1;
96
Daniel Barkalow4250a5e2005-04-30 16:53:56 -070097 if (pull(commit_id))
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070098 return 1;
99
100 return 0;
101}