1 #ifndef COUNTERPART_ENV_NAME
2 #define COUNTERPART_ENV_NAME "GIT_SSH_UPLOAD"
4 #ifndef COUNTERPART_PROGRAM_NAME
5 #define COUNTERPART_PROGRAM_NAME "git-ssh-upload"
7 #ifndef MY_PROGRAM_NAME
8 #define MY_PROGRAM_NAME "git-ssh-fetch"
20 static unsigned char remote_version
;
21 static unsigned char local_version
= 1;
23 static int prefetches
;
25 static struct object_list
*in_transit
;
26 static struct object_list
**end_of_transit
= &in_transit
;
28 void prefetch(unsigned char *sha1
)
31 struct object_list
*node
;
32 if (prefetches
> 100) {
33 fetch(in_transit
->item
->sha1
);
35 node
= xmalloc(sizeof(struct object_list
));
37 node
->item
= lookup_unknown_object(sha1
);
38 *end_of_transit
= node
;
39 end_of_transit
= &node
->next
;
40 /* XXX: what if these writes fail? */
41 write_in_full(fd_out
, &type
, 1);
42 write_in_full(fd_out
, sha1
, 20);
46 static char conn_buf
[4096];
47 static size_t conn_buf_posn
;
49 int fetch(unsigned char *sha1
)
53 struct object_list
*temp
;
55 if (hashcmp(sha1
, in_transit
->item
->sha1
)) {
56 /* we must have already fetched it to clean the queue */
57 return has_sha1_file(sha1
) ? 0 : -1;
61 in_transit
= in_transit
->next
;
63 end_of_transit
= &in_transit
;
68 memmove(conn_buf
, conn_buf
+ 1, --conn_buf_posn
);
70 if (xread(fd_in
, &remote
, 1) < 1)
73 /* fprintf(stderr, "Got %d\n", remote); */
76 ret
= write_sha1_from_fd(sha1
, fd_in
, conn_buf
, 4096, &conn_buf_posn
);
78 pull_say("got %s\n", sha1_to_hex(sha1
));
82 static int get_version(void)
85 if (write_in_full(fd_out
, &type
, 1) != 1 ||
86 write_in_full(fd_out
, &local_version
, 1)) {
87 return error("Couldn't request version from remote end");
89 if (xread(fd_in
, &remote_version
, 1) < 1) {
90 return error("Couldn't read version from remote end");
95 int fetch_ref(char *ref
, unsigned char *sha1
)
99 int length
= strlen(ref
) + 1;
100 if (write_in_full(fd_out
, &type
, 1) != 1 ||
101 write_in_full(fd_out
, ref
, length
) != length
)
104 if (read_in_full(fd_in
, &remote
, 1) != 1)
108 if (read_in_full(fd_in
, sha1
, 20) != 20)
113 static const char ssh_fetch_usage
[] =
115 " [-c] [-t] [-a] [-v] [--recover] [-w ref] commit-id url";
116 int main(int argc
, char **argv
)
118 const char *write_ref
= NULL
;
124 prog
= getenv("GIT_SSH_PUSH");
125 if (!prog
) prog
= "git-ssh-upload";
127 setup_git_directory();
128 git_config(git_default_config
);
130 while (arg
< argc
&& argv
[arg
][0] == '-') {
131 if (argv
[arg
][1] == 't') {
133 } else if (argv
[arg
][1] == 'c') {
135 } else if (argv
[arg
][1] == 'a') {
139 } else if (argv
[arg
][1] == 'v') {
141 } else if (argv
[arg
][1] == 'w') {
142 write_ref
= argv
[arg
+ 1];
144 } else if (!strcmp(argv
[arg
], "--recover")) {
149 if (argc
< arg
+ 2) {
150 usage(ssh_fetch_usage
);
153 commit_id
= argv
[arg
];
156 if (setup_connection(&fd_in
, &fd_out
, prog
, url
, arg
, argv
+ 1))
162 if (pull(1, &commit_id
, &write_ref
, url
))