Skip to content

Commit 4ce9f97

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: improve GIT_COMMIT_ID test
The preferred git commit id reference has the form commit <SHA-1> ("Title line") where SHA-1 is the commit hex hash with a minimum lenth of 12 and ("Title line") is the complete title line of the commit with a (" prefix and ") suffix. The current tests fail when the "Title line" has one or more embedded double quotes. Improve the test that finds the commit SHA-1 hex hash then ("Title line") by using $balanced_parens for a maximum of 3 consecutive lines. [akpm@linux-foundation.org: add missing &&] Link: https://lkml.kernel.org/r/976c6cdd680db4b55ae31b5fc2d1779da5c0dc66.camel@perches.com Signed-off-by: Joe Perches <joe@perches.com> Cc: Dwaipayan Ray <dwaipayanray1@gmail.com> Cc: Lukas Bulwahn <lukas.bulwahn@gmail.com> Cc: Denis Efremov <efremov@linux.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 046fc74 commit 4ce9f97

1 file changed

Lines changed: 51 additions & 31 deletions

File tree

‎scripts/checkpatch.pl‎

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,8 @@ sub git_commit_info {
11811181
# git log --format='%H %s' -1 $line |
11821182
# echo "commit $(cut -c 1-12,41-)"
11831183
# done
1184-
} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
1184+
} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ ||
1185+
$lines[0] =~ /^fatal: bad object $commit/) {
11851186
$id = undef;
11861187
} else {
11871188
$id = substr($lines[0], 0, 12);
@@ -2587,6 +2588,8 @@ sub process {
25872588
my $reported_maintainer_file = 0;
25882589
my $non_utf8_charset = 0;
25892590

2591+
my $last_git_commit_id_linenr = -1;
2592+
25902593
my $last_blank_line = 0;
25912594
my $last_coalesced_string_linenr = -1;
25922595

@@ -3170,10 +3173,20 @@ sub process {
31703173
}
31713174

31723175
# Check for git id commit length and improperly formed commit descriptions
3173-
if ($in_commit_log && !$commit_log_possible_stack_dump &&
3176+
# A correctly formed commit description is:
3177+
# commit <SHA-1 hash length 12+ chars> ("Complete commit subject")
3178+
# with the commit subject '("' prefix and '")' suffix
3179+
# This is a fairly compilicated block as it tests for what appears to be
3180+
# bare SHA-1 hash with minimum length of 5. It also avoids several types of
3181+
# possible SHA-1 matches.
3182+
# A commit match can span multiple lines so this block attempts to find a
3183+
# complete typical commit on a maximum of 3 lines
3184+
if ($perl_version_ok &&
3185+
$in_commit_log && !$commit_log_possible_stack_dump &&
31743186
$line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
31753187
$line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
3176-
($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
3188+
(($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
3189+
($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) ||
31773190
($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
31783191
$line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
31793192
$line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
@@ -3183,49 +3196,56 @@ sub process {
31833196
my $long = 0;
31843197
my $case = 1;
31853198
my $space = 1;
3186-
my $hasdesc = 0;
3187-
my $hasparens = 0;
31883199
my $id = '0123456789ab';
31893200
my $orig_desc = "commit description";
31903201
my $description = "";
3202+
my $herectx = $herecurr;
3203+
my $has_parens = 0;
3204+
my $has_quotes = 0;
3205+
3206+
my $input = $line;
3207+
if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) {
3208+
for (my $n = 0; $n < 2; $n++) {
3209+
if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) {
3210+
$orig_desc = $1;
3211+
$has_parens = 1;
3212+
# Always strip leading/trailing parens then double quotes if existing
3213+
$orig_desc = substr($orig_desc, 1, -1);
3214+
if ($orig_desc =~ /^".*"$/) {
3215+
$orig_desc = substr($orig_desc, 1, -1);
3216+
$has_quotes = 1;
3217+
}
3218+
last;
3219+
}
3220+
last if ($#lines < $linenr + $n);
3221+
$input .= " " . trim($rawlines[$linenr + $n]);
3222+
$herectx .= "$rawlines[$linenr + $n]\n";
3223+
}
3224+
$herectx = $herecurr if (!$has_parens);
3225+
}
31913226

3192-
if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3227+
if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
31933228
$init_char = $1;
31943229
$orig_commit = lc($2);
3195-
} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
3230+
$short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i);
3231+
$long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i);
3232+
$space = 0 if ($input =~ /\bcommit [0-9a-f]/i);
3233+
$case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
3234+
} elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) {
31963235
$orig_commit = lc($1);
31973236
}
31983237

3199-
$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
3200-
$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
3201-
$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
3202-
$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
3203-
if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
3204-
$orig_desc = $1;
3205-
$hasparens = 1;
3206-
} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
3207-
defined $rawlines[$linenr] &&
3208-
$rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
3209-
$orig_desc = $1;
3210-
$hasparens = 1;
3211-
} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
3212-
defined $rawlines[$linenr] &&
3213-
$rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
3214-
$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
3215-
$orig_desc = $1;
3216-
$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
3217-
$orig_desc .= " " . $1;
3218-
$hasparens = 1;
3219-
}
3220-
32213238
($id, $description) = git_commit_info($orig_commit,
32223239
$id, $orig_desc);
32233240

32243241
if (defined($id) &&
3225-
($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
3242+
($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) &&
3243+
$last_git_commit_id_linenr != $linenr - 1) {
32263244
ERROR("GIT_COMMIT_ID",
3227-
"Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
3245+
"Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herectx);
32283246
}
3247+
#don't report the next line if this line ends in commit and the sha1 hash is the next line
3248+
$last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i);
32293249
}
32303250

32313251
# Check for added, moved or deleted files

0 commit comments

Comments
 (0)