git wrapper: more careful argument stuffing
[alt-git.git] / receive-pack.c
blob18735061206a813d523fdf29dd3e5b06899f1a1d
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include "run-command.h"
5 #include <sys/wait.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);
14 return 0;
17 static void write_head_info(void)
19 for_each_ref(show_ref);
22 struct command {
23 struct command *next;
24 unsigned char updated;
25 unsigned char old_sha1[20];
26 unsigned char new_sha1[20];
27 char ref_name[0];
30 static struct command *commands = NULL;
32 static int is_all_zeroes(const char *hex)
34 int i;
35 for (i = 0; i < 40; i++)
36 if (*hex++ != '0')
37 return 0;
38 return 1;
41 static int verify_old_ref(const char *name, char *hex_contents)
43 int fd, ret;
44 char buffer[60];
46 if (is_all_zeroes(hex_contents))
47 return 0;
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;
60 static char update_hook[] = "hooks/update";
62 static int run_update_hook(const char *refname,
63 char *old_hex, char *new_hex)
65 int code;
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;
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",
100 name);
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);
116 if (newfd < 0)
117 return error("unable to create %s (%s)",
118 lock_name, strerror(errno));
120 /* Write the ref with an ending '\n' */
121 new_hex[40] = '\n';
122 new_hex[41] = 0;
123 written = write(newfd, new_hex, 41);
124 /* Remove the '\n' again */
125 new_hex[40] = 0;
127 close(newfd);
128 if (written != 41) {
129 unlink(lock_name);
130 return error("unable to write %s", lock_name);
132 if (verify_old_ref(name, old_hex) < 0) {
133 unlink(lock_name);
134 return error("%s changed during push", name);
136 if (run_update_hook(name, old_hex, new_hex)) {
137 unlink(lock_name);
138 return error("hook declined to update %s\n", name);
140 else if (rename(lock_name, name) < 0) {
141 unlink(lock_name);
142 return error("unable to replace %s", name);
144 else {
145 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
146 return 0;
150 static char update_post_hook[] = "hooks/post-update";
152 static void run_update_post_hook(struct command *cmd)
154 struct command *cmd_p;
155 int argc;
156 char **argv;
158 if (access(update_post_hook, X_OK) < 0)
159 return;
160 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
161 if (!cmd_p->updated)
162 continue;
163 argc++;
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) {
169 if (!cmd_p->updated)
170 continue;
171 argv[argc] = xmalloc(strlen(cmd_p->ref_name) + 1);
172 strcpy(argv[argc], cmd_p->ref_name);
173 argc++;
175 argv[argc] = NULL;
176 run_command_v(argc, argv);
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;
187 while (cmd) {
188 cmd->updated = !update(cmd->ref_name,
189 cmd->old_sha1, cmd->new_sha1);
190 cmd = cmd->next;
192 run_update_post_hook(commands);
195 static void read_head_info(void)
197 struct command **p = &commands;
198 for (;;) {
199 static char line[1000];
200 unsigned char old_sha1[20], new_sha1[20];
201 struct command *cmd;
202 int len;
204 len = packet_read_line(0, line, sizeof(line));
205 if (!len)
206 break;
207 if (line[len-1] == '\n')
208 line[--len] = 0;
209 if (len < 83 ||
210 line[40] != ' ' ||
211 line[81] != ' ' ||
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);
219 cmd->next = NULL;
220 *p = cmd;
221 p = &cmd->next;
225 static void unpack(void)
227 int code = run_command(unpacker, NULL);
228 switch (code) {
229 case 0:
230 return;
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);
243 default:
244 die("%s exited with error code %d", unpacker, -code);
248 int main(int argc, char **argv)
250 int i;
251 char *dir = NULL;
253 argv++;
254 for (i = 1; i < argc; i++) {
255 char *arg = *argv++;
257 if (*arg == '-') {
258 /* Do flag handling here */
259 usage(receive_pack_usage);
261 if (dir)
262 usage(receive_pack_usage);
263 dir = arg;
265 if (!dir)
266 usage(receive_pack_usage);
268 if(!enter_repo(dir, 0))
269 die("'%s': unable to chdir or not a git archive", dir);
271 write_head_info();
273 /* EOF */
274 packet_flush(1);
276 read_head_info();
277 if (commands) {
278 unpack();
279 execute_commands();
281 return 0;