commit: --squash option for use with rebase --autosquash
This option makes it convenient to construct commit messages for use with 'rebase --autosquash'. The resulting commit message will be "squash! ..." where "..." is the subject line of the specified commit message. This option can be used with other commit message options such as -m, -c, -C and -F. If an editor is invoked (as with -c or -eF or no message options) the commit message is seeded with the correctly formatted subject line. Example usage: $ git commit --squash HEAD~2 $ git commit --squash HEAD~2 -m "clever comment" $ git commit --squash HEAD~2 -F msgfile $ git commit --squash HEAD~2 -C deadbeef Signed-off-by: Pat Notz <patnotz@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pat Notz committed
Nov 2, 2010 at 13:59 UTC
89ac1223c08a19a83f6efb32234ed8665d5ad1ee
2 files changed
+41
-3
Documentation/git-commit.txt
+8
-1
@@ -9,7 +9,7 @@ SYNOPSIS
9
--------
10
[verse]
11
'git commit' [-a | --interactive] [-s] [-v] [-u<mode>] [--amend] [--dry-run]
12
- [(-c | -C | --fixup) <commit>] [-F <file> | -m <msg>]
12
+ [(-c | -C | --fixup | --squash) <commit>] [-F <file> | -m <msg>]
13
[--reset-author] [--allow-empty] [--allow-empty-message] [--no-verify]
14
[-e] [--author=<author>] [--date=<date>] [--cleanup=<mode>]
15
[--status | --no-status] [--] [[-i | -o ]<file>...]
@@ -76,6 +76,13 @@ OPTIONS
76
commit with a prefix of "fixup! ". See linkgit:git-rebase[1]
77
for details.
78
79
+--squash=<commit>::
80
+ Construct a commit message for use with `rebase --autosquash`.
81
+ The commit message subject line is taken from the specified
82
+ commit with a prefix of "squash! ". Can be used with additional
83
+ commit message options (`-m`/`-c`/`-C`/`-F`). See
84
+ linkgit:git-rebase[1] for details.
85
+
86
--reset-author::
87
When used with -C/-c/--amend options, declare that the
88
authorship of the resulting commit now belongs of the committer.
builtin/commit.c
+33
-2
@@ -69,7 +69,7 @@ static enum {
69
static const char *logfile, *force_author;
70
static const char *template_file;
71
static char *edit_message, *use_message;
72
-static char *fixup_message;
72
+static char *fixup_message, *squash_message;
73
static char *author_name, *author_email, *author_date;
74
static int all, edit_flag, also, interactive, only, amend, signoff;
75
static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
@@ -126,6 +126,7 @@ static struct option builtin_commit_options[] = {
126
OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit"),
127
OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"),
128
OPT_STRING(0, "fixup", &fixup_message, "COMMIT", "use autosquash formatted message to fixup specified commit"),
129
+ OPT_STRING(0, "squash", &squash_message, "COMMIT", "use autosquash formatted message to squash specified commit"),
130
OPT_BOOLEAN(0, "reset-author", &renew_authorship, "the commit is authored by me now (used with -C-c/--amend)"),
131
OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
132
OPT_FILENAME('t', "template", &template_file, "use specified template file"),
@@ -567,6 +568,25 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
568
if (!no_verify && run_hook(index_file, "pre-commit", NULL))
569
return 0;
570
571
+ if (squash_message) {
572
+ /*
573
+ * Insert the proper subject line before other commit
574
+ * message options add their content.
575
+ */
576
+ if (use_message && !strcmp(use_message, squash_message))
577
+ strbuf_addstr(&sb, "squash! ");
578
+ else {
579
+ struct pretty_print_context ctx = {0};
580
+ struct commit *c;
581
+ c = lookup_commit_reference_by_name(squash_message);
582
+ if (!c)
583
+ die("could not lookup commit %s", squash_message);
584
+ ctx.output_encoding = get_commit_output_encoding();
585
+ format_commit_message(c, "squash! %s\n\n", &sb,
586
+ &ctx);
587
+ }
588
+ }
589
+
590
if (message.len) {
591
strbuf_addbuf(&sb, &message);
592
hook_arg1 = "message";
@@ -619,6 +639,16 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
639
else if (in_merge)
640
hook_arg1 = "merge";
641
642
+ if (squash_message) {
643
+ /*
644
+ * If squash_commit was used for the commit subject,
645
+ * then we're possibly hijacking other commit log options.
646
+ * Reset the hook args to tell the real story.
647
+ */
648
+ hook_arg1 = "message";
649
+ hook_arg2 = "";
650
+ }
651
+
652
fp = fopen(git_path(commit_editmsg), "w");
653
if (fp == NULL)
654
die_errno("could not open '%s'", git_path(commit_editmsg));
@@ -890,7 +920,8 @@ static int parse_and_validate_options(int argc, const char *argv[],
920
die("You have nothing to amend.");
921
if (amend && in_merge)
922
die("You are in the middle of a merge -- cannot amend.");
893
-
923
+ if (fixup_message && squash_message)
924
+ die("Options --squash and --fixup cannot be used together");
925
if (use_message)
926
f++;
927
if (edit_message)