[PATCH] fetch.c: Remove call to parse_object() from process()
[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         safe_create_leading_directories(lock_name);
110
111         newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
112         if (newfd < 0)
113                 return error("unable to create %s (%s)",
114                              lock_name, strerror(errno));
115
116         /* Write the ref with an ending '\n' */
117         new_hex[40] = '\n';
118         new_hex[41] = 0;
119         written = write(newfd, new_hex, 41);
120         /* Remove the '\n' again */
121         new_hex[40] = 0;
122
123         close(newfd);
124         if (written != 41) {
125                 unlink(lock_name);
126                 return error("unable to write %s", lock_name);
127         }
128         if (verify_old_ref(name, old_hex) < 0) {
129                 unlink(lock_name);
130                 return error("%s changed during push", name);
131         }
132         if (run_update_hook(name, old_hex, new_hex)) {
133                 unlink(lock_name);
134                 return error("hook declined to update %s\n", name);
135         }
136         else if (rename(lock_name, name) < 0) {
137                 unlink(lock_name);
138                 return error("unable to replace %s", name);
139         }
140         else {
141                 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
142                 return 0;
143         }
144 }
145
146 static char update_post_hook[] = "hooks/post-update";
147
148 static void run_update_post_hook(struct command *cmd)
149 {
150         struct command *cmd_p;
151         int argc;
152         char **argv;
153
154         if (access(update_post_hook, X_OK) < 0)
155                 return;
156         for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
157                 if (!cmd_p->updated)
158                         continue;
159                 argc++;
160         }
161         argv = xmalloc(sizeof(*argv) * (1 + argc));
162         argv[0] = update_post_hook;
163
164         for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
165                 if (!cmd_p->updated)
166                         continue;
167                 argv[argc] = xmalloc(strlen(cmd_p->ref_name) + 1);
168                 strcpy(argv[argc], cmd_p->ref_name);
169                 argc++;
170         }
171         argv[argc] = NULL;
172         run_command_v(argc, argv);
173 }
174
175 /*
176  * This gets called after(if) we've successfully
177  * unpacked the data payload.
178  */
179 static void execute_commands(void)
180 {
181         struct command *cmd = commands;
182
183         while (cmd) {
184                 cmd->updated = !update(cmd->ref_name,
185                                        cmd->old_sha1, cmd->new_sha1);
186                 cmd = cmd->next;
187         }
188         run_update_post_hook(commands);
189 }
190
191 static void read_head_info(void)
192 {
193         struct command **p = &commands;
194         for (;;) {
195                 static char line[1000];
196                 unsigned char old_sha1[20], new_sha1[20];
197                 struct command *cmd;
198                 int len;
199
200                 len = packet_read_line(0, line, sizeof(line));
201                 if (!len)
202                         break;
203                 if (line[len-1] == '\n')
204                         line[--len] = 0;
205                 if (len < 83 ||
206                     line[40] != ' ' ||
207                     line[81] != ' ' ||
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);
215                 cmd->next = NULL;
216                 *p = cmd;
217                 p = &cmd->next;
218         }
219 }
220
221 static void unpack(void)
222 {
223         int code = run_command(unpacker, NULL);
224         switch (code) {
225         case 0:
226                 return;
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);
239         default:
240                 die("%s exited with error code %d", unpacker, -code);
241         }
242 }
243
244 int main(int argc, char **argv)
245 {
246         int i;
247         const char *dir = NULL;
248
249         argv++;
250         for (i = 1; i < argc; i++) {
251                 const char *arg = *argv++;
252
253                 if (*arg == '-') {
254                         /* Do flag handling here */
255                         usage(receive_pack_usage);
256                 }
257                 if (dir)
258                         usage(receive_pack_usage);
259                 dir = arg;
260         }
261         if (!dir)
262                 usage(receive_pack_usage);
263
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);
268         }
269
270         /* If we have a ".git" directory, chdir to it */
271         chdir(".git");
272         putenv("GIT_DIR=.");
273
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);
276         write_head_info();
277
278         /* EOF */
279         packet_flush(1);
280
281         read_head_info();
282         if (commands) {
283                 unpack();
284                 execute_commands();
285         }
286         return 0;
287 }