Add revspec documentation for ':path', ':[0-3]:path' and git-describe
[git/jrn.git] / receive-pack.c
blobea2dbd4e3398ca90823dcd453cbb52c5dcea135c
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include "run-command.h"
5 #include "commit.h"
6 #include "object.h"
8 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
10 static const char *unpacker[] = { "unpack-objects", NULL };
12 static int report_status;
14 static char capabilities[] = "report-status";
15 static int capabilities_sent;
17 static int show_ref(const char *path, const unsigned char *sha1)
19 if (capabilities_sent)
20 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
21 else
22 packet_write(1, "%s %s%c%s\n",
23 sha1_to_hex(sha1), path, 0, capabilities);
24 capabilities_sent = 1;
25 return 0;
28 static void write_head_info(void)
30 for_each_ref(show_ref);
31 if (!capabilities_sent)
32 show_ref("capabilities^{}", null_sha1);
36 struct command {
37 struct command *next;
38 const char *error_string;
39 unsigned char old_sha1[20];
40 unsigned char new_sha1[20];
41 char ref_name[FLEX_ARRAY]; /* more */
44 static struct command *commands;
46 static int is_all_zeroes(const char *hex)
48 int i;
49 for (i = 0; i < 40; i++)
50 if (*hex++ != '0')
51 return 0;
52 return 1;
55 static int verify_old_ref(const char *name, char *hex_contents)
57 int fd, ret;
58 char buffer[60];
60 if (is_all_zeroes(hex_contents))
61 return 0;
62 fd = open(name, O_RDONLY);
63 if (fd < 0)
64 return -1;
65 ret = read(fd, buffer, 40);
66 close(fd);
67 if (ret != 40)
68 return -1;
69 if (memcmp(buffer, hex_contents, 40))
70 return -1;
71 return 0;
74 static char update_hook[] = "hooks/update";
76 static int run_update_hook(const char *refname,
77 char *old_hex, char *new_hex)
79 int code;
81 if (access(update_hook, X_OK) < 0)
82 return 0;
83 code = run_command(update_hook, refname, old_hex, new_hex, NULL);
84 switch (code) {
85 case 0:
86 return 0;
87 case -ERR_RUN_COMMAND_FORK:
88 return error("hook fork failed");
89 case -ERR_RUN_COMMAND_EXEC:
90 return error("hook execute failed");
91 case -ERR_RUN_COMMAND_WAITPID:
92 return error("waitpid failed");
93 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
94 return error("waitpid is confused");
95 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
96 return error("%s died of signal", update_hook);
97 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
98 return error("%s died strangely", update_hook);
99 default:
100 error("%s exited with error code %d", update_hook, -code);
101 return -code;
105 static int update(struct command *cmd)
107 const char *name = cmd->ref_name;
108 unsigned char *old_sha1 = cmd->old_sha1;
109 unsigned char *new_sha1 = cmd->new_sha1;
110 char new_hex[60], *old_hex, *lock_name;
111 int newfd, namelen, written;
113 cmd->error_string = NULL;
114 if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5)) {
115 cmd->error_string = "funny refname";
116 return error("refusing to create funny ref '%s' locally",
117 name);
120 namelen = strlen(name);
121 lock_name = xmalloc(namelen + 10);
122 memcpy(lock_name, name, namelen);
123 memcpy(lock_name + namelen, ".lock", 6);
125 strcpy(new_hex, sha1_to_hex(new_sha1));
126 old_hex = sha1_to_hex(old_sha1);
127 if (!has_sha1_file(new_sha1)) {
128 cmd->error_string = "bad pack";
129 return error("unpack should have generated %s, "
130 "but I can't find it!", new_hex);
132 if (deny_non_fast_forwards && !is_null_sha1(old_sha1)) {
133 struct commit *old_commit, *new_commit;
134 struct commit_list *bases, *ent;
136 old_commit = (struct commit *)parse_object(old_sha1);
137 new_commit = (struct commit *)parse_object(new_sha1);
138 bases = get_merge_bases(old_commit, new_commit, 1);
139 for (ent = bases; ent; ent = ent->next)
140 if (!hashcmp(old_sha1, ent->item->object.sha1))
141 break;
142 free_commit_list(bases);
143 if (!ent)
144 return error("denying non-fast forward;"
145 " you should pull first");
147 safe_create_leading_directories(lock_name);
149 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
150 if (newfd < 0) {
151 cmd->error_string = "can't lock";
152 return error("unable to create %s (%s)",
153 lock_name, strerror(errno));
156 /* Write the ref with an ending '\n' */
157 new_hex[40] = '\n';
158 new_hex[41] = 0;
159 written = write(newfd, new_hex, 41);
160 /* Remove the '\n' again */
161 new_hex[40] = 0;
163 close(newfd);
164 if (written != 41) {
165 unlink(lock_name);
166 cmd->error_string = "can't write";
167 return error("unable to write %s", lock_name);
169 if (verify_old_ref(name, old_hex) < 0) {
170 unlink(lock_name);
171 cmd->error_string = "raced";
172 return error("%s changed during push", name);
174 if (run_update_hook(name, old_hex, new_hex)) {
175 unlink(lock_name);
176 cmd->error_string = "hook declined";
177 return error("hook declined to update %s", name);
179 else if (rename(lock_name, name) < 0) {
180 unlink(lock_name);
181 cmd->error_string = "can't rename";
182 return error("unable to replace %s", name);
184 else {
185 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
186 return 0;
190 static char update_post_hook[] = "hooks/post-update";
192 static void run_update_post_hook(struct command *cmd)
194 struct command *cmd_p;
195 int argc;
196 const char **argv;
198 if (access(update_post_hook, X_OK) < 0)
199 return;
200 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
201 if (cmd_p->error_string)
202 continue;
203 argc++;
205 argv = xmalloc(sizeof(*argv) * (1 + argc));
206 argv[0] = update_post_hook;
208 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
209 char *p;
210 if (cmd_p->error_string)
211 continue;
212 p = xmalloc(strlen(cmd_p->ref_name) + 1);
213 strcpy(p, cmd_p->ref_name);
214 argv[argc] = p;
215 argc++;
217 argv[argc] = NULL;
218 run_command_v_opt(argc, argv, RUN_COMMAND_NO_STDIO);
222 * This gets called after(if) we've successfully
223 * unpacked the data payload.
225 static void execute_commands(void)
227 struct command *cmd = commands;
229 while (cmd) {
230 update(cmd);
231 cmd = cmd->next;
233 run_update_post_hook(commands);
236 static void read_head_info(void)
238 struct command **p = &commands;
239 for (;;) {
240 static char line[1000];
241 unsigned char old_sha1[20], new_sha1[20];
242 struct command *cmd;
243 char *refname;
244 int len, reflen;
246 len = packet_read_line(0, line, sizeof(line));
247 if (!len)
248 break;
249 if (line[len-1] == '\n')
250 line[--len] = 0;
251 if (len < 83 ||
252 line[40] != ' ' ||
253 line[81] != ' ' ||
254 get_sha1_hex(line, old_sha1) ||
255 get_sha1_hex(line + 41, new_sha1))
256 die("protocol error: expected old/new/ref, got '%s'",
257 line);
259 refname = line + 82;
260 reflen = strlen(refname);
261 if (reflen + 82 < len) {
262 if (strstr(refname + reflen + 1, "report-status"))
263 report_status = 1;
265 cmd = xmalloc(sizeof(struct command) + len - 80);
266 hashcpy(cmd->old_sha1, old_sha1);
267 hashcpy(cmd->new_sha1, new_sha1);
268 memcpy(cmd->ref_name, line + 82, len - 81);
269 cmd->error_string = "n/a (unpacker error)";
270 cmd->next = NULL;
271 *p = cmd;
272 p = &cmd->next;
276 static const char *unpack(int *error_code)
278 int code = run_command_v_opt(1, unpacker, RUN_GIT_CMD);
280 *error_code = 0;
281 switch (code) {
282 case 0:
283 return NULL;
284 case -ERR_RUN_COMMAND_FORK:
285 return "unpack fork failed";
286 case -ERR_RUN_COMMAND_EXEC:
287 return "unpack execute failed";
288 case -ERR_RUN_COMMAND_WAITPID:
289 return "waitpid failed";
290 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
291 return "waitpid is confused";
292 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
293 return "unpacker died of signal";
294 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
295 return "unpacker died strangely";
296 default:
297 *error_code = -code;
298 return "unpacker exited with error code";
302 static void report(const char *unpack_status)
304 struct command *cmd;
305 packet_write(1, "unpack %s\n",
306 unpack_status ? unpack_status : "ok");
307 for (cmd = commands; cmd; cmd = cmd->next) {
308 if (!cmd->error_string)
309 packet_write(1, "ok %s\n",
310 cmd->ref_name);
311 else
312 packet_write(1, "ng %s %s\n",
313 cmd->ref_name, cmd->error_string);
315 packet_flush(1);
318 int main(int argc, char **argv)
320 int i;
321 char *dir = NULL;
323 argv++;
324 for (i = 1; i < argc; i++) {
325 char *arg = *argv++;
327 if (*arg == '-') {
328 /* Do flag handling here */
329 usage(receive_pack_usage);
331 if (dir)
332 usage(receive_pack_usage);
333 dir = arg;
335 if (!dir)
336 usage(receive_pack_usage);
338 if(!enter_repo(dir, 0))
339 die("'%s': unable to chdir or not a git archive", dir);
341 write_head_info();
343 /* EOF */
344 packet_flush(1);
346 read_head_info();
347 if (commands) {
348 int code;
349 const char *unpack_status = unpack(&code);
350 if (!unpack_status)
351 execute_commands();
352 if (report_status)
353 report(unpack_status);
355 return 0;