[PATCH] GIT: Try all addresses for given remote name
[git] / ssh-push.c
1 #include "cache.h"
2 #include "rsh.h"
3 #include "refs.h"
4
5 #include <string.h>
6
7 static unsigned char local_version = 1;
8 static unsigned char remote_version = 0;
9
10 static int verbose = 0;
11
12 static int serve_object(int fd_in, int fd_out) {
13         ssize_t size;
14         unsigned char sha1[20];
15         signed char remote;
16         int posn = 0;
17         do {
18                 size = read(fd_in, sha1 + posn, 20 - posn);
19                 if (size < 0) {
20                         perror("git-ssh-push: read ");
21                         return -1;
22                 }
23                 if (!size)
24                         return -1;
25                 posn += size;
26         } while (posn < 20);
27         
28         if (verbose)
29                 fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1));
30
31         remote = 0;
32         
33         if (!has_sha1_file(sha1)) {
34                 fprintf(stderr, "git-ssh-push: could not find %s\n", 
35                         sha1_to_hex(sha1));
36                 remote = -1;
37         }
38         
39         write(fd_out, &remote, 1);
40         
41         if (remote < 0)
42                 return 0;
43         
44         return write_sha1_to_fd(fd_out, sha1);
45 }
46
47 static int serve_version(int fd_in, int fd_out)
48 {
49         if (read(fd_in, &remote_version, 1) < 1)
50                 return -1;
51         write(fd_out, &local_version, 1);
52         return 0;
53 }
54
55 static int serve_ref(int fd_in, int fd_out)
56 {
57         char ref[PATH_MAX];
58         unsigned char sha1[20];
59         int posn = 0;
60         signed char remote = 0;
61         do {
62                 if (read(fd_in, ref + posn, 1) < 1)
63                         return -1;
64                 posn++;
65         } while (ref[posn - 1]);
66
67         if (verbose)
68                 fprintf(stderr, "Serving %s\n", ref);
69
70         if (get_ref_sha1(ref, sha1))
71                 remote = -1;
72         write(fd_out, &remote, 1);
73         if (remote)
74                 return 0;
75         write(fd_out, sha1, 20);
76         return 0;
77 }
78
79
80 static void service(int fd_in, int fd_out) {
81         char type;
82         int retval;
83         do {
84                 retval = read(fd_in, &type, 1);
85                 if (retval < 1) {
86                         if (retval < 0)
87                                 perror("git-ssh-push: read ");
88                         return;
89                 }
90                 if (type == 'v' && serve_version(fd_in, fd_out))
91                         return;
92                 if (type == 'o' && serve_object(fd_in, fd_out))
93                         return;
94                 if (type == 'r' && serve_ref(fd_in, fd_out))
95                         return;
96         } while (1);
97 }
98
99 static const char *ssh_push_usage =
100         "git-ssh-push [-c] [-t] [-a] [-w ref] commit-id url";
101
102 int main(int argc, char **argv)
103 {
104         int arg = 1;
105         char *commit_id;
106         char *url;
107         int fd_in, fd_out;
108         const char *prog = getenv("GIT_SSH_PULL") ? : "git-ssh-pull";
109         unsigned char sha1[20];
110         char hex[41];
111
112         while (arg < argc && argv[arg][0] == '-') {
113                 if (argv[arg][1] == 'w')
114                         arg++;
115                 arg++;
116         }
117         if (argc < arg + 2)
118                 usage(ssh_push_usage);
119         commit_id = argv[arg];
120         url = argv[arg + 1];
121         if (get_sha1(commit_id, sha1))
122                 usage(ssh_push_usage);
123         memcpy(hex, sha1_to_hex(sha1), sizeof(hex));
124         argv[arg] = hex;
125
126         if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
127                 return 1;
128
129         service(fd_in, fd_out);
130         return 0;
131 }