Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "rsh.h" |
Daniel Barkalow | c7c4bbe | 2005-06-06 16:43:27 -0400 | [diff] [blame] | 3 | #include "refs.h" |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 4 | |
Sven Verdoolaege | ee85cbc | 2005-06-27 21:03:07 +0200 | [diff] [blame] | 5 | #include <string.h> |
| 6 | |
Linus Torvalds | 6da4016 | 2005-07-03 10:10:45 -0700 | [diff] [blame] | 7 | static unsigned char local_version = 1; |
| 8 | static unsigned char remote_version = 0; |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 9 | |
Daniel Barkalow | a5eda52 | 2005-07-10 18:25:38 -0400 | [diff] [blame] | 10 | static int verbose = 0; |
| 11 | |
Linus Torvalds | 6da4016 | 2005-07-03 10:10:45 -0700 | [diff] [blame] | 12 | static int serve_object(int fd_in, int fd_out) { |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 13 | ssize_t size; |
Mika Kukkonen | d565b34 | 2005-06-21 23:04:33 +0300 | [diff] [blame] | 14 | unsigned char sha1[20]; |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 15 | signed char remote; |
Daniel Barkalow | a5eda52 | 2005-07-10 18:25:38 -0400 | [diff] [blame] | 16 | int posn = 0; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 17 | do { |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 18 | size = read(fd_in, sha1 + posn, 20 - posn); |
| 19 | if (size < 0) { |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 20 | perror("git-ssh-upload: read "); |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 21 | return -1; |
| 22 | } |
| 23 | if (!size) |
| 24 | return -1; |
| 25 | posn += size; |
| 26 | } while (posn < 20); |
| 27 | |
Daniel Barkalow | a5eda52 | 2005-07-10 18:25:38 -0400 | [diff] [blame] | 28 | if (verbose) |
| 29 | fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1)); |
| 30 | |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 31 | remote = 0; |
| 32 | |
Daniel Barkalow | a5eda52 | 2005-07-10 18:25:38 -0400 | [diff] [blame] | 33 | if (!has_sha1_file(sha1)) { |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 34 | fprintf(stderr, "git-ssh-upload: could not find %s\n", |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 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 | |
Daniel Barkalow | a5eda52 | 2005-07-10 18:25:38 -0400 | [diff] [blame] | 44 | return write_sha1_to_fd(fd_out, sha1); |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 45 | } |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 46 | |
Linus Torvalds | 6da4016 | 2005-07-03 10:10:45 -0700 | [diff] [blame] | 47 | static int serve_version(int fd_in, int fd_out) |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 48 | { |
| 49 | if (read(fd_in, &remote_version, 1) < 1) |
| 50 | return -1; |
| 51 | write(fd_out, &local_version, 1); |
| 52 | return 0; |
| 53 | } |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 54 | |
Linus Torvalds | 6da4016 | 2005-07-03 10:10:45 -0700 | [diff] [blame] | 55 | static int serve_ref(int fd_in, int fd_out) |
Daniel Barkalow | c7c4bbe | 2005-06-06 16:43:27 -0400 | [diff] [blame] | 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]); |
Daniel Barkalow | a5eda52 | 2005-07-10 18:25:38 -0400 | [diff] [blame] | 66 | |
| 67 | if (verbose) |
| 68 | fprintf(stderr, "Serving %s\n", ref); |
| 69 | |
Daniel Barkalow | c7c4bbe | 2005-06-06 16:43:27 -0400 | [diff] [blame] | 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 | |
Linus Torvalds | 6da4016 | 2005-07-03 10:10:45 -0700 | [diff] [blame] | 80 | static void service(int fd_in, int fd_out) { |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 81 | char type; |
| 82 | int retval; |
| 83 | do { |
| 84 | retval = read(fd_in, &type, 1); |
| 85 | if (retval < 1) { |
| 86 | if (retval < 0) |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 87 | perror("git-ssh-upload: read "); |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 88 | return; |
| 89 | } |
Daniel Barkalow | dba385b | 2005-06-03 17:43:52 -0400 | [diff] [blame] | 90 | if (type == 'v' && serve_version(fd_in, fd_out)) |
| 91 | return; |
| 92 | if (type == 'o' && serve_object(fd_in, fd_out)) |
| 93 | return; |
Daniel Barkalow | c7c4bbe | 2005-06-06 16:43:27 -0400 | [diff] [blame] | 94 | if (type == 'r' && serve_ref(fd_in, fd_out)) |
| 95 | return; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 96 | } while (1); |
| 97 | } |
| 98 | |
Petr Baudis | 4d1f119 | 2005-07-29 11:01:26 +0200 | [diff] [blame] | 99 | static const char ssh_push_usage[] = |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 100 | "git-ssh-upload [-c] [-t] [-a] [-w ref] commit-id url"; |
Sven Verdoolaege | ee85cbc | 2005-06-27 21:03:07 +0200 | [diff] [blame] | 101 | |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 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; |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 108 | const char *prog; |
Sven Verdoolaege | ee85cbc | 2005-06-27 21:03:07 +0200 | [diff] [blame] | 109 | unsigned char sha1[20]; |
| 110 | char hex[41]; |
Linus Torvalds | 001d4a2 | 2005-06-07 14:23:46 -0700 | [diff] [blame] | 111 | |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 112 | prog = getenv("GIT_SSH_PULL"); |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 113 | if (!prog) prog = "git-ssh-fetch"; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 114 | while (arg < argc && argv[arg][0] == '-') { |
Daniel Barkalow | c7c4bbe | 2005-06-06 16:43:27 -0400 | [diff] [blame] | 115 | if (argv[arg][1] == 'w') |
| 116 | arg++; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 117 | arg++; |
| 118 | } |
Sven Verdoolaege | ee85cbc | 2005-06-27 21:03:07 +0200 | [diff] [blame] | 119 | if (argc < arg + 2) |
| 120 | usage(ssh_push_usage); |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 121 | commit_id = argv[arg]; |
| 122 | url = argv[arg + 1]; |
Sven Verdoolaege | ee85cbc | 2005-06-27 21:03:07 +0200 | [diff] [blame] | 123 | if (get_sha1(commit_id, sha1)) |
| 124 | usage(ssh_push_usage); |
| 125 | memcpy(hex, sha1_to_hex(sha1), sizeof(hex)); |
| 126 | argv[arg] = hex; |
| 127 | |
Linus Torvalds | 001d4a2 | 2005-06-07 14:23:46 -0700 | [diff] [blame] | 128 | if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1)) |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 129 | return 1; |
| 130 | |
| 131 | service(fd_in, fd_out); |
| 132 | return 0; |
| 133 | } |