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 safe_create_leading_directories(lock_name);
111 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
113 return error("unable to create %s (%s)",
114 lock_name, strerror(errno));
116 /* Write the ref with an ending '\n' */
119 written = write(newfd, new_hex, 41);
120 /* Remove the '\n' again */
126 return error("unable to write %s", lock_name);
128 if (verify_old_ref(name, old_hex) < 0) {
130 return error("%s changed during push", name);
132 if (run_update_hook(name, old_hex, new_hex)) {
134 return error("hook declined to update %s\n", name);
136 else if (rename(lock_name, name) < 0) {
138 return error("unable to replace %s", name);
141 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
146 static char update_post_hook[] = "hooks/post-update";
148 static void run_update_post_hook(struct command *cmd)
150 struct command *cmd_p;
154 if (access(update_post_hook, X_OK) < 0)
156 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
161 argv = xmalloc(sizeof(*argv) * (1 + argc));
162 argv[0] = update_post_hook;
164 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
167 argv[argc] = xmalloc(strlen(cmd_p->ref_name) + 1);
168 strcpy(argv[argc], cmd_p->ref_name);
172 run_command_v(argc, argv);
176 * This gets called after(if) we've successfully
177 * unpacked the data payload.
179 static void execute_commands(void)
181 struct command *cmd = commands;
184 cmd->updated = !update(cmd->ref_name,
185 cmd->old_sha1, cmd->new_sha1);
188 run_update_post_hook(commands);
191 static void read_head_info(void)
193 struct command **p = &commands;
195 static char line[1000];
196 unsigned char old_sha1[20], new_sha1[20];
200 len = packet_read_line(0, line, sizeof(line));
203 if (line[len-1] == '\n')
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);
221 static void unpack(void)
223 int code = run_command(unpacker, NULL);
227 case -ERR_RUN_COMMAND_FORK:
228 die("unpack fork failed");
229 case -ERR_RUN_COMMAND_EXEC:
230 die("unpack execute failed");
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);
240 die("%s exited with error code %d", unpacker, -code);
244 int main(int argc, char **argv)
247 const char *dir = NULL;
250 for (i = 1; i < argc; i++) {
251 const char *arg = *argv++;
254 /* Do flag handling here */
255 usage(receive_pack_usage);
258 usage(receive_pack_usage);
262 usage(receive_pack_usage);
264 /* chdir to the directory. If that fails, try appending ".git" */
265 if (chdir(dir) < 0) {
266 if (chdir(mkpath("%s.git", dir)) < 0)
267 die("unable to cd to %s", dir);
270 /* If we have a ".git" directory, chdir to it */
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);