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.

1064 lines
28KB

  1. /*
  2. * Builtin "git commit"
  3. *
  4. * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
  5. * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
  6. */
  7. #include "cache.h"
  8. #include "cache-tree.h"
  9. #include "color.h"
  10. #include "dir.h"
  11. #include "builtin.h"
  12. #include "diff.h"
  13. #include "diffcore.h"
  14. #include "commit.h"
  15. #include "revision.h"
  16. #include "wt-status.h"
  17. #include "run-command.h"
  18. #include "refs.h"
  19. #include "log-tree.h"
  20. #include "strbuf.h"
  21. #include "utf8.h"
  22. #include "parse-options.h"
  23. #include "string-list.h"
  24. #include "rerere.h"
  25. #include "unpack-trees.h"
  26. static const char * const builtin_commit_usage[] = {
  27. "git commit [options] [--] <filepattern>...",
  28. NULL
  29. };
  30. static const char * const builtin_status_usage[] = {
  31. "git status [options] [--] <filepattern>...",
  32. NULL
  33. };
  34. static unsigned char head_sha1[20], merge_head_sha1[20];
  35. static char *use_message_buffer;
  36. static const char commit_editmsg[] = "COMMIT_EDITMSG";
  37. static struct lock_file index_lock; /* real index */
  38. static struct lock_file false_lock; /* used only for partial commits */
  39. static enum {
  40. COMMIT_AS_IS = 1,
  41. COMMIT_NORMAL,
  42. COMMIT_PARTIAL,
  43. } commit_style;
  44. static const char *logfile, *force_author;
  45. static const char *template_file;
  46. static char *edit_message, *use_message;
  47. static char *author_name, *author_email, *author_date;
  48. static int all, edit_flag, also, interactive, only, amend, signoff;
  49. static int quiet, verbose, no_verify, allow_empty;
  50. static char *untracked_files_arg;
  51. /*
  52. * The default commit message cleanup mode will remove the lines
  53. * beginning with # (shell comments) and leading and trailing
  54. * whitespaces (empty lines or containing only whitespaces)
  55. * if editor is used, and only the whitespaces if the message
  56. * is specified explicitly.
  57. */
  58. static enum {
  59. CLEANUP_SPACE,
  60. CLEANUP_NONE,
  61. CLEANUP_ALL,
  62. } cleanup_mode;
  63. static char *cleanup_arg;
  64. static int use_editor = 1, initial_commit, in_merge;
  65. static const char *only_include_assumed;
  66. static struct strbuf message;
  67. static int opt_parse_m(const struct option *opt, const char *arg, int unset)
  68. {
  69. struct strbuf *buf = opt->value;
  70. if (unset)
  71. strbuf_setlen(buf, 0);
  72. else {
  73. strbuf_addstr(buf, arg);
  74. strbuf_addstr(buf, "\n\n");
  75. }
  76. return 0;
  77. }
  78. static struct option builtin_commit_options[] = {
  79. OPT__QUIET(&quiet),
  80. OPT__VERBOSE(&verbose),
  81. OPT_GROUP("Commit message options"),
  82. OPT_STRING('F', "file", &logfile, "FILE", "read log from file"),
  83. OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
  84. OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m),
  85. OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit "),
  86. OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"),
  87. OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
  88. OPT_STRING('t', "template", &template_file, "FILE", "use specified template file"),
  89. OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"),
  90. OPT_GROUP("Commit contents options"),
  91. OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
  92. OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
  93. OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
  94. OPT_BOOLEAN('o', "only", &only, "commit only specified files"),
  95. OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
  96. OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
  97. { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
  98. OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
  99. OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
  100. OPT_END()
  101. };
  102. static void rollback_index_files(void)
  103. {
  104. switch (commit_style) {
  105. case COMMIT_AS_IS:
  106. break; /* nothing to do */
  107. case COMMIT_NORMAL:
  108. rollback_lock_file(&index_lock);
  109. break;
  110. case COMMIT_PARTIAL:
  111. rollback_lock_file(&index_lock);
  112. rollback_lock_file(&false_lock);
  113. break;
  114. }
  115. }
  116. static int commit_index_files(void)
  117. {
  118. int err = 0;
  119. switch (commit_style) {
  120. case COMMIT_AS_IS:
  121. break; /* nothing to do */
  122. case COMMIT_NORMAL:
  123. err = commit_lock_file(&index_lock);
  124. break;
  125. case COMMIT_PARTIAL:
  126. err = commit_lock_file(&index_lock);
  127. rollback_lock_file(&false_lock);
  128. break;
  129. }
  130. return err;
  131. }
  132. /*
  133. * Take a union of paths in the index and the named tree (typically, "HEAD"),
  134. * and return the paths that match the given pattern in list.
  135. */
  136. static int list_paths(struct string_list *list, const char *with_tree,
  137. const char *prefix, const char **pattern)
  138. {
  139. int i;
  140. char *m;
  141. for (i = 0; pattern[i]; i++)
  142. ;
  143. m = xcalloc(1, i);
  144. if (with_tree)
  145. overlay_tree_on_cache(with_tree, prefix);
  146. for (i = 0; i < active_nr; i++) {
  147. struct cache_entry *ce = active_cache[i];
  148. if (ce->ce_flags & CE_UPDATE)
  149. continue;
  150. if (!pathspec_match(pattern, m, ce->name, 0))
  151. continue;
  152. string_list_insert(ce->name, list);
  153. }
  154. return report_path_error(m, pattern, prefix ? strlen(prefix) : 0);
  155. }
  156. static void add_remove_files(struct string_list *list)
  157. {
  158. int i;
  159. for (i = 0; i < list->nr; i++) {
  160. struct stat st;
  161. struct string_list_item *p = &(list->items[i]);
  162. if (!lstat(p->string, &st)) {
  163. if (add_to_cache(p->string, &st, 0))
  164. die("updating files failed");
  165. } else
  166. remove_file_from_cache(p->string);
  167. }
  168. }
  169. static void create_base_index(void)
  170. {
  171. struct tree *tree;
  172. struct unpack_trees_options opts;
  173. struct tree_desc t;
  174. if (initial_commit) {
  175. discard_cache();
  176. return;
  177. }
  178. memset(&opts, 0, sizeof(opts));
  179. opts.head_idx = 1;
  180. opts.index_only = 1;
  181. opts.merge = 1;
  182. opts.src_index = &the_index;
  183. opts.dst_index = &the_index;
  184. opts.fn = oneway_merge;
  185. tree = parse_tree_indirect(head_sha1);
  186. if (!tree)
  187. die("failed to unpack HEAD tree object");
  188. parse_tree(tree);
  189. init_tree_desc(&t, tree->buffer, tree->size);
  190. if (unpack_trees(1, &t, &opts))
  191. exit(128); /* We've already reported the error, finish dying */
  192. }
  193. static char *prepare_index(int argc, const char **argv, const char *prefix)
  194. {
  195. int fd;
  196. struct string_list partial;
  197. const char **pathspec = NULL;
  198. if (interactive) {
  199. interactive_add(argc, argv, prefix);
  200. if (read_cache() < 0)
  201. die("index file corrupt");
  202. commit_style = COMMIT_AS_IS;
  203. return get_index_file();
  204. }
  205. if (read_cache() < 0)
  206. die("index file corrupt");
  207. if (*argv)
  208. pathspec = get_pathspec(prefix, argv);
  209. /*
  210. * Non partial, non as-is commit.
  211. *
  212. * (1) get the real index;
  213. * (2) update the_index as necessary;
  214. * (3) write the_index out to the real index (still locked);
  215. * (4) return the name of the locked index file.
  216. *
  217. * The caller should run hooks on the locked real index, and
  218. * (A) if all goes well, commit the real index;
  219. * (B) on failure, rollback the real index.
  220. */
  221. if (all || (also && pathspec && *pathspec)) {
  222. int fd = hold_locked_index(&index_lock, 1);
  223. add_files_to_cache(also ? prefix : NULL, pathspec, 0);
  224. refresh_cache(REFRESH_QUIET);
  225. if (write_cache(fd, active_cache, active_nr) ||
  226. close_lock_file(&index_lock))
  227. die("unable to write new_index file");
  228. commit_style = COMMIT_NORMAL;
  229. return index_lock.filename;
  230. }
  231. /*
  232. * As-is commit.
  233. *
  234. * (1) return the name of the real index file.
  235. *
  236. * The caller should run hooks on the real index, and run
  237. * hooks on the real index, and create commit from the_index.
  238. * We still need to refresh the index here.
  239. */
  240. if (!pathspec || !*pathspec) {
  241. fd = hold_locked_index(&index_lock, 1);
  242. refresh_cache(REFRESH_QUIET);
  243. if (write_cache(fd, active_cache, active_nr) ||
  244. commit_locked_index(&index_lock))
  245. die("unable to write new_index file");
  246. commit_style = COMMIT_AS_IS;
  247. return get_index_file();
  248. }
  249. /*
  250. * A partial commit.
  251. *
  252. * (0) find the set of affected paths;
  253. * (1) get lock on the real index file;
  254. * (2) update the_index with the given paths;
  255. * (3) write the_index out to the real index (still locked);
  256. * (4) get lock on the false index file;
  257. * (5) reset the_index from HEAD;
  258. * (6) update the_index the same way as (2);
  259. * (7) write the_index out to the false index file;
  260. * (8) return the name of the false index file (still locked);
  261. *
  262. * The caller should run hooks on the locked false index, and
  263. * create commit from it. Then
  264. * (A) if all goes well, commit the real index;
  265. * (B) on failure, rollback the real index;
  266. * In either case, rollback the false index.
  267. */
  268. commit_style = COMMIT_PARTIAL;
  269. if (file_exists(git_path("MERGE_HEAD")))
  270. die("cannot do a partial commit during a merge.");
  271. memset(&partial, 0, sizeof(partial));
  272. partial.strdup_strings = 1;
  273. if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec))
  274. exit(1);
  275. discard_cache();
  276. if (read_cache() < 0)
  277. die("cannot read the index");
  278. fd = hold_locked_index(&index_lock, 1);
  279. add_remove_files(&partial);
  280. refresh_cache(REFRESH_QUIET);
  281. if (write_cache(fd, active_cache, active_nr) ||
  282. close_lock_file(&index_lock))
  283. die("unable to write new_index file");
  284. fd = hold_lock_file_for_update(&false_lock,
  285. git_path("next-index-%"PRIuMAX, (uintmax_t) getpid()), 1);
  286. create_base_index();
  287. add_remove_files(&partial);
  288. refresh_cache(REFRESH_QUIET);
  289. if (write_cache(fd, active_cache, active_nr) ||
  290. close_lock_file(&false_lock))
  291. die("unable to write temporary index file");
  292. discard_cache();
  293. read_cache_from(false_lock.filename);
  294. return false_lock.filename;
  295. }
  296. static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn)
  297. {
  298. struct wt_status s;
  299. wt_status_prepare(&s);
  300. if (wt_status_relative_paths)
  301. s.prefix = prefix;
  302. if (amend) {
  303. s.amend = 1;
  304. s.reference = "HEAD^1";
  305. }
  306. s.verbose = verbose;
  307. s.untracked = (show_untracked_files == SHOW_ALL_UNTRACKED_FILES);
  308. s.index_file = index_file;
  309. s.fp = fp;
  310. s.nowarn = nowarn;
  311. wt_status_print(&s);
  312. return s.commitable;
  313. }
  314. static int run_hook(const char *index_file, const char *name, ...)
  315. {
  316. struct child_process hook;
  317. const char *argv[10], *env[2];
  318. char index[PATH_MAX];
  319. va_list args;
  320. int i;
  321. va_start(args, name);
  322. argv[0] = git_path("hooks/%s", name);
  323. i = 0;
  324. do {
  325. if (++i >= ARRAY_SIZE(argv))
  326. die ("run_hook(): too many arguments");
  327. argv[i] = va_arg(args, const char *);
  328. } while (argv[i]);
  329. va_end(args);
  330. snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
  331. env[0] = index;
  332. env[1] = NULL;
  333. if (access(argv[0], X_OK) < 0)
  334. return 0;
  335. memset(&hook, 0, sizeof(hook));
  336. hook.argv = argv;
  337. hook.no_stdin = 1;
  338. hook.stdout_to_stderr = 1;
  339. hook.env = env;
  340. return run_command(&hook);
  341. }
  342. static int is_a_merge(const unsigned char *sha1)
  343. {
  344. struct commit *commit = lookup_commit(sha1);
  345. if (!commit || parse_commit(commit))
  346. die("could not parse HEAD commit");
  347. return !!(commit->parents && commit->parents->next);
  348. }
  349. static const char sign_off_header[] = "Signed-off-by: ";
  350. static void determine_author_info(void)
  351. {
  352. char *name, *email, *date;
  353. name = getenv("GIT_AUTHOR_NAME");
  354. email = getenv("GIT_AUTHOR_EMAIL");
  355. date = getenv("GIT_AUTHOR_DATE");
  356. if (use_message) {
  357. const char *a, *lb, *rb, *eol;
  358. a = strstr(use_message_buffer, "\nauthor ");
  359. if (!a)
  360. die("invalid commit: %s", use_message);
  361. lb = strstr(a + 8, " <");
  362. rb = strstr(a + 8, "> ");
  363. eol = strchr(a + 8, '\n');
  364. if (!lb || !rb || !eol)
  365. die("invalid commit: %s", use_message);
  366. name = xstrndup(a + 8, lb - (a + 8));
  367. email = xstrndup(lb + 2, rb - (lb + 2));
  368. date = xstrndup(rb + 2, eol - (rb + 2));
  369. }
  370. if (force_author) {
  371. const char *lb = strstr(force_author, " <");
  372. const char *rb = strchr(force_author, '>');
  373. if (!lb || !rb)
  374. die("malformed --author parameter");
  375. name = xstrndup(force_author, lb - force_author);
  376. email = xstrndup(lb + 2, rb - (lb + 2));
  377. }
  378. author_name = name;
  379. author_email = email;
  380. author_date = date;
  381. }
  382. static int prepare_to_commit(const char *index_file, const char *prefix)
  383. {
  384. struct stat statbuf;
  385. int commitable, saved_color_setting;
  386. struct strbuf sb;
  387. char *buffer;
  388. FILE *fp;
  389. const char *hook_arg1 = NULL;
  390. const char *hook_arg2 = NULL;
  391. int ident_shown = 0;
  392. if (!no_verify && run_hook(index_file, "pre-commit", NULL))
  393. return 0;
  394. strbuf_init(&sb, 0);
  395. if (message.len) {
  396. strbuf_addbuf(&sb, &message);
  397. hook_arg1 = "message";
  398. } else if (logfile && !strcmp(logfile, "-")) {
  399. if (isatty(0))
  400. fprintf(stderr, "(reading log message from standard input)\n");
  401. if (strbuf_read(&sb, 0, 0) < 0)
  402. die("could not read log from standard input");
  403. hook_arg1 = "message";
  404. } else if (logfile) {
  405. if (strbuf_read_file(&sb, logfile, 0) < 0)
  406. die("could not read log file '%s': %s",
  407. logfile, strerror(errno));
  408. hook_arg1 = "message";
  409. } else if (use_message) {
  410. buffer = strstr(use_message_buffer, "\n\n");
  411. if (!buffer || buffer[2] == '\0')
  412. die("commit has empty message");
  413. strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
  414. hook_arg1 = "commit";
  415. hook_arg2 = use_message;
  416. } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
  417. if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
  418. die("could not read MERGE_MSG: %s", strerror(errno));
  419. hook_arg1 = "merge";
  420. } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
  421. if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
  422. die("could not read SQUASH_MSG: %s", strerror(errno));
  423. hook_arg1 = "squash";
  424. } else if (template_file && !stat(template_file, &statbuf)) {
  425. if (strbuf_read_file(&sb, template_file, 0) < 0)
  426. die("could not read %s: %s",
  427. template_file, strerror(errno));
  428. hook_arg1 = "template";
  429. }
  430. /*
  431. * This final case does not modify the template message,
  432. * it just sets the argument to the prepare-commit-msg hook.
  433. */
  434. else if (in_merge)
  435. hook_arg1 = "merge";
  436. fp = fopen(git_path(commit_editmsg), "w");
  437. if (fp == NULL)
  438. die("could not open %s: %s",
  439. git_path(commit_editmsg), strerror(errno));
  440. if (cleanup_mode != CLEANUP_NONE)
  441. stripspace(&sb, 0);
  442. if (signoff) {
  443. struct strbuf sob;
  444. int i;
  445. strbuf_init(&sob, 0);
  446. strbuf_addstr(&sob, sign_off_header);
  447. strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
  448. getenv("GIT_COMMITTER_EMAIL")));
  449. strbuf_addch(&sob, '\n');
  450. for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
  451. ; /* do nothing */
  452. if (prefixcmp(sb.buf + i, sob.buf)) {
  453. if (prefixcmp(sb.buf + i, sign_off_header))
  454. strbuf_addch(&sb, '\n');
  455. strbuf_addbuf(&sb, &sob);
  456. }
  457. strbuf_release(&sob);
  458. }
  459. if (fwrite(sb.buf, 1, sb.len, fp) < sb.len)
  460. die("could not write commit template: %s", strerror(errno));
  461. strbuf_release(&sb);
  462. determine_author_info();
  463. /* This checks if committer ident is explicitly given */
  464. git_committer_info(0);
  465. if (use_editor) {
  466. char *author_ident;
  467. const char *committer_ident;
  468. if (in_merge)
  469. fprintf(fp,
  470. "#\n"
  471. "# It looks like you may be committing a MERGE.\n"
  472. "# If this is not correct, please remove the file\n"
  473. "# %s\n"
  474. "# and try again.\n"
  475. "#\n",
  476. git_path("MERGE_HEAD"));
  477. fprintf(fp,
  478. "\n"
  479. "# Please enter the commit message for your changes.");
  480. if (cleanup_mode == CLEANUP_ALL)
  481. fprintf(fp,
  482. " Lines starting\n"
  483. "# with '#' will be ignored, and an empty"
  484. " message aborts the commit.\n");
  485. else /* CLEANUP_SPACE, that is. */
  486. fprintf(fp,
  487. " Lines starting\n"
  488. "# with '#' will be kept; you may remove them"
  489. " yourself if you want to.\n"
  490. "# An empty message aborts the commit.\n");
  491. if (only_include_assumed)
  492. fprintf(fp, "# %s\n", only_include_assumed);
  493. author_ident = xstrdup(fmt_name(author_name, author_email));
  494. committer_ident = fmt_name(getenv("GIT_COMMITTER_NAME"),
  495. getenv("GIT_COMMITTER_EMAIL"));
  496. if (strcmp(author_ident, committer_ident))
  497. fprintf(fp,
  498. "%s"
  499. "# Author: %s\n",
  500. ident_shown++ ? "" : "#\n",
  501. author_ident);
  502. free(author_ident);
  503. if (!user_ident_explicitly_given)
  504. fprintf(fp,
  505. "%s"
  506. "# Committer: %s\n",
  507. ident_shown++ ? "" : "#\n",
  508. committer_ident);
  509. if (ident_shown)
  510. fprintf(fp, "#\n");
  511. saved_color_setting = wt_status_use_color;
  512. wt_status_use_color = 0;
  513. commitable = run_status(fp, index_file, prefix, 1);
  514. wt_status_use_color = saved_color_setting;
  515. } else {
  516. struct rev_info rev;
  517. unsigned char sha1[20];
  518. const char *parent = "HEAD";
  519. if (!active_nr && read_cache() < 0)
  520. die("Cannot read index");
  521. if (amend)
  522. parent = "HEAD^1";
  523. if (get_sha1(parent, sha1))
  524. commitable = !!active_nr;
  525. else {
  526. init_revisions(&rev, "");
  527. rev.abbrev = 0;
  528. setup_revisions(0, NULL, &rev, parent);
  529. DIFF_OPT_SET(&rev.diffopt, QUIET);
  530. DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
  531. run_diff_index(&rev, 1 /* cached */);
  532. commitable = !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
  533. }
  534. }
  535. fclose(fp);
  536. if (!commitable && !in_merge && !allow_empty &&
  537. !(amend && is_a_merge(head_sha1))) {
  538. run_status(stdout, index_file, prefix, 0);
  539. unlink(commit_editmsg);
  540. return 0;
  541. }
  542. /*
  543. * Re-read the index as pre-commit hook could have updated it,
  544. * and write it out as a tree. We must do this before we invoke
  545. * the editor and after we invoke run_status above.
  546. */
  547. discard_cache();
  548. read_cache_from(index_file);
  549. if (!active_cache_tree)
  550. active_cache_tree = cache_tree();
  551. if (cache_tree_update(active_cache_tree,
  552. active_cache, active_nr, 0, 0) < 0) {
  553. error("Error building trees");
  554. return 0;
  555. }
  556. if (run_hook(index_file, "prepare-commit-msg",
  557. git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
  558. return 0;
  559. if (use_editor) {
  560. char index[PATH_MAX];
  561. const char *env[2] = { index, NULL };
  562. snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
  563. if (launch_editor(git_path(commit_editmsg), NULL, env)) {
  564. fprintf(stderr,
  565. "Please supply the message using either -m or -F option.\n");
  566. exit(1);
  567. }
  568. }
  569. if (!no_verify &&
  570. run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
  571. return 0;
  572. }
  573. return 1;
  574. }
  575. /*
  576. * Find out if the message starting at position 'start' in the strbuf
  577. * contains only whitespace and Signed-off-by lines.
  578. */
  579. static int message_is_empty(struct strbuf *sb, int start)
  580. {
  581. struct strbuf tmpl;
  582. const char *nl;
  583. int eol, i;
  584. if (cleanup_mode == CLEANUP_NONE && sb->len)
  585. return 0;
  586. /* See if the template is just a prefix of the message. */
  587. strbuf_init(&tmpl, 0);
  588. if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
  589. stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
  590. if (start + tmpl.len <= sb->len &&
  591. memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
  592. start += tmpl.len;
  593. }
  594. strbuf_release(&tmpl);
  595. /* Check if the rest is just whitespace and Signed-of-by's. */
  596. for (i = start; i < sb->len; i++) {
  597. nl = memchr(sb->buf + i, '\n', sb->len - i);
  598. if (nl)
  599. eol = nl - sb->buf;
  600. else
  601. eol = sb->len;
  602. if (strlen(sign_off_header) <= eol - i &&
  603. !prefixcmp(sb->buf + i, sign_off_header)) {
  604. i = eol;
  605. continue;
  606. }
  607. while (i < eol)
  608. if (!isspace(sb->buf[i++]))
  609. return 0;
  610. }
  611. return 1;
  612. }
  613. static int parse_and_validate_options(int argc, const char *argv[],
  614. const char * const usage[],
  615. const char *prefix)
  616. {
  617. int f = 0;
  618. argc = parse_options(argc, argv, builtin_commit_options, usage, 0);
  619. logfile = parse_options_fix_filename(prefix, logfile);
  620. template_file = parse_options_fix_filename(prefix, template_file);
  621. if (logfile || message.len || use_message)
  622. use_editor = 0;
  623. if (edit_flag)
  624. use_editor = 1;
  625. if (!use_editor)
  626. setenv("GIT_EDITOR", ":", 1);
  627. if (get_sha1("HEAD", head_sha1))
  628. initial_commit = 1;
  629. if (!get_sha1("MERGE_HEAD", merge_head_sha1))
  630. in_merge = 1;
  631. /* Sanity check options */
  632. if (amend && initial_commit)
  633. die("You have nothing to amend.");
  634. if (amend && in_merge)
  635. die("You are in the middle of a merge -- cannot amend.");
  636. if (use_message)
  637. f++;
  638. if (edit_message)
  639. f++;
  640. if (logfile)
  641. f++;
  642. if (f > 1)
  643. die("Only one of -c/-C/-F can be used.");
  644. if (message.len && f > 0)
  645. die("Option -m cannot be combined with -c/-C/-F.");
  646. if (edit_message)
  647. use_message = edit_message;
  648. if (amend && !use_message)
  649. use_message = "HEAD";
  650. if (use_message) {
  651. unsigned char sha1[20];
  652. static char utf8[] = "UTF-8";
  653. const char *out_enc;
  654. char *enc, *end;
  655. struct commit *commit;
  656. if (get_sha1(use_message, sha1))
  657. die("could not lookup commit %s", use_message);
  658. commit = lookup_commit_reference(sha1);
  659. if (!commit || parse_commit(commit))
  660. die("could not parse commit %s", use_message);
  661. enc = strstr(commit->buffer, "\nencoding");
  662. if (enc) {
  663. end = strchr(enc + 10, '\n');
  664. enc = xstrndup(enc + 10, end - (enc + 10));
  665. } else {
  666. enc = utf8;
  667. }
  668. out_enc = git_commit_encoding ? git_commit_encoding : utf8;
  669. if (strcmp(out_enc, enc))
  670. use_message_buffer =
  671. reencode_string(commit->buffer, out_enc, enc);
  672. /*
  673. * If we failed to reencode the buffer, just copy it
  674. * byte for byte so the user can try to fix it up.
  675. * This also handles the case where input and output
  676. * encodings are identical.
  677. */
  678. if (use_message_buffer == NULL)
  679. use_message_buffer = xstrdup(commit->buffer);
  680. if (enc != utf8)
  681. free(enc);
  682. }
  683. if (!!also + !!only + !!all + !!interactive > 1)
  684. die("Only one of --include/--only/--all/--interactive can be used.");
  685. if (argc == 0 && (also || (only && !amend)))
  686. die("No paths with --include/--only does not make sense.");
  687. if (argc == 0 && only && amend)
  688. only_include_assumed = "Clever... amending the last one with dirty index.";
  689. if (argc > 0 && !also && !only)
  690. only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths...";
  691. if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
  692. cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
  693. else if (!strcmp(cleanup_arg, "verbatim"))
  694. cleanup_mode = CLEANUP_NONE;
  695. else if (!strcmp(cleanup_arg, "whitespace"))
  696. cleanup_mode = CLEANUP_SPACE;
  697. else if (!strcmp(cleanup_arg, "strip"))
  698. cleanup_mode = CLEANUP_ALL;
  699. else
  700. die("Invalid cleanup mode %s", cleanup_arg);
  701. if (!untracked_files_arg)
  702. ; /* default already initialized */
  703. else if (!strcmp(untracked_files_arg, "no"))
  704. show_untracked_files = SHOW_NO_UNTRACKED_FILES;
  705. else if (!strcmp(untracked_files_arg, "normal"))
  706. show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
  707. else if (!strcmp(untracked_files_arg, "all"))
  708. show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
  709. else
  710. die("Invalid untracked files mode '%s'", untracked_files_arg);
  711. if (all && argc > 0)
  712. die("Paths with -a does not make sense.");
  713. else if (interactive && argc > 0)
  714. die("Paths with --interactive does not make sense.");
  715. return argc;
  716. }
  717. int cmd_status(int argc, const char **argv, const char *prefix)
  718. {
  719. const char *index_file;
  720. int commitable;
  721. git_config(git_status_config, NULL);
  722. if (wt_status_use_color == -1)
  723. wt_status_use_color = git_use_color_default;
  724. argc = parse_and_validate_options(argc, argv, builtin_status_usage, prefix);
  725. index_file = prepare_index(argc, argv, prefix);
  726. commitable = run_status(stdout, index_file, prefix, 0);
  727. rollback_index_files();
  728. return commitable ? 0 : 1;
  729. }
  730. static void print_summary(const char *prefix, const unsigned char *sha1)
  731. {
  732. struct rev_info rev;
  733. struct commit *commit;
  734. commit = lookup_commit(sha1);
  735. if (!commit)
  736. die("couldn't look up newly created commit");
  737. if (!commit || parse_commit(commit))
  738. die("could not parse newly created commit");
  739. init_revisions(&rev, prefix);
  740. setup_revisions(0, NULL, &rev, NULL);
  741. rev.abbrev = 0;
  742. rev.diff = 1;
  743. rev.diffopt.output_format =
  744. DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
  745. rev.verbose_header = 1;
  746. rev.show_root_diff = 1;
  747. get_commit_format("format:%h: %s", &rev);
  748. rev.always_show_header = 0;
  749. rev.diffopt.detect_rename = 1;
  750. rev.diffopt.rename_limit = 100;
  751. rev.diffopt.break_opt = 0;
  752. diff_setup_done(&rev.diffopt);
  753. printf("Created %scommit ", initial_commit ? "initial " : "");
  754. if (!log_tree_commit(&rev, commit)) {
  755. struct strbuf buf = STRBUF_INIT;
  756. format_commit_message(commit, "%h: %s", &buf, DATE_NORMAL);
  757. printf("%s\n", buf.buf);
  758. strbuf_release(&buf);
  759. }
  760. }
  761. static int git_commit_config(const char *k, const char *v, void *cb)
  762. {
  763. if (!strcmp(k, "commit.template"))
  764. return git_config_string(&template_file, k, v);
  765. return git_status_config(k, v, cb);
  766. }
  767. static const char commit_utf8_warn[] =
  768. "Warning: commit message does not conform to UTF-8.\n"
  769. "You may want to amend it after fixing the message, or set the config\n"
  770. "variable i18n.commitencoding to the encoding your project uses.\n";
  771. static void add_parent(struct strbuf *sb, const unsigned char *sha1)
  772. {
  773. struct object *obj = parse_object(sha1);
  774. const char *parent = sha1_to_hex(sha1);
  775. const char *cp;
  776. if (!obj)
  777. die("Unable to find commit parent %s", parent);
  778. if (obj->type != OBJ_COMMIT)
  779. die("Parent %s isn't a proper commit", parent);
  780. for (cp = sb->buf; cp && (cp = strstr(cp, "\nparent ")); cp += 8) {
  781. if (!memcmp(cp + 8, parent, 40) && cp[48] == '\n') {
  782. error("duplicate parent %s ignored", parent);
  783. return;
  784. }
  785. }
  786. strbuf_addf(sb, "parent %s\n", parent);
  787. }
  788. int cmd_commit(int argc, const char **argv, const char *prefix)
  789. {
  790. int header_len;
  791. struct strbuf sb;
  792. const char *index_file, *reflog_msg;
  793. char *nl, *p;
  794. unsigned char commit_sha1[20];
  795. struct ref_lock *ref_lock;
  796. git_config(git_commit_config, NULL);
  797. argc = parse_and_validate_options(argc, argv, builtin_commit_usage, prefix);
  798. index_file = prepare_index(argc, argv, prefix);
  799. /* Set up everything for writing the commit object. This includes
  800. running hooks, writing the trees, and interacting with the user. */
  801. if (!prepare_to_commit(index_file, prefix)) {
  802. rollback_index_files();
  803. return 1;
  804. }
  805. /*
  806. * The commit object
  807. */
  808. strbuf_init(&sb, 0);
  809. strbuf_addf(&sb, "tree %s\n",
  810. sha1_to_hex(active_cache_tree->sha1));
  811. /* Determine parents */
  812. if (initial_commit) {
  813. reflog_msg = "commit (initial)";
  814. } else if (amend) {
  815. struct commit_list *c;
  816. struct commit *commit;
  817. reflog_msg = "commit (amend)";
  818. commit = lookup_commit(head_sha1);
  819. if (!commit || parse_commit(commit))
  820. die("could not parse HEAD commit");
  821. for (c = commit->parents; c; c = c->next)
  822. add_parent(&sb, c->item->object.sha1);
  823. } else if (in_merge) {
  824. struct strbuf m;
  825. FILE *fp;
  826. reflog_msg = "commit (merge)";
  827. add_parent(&sb, head_sha1);
  828. strbuf_init(&m, 0);
  829. fp = fopen(git_path("MERGE_HEAD"), "r");
  830. if (fp == NULL)
  831. die("could not open %s for reading: %s",
  832. git_path("MERGE_HEAD"), strerror(errno));
  833. while (strbuf_getline(&m, fp, '\n') != EOF) {
  834. unsigned char sha1[20];
  835. if (get_sha1_hex(m.buf, sha1) < 0)
  836. die("Corrupt MERGE_HEAD file (%s)", m.buf);
  837. add_parent(&sb, sha1);
  838. }
  839. fclose(fp);
  840. strbuf_release(&m);
  841. } else {
  842. reflog_msg = "commit";
  843. strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
  844. }
  845. strbuf_addf(&sb, "author %s\n",
  846. fmt_ident(author_name, author_email, author_date, IDENT_ERROR_ON_NO_NAME));
  847. strbuf_addf(&sb, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
  848. if (!is_encoding_utf8(git_commit_encoding))
  849. strbuf_addf(&sb, "encoding %s\n", git_commit_encoding);
  850. strbuf_addch(&sb, '\n');
  851. /* Finally, get the commit message */
  852. header_len = sb.len;
  853. if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
  854. rollback_index_files();
  855. die("could not read commit message");
  856. }
  857. /* Truncate the message just before the diff, if any. */
  858. p = strstr(sb.buf, "\ndiff --git a/");
  859. if (p != NULL)
  860. strbuf_setlen(&sb, p - sb.buf + 1);
  861. if (cleanup_mode != CLEANUP_NONE)
  862. stripspace(&sb, cleanup_mode == CLEANUP_ALL);
  863. if (sb.len < header_len || message_is_empty(&sb, header_len)) {
  864. rollback_index_files();
  865. fprintf(stderr, "Aborting commit due to empty commit message.\n");
  866. exit(1);
  867. }
  868. strbuf_addch(&sb, '\0');
  869. if (is_encoding_utf8(git_commit_encoding) && !is_utf8(sb.buf))
  870. fprintf(stderr, commit_utf8_warn);
  871. if (write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1)) {
  872. rollback_index_files();
  873. die("failed to write commit object");
  874. }
  875. ref_lock = lock_any_ref_for_update("HEAD",
  876. initial_commit ? NULL : head_sha1,
  877. 0);
  878. nl = strchr(sb.buf + header_len, '\n');
  879. if (nl)
  880. strbuf_setlen(&sb, nl + 1 - sb.buf);
  881. else
  882. strbuf_addch(&sb, '\n');
  883. strbuf_remove(&sb, 0, header_len);
  884. strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
  885. strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
  886. if (!ref_lock) {
  887. rollback_index_files();
  888. die("cannot lock HEAD ref");
  889. }
  890. if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) {
  891. rollback_index_files();
  892. die("cannot update HEAD ref");
  893. }
  894. unlink(git_path("MERGE_HEAD"));
  895. unlink(git_path("MERGE_MSG"));
  896. unlink(git_path("SQUASH_MSG"));
  897. if (commit_index_files())
  898. die ("Repository has been updated, but unable to write\n"
  899. "new_index file. Check that disk is not full or quota is\n"
  900. "not exceeded, and then \"git reset HEAD\" to recover.");
  901. rerere();
  902. run_hook(get_index_file(), "post-commit", NULL);
  903. if (!quiet)
  904. print_summary(prefix, commit_sha1);
  905. return 0;
  906. }