blob: b675a0b1f1df7d313f8201d59ad25b1c3d53a2f4 [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
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +020015#include <string.h>
16
Linus Torvalds6da40162005-07-03 10:10:45 -070017static unsigned char local_version = 1;
18static unsigned char remote_version = 0;
Daniel Barkalowdba385b2005-06-03 17:43:52 -040019
Daniel Barkalowa5eda522005-07-10 18:25:38 -040020static int verbose = 0;
21
Linus Torvalds6da40162005-07-03 10:10:45 -070022static int serve_object(int fd_in, int fd_out) {
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070023 ssize_t size;
Mika Kukkonend565b342005-06-21 23:04:33 +030024 unsigned char sha1[20];
Daniel Barkalowdba385b2005-06-03 17:43:52 -040025 signed char remote;
Daniel Barkalowa5eda522005-07-10 18:25:38 -040026 int posn = 0;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070027 do {
Daniel Barkalowdba385b2005-06-03 17:43:52 -040028 size = read(fd_in, sha1 + posn, 20 - posn);
29 if (size < 0) {
Junio C Hamano215a7ad2005-09-07 17:26:23 -070030 perror("git-ssh-upload: read ");
Daniel Barkalowdba385b2005-06-03 17:43:52 -040031 return -1;
32 }
33 if (!size)
34 return -1;
35 posn += size;
36 } while (posn < 20);
37
Daniel Barkalowa5eda522005-07-10 18:25:38 -040038 if (verbose)
39 fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1));
40
Daniel Barkalowdba385b2005-06-03 17:43:52 -040041 remote = 0;
42
Daniel Barkalowa5eda522005-07-10 18:25:38 -040043 if (!has_sha1_file(sha1)) {
Junio C Hamano215a7ad2005-09-07 17:26:23 -070044 fprintf(stderr, "git-ssh-upload: could not find %s\n",
Daniel Barkalowdba385b2005-06-03 17:43:52 -040045 sha1_to_hex(sha1));
46 remote = -1;
47 }
48
49 write(fd_out, &remote, 1);
50
51 if (remote < 0)
52 return 0;
53
Daniel Barkalowa5eda522005-07-10 18:25:38 -040054 return write_sha1_to_fd(fd_out, sha1);
Daniel Barkalowdba385b2005-06-03 17:43:52 -040055}
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070056
Linus Torvalds6da40162005-07-03 10:10:45 -070057static int serve_version(int fd_in, int fd_out)
Daniel Barkalowdba385b2005-06-03 17:43:52 -040058{
59 if (read(fd_in, &remote_version, 1) < 1)
60 return -1;
61 write(fd_out, &local_version, 1);
62 return 0;
63}
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070064
Linus Torvalds6da40162005-07-03 10:10:45 -070065static int serve_ref(int fd_in, int fd_out)
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040066{
67 char ref[PATH_MAX];
68 unsigned char sha1[20];
69 int posn = 0;
70 signed char remote = 0;
71 do {
72 if (read(fd_in, ref + posn, 1) < 1)
73 return -1;
74 posn++;
75 } while (ref[posn - 1]);
Daniel Barkalowa5eda522005-07-10 18:25:38 -040076
77 if (verbose)
78 fprintf(stderr, "Serving %s\n", ref);
79
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -040080 if (get_ref_sha1(ref, sha1))
81 remote = -1;
82 write(fd_out, &remote, 1);
83 if (remote)
84 return 0;
85 write(fd_out, sha1, 20);
86 return 0;
87}
88
89
Linus Torvalds6da40162005-07-03 10:10:45 -070090static void service(int fd_in, int fd_out) {
Daniel Barkalowdba385b2005-06-03 17:43:52 -040091 char type;
92 int retval;
93 do {
94 retval = read(fd_in, &type, 1);
95 if (retval < 1) {
96 if (retval < 0)
Junio C Hamano215a7ad2005-09-07 17:26:23 -070097 perror("git-ssh-upload: read ");
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070098 return;
99 }
Daniel Barkalowdba385b2005-06-03 17:43:52 -0400100 if (type == 'v' && serve_version(fd_in, fd_out))
101 return;
102 if (type == 'o' && serve_object(fd_in, fd_out))
103 return;
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -0400104 if (type == 'r' && serve_ref(fd_in, fd_out))
105 return;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700106 } while (1);
107}
108
Petr Baudis4d1f1192005-07-29 11:01:26 +0200109static const char ssh_push_usage[] =
Junio C Hamanof71a69a2005-09-15 14:56:37 -0700110 MY_PROGRAM_NAME " [-c] [-t] [-a] [-w ref] commit-id url";
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +0200111
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700112int main(int argc, char **argv)
113{
114 int arg = 1;
115 char *commit_id;
116 char *url;
117 int fd_in, fd_out;
Jason Riedyc7c81b32005-08-23 13:34:07 -0700118 const char *prog;
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +0200119 unsigned char sha1[20];
120 char hex[41];
Linus Torvalds001d4a22005-06-07 14:23:46 -0700121
Junio C Hamanof71a69a2005-09-15 14:56:37 -0700122 prog = getenv(COUNTERPART_ENV_NAME);
123 if (!prog) prog = COUNTERPART_PROGRAM_NAME;
Junio C Hamano5a327712005-11-26 00:47:59 -0800124
125 setup_git_directory();
126
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700127 while (arg < argc && argv[arg][0] == '-') {
Daniel Barkalowc7c4bbe2005-06-06 16:43:27 -0400128 if (argv[arg][1] == 'w')
129 arg++;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700130 arg++;
131 }
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +0200132 if (argc < arg + 2)
133 usage(ssh_push_usage);
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700134 commit_id = argv[arg];
135 url = argv[arg + 1];
Sven Verdoolaegeee85cbc2005-06-27 21:03:07 +0200136 if (get_sha1(commit_id, sha1))
137 usage(ssh_push_usage);
138 memcpy(hex, sha1_to_hex(sha1), sizeof(hex));
139 argv[arg] = hex;
140
Linus Torvalds001d4a22005-06-07 14:23:46 -0700141 if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700142 return 1;
143
144 service(fd_in, fd_out);
145 return 0;
146}