receive-pack.c 6.4 KB
Newer Older
1
#include "cache.h"
2
#include "refs.h"
3
#include "pkt-line.h"
4
#include "run-command.h"
5

6
static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
7

J
Junio C Hamano 已提交
8
static const char *unpacker[] = { "unpack-objects", NULL };
9

10
static int report_status;
11 12

static char capabilities[] = "report-status";
13
static int capabilities_sent;
14

15
static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
16
{
17 18 19 20 21 22
	if (capabilities_sent)
		packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
	else
		packet_write(1, "%s %s%c%s\n",
			     sha1_to_hex(sha1), path, 0, capabilities);
	capabilities_sent = 1;
23
	return 0;
24 25
}

26
static void write_head_info(void)
27
{
28
	for_each_ref(show_ref, NULL);
29
	if (!capabilities_sent)
30
		show_ref("capabilities^{}", null_sha1, 0, NULL);
31

32 33
}

34 35
struct command {
	struct command *next;
36
	const char *error_string;
37 38
	unsigned char old_sha1[20];
	unsigned char new_sha1[20];
39
	char ref_name[FLEX_ARRAY]; /* more */
40 41
};

42
static struct command *commands;
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57
static char update_hook[] = "hooks/update";

static int run_update_hook(const char *refname,
			   char *old_hex, char *new_hex)
{
	int code;

	if (access(update_hook, X_OK) < 0)
		return 0;
	code = run_command(update_hook, refname, old_hex, new_hex, NULL);
	switch (code) {
	case 0:
		return 0;
	case -ERR_RUN_COMMAND_FORK:
58
		return error("hook fork failed");
59
	case -ERR_RUN_COMMAND_EXEC:
60
		return error("hook execute failed");
61
	case -ERR_RUN_COMMAND_WAITPID:
62
		return error("waitpid failed");
63
	case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
64
		return error("waitpid is confused");
65
	case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
66
		return error("%s died of signal", update_hook);
67
	case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
68
		return error("%s died strangely", update_hook);
69 70 71 72 73 74
	default:
		error("%s exited with error code %d", update_hook, -code);
		return -code;
	}
}

75
static int update(struct command *cmd)
76
{
77 78 79
	const char *name = cmd->ref_name;
	unsigned char *old_sha1 = cmd->old_sha1;
	unsigned char *new_sha1 = cmd->new_sha1;
80 81
	char new_hex[41], old_hex[41];
	struct ref_lock *lock;
82

83 84 85
	cmd->error_string = NULL;
	if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5)) {
		cmd->error_string = "funny refname";
86 87
		return error("refusing to create funny ref '%s' locally",
			     name);
88
	}
89

90
	strcpy(new_hex, sha1_to_hex(new_sha1));
91
	strcpy(old_hex, sha1_to_hex(old_sha1));
92 93
	if (!has_sha1_file(new_sha1)) {
		cmd->error_string = "bad pack";
J
Junio C Hamano 已提交
94 95
		return error("unpack should have generated %s, "
			     "but I can't find it!", new_hex);
96
	}
97
	if (run_update_hook(name, old_hex, new_hex)) {
98
		cmd->error_string = "hook declined";
99
		return error("hook declined to update %s", name);
100
	}
101 102 103 104 105

	lock = lock_any_ref_for_update(name, old_sha1);
	if (!lock) {
		cmd->error_string = "failed to lock";
		return error("failed to lock %s", name);
J
Junio C Hamano 已提交
106
	}
107 108 109 110
	write_ref_sha1(lock, new_sha1, "push");

	fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
	return 0;
111 112
}

J
Junio C Hamano 已提交
113 114 115 116 117 118
static char update_post_hook[] = "hooks/post-update";

static void run_update_post_hook(struct command *cmd)
{
	struct command *cmd_p;
	int argc;
J
Junio C Hamano 已提交
119
	const char **argv;
J
Junio C Hamano 已提交
120 121 122 123

	if (access(update_post_hook, X_OK) < 0)
		return;
	for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
124
		if (cmd_p->error_string)
J
Junio C Hamano 已提交
125 126 127 128 129 130 131
			continue;
		argc++;
	}
	argv = xmalloc(sizeof(*argv) * (1 + argc));
	argv[0] = update_post_hook;

	for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
J
Junio C Hamano 已提交
132
		char *p;
133
		if (cmd_p->error_string)
J
Junio C Hamano 已提交
134
			continue;
