Skip to content

Commit 1751e58

Browse files
peffgitster
authored andcommitted
commit: refactor base-case of adjust_comment_line_char()
When core.commentChar is set to "auto", we check a set of candidate characters against the proposed buffer to see which if any can be used without ambiguity. But before we do that, we optimize for the common case that the default "#" is fine by just seeing if it is present in the buffer at all. The way we do this is a bit subtle, though: we assign the candidate character to comment_line_char preemptively, then check if it works, and return if it does. The subtle part is that sometimes setting comment_line_char is important (after we return, the important outcome is the fact that we have set the variable) and sometimes it is useless (if our optimization fails, we go on to do the more careful checks and eventually assign something else instead). To make it more clear what is happening (and to make further refactoring of comment_line_char easier), let's check our candidate character directly, and then assign as part of returning if it worked out. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3b45450 commit 1751e58

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

builtin/commit.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,10 @@ static void adjust_comment_line_char(const struct strbuf *sb)
684684
char *candidate;
685685
const char *p;
686686

687-
comment_line_char = candidates[0];
688-
if (!memchr(sb->buf, comment_line_char, sb->len))
687+
if (!memchr(sb->buf, candidates[0], sb->len)) {
688+
comment_line_char = candidates[0];
689689
return;
690+
}
690691

691692
p = sb->buf;
692693
candidate = strchr(candidates, *p);

0 commit comments

Comments
 (0)