]> git.ipfire.org Git - thirdparty/git.git/blame - ssh-push.c
Rename the "tools" programs to also have the "git-" prefix
[thirdparty/git.git] / ssh-push.c
CommitLineData
6eb7ed54
DB
1#include "cache.h"
2#include "rsh.h"
c7c4bbe6 3#include "refs.h"
6eb7ed54 4
ee85cbc6
SV
5#include <string.h>
6
6da4016a
LT
7static unsigned char local_version = 1;
8static unsigned char remote_version = 0;
dba385bb 9
a5eda52b
DB
10static int verbose = 0;
11
6da4016a 12static int serve_object(int fd_in, int fd_out) {
6eb7ed54 13 ssize_t size;
d565b341 14 unsigned char sha1[20];
dba385bb 15 signed char remote;
a5eda52b 16 int posn = 0;
dba385bb
DB
17 do {
18 size = read(fd_in, sha1 + posn, 20 - posn);
19 if (size < 0) {
418aaf84 20 perror("git-ssh-push: read ");
dba385bb
DB
21 return -1;
22 }
23 if (!size)
24 return -1;
25 posn += size;
26 } while (posn < 20);
27
a5eda52b
DB
28 if (verbose)
29 fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1));
30
dba385bb
DB
31 remote = 0;
32
a5eda52b 33 if (!has_sha1_file(sha1)) {
418aaf84 34 fprintf(stderr, "git-ssh-push: could not find %s\n",
dba385bb
DB
35 sha1_to_hex(sha1));
36 remote = -1;
37 }
38
39 write(fd_out, &remote, 1);
40
41 if (remote < 0)
42 return 0;
43
a5eda52b 44 return write_sha1_to_fd(fd_out, sha1);
dba385bb 45}
6eb7ed54 46
6da4016a 47static int serve_version(int fd_in, int fd_out)
dba385bb
DB
48{
49 if (read(fd_in, &remote_version, 1) < 1)
50 return -1;
51 write(fd_out, &local_version, 1);
52 return 0;
53}
6eb7ed54 54
6da4016a 55static int serve_ref(int fd_in, int fd_out)
c7c4bbe6
DB
56{
57 char ref[PATH_MAX];
58 unsigned char sha1[20];
59 int posn = 0;
60 signed char remote = 0;
61 do {
62 if (read(fd_in, ref + posn, 1) < 1)
63 return -1;
64 posn++;
65 } while (ref[posn - 1]);
a5eda52b
DB
66
67 if (verbose)
68 fprintf(stderr, "Serving %s\n", ref);
69
c7c4bbe6
DB
70 if (get_ref_sha1(ref, sha1))
71 remote = -1;
72 write(fd_out, &remote, 1);
73 if (remote)
74 return 0;
75 write(fd_out, sha1, 20);
76 return 0;
77}
78
79
6da4016a 80static void service(int fd_in, int fd_out) {
dba385bb
DB
81 char type;
82 int retval;
83 do {
84 retval = read(fd_in, &type, 1);
85 if (retval < 1) {
86 if (retval < 0)
418aaf84 87 perror("git-ssh-push: read ");
6eb7ed54
DB
88 return;
89 }
dba385bb
DB
90 if (type == 'v' && serve_version(fd_in, fd_out))
91 return;
92 if (type == 'o' && serve_object(fd_in, fd_out))
93 return;
c7c4bbe6
DB
94 if (type == 'r' && serve_ref(fd_in, fd_out))
95 return;
6eb7ed54
DB
96 } while (1);
97}
98
ee85cbc6
SV
99static const char *ssh_push_usage =
100 "git-ssh-push [-c] [-t] [-a] [-w ref] commit-id url";
101
6eb7ed54
DB
102int main(int argc, char **argv)
103{
104 int arg = 1;
105 char *commit_id;
106 char *url;
107 int fd_in, fd_out;
001d4a27 108 const char *prog = getenv("GIT_SSH_PULL") ? : "git-ssh-pull";
ee85cbc6
SV
109 unsigned char sha1[20];
110 char hex[41];
001d4a27 111
6eb7ed54 112 while (arg < argc && argv[arg][0] == '-') {
c7c4bbe6
DB
113 if (argv[arg][1] == 'w')
114 arg++;
6eb7ed54
DB
115 arg++;
116 }
ee85cbc6
SV
117 if (argc < arg + 2)
118 usage(ssh_push_usage);
6eb7ed54
DB
119 commit_id = argv[arg];
120 url = argv[arg + 1];
ee85cbc6
SV
121 if (get_sha1(commit_id, sha1))
122 usage(ssh_push_usage);
123 memcpy(hex, sha1_to_hex(sha1), sizeof(hex));
124 argv[arg] = hex;
125
001d4a27 126 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
6eb7ed54
DB
127 return 1;
128
129 service(fd_in, fd_out);
130 return 0;
131}