J
Junio C Hamano 已提交
135 136 137
		p = xmalloc(strlen(cmd_p->ref_name) + 1);
		strcpy(p, cmd_p->ref_name);
		argv[argc] = p;
J
Junio C Hamano 已提交
138 139 140
		argc++;
	}
	argv[argc] = NULL;
141
	run_command_v_opt(argc, argv, RUN_COMMAND_NO_STDIO);
J
Junio C Hamano 已提交
142
}
143

144 145 146 147 148 149
/*
 * This gets called after(if) we've successfully
 * unpacked the data payload.
 */
static void execute_commands(void)
{
150 151 152
	struct command *cmd = commands;

	while (cmd) {
153
		update(cmd);
154
		cmd = cmd->next;
155
	}
J
Junio C Hamano 已提交
156
	run_update_post_hook(commands);
157 158 159 160
}

static void read_head_info(void)
{
161
	struct command **p = &commands;
162 163
	for (;;) {
		static char line[1000];
164 165
		unsigned char old_sha1[20], new_sha1[20];
		struct command *cmd;
166 167
		char *refname;
		int len, reflen;
168 169

		len = packet_read_line(0, line, sizeof(line));
170 171
		if (!len)
			break;
172 173 174 175 176 177 178
		if (line[len-1] == '\n')
			line[--len] = 0;
		if (len < 83 ||
		    line[40] != ' ' ||
		    line[81] != ' ' ||
		    get_sha1_hex(line, old_sha1) ||
		    get_sha1_hex(line + 41, new_sha1))
179 180 181 182 183 184 185 186 187
			die("protocol error: expected old/new/ref, got '%s'",
			    line);

		refname = line + 82;
		reflen = strlen(refname);
		if (reflen + 82 < len) {
			if (strstr(refname + reflen + 1, "report-status"))
				report_status = 1;
		}
188
		cmd = xmalloc(sizeof(struct command) + len - 80);
189 190
		hashcpy(cmd->old_sha1, old_sha1);
		hashcpy(cmd->new_sha1, new_sha1);
191
		memcpy(cmd->ref_name, line + 82, len - 81);
192
		cmd->error_string = "n/a (unpacker error)";
193 194 195
		cmd->next = NULL;
		*p = cmd;
		p = &cmd->next;
196 197 198
	}
}

199
static const char *unpack(int *error_code)
200
{
201
	int code = run_command_v_opt(1, unpacker, RUN_GIT_CMD);
202 203

	*error_code = 0;
204 205
	switch (code) {
	case 0:
206
		return NULL;
207
	case -ERR_RUN_COMMAND_FORK:
208
		return "unpack fork failed";
209
	case -ERR_RUN_COMMAND_EXEC:
210
		return "unpack execute failed";
211
	case -ERR_RUN_COMMAND_WAITPID:
212
		return "waitpid failed";
213
	case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
214
		return "waitpid is confused";
215
	case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
216
		return "unpacker died of signal";
217
	case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
218
		return "unpacker died strangely";
219
	default:
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
		*error_code = -code;
		return "unpacker exited with error code";
	}
}

static void report(const char *unpack_status)
{
	struct command *cmd;
	packet_write(1, "unpack %s\n",
		     unpack_status ? unpack_status : "ok");
	for (cmd = commands; cmd; cmd = cmd->next) {
		if (!cmd->error_string)
			packet_write(1, "ok %s\n",
				     cmd->ref_name);
		else
			packet_write(1, "ng %s %s\n",
				     cmd->ref_name, cmd->error_string);
237
	}
238
	packet_flush(1);
239 240 241 242
}

int main(int argc, char **argv)
{
243
	int i;
244
	char *dir = NULL;
245 246 247

	argv++;
	for (i = 1; i < argc; i++) {
248
		char *arg = *argv++;
249 250 251 252 253

		if (*arg == '-') {
			/* Do flag handling here */
			usage(receive_pack_usage);
		}
254 255
		if (dir)
			usage(receive_pack_usage);
256 257 258 259 260
		dir = arg;
	}
	if (!dir)
		usage(receive_pack_usage);

261
	if (!enter_repo(dir, 0))
262
		die("'%s': unable to chdir or not a git archive", dir);
263

264 265
	git_config(git_default_config);

266
	write_head_info();
267 268

	/* EOF */
269
	packet_flush(1);
270 271

	read_head_info();
272
	if (commands) {
273 274 275 276 277 278
		int code;
		const char *unpack_status = unpack(&code);
		if (!unpack_status)
			execute_commands();
		if (report_status)
			report(unpack_status);
279
	}
280 281
	return 0;
}