Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Linus Torvalds | 8a65ff7 | 2005-07-02 20:23:36 -0700 | [diff] [blame] | 2 | #include "refs.h" |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 3 | #include "pkt-line.h" |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 4 | #include "run-command.h" |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 5 | #include <sys/wait.h> |
| 6 | |
Linus Torvalds | d0efc8a | 2005-06-30 12:28:24 -0700 | [diff] [blame] | 7 | static const char receive_pack_usage[] = "git-receive-pack <git-dir>"; |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 8 | |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 9 | static const char unpacker[] = "git-unpack-objects"; |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 10 | |
Linus Torvalds | 944d858 | 2005-07-03 10:01:38 -0700 | [diff] [blame] | 11 | static int show_ref(const char *path, const unsigned char *sha1) |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 12 | { |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 13 | packet_write(1, "%s %s\n", sha1_to_hex(sha1), path); |
Linus Torvalds | 8a65ff7 | 2005-07-02 20:23:36 -0700 | [diff] [blame] | 14 | return 0; |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 15 | } |
| 16 | |
Linus Torvalds | 8a65ff7 | 2005-07-02 20:23:36 -0700 | [diff] [blame] | 17 | static void write_head_info(void) |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 18 | { |
Linus Torvalds | 8a65ff7 | 2005-07-02 20:23:36 -0700 | [diff] [blame] | 19 | for_each_ref(show_ref); |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 20 | } |
| 21 | |
Linus Torvalds | eb1af2d | 2005-06-29 23:01:14 -0700 | [diff] [blame] | 22 | struct command { |
| 23 | struct command *next; |
Junio C Hamano | 1961433 | 2005-08-02 14:24:22 -0700 | [diff] [blame] | 24 | unsigned char updated; |
Linus Torvalds | eb1af2d | 2005-06-29 23:01:14 -0700 | [diff] [blame] | 25 | unsigned char old_sha1[20]; |
| 26 | unsigned char new_sha1[20]; |
Junio C Hamano | 2c04662 | 2005-08-29 12:41:03 -0700 | [diff] [blame] | 27 | char ref_name[0]; |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Linus Torvalds | 6da4016 | 2005-07-03 10:10:45 -0700 | [diff] [blame] | 30 | static struct command *commands = NULL; |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 31 | |
Linus Torvalds | f65fdf0 | 2005-06-30 11:04:59 -0700 | [diff] [blame] | 32 | static int is_all_zeroes(const char *hex) |
| 33 | { |
| 34 | int i; |
| 35 | for (i = 0; i < 40; i++) |
| 36 | if (*hex++ != '0') |
| 37 | return 0; |
| 38 | return 1; |
| 39 | } |
| 40 | |
Linus Torvalds | 2eca23d | 2005-06-30 10:15:22 -0700 | [diff] [blame] | 41 | static int verify_old_ref(const char *name, char *hex_contents) |
| 42 | { |
| 43 | int fd, ret; |
| 44 | char buffer[60]; |
| 45 | |
Linus Torvalds | f65fdf0 | 2005-06-30 11:04:59 -0700 | [diff] [blame] | 46 | if (is_all_zeroes(hex_contents)) |
| 47 | return 0; |
Linus Torvalds | 2eca23d | 2005-06-30 10:15:22 -0700 | [diff] [blame] | 48 | fd = open(name, O_RDONLY); |
| 49 | if (fd < 0) |
| 50 | return -1; |
| 51 | ret = read(fd, buffer, 40); |
| 52 | close(fd); |
| 53 | if (ret != 40) |
| 54 | return -1; |
| 55 | if (memcmp(buffer, hex_contents, 40)) |
| 56 | return -1; |
| 57 | return 0; |
| 58 | } |
| 59 | |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 60 | static char update_hook[] = "hooks/update"; |
| 61 | |
| 62 | static int run_update_hook(const char *refname, |
| 63 | char *old_hex, char *new_hex) |
| 64 | { |
| 65 | int code; |
| 66 | |
| 67 | if (access(update_hook, X_OK) < 0) |
| 68 | return 0; |
| 69 | code = run_command(update_hook, refname, old_hex, new_hex, NULL); |
| 70 | switch (code) { |
| 71 | case 0: |
| 72 | return 0; |
| 73 | case -ERR_RUN_COMMAND_FORK: |
| 74 | die("hook fork failed"); |
| 75 | case -ERR_RUN_COMMAND_EXEC: |
| 76 | die("hook execute failed"); |
| 77 | case -ERR_RUN_COMMAND_WAITPID: |
| 78 | die("waitpid failed"); |
| 79 | case -ERR_RUN_COMMAND_WAITPID_WRONG_PID: |
| 80 | die("waitpid is confused"); |
| 81 | case -ERR_RUN_COMMAND_WAITPID_SIGNAL: |
| 82 | fprintf(stderr, "%s died of signal", update_hook); |
| 83 | return -1; |
| 84 | case -ERR_RUN_COMMAND_WAITPID_NOEXIT: |
| 85 | die("%s died strangely", update_hook); |
| 86 | default: |
| 87 | error("%s exited with error code %d", update_hook, -code); |
| 88 | return -code; |
| 89 | } |
| 90 | } |
| 91 | |
Junio C Hamano | 1961433 | 2005-08-02 14:24:22 -0700 | [diff] [blame] | 92 | static int update(const char *name, |
| 93 | unsigned char *old_sha1, unsigned char *new_sha1) |
Linus Torvalds | 2eca23d | 2005-06-30 10:15:22 -0700 | [diff] [blame] | 94 | { |
| 95 | char new_hex[60], *old_hex, *lock_name; |
| 96 | int newfd, namelen, written; |
| 97 | |
| 98 | namelen = strlen(name); |
| 99 | lock_name = xmalloc(namelen + 10); |
| 100 | memcpy(lock_name, name, namelen); |
| 101 | memcpy(lock_name + namelen, ".lock", 6); |
| 102 | |
| 103 | strcpy(new_hex, sha1_to_hex(new_sha1)); |
Linus Torvalds | 2eca23d | 2005-06-30 10:15:22 -0700 | [diff] [blame] | 104 | old_hex = sha1_to_hex(old_sha1); |
| 105 | if (!has_sha1_file(new_sha1)) |
Junio C Hamano | 1961433 | 2005-08-02 14:24:22 -0700 | [diff] [blame] | 106 | return error("unpack should have generated %s, " |
| 107 | "but I can't find it!", new_hex); |
Linus Torvalds | 2eca23d | 2005-06-30 10:15:22 -0700 | [diff] [blame] | 108 | |
Junio C Hamano | 29f3b3d | 2005-08-02 18:27:57 -0700 | [diff] [blame] | 109 | safe_create_leading_directories(lock_name); |
| 110 | |
Junio C Hamano | f312de0 | 2005-07-06 01:21:46 -0700 | [diff] [blame] | 111 | newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666); |
Linus Torvalds | 2eca23d | 2005-06-30 10:15:22 -0700 | [diff] [blame] | 112 | if (newfd < 0) |
Junio C Hamano | 1961433 | 2005-08-02 14:24:22 -0700 | [diff] [blame] | 113 | return error("unable to create %s (%s)", |
| 114 | lock_name, strerror(errno)); |
Linus Torvalds | f65fdf0 | 2005-06-30 11:04:59 -0700 | [diff] [blame] | 115 | |
| 116 | /* Write the ref with an ending '\n' */ |
| 117 | new_hex[40] = '\n'; |
| 118 | new_hex[41] = 0; |
Linus Torvalds | 2eca23d | 2005-06-30 10:15:22 -0700 | [diff] [blame] | 119 | written = write(newfd, new_hex, 41); |
Linus Torvalds | f65fdf0 | 2005-06-30 11:04:59 -0700 | [diff] [blame] | 120 | /* Remove the '\n' again */ |
| 121 | new_hex[40] = 0; |
| 122 | |
Linus Torvalds | 2eca23d | 2005-06-30 10:15:22 -0700 | [diff] [blame] | 123 | close(newfd); |
| 124 | if (written != 41) { |
| 125 | unlink(lock_name); |
Junio C Hamano | 1961433 | 2005-08-02 14:24:22 -0700 | [diff] [blame] | 126 | return error("unable to write %s", lock_name); |
Linus Torvalds | 2eca23d | 2005-06-30 10:15:22 -0700 | [diff] [blame] | 127 | } |
| 128 | if (verify_old_ref(name, old_hex) < 0) { |
| 129 | unlink(lock_name); |
Junio C Hamano | 1961433 | 2005-08-02 14:24:22 -0700 | [diff] [blame] | 130 | return error("%s changed during push", name); |
Linus Torvalds | 2eca23d | 2005-06-30 10:15:22 -0700 | [diff] [blame] | 131 | } |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 132 | if (run_update_hook(name, old_hex, new_hex)) { |
| 133 | unlink(lock_name); |
Junio C Hamano | 1961433 | 2005-08-02 14:24:22 -0700 | [diff] [blame] | 134 | return error("hook declined to update %s\n", name); |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 135 | } |
| 136 | else if (rename(lock_name, name) < 0) { |
Linus Torvalds | 2eca23d | 2005-06-30 10:15:22 -0700 | [diff] [blame] | 137 | unlink(lock_name); |
Junio C Hamano | 1961433 | 2005-08-02 14:24:22 -0700 | [diff] [blame] | 138 | return error("unable to replace %s", name); |
Linus Torvalds | 2eca23d | 2005-06-30 10:15:22 -0700 | [diff] [blame] | 139 | } |
Junio C Hamano | 1961433 | 2005-08-02 14:24:22 -0700 | [diff] [blame] | 140 | else { |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 141 | fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex); |
Junio C Hamano | 1961433 | 2005-08-02 14:24:22 -0700 | [diff] [blame] | 142 | return 0; |
| 143 | } |
Linus Torvalds | 2eca23d | 2005-06-30 10:15:22 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Junio C Hamano | 1961433 | 2005-08-02 14:24:22 -0700 | [diff] [blame] | 146 | static char update_post_hook[] = "hooks/post-update"; |
| 147 | |
| 148 | static void run_update_post_hook(struct command *cmd) |
| 149 | { |
| 150 | struct command *cmd_p; |
| 151 | int argc; |
| 152 | char **argv; |
| 153 | |
| 154 | if (access(update_post_hook, X_OK) < 0) |
| 155 | return; |
| 156 | for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) { |
| 157 | if (!cmd_p->updated) |
| 158 | continue; |
| 159 | argc++; |
| 160 | } |
| 161 | argv = xmalloc(sizeof(*argv) * (1 + argc)); |
| 162 | argv[0] = update_post_hook; |
| 163 | |
| 164 | for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) { |
| 165 | if (!cmd_p->updated) |
| 166 | continue; |
| 167 | argv[argc] = xmalloc(strlen(cmd_p->ref_name) + 1); |
| 168 | strcpy(argv[argc], cmd_p->ref_name); |
| 169 | argc++; |
| 170 | } |
| 171 | argv[argc] = NULL; |
| 172 | run_command_v(argc, argv); |
| 173 | } |
Linus Torvalds | 2eca23d | 2005-06-30 10:15:22 -0700 | [diff] [blame] | 174 | |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 175 | /* |
| 176 | * This gets called after(if) we've successfully |
| 177 | * unpacked the data payload. |
| 178 | */ |
| 179 | static void execute_commands(void) |
| 180 | { |
Linus Torvalds | eb1af2d | 2005-06-29 23:01:14 -0700 | [diff] [blame] | 181 | struct command *cmd = commands; |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 182 | |
Linus Torvalds | eb1af2d | 2005-06-29 23:01:14 -0700 | [diff] [blame] | 183 | while (cmd) { |
Junio C Hamano | 1961433 | 2005-08-02 14:24:22 -0700 | [diff] [blame] | 184 | cmd->updated = !update(cmd->ref_name, |
| 185 | cmd->old_sha1, cmd->new_sha1); |
Linus Torvalds | eb1af2d | 2005-06-29 23:01:14 -0700 | [diff] [blame] | 186 | cmd = cmd->next; |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 187 | } |
Junio C Hamano | 1961433 | 2005-08-02 14:24:22 -0700 | [diff] [blame] | 188 | run_update_post_hook(commands); |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void read_head_info(void) |
| 192 | { |
Linus Torvalds | eb1af2d | 2005-06-29 23:01:14 -0700 | [diff] [blame] | 193 | struct command **p = &commands; |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 194 | for (;;) { |
| 195 | static char line[1000]; |
Linus Torvalds | eb1af2d | 2005-06-29 23:01:14 -0700 | [diff] [blame] | 196 | unsigned char old_sha1[20], new_sha1[20]; |
| 197 | struct command *cmd; |
| 198 | int len; |
| 199 | |
| 200 | len = packet_read_line(0, line, sizeof(line)); |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 201 | if (!len) |
| 202 | break; |
Linus Torvalds | eb1af2d | 2005-06-29 23:01:14 -0700 | [diff] [blame] | 203 | if (line[len-1] == '\n') |
| 204 | line[--len] = 0; |
| 205 | if (len < 83 || |
| 206 | line[40] != ' ' || |
| 207 | line[81] != ' ' || |
| 208 | get_sha1_hex(line, old_sha1) || |
| 209 | get_sha1_hex(line + 41, new_sha1)) |
| 210 | die("protocol error: expected old/new/ref, got '%s'", line); |
| 211 | cmd = xmalloc(sizeof(struct command) + len - 80); |
| 212 | memcpy(cmd->old_sha1, old_sha1, 20); |
| 213 | memcpy(cmd->new_sha1, new_sha1, 20); |
| 214 | memcpy(cmd->ref_name, line + 82, len - 81); |
| 215 | cmd->next = NULL; |
| 216 | *p = cmd; |
| 217 | p = &cmd->next; |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | static void unpack(void) |
| 222 | { |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 223 | int code = run_command(unpacker, NULL); |
| 224 | switch (code) { |
| 225 | case 0: |
A Large Angry SCM | c742b81 | 2005-08-01 10:05:57 -0400 | [diff] [blame] | 226 | return; |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 227 | case -ERR_RUN_COMMAND_FORK: |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 228 | die("unpack fork failed"); |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 229 | case -ERR_RUN_COMMAND_EXEC: |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 230 | die("unpack execute failed"); |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 231 | case -ERR_RUN_COMMAND_WAITPID: |
| 232 | die("waitpid failed"); |
| 233 | case -ERR_RUN_COMMAND_WAITPID_WRONG_PID: |
| 234 | die("waitpid is confused"); |
| 235 | case -ERR_RUN_COMMAND_WAITPID_SIGNAL: |
| 236 | die("%s died of signal", unpacker); |
| 237 | case -ERR_RUN_COMMAND_WAITPID_NOEXIT: |
| 238 | die("%s died strangely", unpacker); |
| 239 | default: |
| 240 | die("%s exited with error code %d", unpacker, -code); |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | int main(int argc, char **argv) |
| 245 | { |
Linus Torvalds | d0efc8a | 2005-06-30 12:28:24 -0700 | [diff] [blame] | 246 | int i; |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 247 | const char *dir = NULL; |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 248 | |
| 249 | argv++; |
| 250 | for (i = 1; i < argc; i++) { |
| 251 | const char *arg = *argv++; |
| 252 | |
| 253 | if (*arg == '-') { |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 254 | /* Do flag handling here */ |
| 255 | usage(receive_pack_usage); |
| 256 | } |
Linus Torvalds | d0efc8a | 2005-06-30 12:28:24 -0700 | [diff] [blame] | 257 | if (dir) |
| 258 | usage(receive_pack_usage); |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 259 | dir = arg; |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 260 | } |
| 261 | if (!dir) |
| 262 | usage(receive_pack_usage); |
| 263 | |
| 264 | /* chdir to the directory. If that fails, try appending ".git" */ |
| 265 | if (chdir(dir) < 0) { |
Linus Torvalds | 113b947 | 2005-07-08 16:22:22 -0700 | [diff] [blame] | 266 | if (chdir(mkpath("%s.git", dir)) < 0) |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 267 | die("unable to cd to %s", dir); |
| 268 | } |
| 269 | |
| 270 | /* If we have a ".git" directory, chdir to it */ |
| 271 | chdir(".git"); |
Jason Riedy | e72a7d4 | 2005-08-23 13:52:01 -0700 | [diff] [blame] | 272 | putenv("GIT_DIR=."); |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 273 | |
| 274 | if (access("objects", X_OK) < 0 || access("refs/heads", X_OK) < 0) |
| 275 | die("%s doesn't appear to be a git directory", dir); |
Linus Torvalds | 8a65ff7 | 2005-07-02 20:23:36 -0700 | [diff] [blame] | 276 | write_head_info(); |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 277 | |
| 278 | /* EOF */ |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 279 | packet_flush(1); |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 280 | |
| 281 | read_head_info(); |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 282 | if (commands) { |
| 283 | unpack(); |
| 284 | execute_commands(); |
| 285 | } |
Linus Torvalds | 575f497 | 2005-06-29 17:52:11 -0700 | [diff] [blame] | 286 | return 0; |
| 287 | } |