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 ssize_t
force_write(int fd
, void *buffer
, size_t length
)
26 while (ret
< length
) {
27 ssize_t size
= write(fd
, (char *) buffer
+ ret
, length
- ret
);
39 static int prefetches
;
41 static struct object_list
*in_transit
;
42 static struct object_list
**end_of_transit
= &in_transit
;
44 void prefetch(unsigned char *sha1
)
47 struct object_list
*node
;
48 if (prefetches
> 100) {
49 fetch(in_transit
->item
->sha1
);
51 node
= xmalloc(sizeof(struct object_list
));
53 node
->item
= lookup_unknown_object(sha1
);
54 *end_of_transit
= node
;
55 end_of_transit
= &node
->next
;
56 force_write(fd_out
, &type
, 1);
57 force_write(fd_out
, sha1
, 20);
61 static char conn_buf
[4096];
62 static size_t conn_buf_posn
;
64 int fetch(unsigned char *sha1
)
68 struct object_list
*temp
;
70 if (hashcmp(sha1
, in_transit
->item
->sha1
)) {
71 /* we must have already fetched it to clean the queue */
72 return has_sha1_file(sha1
) ? 0 : -1;
76 in_transit
= in_transit
->next
;
78 end_of_transit
= &in_transit
;
83 memmove(conn_buf
, conn_buf
+ 1, --conn_buf_posn
);
85 if (xread(fd_in
, &remote
, 1) < 1)
88 /* fprintf(stderr, "Got %d\n", remote); */
91 ret
= write_sha1_from_fd(sha1
, fd_in
, conn_buf
, 4096, &conn_buf_posn
);
93 pull_say("got %s\n", sha1_to_hex(sha1
));
97 static int get_version(void)
100 write(fd_out
, &type
, 1);
101 write(fd_out
, &local_version
, 1);
102 if (xread(fd_in
, &remote_version
, 1) < 1) {
103 return error("Couldn't read version from remote end");
108 int fetch_ref(char *ref
, unsigned char *sha1
)
112 write(fd_out
, &type
, 1);
113 write(fd_out
, ref
, strlen(ref
) + 1);
115 if (read_in_full(fd_in
, &remote
, 1) != 1)
119 if (read_in_full(fd_in
, sha1
, 20) != 20)
124 static const char ssh_fetch_usage
[] =
126 " [-c] [-t] [-a] [-v] [--recover] [-w ref] commit-id url";
127 int main(int argc
, char **argv
)
129 const char *write_ref
= NULL
;
135 prog
= getenv("GIT_SSH_PUSH");
136 if (!prog
) prog
= "git-ssh-upload";
139 setup_git_directory();
140 git_config(git_default_config
);
142 while (arg
< argc
&& argv
[arg
][0] == '-') {
143 if (argv
[arg
][1] == 't') {
145 } else if (argv
[arg
][1] == 'c') {
147 } else if (argv
[arg
][1] == 'a') {
151 } else if (argv
[arg
][1] == 'v') {
153 } else if (argv
[arg
][1] == 'w') {
154 write_ref
= argv
[arg
+ 1];
156 } else if (!strcmp(argv
[arg
], "--recover")) {
161 if (argc
< arg
+ 2) {
162 usage(ssh_fetch_usage
);
165 commit_id
= argv
[arg
];
168 if (setup_connection(&fd_in
, &fd_out
, prog
, url
, arg
, argv
+ 1))
174 if (pull(1, &commit_id
, &write_ref
, url
))