Skip to content

Commit 4966b58

Browse files
committed
Merge branch 'js/find-commit-subject-ignore-leading-blanks' into maint
A helper function that takes the contents of a commit object and finds its subject line did not ignore leading blank lines, as is commonly done by other codepaths. Make it ignore leading blank lines to match. * js/find-commit-subject-ignore-leading-blanks: reset --hard: skip blank lines when reporting the commit subject sequencer: use skip_blank_lines() to find the commit subject commit -C: skip blank lines at the beginning of the message commit.c: make find_commit_subject() more robust pretty: make the skip_blank_lines() function public
2 parents 053e2fb + 054a5ae commit 4966b58

File tree

7 files changed

+31
-15
lines changed

7 files changed

+31
-15
lines changed

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
714714
char *buffer;
715715
buffer = strstr(use_message_buffer, "\n\n");
716716
if (buffer)
717-
strbuf_addstr(&sb, buffer + 2);
717+
strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
718718
hook_arg1 = "commit";
719719
hook_arg2 = use_message;
720720
} else if (fixup_message) {

builtin/reset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static void print_new_head_line(struct commit *commit)
103103
if (body) {
104104
const char *eol;
105105
size_t len;
106-
body += 2;
106+
body = skip_blank_lines(body + 2);
107107
eol = strchr(body, '\n');
108108
len = eol ? eol - body : strlen(body);
109109
printf(" %.*s\n", (int) len, body);

commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ int find_commit_subject(const char *commit_buffer, const char **subject)
414414
while (*p && (*p != '\n' || p[1] != '\n'))
415415
p++;
416416
if (*p) {
417-
p += 2;
417+
p = skip_blank_lines(p + 2);
418418
for (eol = p; *eol && *eol != '\n'; eol++)
419419
; /* do nothing */
420420
} else

commit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ extern const char *format_subject(struct strbuf *sb, const char *msg,
178178
const char *line_separator);
179179
extern void userformat_find_requirements(const char *fmt, struct userformat_want *w);
180180
extern int commit_format_is_empty(enum cmit_fmt);
181+
extern const char *skip_blank_lines(const char *msg);
181182
extern void format_commit_message(const struct commit *commit,
182183
const char *format, struct strbuf *sb,
183184
const struct pretty_print_context *context);

pretty.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ void pp_user_info(struct pretty_print_context *pp,
507507
}
508508
}
509509

510-
static int is_empty_line(const char *line, int *len_p)
510+
static int is_blank_line(const char *line, int *len_p)
511511
{
512512
int len = *len_p;
513513
while (len && isspace(line[len - 1]))
@@ -516,14 +516,14 @@ static int is_empty_line(const char *line, int *len_p)
516516
return !len;
517517
}
518518

519-
static const char *skip_empty_lines(const char *msg)
519+
const char *skip_blank_lines(const char *msg)
520520
{
521521
for (;;) {
522522
int linelen = get_one_line(msg);
523523
int ll = linelen;
524524
if (!linelen)
525525
break;
526-
if (!is_empty_line(msg, &ll))
526+
if (!is_blank_line(msg, &ll))
527527
break;
528528
msg += linelen;
529529
}
@@ -875,7 +875,7 @@ const char *format_subject(struct strbuf *sb, const char *msg,
875875
int linelen = get_one_line(line);
876876

877877
msg += linelen;
878-
if (!linelen || is_empty_line(line, &linelen))
878+
if (!linelen || is_blank_line(line, &linelen))
879879
break;
880880

881881
if (!sb)
@@ -894,11 +894,11 @@ static void parse_commit_message(struct format_commit_context *c)
894894
const char *msg = c->message + c->message_off;
895895
const char *start = c->message;
896896

897-
msg = skip_empty_lines(msg);
897+
msg = skip_blank_lines(msg);
898898
c->subject_off = msg - start;
899899

900900
msg = format_subject(NULL, msg, NULL);
901-
msg = skip_empty_lines(msg);
901+
msg = skip_blank_lines(msg);
902902
c->body_off = msg - start;
903903

904904
c->commit_message_parsed = 1;
@@ -1718,7 +1718,7 @@ void pp_remainder(struct pretty_print_context *pp,
17181718
if (!linelen)
17191719
break;
17201720

1721-
if (is_empty_line(line, &linelen)) {
1721+
if (is_blank_line(line, &linelen)) {
17221722
if (first)
17231723
continue;
17241724
if (pp->fmt == CMIT_FMT_SHORT)
@@ -1789,7 +1789,7 @@ void pretty_print_commit(struct pretty_print_context *pp,
17891789
}
17901790

17911791
/* Skip excess blank lines at the beginning of body, if any... */
1792-
msg = skip_empty_lines(msg);
1792+
msg = skip_blank_lines(msg);
17931793

17941794
/* These formats treat the title line specially. */
17951795
if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)

sequencer.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,8 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
544544
* information followed by "\n\n".
545545
*/
546546
p = strstr(msg.message, "\n\n");
547-
if (p) {
548-
p += 2;
549-
strbuf_addstr(&msgbuf, p);
550-
}
547+
if (p)
548+
strbuf_addstr(&msgbuf, skip_blank_lines(p + 2));
551549

552550
if (opts->record_origin) {
553551
if (!has_conforming_footer(&msgbuf, NULL, 0))

t/t8008-blame-formats.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,21 @@ test_expect_success 'blame --line-porcelain output' '
8787
test_cmp expect actual
8888
'
8989

90+
test_expect_success '--porcelain detects first non-blank line as subject' '
91+
(
92+
GIT_INDEX_FILE=.git/tmp-index &&
93+
export GIT_INDEX_FILE &&
94+
echo "This is it" >single-file &&
95+
git add single-file &&
96+
tree=$(git write-tree) &&
97+
commit=$(printf "%s\n%s\n%s\n\n\n \noneline\n\nbody\n" \
98+
"tree $tree" \
99+
"author A <[email protected]> 123456789 +0000" \
100+
"committer C <[email protected]> 123456789 +0000" |
101+
git hash-object -w -t commit --stdin) &&
102+
git blame --porcelain $commit -- single-file >output &&
103+
grep "^summary oneline$" output
104+
)
105+
'
106+
90107
test_done

0 commit comments

Comments
 (0)