]> git.ipfire.org Git - thirdparty/git.git/blob - rpush.c
bd381ac9d1787dc979b1eba5bd72c1fd644a094b
[thirdparty/git.git] / rpush.c
1 #include "cache.h"
2 #include "rsh.h"
3 #include <sys/socket.h>
4 #include <errno.h>
5
6 unsigned char local_version = 1;
7 unsigned char remote_version = 0;
8
9 int serve_object(int fd_in, int fd_out) {
10 ssize_t size;
11 int posn = 0;
12 char sha1[20];
13 unsigned long objsize;
14 void *buf;
15 signed char remote;
16 do {
17 size = read(fd_in, sha1 + posn, 20 - posn);
18 if (size < 0) {
19 perror("git-rpush: read ");
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) {
33 fprintf(stderr, "git-rpush: could not find %s\n",
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;
44 do {
45 size = write(fd_out, buf + posn, objsize - posn);
46 if (size <= 0) {
47 if (!size) {
48 fprintf(stderr, "git-rpush: write closed");
49 } else {
50 perror("git-rpush: write ");
51 }
52 return -1;
53 }
54 posn += size;
55 } while (posn < objsize);
56 return 0;
57 }
58
59 int 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 }
66
67 void 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)
74 perror("rpush: read ");
75 return;
76 }
77 if (type == 'v' && serve_version(fd_in, fd_out))
78 return;
79 if (type == 'o' && serve_object(fd_in, fd_out))
80 return;
81 } while (1);
82 }
83
84 int 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) {
94 usage("git-rpush [-c] [-t] [-a] commit-id url");
95 return 1;
96 }
97 commit_id = argv[arg];
98 url = argv[arg + 1];
99 if (setup_connection(&fd_in, &fd_out, "git-rpull", url, arg, argv + 1))
100 return 1;
101
102 service(fd_in, fd_out);
103 return 0;
104 }