From 77356122443039b4b65a7795d66b3d1fdeedcce8 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 22 Jun 2016 22:20:16 +0200 Subject: pretty: make the skip_blank_lines() function public This function will be used also in the find_commit_subject() function. While at it, rename the function to reflect that it skips not only empty lines, but any lines consisting of only whitespace, too. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano diff --git a/commit.h b/commit.h index 4789839..7efa2d2 100644 --- a/commit.h +++ b/commit.h @@ -176,6 +176,7 @@ extern const char *format_subject(struct strbuf *sb, const char *msg, const char *line_separator); extern void userformat_find_requirements(const char *fmt, struct userformat_want *w); extern int commit_format_is_empty(enum cmit_fmt); +extern const char *skip_blank_lines(const char *msg); extern void format_commit_message(const struct commit *commit, const char *format, struct strbuf *sb, const struct pretty_print_context *context); diff --git a/pretty.c b/pretty.c index 7b49304..0d40b1b 100644 --- a/pretty.c +++ b/pretty.c @@ -505,7 +505,7 @@ void pp_user_info(struct pretty_print_context *pp, } } -static int is_empty_line(const char *line, int *len_p) +static int is_blank_line(const char *line, int *len_p) { int len = *len_p; while (len && isspace(line[len - 1])) @@ -514,14 +514,14 @@ static int is_empty_line(const char *line, int *len_p) return !len; } -static const char *skip_empty_lines(const char *msg) +const char *skip_blank_lines(const char *msg) { for (;;) { int linelen = get_one_line(msg); int ll = linelen; if (!linelen) break; - if (!is_empty_line(msg, &ll)) + if (!is_blank_line(msg, &ll)) break; msg += linelen; } @@ -872,7 +872,7 @@ const char *format_subject(struct strbuf *sb, const char *msg, int linelen = get_one_line(line); msg += linelen; - if (!linelen || is_empty_line(line, &linelen)) + if (!linelen || is_blank_line(line, &linelen)) break; if (!sb) @@ -891,11 +891,11 @@ static void parse_commit_message(struct format_commit_context *c) const char *msg = c->message + c->message_off; const char *start = c->message; - msg = skip_empty_lines(msg); + msg = skip_blank_lines(msg); c->subject_off = msg - start; msg = format_subject(NULL, msg, NULL); - msg = skip_empty_lines(msg); + msg = skip_blank_lines(msg); c->body_off = msg - start; c->commit_message_parsed = 1; @@ -1642,7 +1642,7 @@ void pp_remainder(struct pretty_print_context *pp, if (!linelen) break; - if (is_empty_line(line, &linelen)) { + if (is_blank_line(line, &linelen)) { if (first) continue; if (pp->fmt == CMIT_FMT_SHORT) @@ -1709,7 +1709,7 @@ void pretty_print_commit(struct pretty_print_context *pp, } /* Skip excess blank lines at the beginning of body, if any... */ - msg = skip_empty_lines(msg); + msg = skip_blank_lines(msg); /* These formats treat the title line specially. */ if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL) -- cgit v0.10.2-6-g49f6 From 4e1b06da252a7609f0c6641750e6acbec451e698 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 22 Jun 2016 22:20:20 +0200 Subject: commit.c: make find_commit_subject() more robust Just like the pretty printing machinery, we should simply ignore blank lines at the beginning of the commit messages. This discrepancy was noticed when an early version of the rebase--helper produced commit objects with more than one empty line between the header and the commit message. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano diff --git a/commit.c b/commit.c index 0eee410..116774c 100644 --- a/commit.c +++ b/commit.c @@ -398,7 +398,7 @@ int find_commit_subject(const char *commit_buffer, const char **subject) while (*p && (*p != '\n' || p[1] != '\n')) p++; if (*p) { - p += 2; + p = skip_blank_lines(p + 2); for (eol = p; *eol && *eol != '\n'; eol++) ; /* do nothing */ } else diff --git a/t/t8008-blame-formats.sh b/t/t8008-blame-formats.sh index 29f84a6..92c8e79 100755 --- a/t/t8008-blame-formats.sh +++ b/t/t8008-blame-formats.sh @@ -87,4 +87,21 @@ test_expect_success 'blame --line-porcelain output' ' test_cmp expect actual ' +test_expect_success '--porcelain detects first non-blank line as subject' ' + ( + GIT_INDEX_FILE=.git/tmp-index && + export GIT_INDEX_FILE && + echo "This is it" >single-file && + git add single-file && + tree=$(git write-tree) && + commit=$(printf "%s\n%s\n%s\n\n\n \noneline\n\nbody\n" \ + "tree $tree" \ + "author A 123456789 +0000" \ + "committer C 123456789 +0000" | + git hash-object -w -t commit --stdin) && + git blame --porcelain $commit -- single-file >output && + grep "^summary oneline$" output + ) +' + test_done -- cgit v0.10.2-6-g49f6 From 84e213a30a1d4a3835e23b2f3d6217eb74ea55f7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 29 Jun 2016 16:14:42 +0200 Subject: commit -C: skip blank lines at the beginning of the message Consistent with the pretty-printing machinery, we skip leading blank lines (if any) of existing commit messages. While Git itself only produces commit objects with a single empty line between commit header and commit message, it is legal to have more than one blank line (i.e. lines containing only white space, or no characters) at the beginning of the commit message, and the pretty-printing code already handles that. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano diff --git a/builtin/commit.c b/builtin/commit.c index c2ebea4..b0effbb 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -712,7 +712,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, char *buffer; buffer = strstr(use_message_buffer, "\n\n"); if (buffer) - strbuf_addstr(&sb, buffer + 2); + strbuf_addstr(&sb, skip_blank_lines(buffer + 2)); hook_arg1 = "commit"; hook_arg2 = use_message; } else if (fixup_message) { -- cgit v0.10.2-6-g49f6 From 88ef402f9c18dafee5c8b222200f8f984b17a73e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 29 Jun 2016 16:14:46 +0200 Subject: sequencer: use skip_blank_lines() to find the commit subject Just like we already taught the find_commit_subject() function (to make it consistent with the code in pretty.c), we now simply skip leading blank lines of the commit message. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano diff --git a/sequencer.c b/sequencer.c index c4f4b7d..e286a6e 100644 --- a/sequencer.c +++ b/sequencer.c @@ -568,10 +568,8 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts) * information followed by "\n\n". */ p = strstr(msg.message, "\n\n"); - if (p) { - p += 2; - strbuf_addstr(&msgbuf, p); - } + if (p) + strbuf_addstr(&msgbuf, skip_blank_lines(p + 2)); if (opts->record_origin) { if (!has_conforming_footer(&msgbuf, NULL, 0)) -- cgit v0.10.2-6-g49f6 From 054a5aee6f3e8e90d96f7b3f76f5f55752561c59 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 29 Jun 2016 16:14:50 +0200 Subject: reset --hard: skip blank lines when reporting the commit subject When there are blank lines at the beginning of a commit message, the pretty printing machinery already skips them when showing a commit subject (or the complete commit message). We shall henceforth do the same when reporting the commit subject after the user called git reset --hard Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano diff --git a/builtin/reset.c b/builtin/reset.c index 4c08ddc..34d8b23 100644 --- a/builtin/reset.c +++ b/builtin/reset.c @@ -103,7 +103,7 @@ static void print_new_head_line(struct commit *commit) if (body) { const char *eol; size_t len; - body += 2; + body = skip_blank_lines(body + 2); eol = strchr(body, '\n'); len = eol ? eol - body : strlen(body); printf(" %.*s\n", (int) len, body); -- cgit v0.10.2-6-g49f6