[PATCH] Fix bisection terminating condition
[git/dscho.git] / ssh-push.c
blob5bd4569974c3c6851feb006a838fe69769388673
1 #include "cache.h"
2 #include "rsh.h"
3 #include "refs.h"
5 #include <string.h>
7 static unsigned char local_version = 1;
8 static unsigned char remote_version = 0;
10 static int verbose = 0;
12 static int serve_object(int fd_in, int fd_out) {
13 ssize_t size;
14 unsigned char sha1[20];
15 signed char remote;
16 int posn = 0;
17 do {
18 size = read(fd_in, sha1 + posn, 20 - posn);
19 if (size < 0) {
20 perror("git-ssh-push: read ");
21 return -1;
23 if (!size)
24 return -1;
25 posn += size;
26 } while (posn < 20);
28 if (verbose)
29 fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1));
31 remote = 0;
33 if (!has_sha1_file(sha1)) {
34 fprintf(stderr, "git-ssh-push: could not find %s\n",
35 sha1_to_hex(sha1));
36 remote = -1;
39 write(fd_out, &remote, 1);
41 if (remote < 0)
42 return 0;
44 return write_sha1_to_fd(fd_out, sha1);
47 static int serve_version(int fd_in, int fd_out)
49 if (read(fd_in, &remote_version, 1) < 1)
50 return -1;
51 write(fd_out, &local_version, 1);
52 return 0;
55 static int serve_ref(int fd_in, int fd_out)
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]);
67 if (verbose)
68 fprintf(stderr, "Serving %s\n", ref);
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;
80 static void service(int fd_in, int fd_out) {
81 char type;
82 int retval;
83 do {
84 retval = read(fd_in, &type, 1);
85 if (retval < 1) {
86 if (retval < 0)
87 perror("git-ssh-push: read ");
88 return;
90 if (type == 'v' && serve_version(fd_in, fd_out))
91 return;
92 if (type == 'o' && serve_object(fd_in, fd_out))
93 return;
94 if (type == 'r' && serve_ref(fd_in, fd_out))
95 return;
96 } while (1);
99 static const char ssh_push_usage[] =
100 "git-ssh-push [-c] [-t] [-a] [-w ref] commit-id url";
102 int main(int argc, char **argv)
104 int arg = 1;
105 char *commit_id;
106 char *url;
107 int fd_in, fd_out;
108 const char *prog;
109 unsigned char sha1[20];
110 char hex[41];
112 prog = getenv("GIT_SSH_PULL");
113 if (!prog) prog = "git-ssh-pull";
114 while (arg < argc && argv[arg][0] == '-') {
115 if (argv[arg][1] == 'w')
116 arg++;
117 arg++;
119 if (argc < arg + 2)
120 usage(ssh_push_usage);
121 commit_id = argv[arg];
122 url = argv[arg + 1];
123 if (get_sha1(commit_id, sha1))
124 usage(ssh_push_usage);
125 memcpy(hex, sha1_to_hex(sha1), sizeof(hex));
126 argv[arg] = hex;
128 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
129 return 1;
131 service(fd_in, fd_out);
132 return 0;