Junio C Hamano | f71a69a | 2005-09-15 14:56:37 -0700 | [diff] [blame] | 1 | #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 Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 11 | #include "cache.h" |
| 12 | #include "rsh.h" |
Daniel Barkalow | c7c4bbe | 2005-06-06 16:43:27 -0400 | [diff] [blame] | 13 | #include "refs.h" |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 14 | |
Linus Torvalds | 6da4016 | 2005-07-03 10:10:45 -0700 | [diff] [blame] | 15 | static unsigned char local_version = 1; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 16 | static unsigned char remote_version; |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 17 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 18 | static int verbose; |
Daniel Barkalow | a5eda52 | 2005-07-10 18:25:38 -0400 | [diff] [blame] | 19 | |
Linus Torvalds | 6da4016 | 2005-07-03 10:10:45 -0700 | [diff] [blame] | 20 | static int serve_object(int fd_in, int fd_out) { |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 21 | ssize_t size; |
Mika Kukkonen | d565b34 | 2005-06-21 23:04:33 +0300 | [diff] [blame] | 22 | unsigned char sha1[20]; |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 23 | signed char remote; |
Andy Whitcroft | 93d26e4 | 2007-01-08 15:58:08 +0000 | [diff] [blame] | 24 | |
| 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 Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 32 | |
Daniel Barkalow | a5eda52 | 2005-07-10 18:25:38 -0400 | [diff] [blame] | 33 | if (verbose) |
| 34 | fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1)); |
| 35 | |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 36 | remote = 0; |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 37 | |
Daniel Barkalow | a5eda52 | 2005-07-10 18:25:38 -0400 | [diff] [blame] | 38 | if (!has_sha1_file(sha1)) { |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 39 | fprintf(stderr, "git-ssh-upload: could not find %s\n", |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 40 | sha1_to_hex(sha1)); |
| 41 | remote = -1; |
| 42 | } |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 43 | |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 44 | if (write_in_full(fd_out, &remote, 1) != 1) |
| 45 | return 0; |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 46 | |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 47 | if (remote < 0) |
| 48 | return 0; |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 49 | |
Daniel Barkalow | a5eda52 | 2005-07-10 18:25:38 -0400 | [diff] [blame] | 50 | return write_sha1_to_fd(fd_out, sha1); |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 51 | } |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 52 | |
Linus Torvalds | 6da4016 | 2005-07-03 10:10:45 -0700 | [diff] [blame] | 53 | static int serve_version(int fd_in, int fd_out) |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 54 | { |
Andy Whitcroft | 93d26e4 | 2007-01-08 15:58:08 +0000 | [diff] [blame] | 55 | if (xread(fd_in, &remote_version, 1) < 1) |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 56 | return -1; |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 57 | write_in_full(fd_out, &local_version, 1); |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 58 | return 0; |
| 59 | } |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 60 | |
Linus Torvalds | 6da4016 | 2005-07-03 10:10:45 -0700 | [diff] [blame] | 61 | static int serve_ref(int fd_in, int fd_out) |
Daniel Barkalow | c7c4bbe | 2005-06-06 16:43:27 -0400 | [diff] [blame] | 62 | { |
| 63 | char ref[PATH_MAX]; |
| 64 | unsigned char sha1[20]; |
| 65 | int posn = 0; |
| 66 | signed char remote = 0; |
| 67 | do { |
Andy Whitcroft | 93d26e4 | 2007-01-08 15:58:08 +0000 | [diff] [blame] | 68 | if (posn >= PATH_MAX || xread(fd_in, ref + posn, 1) < 1) |
Daniel Barkalow | c7c4bbe | 2005-06-06 16:43:27 -0400 | [diff] [blame] | 69 | return -1; |
| 70 | posn++; |
| 71 | } while (ref[posn - 1]); |
Daniel Barkalow | a5eda52 | 2005-07-10 18:25:38 -0400 | [diff] [blame] | 72 | |
| 73 | if (verbose) |
| 74 | fprintf(stderr, "Serving %s\n", ref); |
| 75 | |
Daniel Barkalow | c7c4bbe | 2005-06-06 16:43:27 -0400 | [diff] [blame] | 76 | if (get_ref_sha1(ref, sha1)) |
| 77 | remote = -1; |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 78 | if (write_in_full(fd_out, &remote, 1) != 1) |
| 79 | return 0; |
Daniel Barkalow | c7c4bbe | 2005-06-06 16:43:27 -0400 | [diff] [blame] | 80 | if (remote) |
| 81 | return 0; |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 82 | write_in_full(fd_out, sha1, 20); |
Daniel Barkalow | c7c4bbe | 2005-06-06 16:43:27 -0400 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | |
Linus Torvalds | 6da4016 | 2005-07-03 10:10:45 -0700 | [diff] [blame] | 87 | static void service(int fd_in, int fd_out) { |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 88 | char type; |
Johan Herland | 8a912bc | 2007-05-15 14:49:22 +0200 | [diff] [blame] | 89 | ssize_t retval; |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 90 | do { |
Andy Whitcroft | 93d26e4 | 2007-01-08 15:58:08 +0000 | [diff] [blame] | 91 | retval = xread(fd_in, &type, 1); |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 92 | if (retval < 1) { |
| 93 | if (retval < 0) |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 94 | perror("git-ssh-upload: read "); |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 95 | return; |
| 96 | } |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 97 | if (type == 'v' && serve_version(fd_in, fd_out)) |
| 98 | return; |
| 99 | if (type == 'o' && serve_object(fd_in, fd_out)) |
| 100 | return; |
Daniel Barkalow | c7c4bbe | 2005-06-06 16:43:27 -0400 | [diff] [blame] | 101 | if (type == 'r' && serve_ref(fd_in, fd_out)) |
| 102 | return; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 103 | } while (1); |
| 104 | } |
| 105 | |
Petr Baudis | 4d1f119 | 2005-07-29 11:01:26 +0200 | [diff] [blame] | 106 | static const char ssh_push_usage[] = |
Junio C Hamano | f71a69a | 2005-09-15 14:56:37 -0700 | [diff] [blame] | 107 | MY_PROGRAM_NAME " [-c] [-t] [-a] [-w ref] commit-id url"; |
Sven Verdoolaege | ee85cbc | 2005-06-27 21:03:07 +0200 | [diff] [blame] | 108 | |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 109 | int main(int argc, char **argv) |
| 110 | { |
| 111 | int arg = 1; |
| 112 | char *commit_id; |
| 113 | char *url; |
| 114 | int fd_in, fd_out; |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 115 | const char *prog; |
Sven Verdoolaege | ee85cbc | 2005-06-27 21:03:07 +0200 | [diff] [blame] | 116 | unsigned char sha1[20]; |
| 117 | char hex[41]; |
Linus Torvalds | 001d4a2 | 2005-06-07 14:23:46 -0700 | [diff] [blame] | 118 | |
Junio C Hamano | f71a69a | 2005-09-15 14:56:37 -0700 | [diff] [blame] | 119 | prog = getenv(COUNTERPART_ENV_NAME); |
| 120 | if (!prog) prog = COUNTERPART_PROGRAM_NAME; |
Junio C Hamano | 5a32771 | 2005-11-26 00:47:59 -0800 | [diff] [blame] | 121 | |
| 122 | setup_git_directory(); |
| 123 | |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 124 | while (arg < argc && argv[arg][0] == '-') { |
Daniel Barkalow | c7c4bbe | 2005-06-06 16:43:27 -0400 | [diff] [blame] | 125 | if (argv[arg][1] == 'w') |
| 126 | arg++; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 127 | arg++; |
| 128 | } |
Sven Verdoolaege | ee85cbc | 2005-06-27 21:03:07 +0200 | [diff] [blame] | 129 | if (argc < arg + 2) |
| 130 | usage(ssh_push_usage); |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 131 | commit_id = argv[arg]; |
| 132 | url = argv[arg + 1]; |
Sven Verdoolaege | ee85cbc | 2005-06-27 21:03:07 +0200 | [diff] [blame] | 133 | if (get_sha1(commit_id, sha1)) |
Dmitry V. Levin | 31fff30 | 2006-05-09 01:43:38 +0400 | [diff] [blame] | 134 | die("Not a valid object name %s", commit_id); |
Sven Verdoolaege | ee85cbc | 2005-06-27 21:03:07 +0200 | [diff] [blame] | 135 | memcpy(hex, sha1_to_hex(sha1), sizeof(hex)); |
| 136 | argv[arg] = hex; |
| 137 | |
Linus Torvalds | 001d4a2 | 2005-06-07 14:23:46 -0700 | [diff] [blame] | 138 | if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1)) |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 139 | return 1; |
| 140 | |
| 141 | service(fd_in, fd_out); |
| 142 | return 0; |
| 143 | } |