1#include "cache.h"2#include "commit.h"3#include "rsh.h"4#include "pull.h"5#include "refs.h"67static int fd_in;8static int fd_out;910static unsigned char remote_version = 0;11static unsigned char local_version = 1;1213int fetch(unsigned char *sha1)14{15int ret;16signed char remote;17char type = 'o';18if (has_sha1_file(sha1))19return 0;20write(fd_out, &type, 1);21write(fd_out, sha1, 20);22if (read(fd_in, &remote, 1) < 1)23return -1;24if (remote < 0)25return remote;26ret = write_sha1_from_fd(sha1, fd_in);27if (!ret)28pull_say("got %s\n", sha1_to_hex(sha1));29return ret;30}3132int get_version(void)33{34char type = 'v';35write(fd_out, &type, 1);36write(fd_out, &local_version, 1);37if (read(fd_in, &remote_version, 1) < 1) {38return error("Couldn't read version from remote end");39}40return 0;41}4243int fetch_ref(char *ref, unsigned char *sha1)44{45signed char remote;46char type = 'r';47write(fd_out, &type, 1);48write(fd_out, ref, strlen(ref) + 1);49read(fd_in, &remote, 1);50if (remote < 0)51return remote;52read(fd_in, sha1, 20);53return 0;54}5556int main(int argc, char **argv)57{58char *commit_id;59char *url;60int arg = 1;61const char *prog = getenv("GIT_SSH_PUSH") ? : "git-ssh-push";6263while (arg < argc && argv[arg][0] == '-') {64if (argv[arg][1] == 't') {65get_tree = 1;66} else if (argv[arg][1] == 'c') {67get_history = 1;68} else if (argv[arg][1] == 'a') {69get_all = 1;70get_tree = 1;71get_history = 1;72} else if (argv[arg][1] == 'v') {73get_verbosely = 1;74} else if (argv[arg][1] == 'w') {75write_ref = argv[arg + 1];76arg++;77}78arg++;79}80if (argc < arg + 2) {81usage("git-ssh-pull [-c] [-t] [-a] [-v] [-d] [--recover] [-w ref] commit-id url");82return 1;83}84commit_id = argv[arg];85url = argv[arg + 1];8687if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))88return 1;8990if (get_version())91return 1;9293if (pull(commit_id))94return 1;9596return 0;97}