blob: 10a36873391b727a75e5f1d3a42d78acb62ed502 [file] [log] [blame]
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -07001#include "cache.h"
2#include "rsh.h"
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -04003#include "refs.h"
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -07004
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +02005#include <string.h>
6
Linus Torvalds6da40162005-07-03 10:10:45 -07007static unsigned char local_version = 1;
8static unsigned char remote_version = 0;
Daniel Barkalowdba385b2005-06-03 17:43:52 -04009
Daniel Barkalowa5eda522005-07-10 18:25:38 -040010static int verbose = 0;
11
Linus Torvalds6da40162005-07-03 10:10:45 -070012static int serve_object(int fd_in, int fd_out) {
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070013 ssize_t size;
Mika Kukkonend565b342005-06-21 23:04:33 +030014 unsigned char sha1[20];
Daniel Barkalowdba385b2005-06-03 17:43:52 -040015 signed char remote;
Daniel Barkalowa5eda522005-07-10 18:25:38 -040016 int posn = 0;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070017 do {
Daniel Barkalowdba385b2005-06-03 17:43:52 -040018 size = read(fd_in, sha1 + posn, 20 - posn);
19 if (size < 0) {
Junio C Hamano215a7ad2005-09-07 17:26:23 -070020 perror("git-ssh-upload: read ");
Daniel Barkalowdba385b2005-06-03 17:43:52 -040021 return -1;
22 }
23 if (!size)
24 return -1;
25 posn += size;
26 } while (posn < 20);
27
Daniel Barkalowa5eda522005-07-10 18:25:38 -040028 if (verbose)
29 fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1));
30
Daniel Barkalowdba385b2005-06-03 17:43:52 -040031 remote = 0;
32
Daniel Barkalowa5eda522005-07-10 18:25:38 -040033 if (!has_sha1_file(sha1)) {
Junio C Hamano215a7ad2005-09-07 17:26:23 -070034 fprintf(stderr, "git-ssh-upload: could not find %s\n",
Daniel Barkalowdba385b2005-06-03 17:43:52 -040035 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 Barkalowa5eda522005-07-10 18:25:38 -040044 return write_sha1_to_fd(fd_out, sha1);
Daniel Barkalowdba385b2005-06-03 17:43:52 -040045}
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070046
Linus Torvalds6da40162005-07-03 10:10:45 -070047static int serve_version(int fd_in, int fd_out)
Daniel Barkalowdba385b2005-06-03 17:43:52 -040048{
49 if (read(fd_in, &remote_version, 1) < 1)
50 return -1;
51 write(fd_out, &local_version, 1);
52 return 0;
53}
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070054
Linus Torvalds6da40162005-07-03 10:10:45 -070055static int serve_ref(int fd_in, int fd_out)
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040056{
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 Barkalowa5eda522005-07-10 18:25:38 -040066
67 if (verbose)
68 fprintf(stderr, "Serving %s\n", ref);
69
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040070 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 Torvalds6da40162005-07-03 10:10:45 -070080static void service(int fd_in, int fd_out) {
Daniel Barkalowdba385b2005-06-03 17:43:52 -040081 char type;
82 int retval;
83 do {
84 retval = read(fd_in, &type, 1);
85 if (retval < 1) {
86 if (retval < 0)
Junio C Hamano215a7ad2005-09-07 17:26:23 -070087 perror("git-ssh-upload: read ");
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070088 return;
89 }
Daniel Barkalowdba385b2005-06-03 17:43:52 -040090 if (type == 'v' && serve_version(fd_in, fd_out))
91 return;
92 if (type == 'o' && serve_object(fd_in, fd_out))
93 return;
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040094 if (type == 'r' && serve_ref(fd_in, fd_out))
95 return;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070096 } while (1);
97}
98
Petr Baudis4d1f1192005-07-29 11:01:26 +020099static const char ssh_push_usage[] =
Junio C Hamano215a7ad2005-09-07 17:26:23 -0700100 "git-ssh-upload [-c] [-t] [-a] [-w ref] commit-id url";
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +0200101
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700102int main(int argc, char **argv)
103{
104 int arg = 1;
105 char *commit_id;
106 char *url;
107 int fd_in, fd_out;
Jason Riedyc7c81b32005-08-23 13:34:07 -0700108 const char *prog;
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +0200109 unsigned char sha1[20];
110 char hex[41];
Linus Torvalds001d4a22005-06-07 14:23:46 -0700111
Jason Riedyc7c81b32005-08-23 13:34:07 -0700112 prog = getenv("GIT_SSH_PULL");
Junio C Hamano215a7ad2005-09-07 17:26:23 -0700113 if (!prog) prog = "git-ssh-fetch";
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700114 while (arg < argc && argv[arg][0] == '-') {
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -0400115 if (argv[arg][1] == 'w')
116 arg++;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700117 arg++;
118 }
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +0200119 if (argc < arg + 2)
120 usage(ssh_push_usage);
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700121 commit_id = argv[arg];
122 url = argv[arg + 1];
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +0200123 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 Torvalds001d4a22005-06-07 14:23:46 -0700128 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700129 return 1;
130
131 service(fd_in, fd_out);
132 return 0;
133}