receive-pack hooks updates.
[git.git] / receive-pack.c
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include "run-command.h"
5 #include <sys/wait.h>
6
7 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
8
9 static const char unpacker[] = "git-unpack-objects";
10
11 static int show_ref(const char *path, const unsigned char *sha1)
12 {
13         packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
14         return 0;
15 }
16
17 static void write_head_info(void)
18 {
19         for_each_ref(show_ref);
20 }
21
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];
28 };
29
30 static struct command *commands = NULL;
31
32 static 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
41 static int verify_old_ref(const char *name, char *hex_contents)
42 {
43         int fd, ret;
44         char buffer[60];
45
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;
58 }
59
60 static char update_hook[] = "hooks/update";
61
62 static 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
92 static int update(const char *name,
93                   unsigned char *old_sha1, unsigned char *new_sha1)
94 {
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));
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);
108
109         newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
110         if (newfd < 0)
111                 return error("unable to create %s (%s)",
112                              lock_name, strerror(errno));
113
114         /* Write the ref with an ending '\n' */
115         new_hex[40] = '\n';
116         new_hex[41] = 0;
117         written = write(newfd, new_hex, 41);
118         /* Remove the '\n' again */
119         new_hex[40] = 0;
120
121         close(newfd);
122         if (written != 41) {
123                 unlink(lock_name);
124                 return error("unable to write %s", lock_name);
125         }
126         if (verify_old_ref(name, old_hex) < 0) {
127                 unlink(lock_name);
128                 return error("%s changed during push", name);
129         }
130         if (run_update_hook(name, old_hex, new_hex)) {
131                 unlink(lock_name);
132                 return error("hook declined to update %s\n", name);
133         }
134         else if (rename(lock_name, name) < 0) {
135                 unlink(lock_name);
136                 return error("unable to replace %s", name);
137         }
138         else {
139                 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
140                 return 0;
141         }
142 }
143
144 static char update_post_hook[] = "hooks/post-update";
145
146 static void run_update_post_hook(struct command *cmd)
147 {
148         struct command *cmd_p;
149         int argc;
150         char **argv;
151
152         if (access(update_post_hook, X_OK) < 0)
153                 return;
154         for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
155                 if (!cmd_p->updated)
156                         continue;
157                 argc++;
158         }
159         argv = xmalloc(sizeof(*argv) * (1 + argc));
160         argv[0] = update_post_hook;
161
162         for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
163                 if (!cmd_p->updated)
164                         continue;
165                 argv[argc] = xmalloc(strlen(cmd_p->ref_name) + 1);
166                 strcpy(argv[argc], cmd_p->ref_name);
167                 argc++;
168         }
169         argv[argc] = NULL;
170         run_command_v(argc, argv);
171 }
172
173 /*
174  * This gets called after(if) we've successfully
175  * unpacked the data payload.
176  */
177 static void execute_commands(void)
178 {
179         struct command *cmd = commands;
180
181         while (cmd) {
182                 cmd->updated = !update(cmd->ref_name,
183                                        cmd->old_sha1, cmd->new_sha1);
184                 cmd = cmd->next;
185         }
186         run_update_post_hook(commands);
187 }
188
189 static void read_head_info(void)
190 {
191         struct command **p = &commands;
192         for (;;) {
193                 static char line[1000];
194                 unsigned char old_sha1[20], new_sha1[20];
195                 struct command *cmd;
196                 int len;
197
198                 len = packet_read_line(0, line, sizeof(line));
199                 if (!len)
200                         break;
201                 if (line[len-1] == '\n')
202                         line[--len] = 0;
203                 if (len < 83 ||
204                     line[40] != ' ' ||
205                     line[81] != ' ' ||
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);
213                 cmd->next = NULL;
214                 *p = cmd;
215                 p = &cmd->next;
216         }
217 }
218
219 static void unpack(void)
220 {
221         int code = run_command(unpacker, NULL);
222         switch (code) {
223         case 0:
224                 return;
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);
237         default:
238                 die("%s exited with error code %d", unpacker, -code);
239         }
240 }
241
242 int main(int argc, char **argv)
243 {
244         int i;
245         const char *dir = NULL;
246
247         argv++;
248         for (i = 1; i < argc; i++) {
249                 const char *arg = *argv++;
250
251                 if (*arg == '-') {
252                         /* Do flag handling here */
253                         usage(receive_pack_usage);
254                 }
255                 if (dir)
256                         usage(receive_pack_usage);
257                 dir = arg;
258         }
259         if (!dir)
260                 usage(receive_pack_usage);
261
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);
266         }
267
268         /* If we have a ".git" directory, chdir to it */
269         chdir(".git");
270         setenv("GIT_DIR", ".", 1);
271
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);
274         write_head_info();
275
276         /* EOF */
277         packet_flush(1);
278
279         read_head_info();
280         if (commands) {
281                 unpack();
282                 execute_commands();
283         }
284         return 0;
285 }