[PATCH] Ensure list insertion method does not depend on position of --merge-order...
[git/dkf.git] / ssh-push.c
blob090d6f9f8fbde2d736ac5bf563415b0fa402b5aa
1 #include "cache.h"
2 #include "rsh.h"
3 #include "refs.h"
5 #include <string.h>
7 static unsigned char local_version = 1;
8 static unsigned char remote_version = 0;
10 static int serve_object(int fd_in, int fd_out) {
11 ssize_t size;
12 int posn = 0;
13 unsigned char sha1[20];
14 unsigned long objsize;
15 void *buf;
16 signed char remote;
17 do {
18 size = read(fd_in, sha1 + posn, 20 - posn);
19 if (size < 0) {
20 perror("git-ssh-push: read ");
21 return -1;
23 if (!size)
24 return -1;
25 posn += size;
26 } while (posn < 20);
28 /* fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1)); */
29 remote = 0;
31 buf = map_sha1_file(sha1, &objsize);
33 if (!buf) {
34 fprintf(stderr, "git-ssh-push: could not find %s\n",
35 sha1_to_hex(sha1));
36 remote = -1;
39 write(fd_out, &remote, 1);
41 if (remote < 0)
42 return 0;
44 posn = 0;
45 do {
46 size = write(fd_out, buf + posn, objsize - posn);
47 if (size <= 0) {
48 if (!size) {
49 fprintf(stderr, "git-ssh-push: write closed");
50 } else {
51 perror("git-ssh-push: write ");
53 return -1;
55 posn += size;
56 } while (posn < objsize);
57 return 0;
60 static int serve_version(int fd_in, int fd_out)
62 if (read(fd_in, &remote_version, 1) < 1)
63 return -1;
64 write(fd_out, &local_version, 1);
65 return 0;
68 static int serve_ref(int fd_in, int fd_out)
70 char ref[PATH_MAX];
71 unsigned char sha1[20];
72 int posn = 0;
73 signed char remote = 0;
74 do {
75 if (read(fd_in, ref + posn, 1) < 1)
76 return -1;
77 posn++;
78 } while (ref[posn - 1]);
79 if (get_ref_sha1(ref, sha1))
80 remote = -1;
81 write(fd_out, &remote, 1);
82 if (remote)
83 return 0;
84 write(fd_out, sha1, 20);
85 return 0;
89 static void service(int fd_in, int fd_out) {
90 char type;
91 int retval;
92 do {
93 retval = read(fd_in, &type, 1);
94 if (retval < 1) {
95 if (retval < 0)
96 perror("git-ssh-push: read ");
97 return;
99 if (type == 'v' && serve_version(fd_in, fd_out))
100 return;
101 if (type == 'o' && serve_object(fd_in, fd_out))
102 return;
103 if (type == 'r' && serve_ref(fd_in, fd_out))
104 return;
105 } while (1);
108 static const char *ssh_push_usage =
109 "git-ssh-push [-c] [-t] [-a] [-w ref] commit-id url";
111 int main(int argc, char **argv)
113 int arg = 1;
114 char *commit_id;
115 char *url;
116 int fd_in, fd_out;
117 const char *prog = getenv("GIT_SSH_PULL") ? : "git-ssh-pull";
118 unsigned char sha1[20];
119 char hex[41];
121 while (arg < argc && argv[arg][0] == '-') {
122 if (argv[arg][1] == 'w')
123 arg++;
124 arg++;
126 if (argc < arg + 2)
127 usage(ssh_push_usage);
128 commit_id = argv[arg];
129 url = argv[arg + 1];
130 if (get_sha1(commit_id, sha1))
131 usage(ssh_push_usage);
132 memcpy(hex, sha1_to_hex(sha1), sizeof(hex));
133 argv[arg] = hex;
135 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
136 return 1;
138 service(fd_in, fd_out);
139 return 0;