4 #include "run-command.h"
7 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
9 static const char unpacker[] = "git-unpack-objects";
11 static int show_ref(const char *path, const unsigned char *sha1)
13 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
17 static void write_head_info(void)
19 for_each_ref(show_ref);
24 unsigned char updated;
25 unsigned char old_sha1[20];
26 unsigned char new_sha1[20];
30 static struct command *commands = NULL;
32 static int is_all_zeroes(const char *hex)
35 for (i = 0; i < 40; i++)
41 static int verify_old_ref(const char *name, char *hex_contents)
46 if (is_all_zeroes(hex_contents))
48 fd = open(name, O_RDONLY);
51 ret = read(fd, buffer, 40);
55 if (memcmp(buffer, hex_contents, 40))
60 static char update_hook[] = "hooks/update";
62 static int run_update_hook(const char *refname,
63 char *old_hex, char *new_hex)
67 if (access(update_hook, X_OK) < 0)
69 code = run_command(update_hook, refname, old_hex, new_hex, NULL);
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);
84 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
85 die("%s died strangely", update_hook);
87 error("%s exited with error code %d", update_hook, -code);
92 static int update(const char *name,
93 unsigned char *old_sha1, unsigned char *new_sha1)
95 char new_hex[60], *old_hex, *lock_name;
96 int newfd, namelen, written;
98 namelen = strlen(name);
99 lock_name = xmalloc(namelen + 10);
100 memcpy(lock_name, name, namelen);
101 memcpy(lock_name + namelen, ".lock", 6);
103 strcpy(new_hex, sha1_to_hex(new_sha1));
104 old_hex = sha1_to_hex(old_sha1);
105 if (!has_sha1_file(new_sha1))
106 return error("unpack should have generated %s, "
107 "but I can't find it!", new_hex);
109 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
111 return error("unable to create %s (%s)",
112 lock_name, strerror(errno));
114 /* Write the ref with an ending '\n' */
117 written = write(newfd, new_hex, 41);
118 /* Remove the '\n' again */
124 return error("unable to write %s", lock_name);
126 if (verify_old_ref(name, old_hex) < 0) {
128 return error("%s changed during push", name);
130 if (run_update_hook(name, old_hex, new_hex)) {
132 return error("hook declined to update %s\n", name);
134 else if (rename(lock_name, name) < 0) {
136 return error("unable to replace %s", name);
139 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
144 static char update_post_hook[] = "hooks/post-update";
146 static void run_update_post_hook(struct command *cmd)
148 struct command *cmd_p;
152 if (access(update_post_hook, X_OK) < 0)
154 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
159 argv = xmalloc(sizeof(*argv) * (1 + argc));
160 argv[0] = update_post_hook;
162 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
165 argv[argc] = xmalloc(strlen(cmd_p->ref_name) + 1);
166 strcpy(argv[argc], cmd_p->ref_name);
170 run_command_v(argc, argv);
174 * This gets called after(if) we've successfully
175 * unpacked the data payload.
177 static void execute_commands(void)
179 struct command *cmd = commands;
182 cmd->updated = !update(cmd->ref_name,
183 cmd->old_sha1, cmd->new_sha1);
186 run_update_post_hook(commands);
189 static void read_head_info(void)
191 struct command **p = &commands;
193 static char line[1000];
194 unsigned char old_sha1[20], new_sha1[20];
198 len = packet_read_line(0, line, sizeof(line));
201 if (line[len-1] == '\n')
206 get_sha1_hex(line, old_sha1) ||
207 get_sha1_hex(line + 41, new_sha1))
208 die("protocol error: expected old/new/ref, got '%s'", line);
209 cmd = xmalloc(sizeof(struct command) + len - 80);
210 memcpy(cmd->old_sha1, old_sha1, 20);
211 memcpy(cmd->new_sha1, new_sha1, 20);
212 memcpy(cmd->ref_name, line + 82, len - 81);
219 static void unpack(void)
221 int code = run_command(unpacker, NULL);
225 case -ERR_RUN_COMMAND_FORK:
226 die("unpack fork failed");
227 case -ERR_RUN_COMMAND_EXEC:
228 die("unpack execute failed");
229 case -ERR_RUN_COMMAND_WAITPID:
230 die("waitpid failed");
231 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
232 die("waitpid is confused");
233 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
234 die("%s died of signal", unpacker);
235 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
236 die("%s died strangely", unpacker);
238 die("%s exited with error code %d", unpacker, -code);
242 int main(int argc, char **argv)
245 const char *dir = NULL;
248 for (i = 1; i < argc; i++) {
249 const char *arg = *argv++;
252 /* Do flag handling here */
253 usage(receive_pack_usage);
256 usage(receive_pack_usage);
260 usage(receive_pack_usage);
262 /* chdir to the directory. If that fails, try appending ".git" */
263 if (chdir(dir) < 0) {
264 if (chdir(mkpath("%s.git", dir)) < 0)
265 die("unable to cd to %s", dir);
268 /* If we have a ".git" directory, chdir to it */
270 setenv("GIT_DIR", ".", 1);
272 if (access("objects", X_OK) < 0 || access("refs/heads", X_OK) < 0)
273 die("%s doesn't appear to be a git directory", dir);