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"
17 static unsigned char local_version
= 1;
18 static unsigned char remote_version
= 0;
20 static int verbose
= 0;
22 static int serve_object(int fd_in
, int fd_out
) {
24 unsigned char sha1
[20];
28 size
= read(fd_in
, sha1
+ posn
, 20 - posn
);
30 perror("git-ssh-upload: read ");
39 fprintf(stderr
, "Serving %s\n", sha1_to_hex(sha1
));
43 if (!has_sha1_file(sha1
)) {
44 fprintf(stderr
, "git-ssh-upload: could not find %s\n",
49 write(fd_out
, &remote
, 1);
54 return write_sha1_to_fd(fd_out
, sha1
);
57 static int serve_version(int fd_in
, int fd_out
)
59 if (read(fd_in
, &remote_version
, 1) < 1)
61 write(fd_out
, &local_version
, 1);
65 static int serve_ref(int fd_in
, int fd_out
)
68 unsigned char sha1
[20];
70 signed char remote
= 0;
72 if (read(fd_in
, ref
+ posn
, 1) < 1)
75 } while (ref
[posn
- 1]);
78 fprintf(stderr
, "Serving %s\n", ref
);
80 if (get_ref_sha1(ref
, sha1
))
82 write(fd_out
, &remote
, 1);
85 write(fd_out
, sha1
, 20);
90 static void service(int fd_in
, int fd_out
) {
94 retval
= read(fd_in
, &type
, 1);
97 perror("git-ssh-upload: read ");
100 if (type
== 'v' && serve_version(fd_in
, fd_out
))
102 if (type
== 'o' && serve_object(fd_in
, fd_out
))
104 if (type
== 'r' && serve_ref(fd_in
, fd_out
))
109 static const char ssh_push_usage
[] =
110 MY_PROGRAM_NAME
" [-c] [-t] [-a] [-w ref] commit-id url";
112 int main(int argc
, char **argv
)
119 unsigned char sha1
[20];
122 prog
= getenv(COUNTERPART_ENV_NAME
);
123 if (!prog
) prog
= COUNTERPART_PROGRAM_NAME
;
124 while (arg
< argc
&& argv
[arg
][0] == '-') {
125 if (argv
[arg
][1] == 'w')
130 usage(ssh_push_usage
);
131 commit_id
= argv
[arg
];
133 if (get_sha1(commit_id
, sha1
))
134 usage(ssh_push_usage
);
135 memcpy(hex
, sha1_to_hex(sha1
), sizeof(hex
));
138 if (setup_connection(&fd_in
, &fd_out
, prog
, url
, arg
, argv
+ 1))
141 service(fd_in
, fd_out
);