git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patryk Obara <patryk.obara@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com
Subject: [PATCH 2/2] commit: fix ending newline for template files
Date: Tue, 26 May 2015 08:15:08 +0200	[thread overview]
Message-ID: <1432620908-16071-3-git-send-email-patryk.obara@gmail.com> (raw)
In-Reply-To: <1432620908-16071-1-git-send-email-patryk.obara@gmail.com>

git-commit with -t or -F -e uses content of user-supplied file as
initial value for commit msg in editor. There is no guarantee, that this
file ends with newline - it depends on file content and editor used to
create file (some editors append and hide last newline from user while
others do not).

When --status (default) is supplied, additional comment is placed after
template content. If template file ended with newline this results in
additional line being appended (which may be unexpected e.g. when last
line of template is a comment). On the other hand, first line of status
should never be concatenated to last line of template file.

Append newline before status _only_ if template/logfile didn't end with
one already. This way content of template is exactly the way user intended
and there's no chance, that line of status will merge with last line of
template.

Remove unnecessary premature cleanup of commit message, which was
implemented for -F, but not for -t.

Signed-off-by: Patryk Obara <patryk.obara@gmail.com>
---
 builtin/commit.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index da79ac4..eb41e05 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -666,8 +666,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	struct strbuf sb = STRBUF_INIT;
 	const char *hook_arg1 = NULL;
 	const char *hook_arg2 = NULL;
-	int clean_message_contents = (cleanup_mode != CLEANUP_NONE);
 	int old_display_comment_prefix;
+	int sb_ends_with_newline = 0;
 
 	/* This checks and barfs if author is badly specified */
 	determine_author_info(author_ident);
@@ -737,7 +737,6 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		if (strbuf_read_file(&sb, template_file, 0) < 0)
 			die_errno(_("could not read '%s'"), template_file);
 		hook_arg1 = "template";
-		clean_message_contents = 0;
 	}
 
 	/*
@@ -775,9 +774,6 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	 */
 	s->hints = 0;
 
-	if (clean_message_contents)
-		stripspace(&sb, 0);
-
 	if (signoff)
 		append_signoff(&sb, ignore_non_trailer(&sb), 0);
 
@@ -786,6 +782,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 
 	if (auto_comment_line_char)
 		adjust_comment_line_char(&sb);
+
+	sb_ends_with_newline = ends_with(sb.buf, "\n");
+
 	strbuf_release(&sb);
 
 	/* This checks if committer ident is explicitly given */
@@ -794,6 +793,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		int ident_shown = 0;
 		int saved_color_setting;
 		struct ident_split ci, ai;
+		int append_newline = (template_file || logfile) ? !sb_ends_with_newline : 1;
+
+		if (append_newline)
+			fprintf(s->fp, "\n");
 
 		if (whence != FROM_COMMIT) {
 			if (cleanup_mode == CLEANUP_SCISSORS)
@@ -815,7 +818,6 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 					 : "CHERRY_PICK_HEAD"));
 		}
 
-		fprintf(s->fp, "\n");
 		if (cleanup_mode == CLEANUP_ALL)
 			status_printf(s, GIT_COLOR_NORMAL,
 				_("Please enter the commit message for your changes."
-- 
2.4.1

  parent reply	other threads:[~2015-05-26  6:16 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-26  6:15 [PATCH 0/2] commit -t appends newline after template file Patryk Obara
2015-05-26  6:15 ` [PATCH 1/2] t750*: make tests for commit messages more pedantic Patryk Obara
2015-05-28 13:34   ` Eric Sunshine
2015-05-28 18:55     ` Junio C Hamano
2015-05-26  6:15 ` Patryk Obara [this message]
2015-05-28 14:29   ` [PATCH 2/2] commit: fix ending newline for template files Eric Sunshine
2015-05-28 18:22     ` Junio C Hamano
2015-05-28 18:35       ` Eric Sunshine
2015-05-29 20:17         ` Junio C Hamano
2015-05-29 22:25           ` Eric Sunshine
2015-05-30 11:29             ` Patryk Obara
2015-05-31  2:21               ` Eric Sunshine
2015-05-30 16:59             ` Junio C Hamano
2015-05-28 10:06 ` [PATCH 0/2] commit -t appends newline after template file Patryk Obara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1432620908-16071-3-git-send-email-patryk.obara@gmail.com \
    --to=patryk.obara@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).