ssh-fetch.con commit Document git read-tree --trivial (6da0878)
   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
  10
  11#include "cache.h"
  12#include "commit.h"
  13#include "rsh.h"
  14#include "fetch.h"
  15#include "refs.h"
  16
  17static int fd_in;
  18static int fd_out;
  19
  20static unsigned char remote_version;
  21static unsigned char local_version = 1;
  22
  23static int prefetches;
  24
  25static struct object_list *in_transit;
  26static struct object_list **end_of_transit = &in_transit;
  27
  28void prefetch(unsigned char *sha1)
  29{
  30        char type = 'o';
  31        struct object_list *node;
  32        if (prefetches > 100) {
  33                fetch(in_transit->item->sha1);
  34        }
  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++;
  44}
  45
  46static char conn_buf[4096];
  47static size_t conn_buf_posn;
  48
  49int fetch(unsigned char *sha1)
  50{
  51        int ret;
  52        signed char remote;
  53        struct object_list *temp;
  54
  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;
  58        }
  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);
  65
  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;
  72        }
  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;
  80}
  81
  82static int get_version(void)
  83{
  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");
  88        }
  89        if (xread(fd_in, &remote_version, 1) < 1) {
  90                return error("Couldn't read version from remote end");
  91        }
  92        return 0;
  93}
  94
  95int fetch_ref(char *ref, unsigned char *sha1)
  96{
  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;
 103
 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;
 111}
 112
 113static const char ssh_fetch_usage[] =
 114  MY_PROGRAM_NAME
 115  " [-c] [-t] [-a] [-v] [--recover] [-w ref] commit-id url";
 116int main(int argc, char **argv)
 117{
 118        const char *write_ref = NULL;
 119        char *commit_id;
 120        char *url;
 121        int arg = 1;
 122        const char *prog;
 123
 124        prog = getenv("GIT_SSH_PUSH");
 125        if (!prog) prog = "git-ssh-upload";
 126
 127        setup_git_directory();
 128        git_config(git_default_config);
 129
 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;
 146                }
 147                arg++;
 148        }
 149        if (argc < arg + 2) {
 150                usage(ssh_fetch_usage);
 151                return 1;
 152        }
 153        commit_id = argv[arg];
 154        url = argv[arg + 1];
 155
 156        if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
 157                return 1;
 158
 159        if (get_version())
 160                return 1;
 161
 162        if (pull(1, &commit_id, &write_ref, url))
 163                return 1;
 164
 165        return 0;
 166}