blob: 20c35f03dd1fb468c7cecc71f91951657f229f5e [file] [log] [blame]
Junio C Hamanof71a69a2005-09-15 14:56:37 -07001#ifndef COUNTERPART_ENV_NAME
2#define COUNTERPART_ENV_NAME "GIT_SSH_FETCH"
3#endif
4#ifndef COUNTERPART_PROGRAM_NAME
5#define COUNTERPART_PROGRAM_NAME "git-ssh-fetch"
6#endif
7#ifndef MY_PROGRAM_NAME
8#define MY_PROGRAM_NAME "git-ssh-upload"
9#endif
10
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070011#include "cache.h"
12#include "rsh.h"
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040013#include "refs.h"
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070014
Linus Torvalds6da40162005-07-03 10:10:45 -070015static unsigned char local_version = 1;
David Rientjes96f1e582006-08-15 10:23:48 -070016static unsigned char remote_version;
Daniel Barkalowdba385b2005-06-03 17:43:52 -040017
David Rientjes96f1e582006-08-15 10:23:48 -070018static int verbose;
Daniel Barkalowa5eda522005-07-10 18:25:38 -040019
Linus Torvalds6da40162005-07-03 10:10:45 -070020static int serve_object(int fd_in, int fd_out) {
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070021 ssize_t size;
Mika Kukkonend565b342005-06-21 23:04:33 +030022 unsigned char sha1[20];
Daniel Barkalowdba385b2005-06-03 17:43:52 -040023 signed char remote;
Andy Whitcroft93d26e42007-01-08 15:58:08 +000024
25 size = read_in_full(fd_in, sha1, 20);
26 if (size < 0) {
27 perror("git-ssh-upload: read ");
28 return -1;
29 }
30 if (!size)
31 return -1;
Junio C Hamanoa6080a02007-06-07 00:04:01 -070032
Daniel Barkalowa5eda522005-07-10 18:25:38 -040033 if (verbose)
34 fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1));
35
Daniel Barkalowdba385b2005-06-03 17:43:52 -040036 remote = 0;
Junio C Hamanoa6080a02007-06-07 00:04:01 -070037
Daniel Barkalowa5eda522005-07-10 18:25:38 -040038 if (!has_sha1_file(sha1)) {
Junio C Hamano215a7ad2005-09-07 17:26:23 -070039 fprintf(stderr, "git-ssh-upload: could not find %s\n",
Daniel Barkalowdba385b2005-06-03 17:43:52 -040040 sha1_to_hex(sha1));
41 remote = -1;
42 }
Junio C Hamanoa6080a02007-06-07 00:04:01 -070043
Andy Whitcroft93822c22007-01-08 15:58:23 +000044 if (write_in_full(fd_out, &remote, 1) != 1)
45 return 0;
Junio C Hamanoa6080a02007-06-07 00:04:01 -070046
Daniel Barkalowdba385b2005-06-03 17:43:52 -040047 if (remote < 0)
48 return 0;
Junio C Hamanoa6080a02007-06-07 00:04:01 -070049
Daniel Barkalowa5eda522005-07-10 18:25:38 -040050 return write_sha1_to_fd(fd_out, sha1);
Daniel Barkalowdba385b2005-06-03 17:43:52 -040051}
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070052
Linus Torvalds6da40162005-07-03 10:10:45 -070053static int serve_version(int fd_in, int fd_out)
Daniel Barkalowdba385b2005-06-03 17:43:52 -040054{
Andy Whitcroft93d26e42007-01-08 15:58:08 +000055 if (xread(fd_in, &remote_version, 1) < 1)
Daniel Barkalowdba385b2005-06-03 17:43:52 -040056 return -1;
Andy Whitcroft93822c22007-01-08 15:58:23 +000057 write_in_full(fd_out, &local_version, 1);
Daniel Barkalowdba385b2005-06-03 17:43:52 -040058 return 0;
59}
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070060
Linus Torvalds6da40162005-07-03 10:10:45 -070061static int serve_ref(int fd_in, int fd_out)
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040062{
63 char ref[PATH_MAX];
64 unsigned char sha1[20];
65 int posn = 0;
66 signed char remote = 0;
67 do {
Andy Whitcroft93d26e42007-01-08 15:58:08 +000068 if (posn >= PATH_MAX || xread(fd_in, ref + posn, 1) < 1)
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040069 return -1;
70 posn++;
71 } while (ref[posn - 1]);
Daniel Barkalowa5eda522005-07-10 18:25:38 -040072
73 if (verbose)
74 fprintf(stderr, "Serving %s\n", ref);
75
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040076 if (get_ref_sha1(ref, sha1))
77 remote = -1;
Andy Whitcroft93822c22007-01-08 15:58:23 +000078 if (write_in_full(fd_out, &remote, 1) != 1)
79 return 0;
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040080 if (remote)
81 return 0;
Andy Whitcroft93822c22007-01-08 15:58:23 +000082 write_in_full(fd_out, sha1, 20);
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040083 return 0;
84}
85
86
Linus Torvalds6da40162005-07-03 10:10:45 -070087static void service(int fd_in, int fd_out) {
Daniel Barkalowdba385b2005-06-03 17:43:52 -040088 char type;
Johan Herland8a912bc2007-05-15 14:49:22 +020089 ssize_t retval;
Daniel Barkalowdba385b2005-06-03 17:43:52 -040090 do {
Andy Whitcroft93d26e42007-01-08 15:58:08 +000091 retval = xread(fd_in, &type, 1);
Daniel Barkalowdba385b2005-06-03 17:43:52 -040092 if (retval < 1) {
93 if (retval < 0)
Junio C Hamano215a7ad2005-09-07 17:26:23 -070094 perror("git-ssh-upload: read ");
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070095 return;
96 }
Daniel Barkalowdba385b2005-06-03 17:43:52 -040097 if (type == 'v' && serve_version(fd_in, fd_out))
98 return;
99 if (type == 'o' && serve_object(fd_in, fd_out))
100 return;
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -0400101 if (type == 'r' && serve_ref(fd_in, fd_out))
102 return;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700103 } while (1);
104}
105
Petr Baudis4d1f1192005-07-29 11:01:26 +0200106static const char ssh_push_usage[] =
Junio C Hamanof71a69a2005-09-15 14:56:37 -0700107 MY_PROGRAM_NAME " [-c] [-t] [-a] [-w ref] commit-id url";
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +0200108
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700109int main(int argc, char **argv)
110{
111 int arg = 1;
112 char *commit_id;
113 char *url;
114 int fd_in, fd_out;
Jason Riedyc7c81b32005-08-23 13:34:07 -0700115 const char *prog;
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +0200116 unsigned char sha1[20];
117 char hex[41];
Linus Torvalds001d4a22005-06-07 14:23:46 -0700118
Junio C Hamanof71a69a2005-09-15 14:56:37 -0700119 prog = getenv(COUNTERPART_ENV_NAME);
120 if (!prog) prog = COUNTERPART_PROGRAM_NAME;
Junio C Hamano5a327712005-11-26 00:47:59 -0800121
122 setup_git_directory();
123
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700124 while (arg < argc && argv[arg][0] == '-') {
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -0400125 if (argv[arg][1] == 'w')
126 arg++;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700127 arg++;
128 }
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +0200129 if (argc < arg + 2)
130 usage(ssh_push_usage);
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700131 commit_id = argv[arg];
132 url = argv[arg + 1];
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +0200133 if (get_sha1(commit_id, sha1))
Dmitry V. Levin31fff302006-05-09 01:43:38 +0400134 die("Not a valid object name %s", commit_id);
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +0200135 memcpy(hex, sha1_to_hex(sha1), sizeof(hex));
136 argv[arg] = hex;
137
Linus Torvalds001d4a22005-06-07 14:23:46 -0700138 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700139 return 1;
140
141 service(fd_in, fd_out);
142 return 0;
143}