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.

1717 lines
51KB

  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. #define USE_THE_INDEX_COMPATIBILITY_MACROS
  8. #include "cache.h"
  9. #include "config.h"
  10. #include "lockfile.h"
  11. #include "cache-tree.h"
  12. #include "color.h"
  13. #include "dir.h"
  14. #include "builtin.h"
  15. #include "diff.h"
  16. #include "diffcore.h"
  17. #include "commit.h"
  18. #include "revision.h"
  19. #include "wt-status.h"
  20. #include "run-command.h"
  21. #include "refs.h"
  22. #include "log-tree.h"
  23. #include "strbuf.h"
  24. #include "utf8.h"
  25. #include "parse-options.h"
  26. #include "string-list.h"
  27. #include "rerere.h"
  28. #include "unpack-trees.h"
  29. #include "quote.h"
  30. #include "submodule.h"
  31. #include "gpg-interface.h"
  32. #include "column.h"
  33. #include "sequencer.h"
  34. #include "mailmap.h"
  35. #include "help.h"
  36. #include "commit-reach.h"
  37. #include "commit-graph.h"
  38. static const char * const builtin_commit_usage[] = {
  39. N_("git commit [<options>] [--] <pathspec>..."),
  40. NULL
  41. };
  42. static const char * const builtin_status_usage[] = {
  43. N_("git status [<options>] [--] <pathspec>..."),
  44. NULL
  45. };
  46. static const char empty_amend_advice[] =
  47. N_("You asked to amend the most recent commit, but doing so would make\n"
  48. "it empty. You can repeat your command with --allow-empty, or you can\n"
  49. "remove the commit entirely with \"git reset HEAD^\".\n");
  50. static const char empty_cherry_pick_advice[] =
  51. N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
  52. "If you wish to commit it anyway, use:\n"
  53. "\n"
  54. " git commit --allow-empty\n"
  55. "\n");
  56. static const char empty_cherry_pick_advice_single[] =
  57. N_("Otherwise, please use 'git cherry-pick --skip'\n");
  58. static const char empty_cherry_pick_advice_multi[] =
  59. N_("and then use:\n"
  60. "\n"
  61. " git cherry-pick --continue\n"
  62. "\n"
  63. "to resume cherry-picking the remaining commits.\n"
  64. "If you wish to skip this commit, use:\n"
  65. "\n"
  66. " git cherry-pick --skip\n"
  67. "\n");
  68. static const char *color_status_slots[] = {
  69. [WT_STATUS_HEADER] = "header",
  70. [WT_STATUS_UPDATED] = "updated",
  71. [WT_STATUS_CHANGED] = "changed",
  72. [WT_STATUS_UNTRACKED] = "untracked",
  73. [WT_STATUS_NOBRANCH] = "noBranch",
  74. [WT_STATUS_UNMERGED] = "unmerged",
  75. [WT_STATUS_LOCAL_BRANCH] = "localBranch",
  76. [WT_STATUS_REMOTE_BRANCH] = "remoteBranch",
  77. [WT_STATUS_ONBRANCH] = "branch",
  78. };
  79. static const char *use_message_buffer;
  80. static struct lock_file index_lock; /* real index */
  81. static struct lock_file false_lock; /* used only for partial commits */
  82. static enum {
  83. COMMIT_AS_IS = 1,
  84. COMMIT_NORMAL,
  85. COMMIT_PARTIAL
  86. } commit_style;
  87. static const char *logfile, *force_author;
  88. static const char *template_file;
  89. /*
  90. * The _message variables are commit names from which to take
  91. * the commit message and/or authorship.
  92. */
  93. static const char *author_message, *author_message_buffer;
  94. static char *edit_message, *use_message;
  95. static char *fixup_message, *squash_message;
  96. static int all, also, interactive, patch_interactive, only, amend, signoff;
  97. static int edit_flag = -1; /* unspecified */
  98. static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
  99. static int config_commit_verbose = -1; /* unspecified */
  100. static int no_post_rewrite, allow_empty_message;
  101. static char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
  102. static char *sign_commit;
  103. /*
  104. * The default commit message cleanup mode will remove the lines
  105. * beginning with # (shell comments) and leading and trailing
  106. * whitespaces (empty lines or containing only whitespaces)
  107. * if editor is used, and only the whitespaces if the message
  108. * is specified explicitly.
  109. */
  110. static enum commit_msg_cleanup_mode cleanup_mode;
  111. static const char *cleanup_arg;
  112. static enum commit_whence whence;
  113. static int sequencer_in_use;
  114. static int use_editor = 1, include_status = 1;
  115. static int have_option_m;
  116. static struct strbuf message = STRBUF_INIT;
  117. static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED;
  118. static int opt_parse_porcelain(const struct option *opt, const char *arg, int unset)
  119. {
  120. enum wt_status_format *value = (enum wt_status_format *)opt->value;
  121. if (unset)
  122. *value = STATUS_FORMAT_NONE;
  123. else if (!arg)
  124. *value = STATUS_FORMAT_PORCELAIN;
  125. else if (!strcmp(arg, "v1") || !strcmp(arg, "1"))
  126. *value = STATUS_FORMAT_PORCELAIN;
  127. else if (!strcmp(arg, "v2") || !strcmp(arg, "2"))
  128. *value = STATUS_FORMAT_PORCELAIN_V2;
  129. else
  130. die("unsupported porcelain version '%s'", arg);
  131. return 0;
  132. }
  133. static int opt_parse_m(const struct option *opt, const char *arg, int unset)
  134. {
  135. struct strbuf *buf = opt->value;
  136. if (unset) {
  137. have_option_m = 0;
  138. strbuf_setlen(buf, 0);
  139. } else {
  140. have_option_m = 1;
  141. if (buf->len)
  142. strbuf_addch(buf, '\n');
  143. strbuf_addstr(buf, arg);
  144. strbuf_complete_line(buf);
  145. }
  146. return 0;
  147. }
  148. static int opt_parse_rename_score(const struct option *opt, const char *arg, int unset)
  149. {
  150. const char **value = opt->value;
  151. BUG_ON_OPT_NEG(unset);
  152. if (arg != NULL && *arg == '=')
  153. arg = arg + 1;
  154. *value = arg;
  155. return 0;
  156. }
  157. static void determine_whence(struct wt_status *s)
  158. {
  159. if (file_exists(git_path_merge_head(the_repository)))
  160. whence = FROM_MERGE;
  161. else if (file_exists(git_path_cherry_pick_head(the_repository))) {
  162. whence = FROM_CHERRY_PICK;
  163. if (file_exists(git_path_seq_dir()))
  164. sequencer_in_use = 1;
  165. }
  166. else
  167. whence = FROM_COMMIT;
  168. if (s)
  169. s->whence = whence;
  170. }
  171. static void status_init_config(struct wt_status *s, config_fn_t fn)
  172. {
  173. wt_status_prepare(the_repository, s);
  174. init_diff_ui_defaults();
  175. git_config(fn, s);
  176. determine_whence(s);
  177. s->hints = advice_status_hints; /* must come after git_config() */
  178. }
  179. static void rollback_index_files(void)
  180. {
  181. switch (commit_style) {
  182. case COMMIT_AS_IS:
  183. break; /* nothing to do */
  184. case COMMIT_NORMAL:
  185. rollback_lock_file(&index_lock);
  186. break;
  187. case COMMIT_PARTIAL:
  188. rollback_lock_file(&index_lock);
  189. rollback_lock_file(&false_lock);
  190. break;
  191. }
  192. }
  193. static int commit_index_files(void)
  194. {
  195. int err = 0;
  196. switch (commit_style) {
  197. case COMMIT_AS_IS:
  198. break; /* nothing to do */
  199. case COMMIT_NORMAL:
  200. err = commit_lock_file(&index_lock);
  201. break;
  202. case COMMIT_PARTIAL:
  203. err = commit_lock_file(&index_lock);
  204. rollback_lock_file(&false_lock);
  205. break;
  206. }
  207. return err;
  208. }
  209. /*
  210. * Take a union of paths in the index and the named tree (typically, "HEAD"),
  211. * and return the paths that match the given pattern in list.
  212. */
  213. static int list_paths(struct string_list *list, const char *with_tree,
  214. const struct pathspec *pattern)
  215. {
  216. int i, ret;
  217. char *m;
  218. if (!pattern->nr)
  219. return 0;
  220. m = xcalloc(1, pattern->nr);
  221. if (with_tree) {
  222. char *max_prefix = common_prefix(pattern);
  223. overlay_tree_on_index(&the_index, with_tree, max_prefix);
  224. free(max_prefix);
  225. }
  226. for (i = 0; i < active_nr; i++) {
  227. const struct cache_entry *ce = active_cache[i];
  228. struct string_list_item *item;
  229. if (ce->ce_flags & CE_UPDATE)
  230. continue;
  231. if (!ce_path_match(&the_index, ce, pattern, m))
  232. continue;
  233. item = string_list_insert(list, ce->name);
  234. if (ce_skip_worktree(ce))
  235. item->util = item; /* better a valid pointer than a fake one */
  236. }
  237. ret = report_path_error(m, pattern);
  238. free(m);
  239. return ret;
  240. }
  241. static void add_remove_files(struct string_list *list)
  242. {
  243. int i;
  244. for (i = 0; i < list->nr; i++) {
  245. struct stat st;
  246. struct string_list_item *p = &(list->items[i]);
  247. /* p->util is skip-worktree */
  248. if (p->util)
  249. continue;
  250. if (!lstat(p->string, &st)) {
  251. if (add_to_cache(p->string, &st, 0))
  252. die(_("updating files failed"));
  253. } else
  254. remove_file_from_cache(p->string);
  255. }
  256. }
  257. static void create_base_index(const struct commit *current_head)
  258. {
  259. struct tree *tree;
  260. struct unpack_trees_options opts;
  261. struct tree_desc t;
  262. if (!current_head) {
  263. discard_cache();
  264. return;
  265. }
  266. memset(&opts, 0, sizeof(opts));
  267. opts.head_idx = 1;
  268. opts.index_only = 1;
  269. opts.merge = 1;
  270. opts.src_index = &the_index;
  271. opts.dst_index = &the_index;
  272. opts.fn = oneway_merge;
  273. tree = parse_tree_indirect(&current_head->object.oid);
  274. if (!tree)
  275. die(_("failed to unpack HEAD tree object"));
  276. parse_tree(tree);
  277. init_tree_desc(&t, tree->buffer, tree->size);
  278. if (unpack_trees(1, &t, &opts))
  279. exit(128); /* We've already reported the error, finish dying */
  280. }
  281. static void refresh_cache_or_die(int refresh_flags)
  282. {
  283. /*
  284. * refresh_flags contains REFRESH_QUIET, so the only errors
  285. * are for unmerged entries.
  286. */
  287. if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
  288. die_resolve_conflict("commit");
  289. }
  290. static const char *prepare_index(int argc, const char **argv, const char *prefix,
  291. const struct commit *current_head, int is_status)
  292. {
  293. struct string_list partial = STRING_LIST_INIT_DUP;
  294. struct pathspec pathspec;
  295. int refresh_flags = REFRESH_QUIET;
  296. const char *ret;
  297. if (is_status)
  298. refresh_flags |= REFRESH_UNMERGED;
  299. parse_pathspec(&pathspec, 0,
  300. PATHSPEC_PREFER_FULL,
  301. prefix, argv);
  302. if (read_cache_preload(&pathspec) < 0)
  303. die(_("index file corrupt"));
  304. if (interactive) {
  305. char *old_index_env = NULL;
  306. hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
  307. refresh_cache_or_die(refresh_flags);
  308. if (write_locked_index(&the_index, &index_lock, 0))
  309. die(_("unable to create temporary index"));
  310. old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
  311. setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);
  312. if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
  313. die(_("interactive add failed"));
  314. if (old_index_env && *old_index_env)
  315. setenv(INDEX_ENVIRONMENT, old_index_env, 1);
  316. else
  317. unsetenv(INDEX_ENVIRONMENT);
  318. FREE_AND_NULL(old_index_env);
  319. discard_cache();
  320. read_cache_from(get_lock_file_path(&index_lock));
  321. if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
  322. if (reopen_lock_file(&index_lock) < 0)
  323. die(_("unable to write index file"));
  324. if (write_locked_index(&the_index, &index_lock, 0))
  325. die(_("unable to update temporary index"));
  326. } else
  327. warning(_("Failed to update main cache tree"));
  328. commit_style = COMMIT_NORMAL;
  329. ret = get_lock_file_path(&index_lock);
  330. goto out;
  331. }
  332. /*
  333. * Non partial, non as-is commit.
  334. *
  335. * (1) get the real index;
  336. * (2) update the_index as necessary;
  337. * (3) write the_index out to the real index (still locked);
  338. * (4) return the name of the locked index file.
  339. *
  340. * The caller should run hooks on the locked real index, and
  341. * (A) if all goes well, commit the real index;
  342. * (B) on failure, rollback the real index.
  343. */
  344. if (all || (also && pathspec.nr)) {
  345. hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
  346. add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
  347. refresh_cache_or_die(refresh_flags);
  348. update_main_cache_tree(WRITE_TREE_SILENT);
  349. if (write_locked_index(&the_index, &index_lock, 0))
  350. die(_("unable to write new_index file"));
  351. commit_style = COMMIT_NORMAL;
  352. ret = get_lock_file_path(&index_lock);
  353. goto out;
  354. }
  355. /*
  356. * As-is commit.
  357. *
  358. * (1) return the name of the real index file.
  359. *
  360. * The caller should run hooks on the real index,
  361. * and create commit from the_index.
  362. * We still need to refresh the index here.
  363. */
  364. if (!only && !pathspec.nr) {
  365. hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
  366. refresh_cache_or_die(refresh_flags);
  367. if (active_cache_changed
  368. || !cache_tree_fully_valid(active_cache_tree))
  369. update_main_cache_tree(WRITE_TREE_SILENT);
  370. if (write_locked_index(&the_index, &index_lock,
  371. COMMIT_LOCK | SKIP_IF_UNCHANGED))
  372. die(_("unable to write new_index file"));
  373. commit_style = COMMIT_AS_IS;
  374. ret = get_index_file();
  375. goto out;
  376. }
  377. /*
  378. * A partial commit.
  379. *
  380. * (0) find the set of affected paths;
  381. * (1) get lock on the real index file;
  382. * (2) update the_index with the given paths;
  383. * (3) write the_index out to the real index (still locked);
  384. * (4) get lock on the false index file;
  385. * (5) reset the_index from HEAD;
  386. * (6) update the_index the same way as (2);
  387. * (7) write the_index out to the false index file;
  388. * (8) return the name of the false index file (still locked);
  389. *
  390. * The caller should run hooks on the locked false index, and
  391. * create commit from it. Then
  392. * (A) if all goes well, commit the real index;
  393. * (B) on failure, rollback the real index;
  394. * In either case, rollback the false index.
  395. */
  396. commit_style = COMMIT_PARTIAL;
  397. if (whence != FROM_COMMIT) {
  398. if (whence == FROM_MERGE)
  399. die(_("cannot do a partial commit during a merge."));
  400. else if (whence == FROM_CHERRY_PICK)
  401. die(_("cannot do a partial commit during a cherry-pick."));
  402. }
  403. if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
  404. exit(1);
  405. discard_cache();
  406. if (read_cache() < 0)
  407. die(_("cannot read the index"));
  408. hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
  409. add_remove_files(&partial);
  410. refresh_cache(REFRESH_QUIET);
  411. update_main_cache_tree(WRITE_TREE_SILENT);
  412. if (write_locked_index(&the_index, &index_lock, 0))
  413. die(_("unable to write new_index file"));
  414. hold_lock_file_for_update(&false_lock,
  415. git_path("next-index-%"PRIuMAX,
  416. (uintmax_t) getpid()),
  417. LOCK_DIE_ON_ERROR);
  418. create_base_index(current_head);
  419. add_remove_files(&partial);
  420. refresh_cache(REFRESH_QUIET);
  421. if (write_locked_index(&the_index, &false_lock, 0))
  422. die(_("unable to write temporary index file"));
  423. discard_cache();
  424. ret = get_lock_file_path(&false_lock);
  425. read_cache_from(ret);
  426. out:
  427. string_list_clear(&partial, 0);
  428. clear_pathspec(&pathspec);
  429. return ret;
  430. }
  431. static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
  432. struct wt_status *s)
  433. {
  434. struct object_id oid;
  435. if (s->relative_paths)
  436. s->prefix = prefix;
  437. if (amend) {
  438. s->amend = 1;
  439. s->reference = "HEAD^1";
  440. }
  441. s->verbose = verbose;
  442. s->index_file = index_file;
  443. s->fp = fp;
  444. s->nowarn = nowarn;
  445. s->is_initial = get_oid(s->reference, &oid) ? 1 : 0;
  446. if (!s->is_initial)
  447. oidcpy(&s->oid_commit, &oid);
  448. s->status_format = status_format;
  449. s->ignore_submodule_arg = ignore_submodule_arg;
  450. wt_status_collect(s);
  451. wt_status_print(s);
  452. wt_status_collect_free_buffers(s);
  453. return s->committable;
  454. }
  455. static int is_a_merge(const struct commit *current_head)
  456. {
  457. return !!(current_head->parents && current_head->parents->next);
  458. }
  459. static void assert_split_ident(struct ident_split *id, const struct strbuf *buf)
  460. {
  461. if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin)
  462. BUG("unable to parse our own ident: %s", buf->buf);
  463. }
  464. static void export_one(const char *var, const char *s, const char *e, int hack)
  465. {
  466. struct strbuf buf = STRBUF_INIT;
  467. if (hack)
  468. strbuf_addch(&buf, hack);
  469. strbuf_addf(&buf, "%.*s", (int)(e - s), s);
  470. setenv(var, buf.buf, 1);
  471. strbuf_release(&buf);
  472. }
  473. static int parse_force_date(const char *in, struct strbuf *out)
  474. {
  475. strbuf_addch(out, '@');
  476. if (parse_date(in, out) < 0) {
  477. int errors = 0;
  478. unsigned long t = approxidate_careful(in, &errors);
  479. if (errors)
  480. return -1;
  481. strbuf_addf(out, "%lu", t);
  482. }
  483. return 0;
  484. }
  485. static void set_ident_var(char **buf, char *val)
  486. {
  487. free(*buf);
  488. *buf = val;
  489. }
  490. static void determine_author_info(struct strbuf *author_ident)
  491. {
  492. char *name, *email, *date;
  493. struct ident_split author;
  494. name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
  495. email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
  496. date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
  497. if (author_message) {
  498. struct ident_split ident;
  499. size_t len;
  500. const char *a;
  501. a = find_commit_header(author_message_buffer, "author", &len);
  502. if (!a)
  503. die(_("commit '%s' lacks author header"), author_message);
  504. if (split_ident_line(&ident, a, len) < 0)
  505. die(_("commit '%s' has malformed author line"), author_message);
  506. set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
  507. set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
  508. if (ident.date_begin) {
  509. struct strbuf date_buf = STRBUF_INIT;
  510. strbuf_addch(&date_buf, '@');
  511. strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin);
  512. strbuf_addch(&date_buf, ' ');
  513. strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin);
  514. set_ident_var(&date, strbuf_detach(&date_buf, NULL));
  515. }
  516. }
  517. if (force_author) {
  518. struct ident_split ident;
  519. if (split_ident_line(&ident, force_author, strlen(force_author)) < 0)
  520. die(_("malformed --author parameter"));
  521. set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
  522. set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
  523. }
  524. if (force_date) {
  525. struct strbuf date_buf = STRBUF_INIT;
  526. if (parse_force_date(force_date, &date_buf))
  527. die(_("invalid date format: %s"), force_date);
  528. set_ident_var(&date, strbuf_detach(&date_buf, NULL));
  529. }
  530. strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date,
  531. IDENT_STRICT));
  532. assert_split_ident(&author, author_ident);
  533. export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
  534. export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
  535. export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
  536. free(name);
  537. free(email);
  538. free(date);
  539. }
  540. static int author_date_is_interesting(void)
  541. {
  542. return author_message || force_date;
  543. }
  544. static void adjust_comment_line_char(const struct strbuf *sb)
  545. {
  546. char candidates[] = "#;@!$%^&|:";
  547. char *candidate;
  548. const char *p;
  549. comment_line_char = candidates[0];
  550. if (!memchr(sb->buf, comment_line_char, sb->len))
  551. return;
  552. p = sb->buf;
  553. candidate = strchr(candidates, *p);
  554. if (candidate)
  555. *candidate = ' ';
  556. for (p = sb->buf; *p; p++) {
  557. if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
  558. candidate = strchr(candidates, p[1]);
  559. if (candidate)
  560. *candidate = ' ';
  561. }
  562. }
  563. for (p = candidates; *p == ' '; p++)
  564. ;
  565. if (!*p)
  566. die(_("unable to select a comment character that is not used\n"
  567. "in the current commit message"));
  568. comment_line_char = *p;
  569. }
  570. static int prepare_to_commit(const char *index_file, const char *prefix,
  571. struct commit *current_head,
  572. struct wt_status *s,
  573. struct strbuf *author_ident)
  574. {
  575. struct stat statbuf;
  576. struct strbuf committer_ident = STRBUF_INIT;
  577. int committable;
  578. struct strbuf sb = STRBUF_INIT;
  579. const char *hook_arg1 = NULL;
  580. const char *hook_arg2 = NULL;
  581. int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
  582. int old_display_comment_prefix;
  583. int merge_contains_scissors = 0;
  584. /* This checks and barfs if author is badly specified */
  585. determine_author_info(author_ident);
  586. if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL))
  587. return 0;
  588. if (squash_message) {
  589. /*
  590. * Insert the proper subject line before other commit
  591. * message options add their content.
  592. */
  593. if (use_message && !strcmp(use_message, squash_message))
  594. strbuf_addstr(&sb, "squash! ");
  595. else {
  596. struct pretty_print_context ctx = {0};
  597. struct commit *c;
  598. c = lookup_commit_reference_by_name(squash_message);
  599. if (!c)
  600. die(_("could not lookup commit %s"), squash_message);
  601. ctx.output_encoding = get_commit_output_encoding();
  602. format_commit_message(c, "squash! %s\n\n", &sb,
  603. &ctx);
  604. }
  605. }
  606. if (have_option_m && !fixup_message) {
  607. strbuf_addbuf(&sb, &message);
  608. hook_arg1 = "message";
  609. } else if (logfile && !strcmp(logfile, "-")) {
  610. if (isatty(0))
  611. fprintf(stderr, _("(reading log message from standard input)\n"));
  612. if (strbuf_read(&sb, 0, 0) < 0)
  613. die_errno(_("could not read log from standard input"));
  614. hook_arg1 = "message";
  615. } else if (logfile) {
  616. if (strbuf_read_file(&sb, logfile, 0) < 0)
  617. die_errno(_("could not read log file '%s'"),
  618. logfile);
  619. hook_arg1 = "message";
  620. } else if (use_message) {
  621. char *buffer;
  622. buffer = strstr(use_message_buffer, "\n\n");
  623. if (buffer)
  624. strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
  625. hook_arg1 = "commit";
  626. hook_arg2 = use_message;
  627. } else if (fixup_message) {
  628. struct pretty_print_context ctx = {0};
  629. struct commit *commit;
  630. commit = lookup_commit_reference_by_name(fixup_message);
  631. if (!commit)
  632. die(_("could not lookup commit %s"), fixup_message);
  633. ctx.output_encoding = get_commit_output_encoding();
  634. format_commit_message(commit, "fixup! %s\n\n",
  635. &sb, &ctx);
  636. if (have_option_m)
  637. strbuf_addbuf(&sb, &message);
  638. hook_arg1 = "message";
  639. } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
  640. size_t merge_msg_start;
  641. /*
  642. * prepend SQUASH_MSG here if it exists and a
  643. * "merge --squash" was originally performed
  644. */
  645. if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
  646. if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
  647. die_errno(_("could not read SQUASH_MSG"));
  648. hook_arg1 = "squash";
  649. } else
  650. hook_arg1 = "merge";
  651. merge_msg_start = sb.len;
  652. if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
  653. die_errno(_("could not read MERGE_MSG"));
  654. if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
  655. wt_status_locate_end(sb.buf + merge_msg_start,
  656. sb.len - merge_msg_start) <
  657. sb.len - merge_msg_start)
  658. merge_contains_scissors = 1;
  659. } else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
  660. if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
  661. die_errno(_("could not read SQUASH_MSG"));
  662. hook_arg1 = "squash";
  663. } else if (template_file) {
  664. if (strbuf_read_file(&sb, template_file, 0) < 0)
  665. die_errno(_("could not read '%s'"), template_file);
  666. hook_arg1 = "template";
  667. clean_message_contents = 0;
  668. }
  669. /*
  670. * The remaining cases don't modify the template message, but
  671. * just set the argument(s) to the prepare-commit-msg hook.
  672. */
  673. else if (whence == FROM_MERGE)
  674. hook_arg1 = "merge";
  675. else if (whence == FROM_CHERRY_PICK) {
  676. hook_arg1 = "commit";
  677. hook_arg2 = "CHERRY_PICK_HEAD";
  678. }
  679. if (squash_message) {
  680. /*
  681. * If squash_commit was used for the commit subject,
  682. * then we're possibly hijacking other commit log options.
  683. * Reset the hook args to tell the real story.
  684. */
  685. hook_arg1 = "message";
  686. hook_arg2 = "";
  687. }
  688. s->fp = fopen_for_writing(git_path_commit_editmsg());
  689. if (s->fp == NULL)
  690. die_errno(_("could not open '%s'"), git_path_commit_editmsg());
  691. /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
  692. old_display_comment_prefix = s->display_comment_prefix;
  693. s->display_comment_prefix = 1;
  694. /*
  695. * Most hints are counter-productive when the commit has
  696. * already started.
  697. */
  698. s->hints = 0;
  699. if (clean_message_contents)
  700. strbuf_stripspace(&sb, 0);
  701. if (signoff)
  702. append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
  703. if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
  704. die_errno(_("could not write commit template"));
  705. if (auto_comment_line_char)
  706. adjust_comment_line_char(&sb);
  707. strbuf_release(&sb);
  708. /* This checks if committer ident is explicitly given */
  709. strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
  710. if (use_editor && include_status) {
  711. int ident_shown = 0;
  712. int saved_color_setting;
  713. struct ident_split ci, ai;
  714. if (whence != FROM_COMMIT) {
  715. if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
  716. !merge_contains_scissors)
  717. wt_status_add_cut_line(s->fp);
  718. status_printf_ln(s, GIT_COLOR_NORMAL,
  719. whence == FROM_MERGE
  720. ? _("\n"
  721. "It looks like you may be committing a merge.\n"
  722. "If this is not correct, please remove the file\n"
  723. " %s\n"
  724. "and try again.\n")
  725. : _("\n"
  726. "It looks like you may be committing a cherry-pick.\n"
  727. "If this is not correct, please remove the file\n"
  728. " %s\n"
  729. "and try again.\n"),
  730. whence == FROM_MERGE ?
  731. git_path_merge_head(the_repository) :
  732. git_path_cherry_pick_head(the_repository));
  733. }
  734. fprintf(s->fp, "\n");
  735. if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL)
  736. status_printf(s, GIT_COLOR_NORMAL,
  737. _("Please enter the commit message for your changes."
  738. " Lines starting\nwith '%c' will be ignored, and an empty"
  739. " message aborts the commit.\n"), comment_line_char);
  740. else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
  741. if (whence == FROM_COMMIT && !merge_contains_scissors)
  742. wt_status_add_cut_line(s->fp);
  743. } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
  744. status_printf(s, GIT_COLOR_NORMAL,
  745. _("Please enter the commit message for your changes."
  746. " Lines starting\n"
  747. "with '%c' will be kept; you may remove them"
  748. " yourself if you want to.\n"
  749. "An empty message aborts the commit.\n"), comment_line_char);
  750. /*
  751. * These should never fail because they come from our own
  752. * fmt_ident. They may fail the sane_ident test, but we know
  753. * that the name and mail pointers will at least be valid,
  754. * which is enough for our tests and printing here.
  755. */
  756. assert_split_ident(&ai, author_ident);
  757. assert_split_ident(&ci, &committer_ident);
  758. if (ident_cmp(&ai, &ci))
  759. status_printf_ln(s, GIT_COLOR_NORMAL,
  760. _("%s"
  761. "Author: %.*s <%.*s>"),
  762. ident_shown++ ? "" : "\n",
  763. (int)(ai.name_end - ai.name_begin), ai.name_begin,
  764. (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
  765. if (author_date_is_interesting())
  766. status_printf_ln(s, GIT_COLOR_NORMAL,
  767. _("%s"
  768. "Date: %s"),
  769. ident_shown++ ? "" : "\n",
  770. show_ident_date(&ai, DATE_MODE(NORMAL)));
  771. if (!committer_ident_sufficiently_given())
  772. status_printf_ln(s, GIT_COLOR_NORMAL,
  773. _("%s"
  774. "Committer: %.*s <%.*s>"),
  775. ident_shown++ ? "" : "\n",
  776. (int)(ci.name_end - ci.name_begin), ci.name_begin,
  777. (int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
  778. status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */
  779. saved_color_setting = s->use_color;
  780. s->use_color = 0;
  781. committable = run_status(s->fp, index_file, prefix, 1, s);
  782. s->use_color = saved_color_setting;
  783. string_list_clear(&s->change, 1);
  784. } else {
  785. struct object_id oid;
  786. const char *parent = "HEAD";
  787. if (!active_nr && read_cache() < 0)
  788. die(_("Cannot read index"));
  789. if (amend)
  790. parent = "HEAD^1";
  791. if (get_oid(parent, &oid)) {
  792. int i, ita_nr = 0;
  793. for (i = 0; i < active_nr; i++)
  794. if (ce_intent_to_add(active_cache[i]))
  795. ita_nr++;
  796. committable = active_nr - ita_nr > 0;
  797. } else {
  798. /*
  799. * Unless the user did explicitly request a submodule
  800. * ignore mode by passing a command line option we do
  801. * not ignore any changed submodule SHA-1s when
  802. * comparing index and parent, no matter what is
  803. * configured. Otherwise we won't commit any
  804. * submodules which were manually staged, which would
  805. * be really confusing.
  806. */
  807. struct diff_flags flags = DIFF_FLAGS_INIT;
  808. flags.override_submodule_config = 1;
  809. if (ignore_submodule_arg &&
  810. !strcmp(ignore_submodule_arg, "all"))
  811. flags.ignore_submodules = 1;
  812. committable = index_differs_from(the_repository,
  813. parent, &flags, 1);
  814. }
  815. }
  816. strbuf_release(&committer_ident);
  817. fclose(s->fp);
  818. /*
  819. * Reject an attempt to record a non-merge empty commit without
  820. * explicit --allow-empty. In the cherry-pick case, it may be
  821. * empty due to conflict resolution, which the user should okay.
  822. */
  823. if (!committable && whence != FROM_MERGE && !allow_empty &&
  824. !(amend && is_a_merge(current_head))) {
  825. s->display_comment_prefix = old_display_comment_prefix;
  826. run_status(stdout, index_file, prefix, 0, s);
  827. if (amend)
  828. fputs(_(empty_amend_advice), stderr);
  829. else if (whence == FROM_CHERRY_PICK) {
  830. fputs(_(empty_cherry_pick_advice), stderr);
  831. if (!sequencer_in_use)
  832. fputs(_(empty_cherry_pick_advice_single), stderr);
  833. else
  834. fputs(_(empty_cherry_pick_advice_multi), stderr);
  835. }
  836. return 0;
  837. }
  838. if (!no_verify && find_hook("pre-commit")) {
  839. /*
  840. * Re-read the index as pre-commit hook could have updated it,
  841. * and write it out as a tree. We must do this before we invoke
  842. * the editor and after we invoke run_status above.
  843. */
  844. discard_cache();
  845. }
  846. read_cache_from(index_file);
  847. if (update_main_cache_tree(0)) {
  848. error(_("Error building trees"));
  849. return 0;
  850. }
  851. if (run_commit_hook(use_editor, index_file, "prepare-commit-msg",
  852. git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
  853. return 0;
  854. if (use_editor) {
  855. struct argv_array env = ARGV_ARRAY_INIT;
  856. argv_array_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
  857. if (launch_editor(git_path_commit_editmsg(), NULL, env.argv)) {
  858. fprintf(stderr,
  859. _("Please supply the message using either -m or -F option.\n"));
  860. exit(1);
  861. }
  862. argv_array_clear(&env);
  863. }
  864. if (!no_verify &&
  865. run_commit_hook(use_editor, index_file, "commit-msg", git_path_commit_editmsg(), NULL)) {
  866. return 0;
  867. }
  868. return 1;
  869. }
  870. static const char *find_author_by_nickname(const char *name)
  871. {
  872. struct rev_info revs;
  873. struct commit *commit;
  874. struct strbuf buf = STRBUF_INIT;
  875. struct string_list mailmap = STRING_LIST_INIT_NODUP;
  876. const char *av[20];
  877. int ac = 0;
  878. repo_init_revisions(the_repository, &revs, NULL);
  879. strbuf_addf(&buf, "--author=%s", name);
  880. av[++ac] = "--all";
  881. av[++ac] = "-i";
  882. av[++ac] = buf.buf;
  883. av[++ac] = NULL;
  884. setup_revisions(ac, av, &revs, NULL);
  885. revs.mailmap = &mailmap;
  886. read_mailmap(revs.mailmap, NULL);
  887. if (prepare_revision_walk(&revs))
  888. die(_("revision walk setup failed"));
  889. commit = get_revision(&revs);
  890. if (commit) {
  891. struct pretty_print_context ctx = {0};
  892. ctx.date_mode.type = DATE_NORMAL;
  893. strbuf_release(&buf);
  894. format_commit_message(commit, "%aN <%aE>", &buf, &ctx);
  895. clear_mailmap(&mailmap);
  896. return strbuf_detach(&buf, NULL);
  897. }
  898. die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name);
  899. }
  900. static void handle_ignored_arg(struct wt_status *s)
  901. {
  902. if (!ignored_arg)
  903. ; /* default already initialized */
  904. else if (!strcmp(ignored_arg, "traditional"))
  905. s->show_ignored_mode = SHOW_TRADITIONAL_IGNORED;
  906. else if (!strcmp(ignored_arg, "no"))
  907. s->show_ignored_mode = SHOW_NO_IGNORED;
  908. else if (!strcmp(ignored_arg, "matching"))
  909. s->show_ignored_mode = SHOW_MATCHING_IGNORED;
  910. else
  911. die(_("Invalid ignored mode '%s'"), ignored_arg);
  912. }
  913. static void handle_untracked_files_arg(struct wt_status *s)
  914. {
  915. if (!untracked_files_arg)
  916. ; /* default already initialized */
  917. else if (!strcmp(untracked_files_arg, "no"))
  918. s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
  919. else if (!strcmp(untracked_files_arg, "normal"))
  920. s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
  921. else if (!strcmp(untracked_files_arg, "all"))
  922. s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
  923. /*
  924. * Please update $__git_untracked_file_modes in
  925. * git-completion.bash when you add new options
  926. */
  927. else
  928. die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
  929. }
  930. static const char *read_commit_message(const char *name)
  931. {
  932. const char *out_enc;
  933. struct commit *commit;
  934. commit = lookup_commit_reference_by_name(name);
  935. if (!commit)
  936. die(_("could not lookup commit %s"), name);
  937. out_enc = get_commit_output_encoding();
  938. return logmsg_reencode(commit, NULL, out_enc);
  939. }
  940. /*
  941. * Enumerate what needs to be propagated when --porcelain
  942. * is not in effect here.
  943. */
  944. static struct status_deferred_config {
  945. enum wt_status_format status_format;
  946. int show_branch;
  947. enum ahead_behind_flags ahead_behind;
  948. } status_deferred_config = {
  949. STATUS_FORMAT_UNSPECIFIED,
  950. -1, /* unspecified */
  951. AHEAD_BEHIND_UNSPECIFIED,
  952. };
  953. static void finalize_deferred_config(struct wt_status *s)
  954. {
  955. int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
  956. status_format != STATUS_FORMAT_PORCELAIN_V2 &&
  957. !s->null_termination);
  958. if (s->null_termination) {
  959. if (status_format == STATUS_FORMAT_NONE ||
  960. status_format == STATUS_FORMAT_UNSPECIFIED)
  961. status_format = STATUS_FORMAT_PORCELAIN;
  962. else if (status_format == STATUS_FORMAT_LONG)
  963. die(_("--long and -z are incompatible"));
  964. }
  965. if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
  966. status_format = status_deferred_config.status_format;
  967. if (status_format == STATUS_FORMAT_UNSPECIFIED)
  968. status_format = STATUS_FORMAT_NONE;
  969. if (use_deferred_config && s->show_branch < 0)
  970. s->show_branch = status_deferred_config.show_branch;
  971. if (s->show_branch < 0)
  972. s->show_branch = 0;
  973. /*
  974. * If the user did not give a "--[no]-ahead-behind" command
  975. * line argument *AND* we will print in a human-readable format
  976. * (short, long etc.) then we inherit from the status.aheadbehind
  977. * config setting. In all other cases (and porcelain V[12] formats
  978. * in particular), we inherit _FULL for backwards compatibility.
  979. */
  980. if (use_deferred_config &&
  981. s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
  982. s->ahead_behind_flags = status_deferred_config.ahead_behind;
  983. if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
  984. s->ahead_behind_flags = AHEAD_BEHIND_FULL;
  985. }
  986. static int parse_and_validate_options(int argc, const char *argv[],
  987. const struct option *options,
  988. const char * const usage[],
  989. const char *prefix,
  990. struct commit *current_head,
  991. struct wt_status *s)
  992. {
  993. int f = 0;
  994. argc = parse_options(argc, argv, prefix, options, usage, 0);
  995. finalize_deferred_config(s);
  996. if (force_author && !strchr(force_author, '>'))
  997. force_author = find_author_by_nickname(force_author);
  998. if (force_author && renew_authorship)
  999. die(_("Using both --reset-author and --author does not make sense"));
  1000. if (logfile || have_option_m || use_message || fixup_message)
  1001. use_editor = 0;
  1002. if (0 <= edit_flag)
  1003. use_editor = edit_flag;
  1004. /* Sanity check options */
  1005. if (amend && !current_head)
  1006. die(_("You have nothing to amend."));
  1007. if (amend && whence != FROM_COMMIT) {
  1008. if (whence == FROM_MERGE)
  1009. die(_("You are in the middle of a merge -- cannot amend."));
  1010. else if (whence == FROM_CHERRY_PICK)
  1011. die(_("You are in the middle of a cherry-pick -- cannot amend."));
  1012. }
  1013. if (fixup_message && squash_message)
  1014. die(_("Options --squash and --fixup cannot be used together"));
  1015. if (use_message)
  1016. f++;
  1017. if (edit_message)
  1018. f++;
  1019. if (fixup_message)
  1020. f++;
  1021. if (logfile)
  1022. f++;
  1023. if (f > 1)
  1024. die(_("Only one of -c/-C/-F/--fixup can be used."));
  1025. if (have_option_m && (edit_message || use_message || logfile))
  1026. die((_("Option -m cannot be combined with -c/-C/-F.")));
  1027. if (f || have_option_m)
  1028. template_file = NULL;
  1029. if (edit_message)
  1030. use_message = edit_message;
  1031. if (amend && !use_message && !fixup_message)
  1032. use_message = "HEAD";
  1033. if (!use_message && whence != FROM_CHERRY_PICK && renew_authorship)
  1034. die(_("--reset-author can be used only with -C, -c or --amend."));
  1035. if (use_message) {
  1036. use_message_buffer = read_commit_message(use_message);
  1037. if (!renew_authorship) {
  1038. author_message = use_message;
  1039. author_message_buffer = use_message_buffer;
  1040. }
  1041. }
  1042. if (whence == FROM_CHERRY_PICK && !renew_authorship) {
  1043. author_message = "CHERRY_PICK_HEAD";
  1044. author_message_buffer = read_commit_message(author_message);
  1045. }
  1046. if (patch_interactive)
  1047. interactive = 1;
  1048. if (also + only + all + interactive > 1)
  1049. die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
  1050. if (argc == 0 && (also || (only && !amend && !allow_empty)))
  1051. die(_("No paths with --include/--only does not make sense."));
  1052. cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor);
  1053. handle_untracked_files_arg(s);
  1054. if (all && argc > 0)
  1055. die(_("paths '%s ...' with -a does not make sense"),
  1056. argv[0]);
  1057. if (status_format != STATUS_FORMAT_NONE)
  1058. dry_run = 1;
  1059. return argc;
  1060. }
  1061. static int dry_run_commit(int argc, const char **argv, const char *prefix,
  1062. const struct commit *current_head, struct wt_status *s)
  1063. {
  1064. int committable;
  1065. const char *index_file;
  1066. index_file = prepare_index(argc, argv, prefix, current_head, 1);
  1067. committable = run_status(stdout, index_file, prefix, 0, s);
  1068. rollback_index_files();
  1069. return committable ? 0 : 1;
  1070. }
  1071. define_list_config_array_extra(color_status_slots, {"added"});
  1072. static int parse_status_slot(const char *slot)
  1073. {
  1074. if (!strcasecmp(slot, "added"))
  1075. return WT_STATUS_UPDATED;
  1076. return LOOKUP_CONFIG(color_status_slots, slot);
  1077. }
  1078. static int git_status_config(const char *k, const char *v, void *cb)
  1079. {
  1080. struct wt_status *s = cb;
  1081. const char *slot_name;
  1082. if (starts_with(k, "column."))
  1083. return git_column_config(k, v, "status", &s->colopts);
  1084. if (!strcmp(k, "status.submodulesummary")) {
  1085. int is_bool;
  1086. s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
  1087. if (is_bool && s->submodule_summary)
  1088. s->submodule_summary = -1;
  1089. return 0;
  1090. }
  1091. if (!strcmp(k, "status.short")) {
  1092. if (git_config_bool(k, v))
  1093. status_deferred_config.status_format = STATUS_FORMAT_SHORT;
  1094. else
  1095. status_deferred_config.status_format = STATUS_FORMAT_NONE;
  1096. return 0;
  1097. }
  1098. if (!strcmp(k, "status.branch")) {
  1099. status_deferred_config.show_branch = git_config_bool(k, v);
  1100. return 0;
  1101. }
  1102. if (!strcmp(k, "status.aheadbehind")) {
  1103. status_deferred_config.ahead_behind = git_config_bool(k, v);
  1104. return 0;
  1105. }
  1106. if (!strcmp(k, "status.showstash")) {
  1107. s->show_stash = git_config_bool(k, v);
  1108. return 0;
  1109. }
  1110. if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
  1111. s->use_color = git_config_colorbool(k, v);
  1112. return 0;
  1113. }
  1114. if (!strcmp(k, "status.displaycommentprefix")) {
  1115. s->display_comment_prefix = git_config_bool(k, v);
  1116. return 0;
  1117. }
  1118. if (skip_prefix(k, "status.color.", &slot_name) ||
  1119. skip_prefix(k, "color.status.", &slot_name)) {
  1120. int slot = parse_status_slot(slot_name);
  1121. if (slot < 0)
  1122. return 0;
  1123. if (!v)
  1124. return config_error_nonbool(k);
  1125. return color_parse(v, s->color_palette[slot]);
  1126. }
  1127. if (!strcmp(k, "status.relativepaths")) {
  1128. s->relative_paths = git_config_bool(k, v);
  1129. return 0;
  1130. }
  1131. if (!strcmp(k, "status.showuntrackedfiles")) {
  1132. if (!v)
  1133. return config_error_nonbool(k);
  1134. else if (!strcmp(v, "no"))
  1135. s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
  1136. else if (!strcmp(v, "normal"))
  1137. s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
  1138. else if (!strcmp(v, "all"))
  1139. s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
  1140. else
  1141. return error(_("Invalid untracked files mode '%s'"), v);
  1142. return 0;
  1143. }
  1144. if (!strcmp(k, "diff.renamelimit")) {
  1145. if (s->rename_limit == -1)
  1146. s->rename_limit = git_config_int(k, v);
  1147. return 0;
  1148. }
  1149. if (!strcmp(k, "status.renamelimit")) {
  1150. s->rename_limit = git_config_int(k, v);
  1151. return 0;
  1152. }
  1153. if (!strcmp(k, "diff.renames")) {
  1154. if (s->detect_rename == -1)
  1155. s->detect_rename = git_config_rename(k, v);
  1156. return 0;
  1157. }
  1158. if (!strcmp(k, "status.renames")) {
  1159. s->detect_rename = git_config_rename(k, v);
  1160. return 0;
  1161. }
  1162. return git_diff_ui_config(k, v, NULL);
  1163. }
  1164. int cmd_status(int argc, const char **argv, const char *prefix)
  1165. {
  1166. static int no_renames = -1;
  1167. static const char *rename_score_arg = (const char *)-1;
  1168. static struct wt_status s;
  1169. unsigned int progress_flag = 0;
  1170. int fd;
  1171. struct object_id oid;
  1172. static struct option builtin_status_options[] = {
  1173. OPT__VERBOSE(&verbose, N_("be verbose")),
  1174. OPT_SET_INT('s', "short", &status_format,
  1175. N_("show status concisely"), STATUS_FORMAT_SHORT),
  1176. OPT_BOOL('b', "branch", &s.show_branch,
  1177. N_("show branch information")),
  1178. OPT_BOOL(0, "show-stash", &s.show_stash,
  1179. N_("show stash information")),
  1180. OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
  1181. N_("compute full ahead/behind values")),
  1182. { OPTION_CALLBACK, 0, "porcelain", &status_format,
  1183. N_("version"), N_("machine-readable output"),
  1184. PARSE_OPT_OPTARG, opt_parse_porcelain },
  1185. OPT_SET_INT(0, "long", &status_format,
  1186. N_("show status in long format (default)"),
  1187. STATUS_FORMAT_LONG),
  1188. OPT_BOOL('z', "null", &s.null_termination,
  1189. N_("terminate entries with NUL")),
  1190. { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
  1191. N_("mode"),
  1192. N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
  1193. PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
  1194. { OPTION_STRING, 0, "ignored", &ignored_arg,
  1195. N_("mode"),
  1196. N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
  1197. PARSE_OPT_OPTARG, NULL, (intptr_t)"traditional" },
  1198. { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"),
  1199. N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
  1200. PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
  1201. OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
  1202. OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")),
  1203. { OPTION_CALLBACK, 'M', "find-renames", &rename_score_arg,
  1204. N_("n"), N_("detect renames, optionally set similarity index"),
  1205. PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_rename_score },
  1206. OPT_END(),
  1207. };
  1208. if (argc == 2 && !strcmp(argv[1], "-h"))
  1209. usage_with_options(builtin_status_usage, builtin_status_options);
  1210. status_init_config(&s, git_status_config);
  1211. argc = parse_options(argc, argv, prefix,
  1212. builtin_status_options,
  1213. builtin_status_usage, 0);
  1214. finalize_colopts(&s.colopts, -1);
  1215. finalize_deferred_config(&s);
  1216. handle_untracked_files_arg(&s);
  1217. handle_ignored_arg(&s);
  1218. if (s.show_ignored_mode == SHOW_MATCHING_IGNORED &&
  1219. s.show_untracked_files == SHOW_NO_UNTRACKED_FILES)
  1220. die(_("Unsupported combination of ignored and untracked-files arguments"));
  1221. parse_pathspec(&s.pathspec, 0,
  1222. PATHSPEC_PREFER_FULL,
  1223. prefix, argv);
  1224. if (status_format != STATUS_FORMAT_PORCELAIN &&
  1225. status_format != STATUS_FORMAT_PORCELAIN_V2)
  1226. progress_flag = REFRESH_PROGRESS;
  1227. repo_read_index(the_repository);
  1228. refresh_index(&the_index,
  1229. REFRESH_QUIET|REFRESH_UNMERGED|progress_flag,
  1230. &s.pathspec, NULL, NULL);
  1231. if (use_optional_locks())
  1232. fd = hold_locked_index(&index_lock, 0);
  1233. else
  1234. fd = -1;
  1235. s.is_initial = get_oid(s.reference, &oid) ? 1 : 0;
  1236. if (!s.is_initial)
  1237. oidcpy(&s.oid_commit, &oid);
  1238. s.ignore_submodule_arg = ignore_submodule_arg;
  1239. s.status_format = status_format;
  1240. s.verbose = verbose;
  1241. if (no_renames != -1)
  1242. s.detect_rename = !no_renames;
  1243. if ((intptr_t)rename_score_arg != -1) {
  1244. if (s.detect_rename < DIFF_DETECT_RENAME)
  1245. s.detect_rename = DIFF_DETECT_RENAME;
  1246. if (rename_score_arg)
  1247. s.rename_score = parse_rename_score(&rename_score_arg);
  1248. }
  1249. wt_status_collect(&s);
  1250. if (0 <= fd)
  1251. repo_update_index_if_able(the_repository, &index_lock);
  1252. if (s.relative_paths)
  1253. s.prefix = prefix;
  1254. wt_status_print(&s);
  1255. wt_status_collect_free_buffers(&s);
  1256. return 0;
  1257. }
  1258. static int git_commit_config(const char *k, const char *v, void *cb)
  1259. {
  1260. struct wt_status *s = cb;
  1261. int status;
  1262. if (!strcmp(k, "commit.template"))
  1263. return git_config_pathname(&template_file, k, v);
  1264. if (!strcmp(k, "commit.status")) {
  1265. include_status = git_config_bool(k, v);
  1266. return 0;
  1267. }
  1268. if (!strcmp(k, "commit.cleanup"))
  1269. return git_config_string(&cleanup_arg, k, v);
  1270. if (!strcmp(k, "commit.gpgsign")) {
  1271. sign_commit = git_config_bool(k, v) ? "" : NULL;
  1272. return 0;
  1273. }
  1274. if (!strcmp(k, "commit.verbose")) {
  1275. int is_bool;
  1276. config_commit_verbose = git_config_bool_or_int(k, v, &is_bool);
  1277. return 0;
  1278. }
  1279. status = git_gpg_config(k, v, NULL);
  1280. if (status)
  1281. return status;
  1282. return git_status_config(k, v, s);
  1283. }
  1284. int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...)
  1285. {
  1286. struct argv_array hook_env = ARGV_ARRAY_INIT;
  1287. va_list args;
  1288. int ret;
  1289. argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
  1290. /*
  1291. * Let the hook know that no editor will be launched.
  1292. */
  1293. if (!editor_is_used)
  1294. argv_array_push(&hook_env, "GIT_EDITOR=:");
  1295. va_start(args, name);
  1296. ret = run_hook_ve(hook_env.argv,name, args);
  1297. va_end(args);
  1298. argv_array_clear(&hook_env);
  1299. return ret;
  1300. }
  1301. int cmd_commit(int argc, const char **argv, const char *prefix)
  1302. {
  1303. const char *argv_gc_auto[] = {"gc", "--auto", NULL};
  1304. static struct wt_status s;
  1305. static struct option builtin_commit_options[] = {
  1306. OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
  1307. OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
  1308. OPT_GROUP(N_("Commit message options")),
  1309. OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
  1310. OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
  1311. OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
  1312. OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
  1313. OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
  1314. OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
  1315. OPT_STRING(0, "fixup", &fixup_message, N_("commit"), N_("use autosquash formatted message to fixup specified commit")),
  1316. OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
  1317. OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
  1318. OPT_BOOL('s', "signoff", &signoff, N_("add Signed-off-by:")),
  1319. OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
  1320. OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
  1321. OPT_CLEANUP(&cleanup_arg),
  1322. OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")),
  1323. { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
  1324. N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
  1325. /* end commit message options */
  1326. OPT_GROUP(N_("Commit contents options")),
  1327. OPT_BOOL('a', "all", &all, N_("commit all changed files")),
  1328. OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")),
  1329. OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")),
  1330. OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")),
  1331. OPT_BOOL('o', "only", &only, N_("commit only specified files")),
  1332. OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit and commit-msg hooks")),
  1333. OPT_BOOL(0, "dry-run", &dry_run, N_("show what would be committed")),
  1334. OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
  1335. STATUS_FORMAT_SHORT),
  1336. OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
  1337. OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
  1338. N_("compute full ahead/behind values")),
  1339. OPT_SET_INT(0, "porcelain", &status_format,
  1340. N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
  1341. OPT_SET_INT(0, "long", &status_format,
  1342. N_("show status in long format (default)"),
  1343. STATUS_FORMAT_LONG),
  1344. OPT_BOOL('z', "null", &s.null_termination,
  1345. N_("terminate entries with NUL")),
  1346. OPT_BOOL(0, "amend", &amend, N_("amend previous commit")),
  1347. OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")),
  1348. { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, N_("mode"), N_("show untracked files, optional modes: all, normal, no. (Default: all)"), PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
  1349. /* end commit contents options */
  1350. OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty,
  1351. N_("ok to record an empty change")),
  1352. OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message,
  1353. N_("ok to record a change with an empty message")),
  1354. OPT_END()
  1355. };
  1356. struct strbuf sb = STRBUF_INIT;
  1357. struct strbuf author_ident = STRBUF_INIT;
  1358. const char *index_file, *reflog_msg;
  1359. struct object_id oid;
  1360. struct commit_list *parents = NULL;
  1361. struct stat statbuf;
  1362. struct commit *current_head = NULL;
  1363. struct commit_extra_header *extra = NULL;
  1364. struct strbuf err = STRBUF_INIT;
  1365. if (argc == 2 && !strcmp(argv[1], "-h"))
  1366. usage_with_options(builtin_commit_usage, builtin_commit_options);
  1367. status_init_config(&s, git_commit_config);
  1368. s.commit_template = 1;
  1369. status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
  1370. s.colopts = 0;
  1371. if (get_oid("HEAD", &oid))
  1372. current_head = NULL;
  1373. else {
  1374. current_head = lookup_commit_or_die(&oid, "HEAD");
  1375. if (parse_commit(current_head))
  1376. die(_("could not parse HEAD commit"));
  1377. }
  1378. verbose = -1; /* unspecified */
  1379. argc = parse_and_validate_options(argc, argv, builtin_commit_options,
  1380. builtin_commit_usage,
  1381. prefix, current_head, &s);
  1382. if (verbose == -1)
  1383. verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;
  1384. if (dry_run)
  1385. return dry_run_commit(argc, argv, prefix, current_head, &s);
  1386. index_file = prepare_index(argc, argv, prefix, current_head, 0);
  1387. /* Set up everything for writing the commit object. This includes
  1388. running hooks, writing the trees, and interacting with the user. */
  1389. if (!prepare_to_commit(index_file, prefix,
  1390. current_head, &s, &author_ident)) {
  1391. rollback_index_files();
  1392. return 1;
  1393. }
  1394. /* Determine parents */
  1395. reflog_msg = getenv("GIT_REFLOG_ACTION");
  1396. if (!current_head) {
  1397. if (!reflog_msg)
  1398. reflog_msg = "commit (initial)";
  1399. } else if (amend) {
  1400. if (!reflog_msg)
  1401. reflog_msg = "commit (amend)";
  1402. parents = copy_commit_list(current_head->parents);
  1403. } else if (whence == FROM_MERGE) {
  1404. struct strbuf m = STRBUF_INIT;
  1405. FILE *fp;
  1406. int allow_fast_forward = 1;
  1407. struct commit_list **pptr = &parents;
  1408. if (!reflog_msg)
  1409. reflog_msg = "commit (merge)";
  1410. pptr = commit_list_append(current_head, pptr);
  1411. fp = xfopen(git_path_merge_head(the_repository), "r");
  1412. while (strbuf_getline_lf(&m, fp) != EOF) {
  1413. struct commit *parent;
  1414. parent = get_merge_parent(m.buf);
  1415. if (!parent)
  1416. die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
  1417. pptr = commit_list_append(parent, pptr);
  1418. }
  1419. fclose(fp);
  1420. strbuf_release(&m);
  1421. if (!stat(git_path_merge_mode(the_repository), &statbuf)) {
  1422. if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0)
  1423. die_errno(_("could not read MERGE_MODE"));
  1424. if (!strcmp(sb.buf, "no-ff"))
  1425. allow_fast_forward = 0;
  1426. }
  1427. if (allow_fast_forward)
  1428. reduce_heads_replace(&parents);
  1429. } else {
  1430. if (!reflog_msg)
  1431. reflog_msg = (whence == FROM_CHERRY_PICK)
  1432. ? "commit (cherry-pick)"
  1433. : "commit";
  1434. commit_list_insert(current_head, &parents);
  1435. }
  1436. /* Finally, get the commit message */
  1437. strbuf_reset(&sb);
  1438. if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) {
  1439. int saved_errno = errno;
  1440. rollback_index_files();
  1441. die(_("could not read commit message: %s"), strerror(saved_errno));
  1442. }
  1443. cleanup_message(&sb, cleanup_mode, verbose);
  1444. if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) {
  1445. rollback_index_files();
  1446. fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
  1447. exit(1);
  1448. }
  1449. if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) {
  1450. rollback_index_files();
  1451. fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
  1452. exit(1);
  1453. }
  1454. if (amend) {
  1455. const char *exclude_gpgsig[2] = { "gpgsig", NULL };
  1456. extra = read_commit_extra_headers(current_head, exclude_gpgsig);
  1457. } else {
  1458. struct commit_extra_header **tail = &extra;
  1459. append_merge_tag_headers(parents, &tail);
  1460. }
  1461. if (commit_tree_extended(sb.buf, sb.len, &active_cache_tree->oid,
  1462. parents, &oid, author_ident.buf, sign_commit,
  1463. extra)) {
  1464. rollback_index_files();
  1465. die(_("failed to write commit object"));
  1466. }
  1467. strbuf_release(&author_ident);
  1468. free_commit_extra_headers(extra);
  1469. if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb,
  1470. &err)) {
  1471. rollback_index_files();
  1472. die("%s", err.buf);
  1473. }
  1474. sequencer_post_commit_cleanup(the_repository, 0);
  1475. unlink(git_path_merge_head(the_repository));
  1476. unlink(git_path_merge_msg(the_repository));
  1477. unlink(git_path_merge_mode(the_repository));
  1478. unlink(git_path_squash_msg(the_repository));
  1479. if (commit_index_files())
  1480. die(_("repository has been updated, but unable to write\n"
  1481. "new_index file. Check that disk is not full and quota is\n"
  1482. "not exceeded, and then \"git restore --staged :/\" to recover."));
  1483. if (git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
  1484. write_commit_graph_reachable(get_object_directory(), 0, NULL))
  1485. return 1;
  1486. repo_rerere(the_repository, 0);
  1487. run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
  1488. run_commit_hook(use_editor, get_index_file(), "post-commit", NULL);
  1489. if (amend && !no_post_rewrite) {
  1490. commit_post_rewrite(the_repository, current_head, &oid);
  1491. }
  1492. if (!quiet) {
  1493. unsigned int flags = 0;
  1494. if (!current_head)
  1495. flags |= SUMMARY_INITIAL_COMMIT;
  1496. if (author_date_is_interesting())
  1497. flags |= SUMMARY_SHOW_AUTHOR_DATE;
  1498. print_commit_summary(the_repository, prefix,
  1499. &oid, flags);
  1500. }
  1501. UNLEAK(err);
  1502. UNLEAK(sb);
  1503. return 0;
  1504. }