blob: 06857eb77fef93788f54d17cca68924f5213c9db [file] [log] [blame]
Linus Torvalds575f4972005-06-29 17:52:11 -07001#include "cache.h"
Linus Torvalds8a65ff72005-07-02 20:23:36 -07002#include "refs.h"
Linus Torvaldsf3a32142005-06-29 20:50:15 -07003#include "pkt-line.h"
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +02004#include "run-command.h"
Linus Torvalds575f4972005-06-29 17:52:11 -07005#include <sys/wait.h>
6
Linus Torvaldsd0efc8a2005-06-30 12:28:24 -07007static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
Linus Torvalds575f4972005-06-29 17:52:11 -07008
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +02009static const char unpacker[] = "git-unpack-objects";
Linus Torvalds575f4972005-06-29 17:52:11 -070010
Linus Torvalds944d8582005-07-03 10:01:38 -070011static int show_ref(const char *path, const unsigned char *sha1)
Linus Torvalds575f4972005-06-29 17:52:11 -070012{
Linus Torvaldsf3a32142005-06-29 20:50:15 -070013 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
Linus Torvalds8a65ff72005-07-02 20:23:36 -070014 return 0;
Linus Torvalds575f4972005-06-29 17:52:11 -070015}
16
Linus Torvalds8a65ff72005-07-02 20:23:36 -070017static void write_head_info(void)
Linus Torvalds575f4972005-06-29 17:52:11 -070018{
Linus Torvalds8a65ff72005-07-02 20:23:36 -070019 for_each_ref(show_ref);
Linus Torvalds575f4972005-06-29 17:52:11 -070020}
21
Linus Torvaldseb1af2d2005-06-29 23:01:14 -070022struct command {
23 struct command *next;
Junio C Hamano19614332005-08-02 14:24:22 -070024 unsigned char updated;
Linus Torvaldseb1af2d2005-06-29 23:01:14 -070025 unsigned char old_sha1[20];
26 unsigned char new_sha1[20];
Junio C Hamano2c046622005-08-29 12:41:03 -070027 char ref_name[0];
Linus Torvalds575f4972005-06-29 17:52:11 -070028};
29
Linus Torvalds6da40162005-07-03 10:10:45 -070030static struct command *commands = NULL;
Linus Torvalds575f4972005-06-29 17:52:11 -070031
Linus Torvaldsf65fdf02005-06-30 11:04:59 -070032static 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 Torvalds2eca23d2005-06-30 10:15:22 -070041static int verify_old_ref(const char *name, char *hex_contents)
42{
43 int fd, ret;
44 char buffer[60];
45
Linus Torvaldsf65fdf02005-06-30 11:04:59 -070046 if (is_all_zeroes(hex_contents))
47 return 0;
Linus Torvalds2eca23d2005-06-30 10:15:22 -070048 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 Weidendorferb1bf95b2005-07-31 21:17:43 +020060static char update_hook[] = "hooks/update";
61
62static 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 Hamano19614332005-08-02 14:24:22 -070092static int update(const char *name,
93 unsigned char *old_sha1, unsigned char *new_sha1)
Linus Torvalds2eca23d2005-06-30 10:15:22 -070094{
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 Torvalds2eca23d2005-06-30 10:15:22 -0700104 old_hex = sha1_to_hex(old_sha1);
105 if (!has_sha1_file(new_sha1))
Junio C Hamano19614332005-08-02 14:24:22 -0700106 return error("unpack should have generated %s, "
107 "but I can't find it!", new_hex);
Linus Torvalds2eca23d2005-06-30 10:15:22 -0700108
Junio C Hamano29f3b3d2005-08-02 18:27:57 -0700109 safe_create_leading_directories(lock_name);
110
Junio C Hamanof312de02005-07-06 01:21:46 -0700111 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
Linus Torvalds2eca23d2005-06-30 10:15:22 -0700112 if (newfd < 0)
Junio C Hamano19614332005-08-02 14:24:22 -0700113 return error("unable to create %s (%s)",
114 lock_name, strerror(errno));
Linus Torvaldsf65fdf02005-06-30 11:04:59 -0700115
116 /* Write the ref with an ending '\n' */
117 new_hex[40] = '\n';
118 new_hex[41] = 0;
Linus Torvalds2eca23d2005-06-30 10:15:22 -0700119 written = write(newfd, new_hex, 41);
Linus Torvaldsf65fdf02005-06-30 11:04:59 -0700120 /* Remove the '\n' again */
121 new_hex[40] = 0;
122
Linus Torvalds2eca23d2005-06-30 10:15:22 -0700123 close(newfd);
124 if (written != 41) {
125 unlink(lock_name);
Junio C Hamano19614332005-08-02 14:24:22 -0700126 return error("unable to write %s", lock_name);
Linus Torvalds2eca23d2005-06-30 10:15:22 -0700127 }
128 if (verify_old_ref(name, old_hex) < 0) {
129 unlink(lock_name);
Junio C Hamano19614332005-08-02 14:24:22 -0700130 return error("%s changed during push", name);
Linus Torvalds2eca23d2005-06-30 10:15:22 -0700131 }
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200132 if (run_update_hook(name, old_hex, new_hex)) {
133 unlink(lock_name);
Junio C Hamano19614332005-08-02 14:24:22 -0700134 return error("hook declined to update %s\n", name);
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200135 }
136 else if (rename(lock_name, name) < 0) {
Linus Torvalds2eca23d2005-06-30 10:15:22 -0700137 unlink(lock_name);
Junio C Hamano19614332005-08-02 14:24:22 -0700138 return error("unable to replace %s", name);
Linus Torvalds2eca23d2005-06-30 10:15:22 -0700139 }
Junio C Hamano19614332005-08-02 14:24:22 -0700140 else {
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200141 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
Junio C Hamano19614332005-08-02 14:24:22 -0700142 return 0;
143 }
Linus Torvalds2eca23d2005-06-30 10:15:22 -0700144}
145
Junio C Hamano19614332005-08-02 14:24:22 -0700146static char update_post_hook[] = "hooks/post-update";
147
148static 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 Torvalds2eca23d2005-06-30 10:15:22 -0700174
Linus Torvalds575f4972005-06-29 17:52:11 -0700175/*
176 * This gets called after(if) we've successfully
177 * unpacked the data payload.
178 */
179static void execute_commands(void)
180{
Linus Torvaldseb1af2d2005-06-29 23:01:14 -0700181 struct command *cmd = commands;
Linus Torvalds575f4972005-06-29 17:52:11 -0700182
Linus Torvaldseb1af2d2005-06-29 23:01:14 -0700183 while (cmd) {
Junio C Hamano19614332005-08-02 14:24:22 -0700184 cmd->updated = !update(cmd->ref_name,
185 cmd->old_sha1, cmd->new_sha1);
Linus Torvaldseb1af2d2005-06-29 23:01:14 -0700186 cmd = cmd->next;
Linus Torvalds575f4972005-06-29 17:52:11 -0700187 }
Junio C Hamano19614332005-08-02 14:24:22 -0700188 run_update_post_hook(commands);
Linus Torvalds575f4972005-06-29 17:52:11 -0700189}
190
191static void read_head_info(void)
192{
Linus Torvaldseb1af2d2005-06-29 23:01:14 -0700193 struct command **p = &commands;
Linus Torvalds575f4972005-06-29 17:52:11 -0700194 for (;;) {
195 static char line[1000];
Linus Torvaldseb1af2d2005-06-29 23:01:14 -0700196 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 Torvalds575f4972005-06-29 17:52:11 -0700201 if (!len)
202 break;
Linus Torvaldseb1af2d2005-06-29 23:01:14 -0700203 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 Torvalds575f4972005-06-29 17:52:11 -0700218 }
219}
220
221static void unpack(void)
222{
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200223 int code = run_command(unpacker, NULL);
224 switch (code) {
225 case 0:
A Large Angry SCMc742b812005-08-01 10:05:57 -0400226 return;
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200227 case -ERR_RUN_COMMAND_FORK:
Linus Torvalds575f4972005-06-29 17:52:11 -0700228 die("unpack fork failed");
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200229 case -ERR_RUN_COMMAND_EXEC:
Linus Torvalds575f4972005-06-29 17:52:11 -0700230 die("unpack execute failed");
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200231 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 Torvalds575f4972005-06-29 17:52:11 -0700241 }
242}
243
244int main(int argc, char **argv)
245{
Linus Torvaldsd0efc8a2005-06-30 12:28:24 -0700246 int i;
Linus Torvalds575f4972005-06-29 17:52:11 -0700247 const char *dir = NULL;
Linus Torvalds575f4972005-06-29 17:52:11 -0700248
249 argv++;
250 for (i = 1; i < argc; i++) {
251 const char *arg = *argv++;
252
253 if (*arg == '-') {
Linus Torvalds575f4972005-06-29 17:52:11 -0700254 /* Do flag handling here */
255 usage(receive_pack_usage);
256 }
Linus Torvaldsd0efc8a2005-06-30 12:28:24 -0700257 if (dir)
258 usage(receive_pack_usage);
Linus Torvalds575f4972005-06-29 17:52:11 -0700259 dir = arg;
Linus Torvalds575f4972005-06-29 17:52:11 -0700260 }
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 Torvalds113b9472005-07-08 16:22:22 -0700266 if (chdir(mkpath("%s.git", dir)) < 0)
Linus Torvalds575f4972005-06-29 17:52:11 -0700267 die("unable to cd to %s", dir);
268 }
269
270 /* If we have a ".git" directory, chdir to it */
271 chdir(".git");
Jason Riedye72a7d42005-08-23 13:52:01 -0700272 putenv("GIT_DIR=.");
Linus Torvalds575f4972005-06-29 17:52:11 -0700273
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 Torvalds8a65ff72005-07-02 20:23:36 -0700276 write_head_info();
Linus Torvalds575f4972005-06-29 17:52:11 -0700277
278 /* EOF */
Linus Torvaldsf3a32142005-06-29 20:50:15 -0700279 packet_flush(1);
Linus Torvalds575f4972005-06-29 17:52:11 -0700280
281 read_head_info();
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700282 if (commands) {
283 unpack();
284 execute_commands();
285 }
Linus Torvalds575f4972005-06-29 17:52:11 -0700286 return 0;
287}