ssh-pull.con commit Merge with gitk (379955c)
   1#include "cache.h"
   2#include "commit.h"
   3#include "rsh.h"
   4#include "pull.h"
   5#include "refs.h"
   6
   7static int fd_in;
   8static int fd_out;
   9
  10static unsigned char remote_version = 0;
  11static unsigned char local_version = 1;
  12
  13static ssize_t force_write(int fd, void *buffer, size_t length)
  14{
  15        ssize_t ret = 0;
  16        while (ret < length) {
  17                ssize_t size = write(fd, buffer + ret, length - ret);
  18                if (size < 0) {
  19                        return size;
  20                }
  21                if (size == 0) {
  22                        return ret;
  23                }
  24                ret += size;
  25        }
  26        return ret;
  27}
  28
  29void prefetch(unsigned char *sha1)
  30{
  31        char type = 'o';
  32        force_write(fd_out, &type, 1);
  33        force_write(fd_out, sha1, 20);
  34        //memcpy(requested + 20 * prefetches++, sha1, 20);
  35}
  36
  37static char conn_buf[4096];
  38static size_t conn_buf_posn = 0;
  39
  40int fetch(unsigned char *sha1)
  41{
  42        int ret;
  43        signed char remote;
  44
  45        if (conn_buf_posn) {
  46                remote = conn_buf[0];
  47                memmove(conn_buf, conn_buf + 1, --conn_buf_posn);
  48        } else {
  49                if (read(fd_in, &remote, 1) < 1)
  50                        return -1;
  51        }
  52        //fprintf(stderr, "Got %d\n", remote);
  53        if (remote < 0)
  54                return remote;
  55        ret = write_sha1_from_fd(sha1, fd_in, conn_buf, 4096, &conn_buf_posn);
  56        if (!ret)
  57                pull_say("got %s\n", sha1_to_hex(sha1));
  58        return ret;
  59}
  60
  61static int get_version(void)
  62{
  63        char type = 'v';
  64        write(fd_out, &type, 1);
  65        write(fd_out, &local_version, 1);
  66        if (read(fd_in, &remote_version, 1) < 1) {
  67                return error("Couldn't read version from remote end");
  68        }
  69        return 0;
  70}
  71
  72int fetch_ref(char *ref, unsigned char *sha1)
  73{
  74        signed char remote;
  75        char type = 'r';
  76        write(fd_out, &type, 1);
  77        write(fd_out, ref, strlen(ref) + 1);
  78        read(fd_in, &remote, 1);
  79        if (remote < 0)
  80                return remote;
  81        read(fd_in, sha1, 20);
  82        return 0;
  83}
  84
  85int main(int argc, char **argv)
  86{
  87        char *commit_id;
  88        char *url;
  89        int arg = 1;
  90        const char *prog = getenv("GIT_SSH_PUSH") ? : "git-ssh-push";
  91
  92        while (arg < argc && argv[arg][0] == '-') {
  93                if (argv[arg][1] == 't') {
  94                        get_tree = 1;
  95                } else if (argv[arg][1] == 'c') {
  96                        get_history = 1;
  97                } else if (argv[arg][1] == 'a') {
  98                        get_all = 1;
  99                        get_tree = 1;
 100                        get_history = 1;
 101                } else if (argv[arg][1] == 'v') {
 102                        get_verbosely = 1;
 103                } else if (argv[arg][1] == 'w') {
 104                        write_ref = argv[arg + 1];
 105                        arg++;
 106                }
 107                arg++;
 108        }
 109        if (argc < arg + 2) {
 110                usage("git-ssh-pull [-c] [-t] [-a] [-v] [-d] [--recover] [-w ref] commit-id url");
 111                return 1;
 112        }
 113        commit_id = argv[arg];
 114        url = argv[arg + 1];
 115
 116        if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
 117                return 1;
 118
 119        if (get_version())
 120                return 1;
 121
 122        if (pull(commit_id))
 123                return 1;
 124
 125        return 0;
 126}