]> git.ipfire.org Git - thirdparty/git.git/blame - receive-pack.c
Merge refs/heads/portable from http://www.cs.berkeley.edu/~ejr/gits/git.git
[thirdparty/git.git] / receive-pack.c
CommitLineData
575f4974 1#include "cache.h"
8a65ff76 2#include "refs.h"
f3a3214e 3#include "pkt-line.h"
b1bf95bb 4#include "run-command.h"
575f4974
LT
5#include <sys/wait.h>
6
d0efc8a7 7static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
575f4974 8
b1bf95bb 9static const char unpacker[] = "git-unpack-objects";
575f4974 10
944d8589 11static int show_ref(const char *path, const unsigned char *sha1)
575f4974 12{
f3a3214e 13 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
8a65ff76 14 return 0;
575f4974
LT
15}
16
8a65ff76 17static void write_head_info(void)
575f4974 18{
8a65ff76 19 for_each_ref(show_ref);
575f4974
LT
20}
21
eb1af2df
LT
22struct command {
23 struct command *next;
19614330 24 unsigned char updated;
eb1af2df
LT
25 unsigned char old_sha1[20];
26 unsigned char new_sha1[20];
6c5f9baa 27 char ref_name[];
575f4974
LT
28};
29
6da4016a 30static struct command *commands = NULL;
575f4974 31
f65fdf04
LT
32static 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
2eca23da
LT
41static int verify_old_ref(const char *name, char *hex_contents)
42{
43 int fd, ret;
44 char buffer[60];
45
f65fdf04
LT
46 if (is_all_zeroes(hex_contents))
47 return 0;
2eca23da
LT
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
b1bf95bb
JW
60static char update_hook[] = "hooks/update";
61
62static 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
19614330
JH
92static int update(const char *name,
93 unsigned char *old_sha1, unsigned char *new_sha1)
2eca23da
LT
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));
2eca23da
LT
104 old_hex = sha1_to_hex(old_sha1);
105 if (!has_sha1_file(new_sha1))
19614330
JH
106 return error("unpack should have generated %s, "
107 "but I can't find it!", new_hex);
2eca23da 108
29f3b3de
JH
109 safe_create_leading_directories(lock_name);
110
f312de01 111 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
2eca23da 112 if (newfd < 0)
19614330
JH
113 return error("unable to create %s (%s)",
114 lock_name, strerror(errno));
f65fdf04
LT
115
116 /* Write the ref with an ending '\n' */
117 new_hex[40] = '\n';
118 new_hex[41] = 0;
2eca23da 119 written = write(newfd, new_hex, 41);
f65fdf04
LT
120 /* Remove the '\n' again */
121 new_hex[40] = 0;
122
2eca23da
LT
123 close(newfd);
124 if (written != 41) {
125 unlink(lock_name);
19614330 126 return error("unable to write %s", lock_name);
2eca23da
LT
127 }
128 if (verify_old_ref(name, old_hex) < 0) {
129 unlink(lock_name);
19614330 130 return error("%s changed during push", name);
2eca23da 131 }
b1bf95bb
JW
132 if (run_update_hook(name, old_hex, new_hex)) {
133 unlink(lock_name);
19614330 134 return error("hook declined to update %s\n", name);
b1bf95bb
JW
135 }
136 else if (rename(lock_name, name) < 0) {
2eca23da 137 unlink(lock_name);
19614330 138 return error("unable to replace %s", name);
2eca23da 139 }
19614330 140 else {
b1bf95bb 141 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
19614330
JH
142 return 0;
143 }
2eca23da
LT
144}
145
19614330
JH
146static char update_post_hook[] = "hooks/post-update";
147
148static 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}
2eca23da 174
575f4974
LT
175/*
176 * This gets called after(if) we've successfully
177 * unpacked the data payload.
178 */
179static void execute_commands(void)
180{
eb1af2df
LT
181 struct command *cmd = commands;
182
183 while (cmd) {
19614330
JH
184 cmd->updated = !update(cmd->ref_name,
185 cmd->old_sha1, cmd->new_sha1);
eb1af2df 186 cmd = cmd->next;
575f4974 187 }
19614330 188 run_update_post_hook(commands);
575f4974
LT
189}
190
191static void read_head_info(void)
192{
eb1af2df 193 struct command **p = &commands;
575f4974
LT
194 for (;;) {
195 static char line[1000];
eb1af2df
LT
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));
575f4974
LT
201 if (!len)
202 break;
eb1af2df
LT
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;
575f4974
LT
218 }
219}
220
221static void unpack(void)
222{
b1bf95bb
JW
223 int code = run_command(unpacker, NULL);
224 switch (code) {
225 case 0:
c742b813 226 return;
b1bf95bb 227 case -ERR_RUN_COMMAND_FORK:
575f4974 228 die("unpack fork failed");
b1bf95bb 229 case -ERR_RUN_COMMAND_EXEC:
575f4974 230 die("unpack execute failed");
b1bf95bb
JW
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);
575f4974
LT
241 }
242}
243
244int main(int argc, char **argv)
245{
d0efc8a7 246 int i;
575f4974 247 const char *dir = NULL;
575f4974
LT
248
249 argv++;
250 for (i = 1; i < argc; i++) {
251 const char *arg = *argv++;
252
253 if (*arg == '-') {
575f4974
LT
254 /* Do flag handling here */
255 usage(receive_pack_usage);
256 }
d0efc8a7
LT
257 if (dir)
258 usage(receive_pack_usage);
575f4974 259 dir = arg;
575f4974
LT
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) {
113b9475 266 if (chdir(mkpath("%s.git", dir)) < 0)
575f4974
LT
267 die("unable to cd to %s", dir);
268 }
269
270 /* If we have a ".git" directory, chdir to it */
271 chdir(".git");
e72a7d45 272 putenv("GIT_DIR=.");
575f4974
LT
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);
8a65ff76 276 write_head_info();
575f4974
LT
277
278 /* EOF */
f3a3214e 279 packet_flush(1);
575f4974
LT
280
281 read_head_info();
7f8e9828
LT
282 if (commands) {
283 unpack();
284 execute_commands();
285 }
575f4974
LT
286 return 0;
287}