]> git.ipfire.org Git - thirdparty/git.git/blame - receive-pack.c
Remove unused variable in receive-pack.
[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"
11031d7e
JS
5#include "commit.h"
6#include "object.h"
575f4974 7
d0efc8a7 8static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
575f4974 9
9201c707 10static const char *unpacker[] = { "unpack-objects", NULL };
575f4974 11
96f1e58f 12static int report_status;
cfee10a7
JH
13
14static char capabilities[] = "report-status";
96f1e58f 15static int capabilities_sent;
cfee10a7 16
944d8589 17static int show_ref(const char *path, const unsigned char *sha1)
575f4974 18{
cfee10a7
JH
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;
8a65ff76 25 return 0;
575f4974
LT
26}
27
8a65ff76 28static void write_head_info(void)
575f4974 29{
8a65ff76 30 for_each_ref(show_ref);
cfee10a7
JH
31 if (!capabilities_sent)
32 show_ref("capabilities^{}", null_sha1);
33
575f4974
LT
34}
35
eb1af2df
LT
36struct command {
37 struct command *next;
cfee10a7 38 const char *error_string;
eb1af2df
LT
39 unsigned char old_sha1[20];
40 unsigned char new_sha1[20];
8f1d2e6f 41 char ref_name[FLEX_ARRAY]; /* more */
575f4974
LT
42};
43
96f1e58f 44static struct command *commands;
575f4974 45
f65fdf04
LT
46static int is_all_zeroes(const char *hex)
47{
48 int i;
49 for (i = 0; i < 40; i++)
50 if (*hex++ != '0')
51 return 0;
52 return 1;
53}
54
2eca23da
LT
55static int verify_old_ref(const char *name, char *hex_contents)
56{
57 int fd, ret;
58 char buffer[60];
59
f65fdf04
LT
60 if (is_all_zeroes(hex_contents))
61 return 0;
2eca23da
LT
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;
72}
73
b1bf95bb
JW
74static char update_hook[] = "hooks/update";
75
76static int run_update_hook(const char *refname,
77 char *old_hex, char *new_hex)
78{
79 int code;
80
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:
cfee10a7 88 return error("hook fork failed");
b1bf95bb 89 case -ERR_RUN_COMMAND_EXEC:
cfee10a7 90 return error("hook execute failed");
b1bf95bb 91 case -ERR_RUN_COMMAND_WAITPID:
cfee10a7 92 return error("waitpid failed");
b1bf95bb 93 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
cfee10a7 94 return error("waitpid is confused");
b1bf95bb 95 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
bd2afde8 96 return error("%s died of signal", update_hook);
b1bf95bb 97 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
cfee10a7 98 return error("%s died strangely", update_hook);
b1bf95bb
JW
99 default:
100 error("%s exited with error code %d", update_hook, -code);
101 return -code;
102 }
103}
104
cfee10a7 105static int update(struct command *cmd)
2eca23da 106{
cfee10a7
JH
107 const char *name = cmd->ref_name;
108 unsigned char *old_sha1 = cmd->old_sha1;
109 unsigned char *new_sha1 = cmd->new_sha1;
2eca23da
LT
110 char new_hex[60], *old_hex, *lock_name;
111 int newfd, namelen, written;
112
cfee10a7
JH
113 cmd->error_string = NULL;
114 if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5)) {
115 cmd->error_string = "funny refname";
d8a1deec
JH
116 return error("refusing to create funny ref '%s' locally",
117 name);
cfee10a7 118 }
d8a1deec 119
2eca23da
LT
120 namelen = strlen(name);
121 lock_name = xmalloc(namelen + 10);
122 memcpy(lock_name, name, namelen);
123 memcpy(lock_name + namelen, ".lock", 6);
124
125 strcpy(new_hex, sha1_to_hex(new_sha1));
2eca23da 126 old_hex = sha1_to_hex(old_sha1);
cfee10a7
JH
127 if (!has_sha1_file(new_sha1)) {
128 cmd->error_string = "bad pack";
19614330
JH
129 return error("unpack should have generated %s, "
130 "but I can't find it!", new_hex);
cfee10a7 131 }
11031d7e
JS
132 if (deny_non_fast_forwards && !is_null_sha1(old_sha1)) {
133 struct commit *old_commit, *new_commit;
9edd7e46 134 struct commit_list *bases, *ent;
11031d7e
JS
135
136 old_commit = (struct commit *)parse_object(old_sha1);
137 new_commit = (struct commit *)parse_object(new_sha1);
9edd7e46
JS
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))
11031d7e 141 break;
9edd7e46
JS
142 free_commit_list(bases);
143 if (!ent)
11031d7e 144 return error("denying non-fast forward;"
9edd7e46 145 " you should pull first");
11031d7e 146 }
29f3b3de
JH
147 safe_create_leading_directories(lock_name);
148
f312de01 149 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
cfee10a7
JH
150 if (newfd < 0) {
151 cmd->error_string = "can't lock";
19614330
JH
152 return error("unable to create %s (%s)",
153 lock_name, strerror(errno));
cfee10a7 154 }
f65fdf04
LT
155
156 /* Write the ref with an ending '\n' */
157 new_hex[40] = '\n';
158 new_hex[41] = 0;
2eca23da 159 written = write(newfd, new_hex, 41);
f65fdf04
LT
160 /* Remove the '\n' again */
161 new_hex[40] = 0;
162
2eca23da
LT
163 close(newfd);
164 if (written != 41) {
165 unlink(lock_name);
cfee10a7 166 cmd->error_string = "can't write";
19614330 167 return error("unable to write %s", lock_name);
2eca23da
LT
168 }
169 if (verify_old_ref(name, old_hex) < 0) {
170 unlink(lock_name);
cfee10a7 171 cmd->error_string = "raced";
19614330 172 return error("%s changed during push", name);
2eca23da 173 }
b1bf95bb
JW
174 if (run_update_hook(name, old_hex, new_hex)) {
175 unlink(lock_name);
cfee10a7 176 cmd->error_string = "hook declined";
bd2afde8 177 return error("hook declined to update %s", name);
b1bf95bb
JW
178 }
179 else if (rename(lock_name, name) < 0) {
2eca23da 180 unlink(lock_name);
cfee10a7 181 cmd->error_string = "can't rename";
19614330 182 return error("unable to replace %s", name);
2eca23da 183 }
19614330 184 else {
b1bf95bb 185 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
19614330
JH
186 return 0;
187 }
2eca23da
LT
188}
189
19614330
JH
190static char update_post_hook[] = "hooks/post-update";
191
192static void run_update_post_hook(struct command *cmd)
193{
194 struct command *cmd_p;
195 int argc;
9201c707 196 const char **argv;
19614330
JH
197
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) {
cfee10a7 201 if (cmd_p->error_string)
19614330
JH
202 continue;
203 argc++;
204 }
205 argv = xmalloc(sizeof(*argv) * (1 + argc));
206 argv[0] = update_post_hook;
207
208 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
9201c707 209 char *p;
cfee10a7 210 if (cmd_p->error_string)
19614330 211 continue;
9201c707
JH
212 p = xmalloc(strlen(cmd_p->ref_name) + 1);
213 strcpy(p, cmd_p->ref_name);
214 argv[argc] = p;
19614330
JH
215 argc++;
216 }
217 argv[argc] = NULL;
128aed68 218 run_command_v_opt(argc, argv, RUN_COMMAND_NO_STDIO);
19614330 219}
2eca23da 220
575f4974
LT
221/*
222 * This gets called after(if) we've successfully
223 * unpacked the data payload.
224 */
225static void execute_commands(void)
226{
eb1af2df
LT
227 struct command *cmd = commands;
228
229 while (cmd) {
cfee10a7 230 update(cmd);
eb1af2df 231 cmd = cmd->next;
575f4974 232 }
19614330 233 run_update_post_hook(commands);
575f4974
LT
234}
235
236static void read_head_info(void)
237{
eb1af2df 238 struct command **p = &commands;
575f4974
LT
239 for (;;) {
240 static char line[1000];
eb1af2df
LT
241 unsigned char old_sha1[20], new_sha1[20];
242 struct command *cmd;
cfee10a7
JH
243 char *refname;
244 int len, reflen;
eb1af2df
LT
245
246 len = packet_read_line(0, line, sizeof(line));
575f4974
LT
247 if (!len)
248 break;
eb1af2df
LT
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))
cfee10a7
JH
256 die("protocol error: expected old/new/ref, got '%s'",
257 line);
258
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;
264 }
eb1af2df 265 cmd = xmalloc(sizeof(struct command) + len - 80);
e702496e
SP
266 hashcpy(cmd->old_sha1, old_sha1);
267 hashcpy(cmd->new_sha1, new_sha1);
eb1af2df 268 memcpy(cmd->ref_name, line + 82, len - 81);
cfee10a7 269 cmd->error_string = "n/a (unpacker error)";
eb1af2df
LT
270 cmd->next = NULL;
271 *p = cmd;
272 p = &cmd->next;
575f4974
LT
273 }
274}
275
861ed121 276static const char *unpack(void)
575f4974 277{
77cb17e9 278 int code = run_command_v_opt(1, unpacker, RUN_GIT_CMD);
cfee10a7 279
b1bf95bb
JW
280 switch (code) {
281 case 0:
cfee10a7 282 return NULL;
b1bf95bb 283 case -ERR_RUN_COMMAND_FORK:
cfee10a7 284 return "unpack fork failed";
b1bf95bb 285 case -ERR_RUN_COMMAND_EXEC:
cfee10a7 286 return "unpack execute failed";
b1bf95bb 287 case -ERR_RUN_COMMAND_WAITPID:
cfee10a7 288 return "waitpid failed";
b1bf95bb 289 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
cfee10a7 290 return "waitpid is confused";
b1bf95bb 291 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
cfee10a7 292 return "unpacker died of signal";
b1bf95bb 293 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
cfee10a7 294 return "unpacker died strangely";
b1bf95bb 295 default:
cfee10a7
JH
296 return "unpacker exited with error code";
297 }
298}
299
300static void report(const char *unpack_status)
301{
302 struct command *cmd;
303 packet_write(1, "unpack %s\n",
304 unpack_status ? unpack_status : "ok");
305 for (cmd = commands; cmd; cmd = cmd->next) {
306 if (!cmd->error_string)
307 packet_write(1, "ok %s\n",
308 cmd->ref_name);
309 else
310 packet_write(1, "ng %s %s\n",
311 cmd->ref_name, cmd->error_string);
575f4974 312 }
cfee10a7 313 packet_flush(1);
575f4974
LT
314}
315
316int main(int argc, char **argv)
317{
d0efc8a7 318 int i;
8d630132 319 char *dir = NULL;
575f4974
LT
320
321 argv++;
322 for (i = 1; i < argc; i++) {
8d630132 323 char *arg = *argv++;
575f4974
LT
324
325 if (*arg == '-') {
575f4974
LT
326 /* Do flag handling here */
327 usage(receive_pack_usage);
328 }
d0efc8a7
LT
329 if (dir)
330 usage(receive_pack_usage);
575f4974 331 dir = arg;
575f4974
LT
332 }
333 if (!dir)
334 usage(receive_pack_usage);
335
8d630132
AE
336 if(!enter_repo(dir, 0))
337 die("'%s': unable to chdir or not a git archive", dir);
575f4974 338
8a65ff76 339 write_head_info();
575f4974
LT
340
341 /* EOF */
f3a3214e 342 packet_flush(1);
575f4974
LT
343
344 read_head_info();
7f8e9828 345 if (commands) {
861ed121 346 const char *unpack_status = unpack();
cfee10a7
JH
347 if (!unpack_status)
348 execute_commands();
349 if (report_status)
350 report(unpack_status);
7f8e9828 351 }
575f4974
LT
352 return 0;
353}