[PATCH] Generic support for pulling refs
[git/git.git] / ssh-push.c
CommitLineData
6eb7ed54
DB
1#include "cache.h"
2#include "rsh.h"
3#include <sys/socket.h>
4#include <errno.h>
5
dba385bb
DB
6unsigned char local_version = 1;
7unsigned char remote_version = 0;
8
9int serve_object(int fd_in, int fd_out) {
6eb7ed54 10 ssize_t size;
dba385bb
DB
11 int posn = 0;
12 char sha1[20];
6eb7ed54
DB
13 unsigned long objsize;
14 void *buf;
dba385bb
DB
15 signed char remote;
16 do {
17 size = read(fd_in, sha1 + posn, 20 - posn);
18 if (size < 0) {
418aaf84 19 perror("git-ssh-push: read ");
dba385bb
DB
20 return -1;
21 }
22 if (!size)
23 return -1;
24 posn += size;
25 } while (posn < 20);
26
27 /* fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1)); */
28 remote = 0;
29
30 buf = map_sha1_file(sha1, &objsize);
31
32 if (!buf) {
418aaf84 33 fprintf(stderr, "git-ssh-push: could not find %s\n",
dba385bb
DB
34 sha1_to_hex(sha1));
35 remote = -1;
36 }
37
38 write(fd_out, &remote, 1);
39
40 if (remote < 0)
41 return 0;
42
43 posn = 0;
6eb7ed54 44 do {
dba385bb
DB
45 size = write(fd_out, buf + posn, objsize - posn);
46 if (size <= 0) {
47 if (!size) {
418aaf84 48 fprintf(stderr, "git-ssh-push: write closed");
dba385bb 49 } else {
418aaf84 50 perror("git-ssh-push: write ");
6eb7ed54 51 }
dba385bb
DB
52 return -1;
53 }
54 posn += size;
55 } while (posn < objsize);
56 return 0;
57}
6eb7ed54 58
dba385bb
DB
59int serve_version(int fd_in, int fd_out)
60{
61 if (read(fd_in, &remote_version, 1) < 1)
62 return -1;
63 write(fd_out, &local_version, 1);
64 return 0;
65}
6eb7ed54 66
dba385bb
DB
67void service(int fd_in, int fd_out) {
68 char type;
69 int retval;
70 do {
71 retval = read(fd_in, &type, 1);
72 if (retval < 1) {
73 if (retval < 0)
418aaf84 74 perror("git-ssh-push: read ");
6eb7ed54
DB
75 return;
76 }
dba385bb
DB
77 if (type == 'v' && serve_version(fd_in, fd_out))
78 return;
79 if (type == 'o' && serve_object(fd_in, fd_out))
80 return;
6eb7ed54
DB
81 } while (1);
82}
83
84int main(int argc, char **argv)
85{
86 int arg = 1;
87 char *commit_id;
88 char *url;
89 int fd_in, fd_out;
90 while (arg < argc && argv[arg][0] == '-') {
91 arg++;
92 }
93 if (argc < arg + 2) {
418aaf84 94 usage("git-ssh-push [-c] [-t] [-a] commit-id url");
6eb7ed54
DB
95 return 1;
96 }
97 commit_id = argv[arg];
98 url = argv[arg + 1];
418aaf84 99 if (setup_connection(&fd_in, &fd_out, "git-ssh-pull", url, arg, argv + 1))
6eb7ed54
DB
100 return 1;
101
102 service(fd_in, fd_out);
103 return 0;
104}