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\n", 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 if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5))
99 return error("refusing to create funny ref '%s' locally",
102 namelen = strlen(name);
103 lock_name = xmalloc(namelen + 10);
104 memcpy(lock_name, name, namelen);
105 memcpy(lock_name + namelen, ".lock", 6);
107 strcpy(new_hex, sha1_to_hex(new_sha1));
108 old_hex = sha1_to_hex(old_sha1);
109 if (!has_sha1_file(new_sha1))
110 return error("unpack should have generated %s, "
111 "but I can't find it!", new_hex);
113 safe_create_leading_directories(lock_name);
115 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
117 return error("unable to create %s (%s)",
118 lock_name, strerror(errno));
120 /* Write the ref with an ending '\n' */
123 written = write(newfd, new_hex, 41);
124 /* Remove the '\n' again */
130 return error("unable to write %s", lock_name);
132 if (verify_old_ref(name, old_hex) < 0) {
134 return error("%s changed during push", name);
136 if (run_update_hook(name, old_hex, new_hex)) {
138 return error("hook declined to update %s\n", name);
140 else if (rename(lock_name, name) < 0) {
142 return error("unable to replace %s", name);
145 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
150 static char update_post_hook[] = "hooks/post-update";
152 static void run_update_post_hook(struct command *cmd)
154 struct command *cmd_p;
158 if (access(update_post_hook, X_OK) < 0)
160 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
165 argv = xmalloc(sizeof(*argv) * (1 + argc));
166 argv[0] = update_post_hook;
168 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
171 argv[argc] = xmalloc(strlen(cmd_p->ref_name) + 1);
172 strcpy(argv[argc], cmd_p->ref_name);
176 run_command_v_opt(argc, argv, RUN_COMMAND_NO_STDIO);
180 * This gets called after(if) we've successfully
181 * unpacked the data payload.
183 static void execute_commands(void)
185 struct command *cmd = commands;
188 cmd->updated = !update(cmd->ref_name,
189 cmd->old_sha1, cmd->new_sha1);
192 run_update_post_hook(commands);
195 static void read_head_info(void)
197 struct command **p = &commands;
199 static char line[1000];
200 unsigned char old_sha1[20], new_sha1[20];
204 len = packet_read_line(0, line, sizeof(line));
207 if (line[len-1] == '\n')
212 get_sha1_hex(line, old_sha1) ||
213 get_sha1_hex(line + 41, new_sha1))
214 die("protocol error: expected old/new/ref, got '%s'", line);
215 cmd = xmalloc(sizeof(struct command) + len - 80);
216 memcpy(cmd->old_sha1, old_sha1, 20);
217 memcpy(cmd->new_sha1, new_sha1, 20);
218 memcpy(cmd->ref_name, line + 82, len - 81);
225 static void unpack(void)
227 int code = run_command(unpacker, NULL);
231 case -ERR_RUN_COMMAND_FORK:
232 die("unpack fork failed");
233 case -ERR_RUN_COMMAND_EXEC:
234 die("unpack execute failed");
235 case -ERR_RUN_COMMAND_WAITPID:
236 die("waitpid failed");
237 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
238 die("waitpid is confused");
239 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
240 die("%s died of signal", unpacker);
241 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
242 die("%s died strangely", unpacker);
244 die("%s exited with error code %d", unpacker, -code);
248 int main(int argc, char **argv)
254 for (i = 1; i < argc; i++) {
258 /* Do flag handling here */
259 usage(receive_pack_usage);
262 usage(receive_pack_usage);
266 usage(receive_pack_usage);
268 if(!enter_repo(dir, 0))
269 die("'%s': unable to chdir or not a git archive", dir);