THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

521 lines
12KB

  1. #include "cache.h"
  2. #include "pack.h"
  3. #include "refs.h"
  4. #include "pkt-line.h"
  5. #include "run-command.h"
  6. #include "exec_cmd.h"
  7. #include "commit.h"
  8. #include "object.h"
  9. static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
  10. static int deny_non_fast_forwards = 0;
  11. static int receive_fsck_objects;
  12. static int receive_unpack_limit = -1;
  13. static int transfer_unpack_limit = -1;
  14. static int unpack_limit = 100;
  15. static int report_status;
  16. static char capabilities[] = " report-status delete-refs ";
  17. static int capabilities_sent;
  18. static int receive_pack_config(const char *var, const char *value, void *cb)
  19. {
  20. if (strcmp(var, "receive.denynonfastforwards") == 0) {
  21. deny_non_fast_forwards = git_config_bool(var, value);
  22. return 0;
  23. }
  24. if (strcmp(var, "receive.unpacklimit") == 0) {
  25. receive_unpack_limit = git_config_int(var, value);
  26. return 0;
  27. }
  28. if (strcmp(var, "transfer.unpacklimit") == 0) {
  29. transfer_unpack_limit = git_config_int(var, value);
  30. return 0;
  31. }
  32. if (strcmp(var, "receive.fsckobjects") == 0) {
  33. receive_fsck_objects = git_config_bool(var, value);
  34. return 0;
  35. }
  36. return git_default_config(var, value, cb);
  37. }
  38. static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
  39. {
  40. if (capabilities_sent)
  41. packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
  42. else
  43. packet_write(1, "%s %s%c%s\n",
  44. sha1_to_hex(sha1), path, 0, capabilities);
  45. capabilities_sent = 1;
  46. return 0;
  47. }
  48. static void write_head_info(void)
  49. {
  50. for_each_ref(show_ref, NULL);
  51. if (!capabilities_sent)
  52. show_ref("capabilities^{}", null_sha1, 0, NULL);
  53. }
  54. struct command {
  55. struct command *next;
  56. const char *error_string;
  57. unsigned char old_sha1[20];
  58. unsigned char new_sha1[20];
  59. char ref_name[FLEX_ARRAY]; /* more */
  60. };
  61. static struct command *commands;
  62. static const char pre_receive_hook[] = "hooks/pre-receive";
  63. static const char post_receive_hook[] = "hooks/post-receive";
  64. static int hook_status(int code, const char *hook_name)
  65. {
  66. switch (code) {
  67. case 0:
  68. return 0;
  69. case -ERR_RUN_COMMAND_FORK:
  70. return error("hook fork failed");
  71. case -ERR_RUN_COMMAND_EXEC:
  72. return error("hook execute failed");
  73. case -ERR_RUN_COMMAND_PIPE:
  74. return error("hook pipe failed");
  75. case -ERR_RUN_COMMAND_WAITPID:
  76. return error("waitpid failed");
  77. case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
  78. return error("waitpid is confused");
  79. case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
  80. return error("%s died of signal", hook_name);
  81. case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
  82. return error("%s died strangely", hook_name);
  83. default:
  84. error("%s exited with error code %d", hook_name, -code);
  85. return -code;
  86. }
  87. }
  88. static int run_hook(const char *hook_name)
  89. {
  90. static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
  91. struct command *cmd;
  92. struct child_process proc;
  93. const char *argv[2];
  94. int have_input = 0, code;
  95. for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
  96. if (!cmd->error_string)
  97. have_input = 1;
  98. }
  99. if (!have_input || access(hook_name, X_OK) < 0)
  100. return 0;
  101. argv[0] = hook_name;
  102. argv[1] = NULL;
  103. memset(&proc, 0, sizeof(proc));
  104. proc.argv = argv;
  105. proc.in = -1;
  106. proc.stdout_to_stderr = 1;
  107. code = start_command(&proc);
  108. if (code)
  109. return hook_status(code, hook_name);
  110. for (cmd = commands; cmd; cmd = cmd->next) {
  111. if (!cmd->error_string) {
  112. size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n",
  113. sha1_to_hex(cmd->old_sha1),
  114. sha1_to_hex(cmd->new_sha1),
  115. cmd->ref_name);
  116. if (write_in_full(proc.in, buf, n) != n)
  117. break;
  118. }
  119. }
  120. close(proc.in);
  121. return hook_status(finish_command(&proc), hook_name);
  122. }
  123. static int run_update_hook(struct command *cmd)
  124. {
  125. static const char update_hook[] = "hooks/update";
  126. struct child_process proc;
  127. const char *argv[5];
  128. if (access(update_hook, X_OK) < 0)
  129. return 0;
  130. argv[0] = update_hook;
  131. argv[1] = cmd->ref_name;
  132. argv[2] = sha1_to_hex(cmd->old_sha1);
  133. argv[3] = sha1_to_hex(cmd->new_sha1);
  134. argv[4] = NULL;
  135. memset(&proc, 0, sizeof(proc));
  136. proc.argv = argv;
  137. proc.no_stdin = 1;
  138. proc.stdout_to_stderr = 1;
  139. return hook_status(run_command(&proc), update_hook);
  140. }
  141. static const char *update(struct command *cmd)
  142. {
  143. const char *name = cmd->ref_name;
  144. unsigned char *old_sha1 = cmd->old_sha1;
  145. unsigned char *new_sha1 = cmd->new_sha1;
  146. struct ref_lock *lock;
  147. /* only refs/... are allowed */
  148. if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) {
  149. error("refusing to create funny ref '%s' remotely", name);
  150. return "funny refname";
  151. }
  152. if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
  153. error("unpack should have generated %s, "
  154. "but I can't find it!", sha1_to_hex(new_sha1));
  155. return "bad pack";
  156. }
  157. if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
  158. !is_null_sha1(old_sha1) &&
  159. !prefixcmp(name, "refs/heads/")) {
  160. struct object *old_object, *new_object;
  161. struct commit *old_commit, *new_commit;
  162. struct commit_list *bases, *ent;
  163. old_object = parse_object(old_sha1);
  164. new_object = parse_object(new_sha1);
  165. if (!old_object || !new_object ||
  166. old_object->type != OBJ_COMMIT ||
  167. new_object->type != OBJ_COMMIT) {
  168. error("bad sha1 objects for %s", name);
  169. return "bad ref";
  170. }
  171. old_commit = (struct commit *)old_object;
  172. new_commit = (struct commit *)new_object;
  173. bases = get_merge_bases(old_commit, new_commit, 1);
  174. for (ent = bases; ent; ent = ent->next)
  175. if (!hashcmp(old_sha1, ent->item->object.sha1))
  176. break;
  177. free_commit_list(bases);
  178. if (!ent) {
  179. error("denying non-fast forward %s"
  180. " (you should pull first)", name);
  181. return "non-fast forward";
  182. }
  183. }
  184. if (run_update_hook(cmd)) {
  185. error("hook declined to update %s", name);
  186. return "hook declined";
  187. }
  188. if (is_null_sha1(new_sha1)) {
  189. if (!parse_object(old_sha1)) {
  190. warning ("Allowing deletion of corrupt ref.");
  191. old_sha1 = NULL;
  192. }
  193. if (delete_ref(name, old_sha1)) {
  194. error("failed to delete %s", name);
  195. return "failed to delete";
  196. }
  197. return NULL; /* good */
  198. }
  199. else {
  200. lock = lock_any_ref_for_update(name, old_sha1, 0);
  201. if (!lock) {
  202. error("failed to lock %s", name);
  203. return "failed to lock";
  204. }
  205. if (write_ref_sha1(lock, new_sha1, "push")) {
  206. return "failed to write"; /* error() already called */
  207. }
  208. return NULL; /* good */
  209. }
  210. }
  211. static char update_post_hook[] = "hooks/post-update";
  212. static void run_update_post_hook(struct command *cmd)
  213. {
  214. struct command *cmd_p;
  215. int argc;
  216. const char **argv;
  217. for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
  218. if (cmd_p->error_string)
  219. continue;
  220. argc++;
  221. }
  222. if (!argc || access(update_post_hook, X_OK) < 0)
  223. return;
  224. argv = xmalloc(sizeof(*argv) * (2 + argc));
  225. argv[0] = update_post_hook;
  226. for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
  227. char *p;
  228. if (cmd_p->error_string)
  229. continue;
  230. p = xmalloc(strlen(cmd_p->ref_name) + 1);
  231. strcpy(p, cmd_p->ref_name);
  232. argv[argc] = p;
  233. argc++;
  234. }
  235. argv[argc] = NULL;
  236. run_command_v_opt(argv, RUN_COMMAND_NO_STDIN
  237. | RUN_COMMAND_STDOUT_TO_STDERR);
  238. }
  239. static void execute_commands(const char *unpacker_error)
  240. {
  241. struct command *cmd = commands;
  242. if (unpacker_error) {
  243. while (cmd) {
  244. cmd->error_string = "n/a (unpacker error)";
  245. cmd = cmd->next;
  246. }
  247. return;
  248. }
  249. if (run_hook(pre_receive_hook)) {
  250. while (cmd) {
  251. cmd->error_string = "pre-receive hook declined";
  252. cmd = cmd->next;
  253. }
  254. return;
  255. }
  256. while (cmd) {
  257. cmd->error_string = update(cmd);
  258. cmd = cmd->next;
  259. }
  260. }
  261. static void read_head_info(void)
  262. {
  263. struct command **p = &commands;
  264. for (;;) {
  265. static char line[1000];
  266. unsigned char old_sha1[20], new_sha1[20];
  267. struct command *cmd;
  268. char *refname;
  269. int len, reflen;
  270. len = packet_read_line(0, line, sizeof(line));
  271. if (!len)
  272. break;
  273. if (line[len-1] == '\n')
  274. line[--len] = 0;
  275. if (len < 83 ||
  276. line[40] != ' ' ||
  277. line[81] != ' ' ||
  278. get_sha1_hex(line, old_sha1) ||
  279. get_sha1_hex(line + 41, new_sha1))
  280. die("protocol error: expected old/new/ref, got '%s'",
  281. line);
  282. refname = line + 82;
  283. reflen = strlen(refname);
  284. if (reflen + 82 < len) {
  285. if (strstr(refname + reflen + 1, "report-status"))
  286. report_status = 1;
  287. }
  288. cmd = xmalloc(sizeof(struct command) + len - 80);
  289. hashcpy(cmd->old_sha1, old_sha1);
  290. hashcpy(cmd->new_sha1, new_sha1);
  291. memcpy(cmd->ref_name, line + 82, len - 81);
  292. cmd->error_string = NULL;
  293. cmd->next = NULL;
  294. *p = cmd;
  295. p = &cmd->next;
  296. }
  297. }
  298. static const char *parse_pack_header(struct pack_header *hdr)
  299. {
  300. switch (read_pack_header(0, hdr)) {
  301. case PH_ERROR_EOF:
  302. return "eof before pack header was fully read";
  303. case PH_ERROR_PACK_SIGNATURE:
  304. return "protocol error (pack signature mismatch detected)";
  305. case PH_ERROR_PROTOCOL:
  306. return "protocol error (pack version unsupported)";
  307. default:
  308. return "unknown error in parse_pack_header";
  309. case 0:
  310. return NULL;
  311. }
  312. }
  313. static const char *pack_lockfile;
  314. static const char *unpack(void)
  315. {
  316. struct pack_header hdr;
  317. const char *hdr_err;
  318. char hdr_arg[38];
  319. hdr_err = parse_pack_header(&hdr);
  320. if (hdr_err)
  321. return hdr_err;
  322. snprintf(hdr_arg, sizeof(hdr_arg),
  323. "--pack_header=%"PRIu32",%"PRIu32,
  324. ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
  325. if (ntohl(hdr.hdr_entries) < unpack_limit) {
  326. int code, i = 0;
  327. const char *unpacker[4];
  328. unpacker[i++] = "unpack-objects";
  329. if (receive_fsck_objects)
  330. unpacker[i++] = "--strict";
  331. unpacker[i++] = hdr_arg;
  332. unpacker[i++] = NULL;
  333. code = run_command_v_opt(unpacker, RUN_GIT_CMD);
  334. switch (code) {
  335. case 0:
  336. return NULL;
  337. case -ERR_RUN_COMMAND_FORK:
  338. return "unpack fork failed";
  339. case -ERR_RUN_COMMAND_EXEC:
  340. return "unpack execute failed";
  341. case -ERR_RUN_COMMAND_WAITPID:
  342. return "waitpid failed";
  343. case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
  344. return "waitpid is confused";
  345. case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
  346. return "unpacker died of signal";
  347. case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
  348. return "unpacker died strangely";
  349. default:
  350. return "unpacker exited with error code";
  351. }
  352. } else {
  353. const char *keeper[7];
  354. int s, status, i = 0;
  355. char keep_arg[256];
  356. struct child_process ip;
  357. s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
  358. if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
  359. strcpy(keep_arg + s, "localhost");
  360. keeper[i++] = "index-pack";
  361. keeper[i++] = "--stdin";
  362. if (receive_fsck_objects)
  363. keeper[i++] = "--strict";
  364. keeper[i++] = "--fix-thin";
  365. keeper[i++] = hdr_arg;
  366. keeper[i++] = keep_arg;
  367. keeper[i++] = NULL;
  368. memset(&ip, 0, sizeof(ip));
  369. ip.argv = keeper;
  370. ip.out = -1;
  371. ip.git_cmd = 1;
  372. if (start_command(&ip))
  373. return "index-pack fork failed";
  374. pack_lockfile = index_pack_lockfile(ip.out);
  375. close(ip.out);
  376. status = finish_command(&ip);
  377. if (!status) {
  378. reprepare_packed_git();
  379. return NULL;
  380. }
  381. return "index-pack abnormal exit";
  382. }
  383. }
  384. static void report(const char *unpack_status)
  385. {
  386. struct command *cmd;
  387. packet_write(1, "unpack %s\n",
  388. unpack_status ? unpack_status : "ok");
  389. for (cmd = commands; cmd; cmd = cmd->next) {
  390. if (!cmd->error_string)
  391. packet_write(1, "ok %s\n",
  392. cmd->ref_name);
  393. else
  394. packet_write(1, "ng %s %s\n",
  395. cmd->ref_name, cmd->error_string);
  396. }
  397. packet_flush(1);
  398. }
  399. static int delete_only(struct command *cmd)
  400. {
  401. while (cmd) {
  402. if (!is_null_sha1(cmd->new_sha1))
  403. return 0;
  404. cmd = cmd->next;
  405. }
  406. return 1;
  407. }
  408. int main(int argc, char **argv)
  409. {
  410. int i;
  411. char *dir = NULL;
  412. argv++;
  413. for (i = 1; i < argc; i++) {
  414. char *arg = *argv++;
  415. if (*arg == '-') {
  416. /* Do flag handling here */
  417. usage(receive_pack_usage);
  418. }
  419. if (dir)
  420. usage(receive_pack_usage);
  421. dir = arg;
  422. }
  423. if (!dir)
  424. usage(receive_pack_usage);
  425. setup_path();
  426. if (!enter_repo(dir, 0))
  427. die("'%s': unable to chdir or not a git archive", dir);
  428. if (is_repository_shallow())
  429. die("attempt to push into a shallow repository");
  430. git_config(receive_pack_config, NULL);
  431. if (0 <= transfer_unpack_limit)
  432. unpack_limit = transfer_unpack_limit;
  433. else if (0 <= receive_unpack_limit)
  434. unpack_limit = receive_unpack_limit;
  435. write_head_info();
  436. /* EOF */
  437. packet_flush(1);
  438. read_head_info();
  439. if (commands) {
  440. const char *unpack_status = NULL;
  441. if (!delete_only(commands))
  442. unpack_status = unpack();
  443. execute_commands(unpack_status);
  444. if (pack_lockfile)
  445. unlink(pack_lockfile);
  446. if (report_status)
  447. report(unpack_status);
  448. run_hook(post_receive_hook);
  449. run_update_post_hook(commands);
  450. }
  451. return 0;
  452. }