1 #ifndef COUNTERPART_ENV_NAME
2 #define COUNTERPART_ENV_NAME "GIT_SSH_FETCH"
4 #ifndef COUNTERPART_PROGRAM_NAME
5 #define COUNTERPART_PROGRAM_NAME "git-ssh-fetch"
7 #ifndef MY_PROGRAM_NAME
8 #define MY_PROGRAM_NAME "git-ssh-upload"
15 static unsigned char local_version = 1;
16 static unsigned char remote_version;
20 static int serve_object(int fd_in, int fd_out) {
22 unsigned char sha1[20];
25 size = read_in_full(fd_in, sha1, 20);
27 perror("git-ssh-upload: read ");
34 fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1));
38 if (!has_sha1_file(sha1)) {
39 fprintf(stderr, "git-ssh-upload: could not find %s\n",
44 write(fd_out, &remote, 1);
49 return write_sha1_to_fd(fd_out, sha1);
52 static int serve_version(int fd_in, int fd_out)
54 if (xread(fd_in, &remote_version, 1) < 1)
56 write(fd_out, &local_version, 1);
60 static int serve_ref(int fd_in, int fd_out)
63 unsigned char sha1[20];
65 signed char remote = 0;
67 if (posn >= PATH_MAX || xread(fd_in, ref + posn, 1) < 1)
70 } while (ref[posn - 1]);
73 fprintf(stderr, "Serving %s\n", ref);
75 if (get_ref_sha1(ref, sha1))
77 write(fd_out, &remote, 1);
80 write(fd_out, sha1, 20);
85 static void service(int fd_in, int fd_out) {
89 retval = xread(fd_in, &type, 1);
92 perror("git-ssh-upload: read ");
95 if (type == 'v' && serve_version(fd_in, fd_out))
97 if (type == 'o' && serve_object(fd_in, fd_out))
99 if (type == 'r' && serve_ref(fd_in, fd_out))
104 static const char ssh_push_usage[] =
105 MY_PROGRAM_NAME " [-c] [-t] [-a] [-w ref] commit-id url";
107 int main(int argc, char **argv)
114 unsigned char sha1[20];
117 prog = getenv(COUNTERPART_ENV_NAME);
118 if (!prog) prog = COUNTERPART_PROGRAM_NAME;
120 setup_git_directory();
122 while (arg < argc && argv[arg][0] == '-') {
123 if (argv[arg][1] == 'w')
128 usage(ssh_push_usage);
129 commit_id = argv[arg];
131 if (get_sha1(commit_id, sha1))
132 die("Not a valid object name %s", commit_id);
133 memcpy(hex, sha1_to_hex(sha1), sizeof(hex));
136 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
139 service(fd_in, fd_out);