Modified the checkpatch script to ensure that commit tags (e.g.,
Signed-off-by, Reviewed-by, Acked-by, Tested-by, etc.) appear in the
correct order according to kernel conventions [1].

checkpatch.pl will now emit a BAD_TAG_ORDER warning when tags are out of
the expected sequence. Multiple tags of the same type are allowed, but
they must also follow the order. 'Link:' tags in the changelog are still
allowed before the tag sequence begins, but once the sequence has started,
any 'Link:' tags must follow the ordered commit tags. 

Link: 
https://www.kernel.org/doc/html/latest/process/maintainer-tip.html#ordering-of-commit-tags
 # [1]

Signed-off-by: Hendrik Hamerlinck <[email protected]>
---
 Documentation/dev-tools/checkpatch.rst |  6 ++++
 scripts/checkpatch.pl                  | 40 ++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/Documentation/dev-tools/checkpatch.rst 
b/Documentation/dev-tools/checkpatch.rst
index 76bd0ddb0041..696b42bf4ff5 100644
--- a/Documentation/dev-tools/checkpatch.rst
+++ b/Documentation/dev-tools/checkpatch.rst
@@ -599,6 +599,12 @@ Commit message
 
     See: 
https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
 
+  **BAD_TAG_ORDER**
+    The tags in the commit message are not in the correct order according to
+    community conventions. Common tags like Signed-off-by, Reviewed-by,
+    Tested-by, Acked-by, Fixes, Cc, etc., should follow a standardized 
sequence.
+
+    See: 
https://www.kernel.org/doc/html/latest/process/maintainer-tip.html#ordering-of-commit-tags
 
 Comparison style
 ----------------
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 664f7b7a622c..267ec02de9ec 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -661,6 +661,24 @@ foreach my $entry (@link_tags) {
 }
 $link_tags_search = "(?:${link_tags_search})";
 
+# Ordered commit tags
+our @commit_tags = (
+       "Fixes:",
+       "Reported-by:",
+       "Closes:",
+       "Originally-by:",
+       "Suggested-by:",
+       "Co-developed-by:",
+       "Signed-off-by:",
+       "Tested-by:",
+       "Reviewed-by",
+       "Acked-by:",
+       "Cc:",
+       "Link:"
+);
+our $commit_tag_pattern = join '|', map { quotemeta($_) } @commit_tags;
+our $commit_tags_regex = qr{(?xi: ^\s*($commit_tag_pattern))};
+
 our $tracing_logging_tags = qr{(?xi:
        [=-]*> |
        <[=-]* |
@@ -2712,6 +2730,8 @@ sub process {
 
        my $checklicenseline = 1;
 
+       my $last_matched_tag;
+
        sanitise_line_reset();
        my $line;
        foreach my $rawline (@rawlines) {
@@ -3258,6 +3278,26 @@ sub process {
                        }
                }
 
+# Check commit tags sorting
+               if (!$in_header_lines && $line =~ $commit_tags_regex) {
+                       my $tag = $1;
+                       my ($tag_index) = grep { lc($commit_tags[$_]) eq 
lc($tag) } 0..$#commit_tags;
+
+                       if ($last_matched_tag &&
+                           $last_matched_tag->{tag_index} > $tag_index) {
+                               WARN("BAD_TAG_ORDER",
+                                    "Tag '$tag' is out of order. Should come 
before '$last_matched_tag->{tag}'\n" . $herecurr);
+                       }
+
+                       # Allow link tags to occur before the commit tags
+                       if (lc($tag) ne "link:" || defined $last_matched_tag) {
+                               $last_matched_tag = {
+                                       tag       => $tag,
+                                       tag_index => $tag_index,
+                               };
+                       }
+               }
+
 # Check email subject for common tools that don't need to be mentioned
                if ($in_header_lines &&
                    $line =~ 
/^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
-- 
2.43.0


Reply via email to