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) {
14 unsigned char sha1[20];
18 size = read(fd_in, sha1 + posn, 20 - posn);
20 perror("git-ssh-push: read ");
29 fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1));
33 if (!has_sha1_file(sha1)) {
34 fprintf(stderr, "git-ssh-push: could not find %s\n",
39 write(fd_out, &remote, 1);
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)
51 write(fd_out, &local_version, 1);
55 static int serve_ref(int fd_in, int fd_out)
58 unsigned char sha1[20];
60 signed char remote = 0;
62 if (read(fd_in, ref + posn, 1) < 1)
65 } while (ref[posn - 1]);
68 fprintf(stderr, "Serving %s\n", ref);
70 if (get_ref_sha1(ref, sha1))
72 write(fd_out, &remote, 1);
75 write(fd_out, sha1, 20);
80 static void service(int fd_in, int fd_out) {
84 retval = read(fd_in, &type, 1);
87 perror("git-ssh-push: read ");
90 if (type == 'v' && serve_version(fd_in, fd_out))
92 if (type == 'o' && serve_object(fd_in, fd_out))
94 if (type == 'r' && serve_ref(fd_in, fd_out))
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)
109 unsigned char sha1[20];
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')
120 usage(ssh_push_usage);
121 commit_id = argv[arg];
123 if (get_sha1(commit_id, sha1))
124 usage(ssh_push_usage);
125 memcpy(hex, sha1_to_hex(sha1), sizeof(hex));
128 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
131 service(fd_in, fd_out);