7 unsigned char local_version = 1;
8 unsigned char remote_version = 0;
10 int serve_object(int fd_in, int fd_out) {
13 unsigned char sha1[20];
14 unsigned long objsize;
18 size = read(fd_in, sha1 + posn, 20 - posn);
20 perror("git-ssh-push: read ");
28 /* fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1)); */
31 buf = map_sha1_file(sha1, &objsize);
34 fprintf(stderr, "git-ssh-push: could not find %s\n",
39 write(fd_out, &remote, 1);
46 size = write(fd_out, buf + posn, objsize - posn);
49 fprintf(stderr, "git-ssh-push: write closed");
51 perror("git-ssh-push: write ");
56 } while (posn < objsize);
60 int serve_version(int fd_in, int fd_out)
62 if (read(fd_in, &remote_version, 1) < 1)
64 write(fd_out, &local_version, 1);
68 int serve_ref(int fd_in, int fd_out)
71 unsigned char sha1[20];
73 signed char remote = 0;
75 if (read(fd_in, ref + posn, 1) < 1)
78 } while (ref[posn - 1]);
79 if (get_ref_sha1(ref, sha1))
81 write(fd_out, &remote, 1);
84 write(fd_out, sha1, 20);
89 void service(int fd_in, int fd_out) {
93 retval = read(fd_in, &type, 1);
96 perror("git-ssh-push: read ");
99 if (type == 'v' && serve_version(fd_in, fd_out))
101 if (type == 'o' && serve_object(fd_in, fd_out))
103 if (type == 'r' && serve_ref(fd_in, fd_out))
108 static const char *ssh_push_usage =
109 "git-ssh-push [-c] [-t] [-a] [-w ref] commit-id url";
111 int main(int argc, char **argv)
117 const char *prog = getenv("GIT_SSH_PULL") ? : "git-ssh-pull";
118 unsigned char sha1[20];
121 while (arg < argc && argv[arg][0] == '-') {
122 if (argv[arg][1] == 'w')
127 usage(ssh_push_usage);
128 commit_id = argv[arg];
130 if (get_sha1(commit_id, sha1))
131 usage(ssh_push_usage);
132 memcpy(hex, sha1_to_hex(sha1), sizeof(hex));
135 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
138 service(fd_in, fd_out);