ssh-upload.con commit Merge branch 'jc/branch-remove-remote' (55e268e)
   1#ifndef COUNTERPART_ENV_NAME
   2#define COUNTERPART_ENV_NAME "GIT_SSH_FETCH"
   3#endif
   4#ifndef COUNTERPART_PROGRAM_NAME
   5#define COUNTERPART_PROGRAM_NAME "git-ssh-fetch"
   6#endif
   7#ifndef MY_PROGRAM_NAME
   8#define MY_PROGRAM_NAME "git-ssh-upload"
   9#endif
  10
  11#include "cache.h"
  12#include "rsh.h"
  13#include "refs.h"
  14
  15static unsigned char local_version = 1;
  16static unsigned char remote_version;
  17
  18static int verbose;
  19
  20static int serve_object(int fd_in, int fd_out) {
  21        ssize_t size;
  22        unsigned char sha1[20];
  23        signed char remote;
  24        int posn = 0;
  25        do {
  26                size = read(fd_in, sha1 + posn, 20 - posn);
  27                if (size < 0) {
  28                        perror("git-ssh-upload: read ");
  29                        return -1;
  30                }
  31                if (!size)
  32                        return -1;
  33                posn += size;
  34        } while (posn < 20);
  35        
  36        if (verbose)
  37                fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1));
  38
  39        remote = 0;
  40        
  41        if (!has_sha1_file(sha1)) {
  42                fprintf(stderr, "git-ssh-upload: could not find %s\n",
  43                        sha1_to_hex(sha1));
  44                remote = -1;
  45        }
  46        
  47        write(fd_out, &remote, 1);
  48        
  49        if (remote < 0)
  50                return 0;
  51        
  52        return write_sha1_to_fd(fd_out, sha1);
  53}
  54
  55static int serve_version(int fd_in, int fd_out)
  56{
  57        if (read(fd_in, &remote_version, 1) < 1)
  58                return -1;
  59        write(fd_out, &local_version, 1);
  60        return 0;
  61}
  62
  63static int serve_ref(int fd_in, int fd_out)
  64{
  65        char ref[PATH_MAX];
  66        unsigned char sha1[20];
  67        int posn = 0;
  68        signed char remote = 0;
  69        do {
  70                if (read(fd_in, ref + posn, 1) < 1)
  71                        return -1;
  72                posn++;
  73        } while (ref[posn - 1]);
  74
  75        if (verbose)
  76                fprintf(stderr, "Serving %s\n", ref);
  77
  78        if (get_ref_sha1(ref, sha1))
  79                remote = -1;
  80        write(fd_out, &remote, 1);
  81        if (remote)
  82                return 0;
  83        write(fd_out, sha1, 20);
  84        return 0;
  85}
  86
  87
  88static void service(int fd_in, int fd_out) {
  89        char type;
  90        int retval;
  91        do {
  92                retval = read(fd_in, &type, 1);
  93                if (retval < 1) {
  94                        if (retval < 0)
  95                                perror("git-ssh-upload: read ");
  96                        return;
  97                }
  98                if (type == 'v' && serve_version(fd_in, fd_out))
  99                        return;
 100                if (type == 'o' && serve_object(fd_in, fd_out))
 101                        return;
 102                if (type == 'r' && serve_ref(fd_in, fd_out))
 103                        return;
 104        } while (1);
 105}
 106
 107static const char ssh_push_usage[] =
 108        MY_PROGRAM_NAME " [-c] [-t] [-a] [-w ref] commit-id url";
 109
 110int main(int argc, char **argv)
 111{
 112        int arg = 1;
 113        char *commit_id;
 114        char *url;
 115        int fd_in, fd_out;
 116        const char *prog;
 117        unsigned char sha1[20];
 118        char hex[41];
 119
 120        prog = getenv(COUNTERPART_ENV_NAME);
 121        if (!prog) prog = COUNTERPART_PROGRAM_NAME;
 122
 123        setup_git_directory();
 124
 125        while (arg < argc && argv[arg][0] == '-') {
 126                if (argv[arg][1] == 'w')
 127                        arg++;
 128                arg++;
 129        }
 130        if (argc < arg + 2)
 131                usage(ssh_push_usage);
 132        commit_id = argv[arg];
 133        url = argv[arg + 1];
 134        if (get_sha1(commit_id, sha1))
 135                die("Not a valid object name %s", commit_id);
 136        memcpy(hex, sha1_to_hex(sha1), sizeof(hex));
 137        argv[arg] = hex;
 138
 139        if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
 140                return 1;
 141
 142        service(fd_in, fd_out);
 143        return 0;
 144}