linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] checkpatch.pl: warn on invalid commit hash
@ 2019-07-10 23:19 Matteo Croce
  2019-07-10 23:42 ` Joe Perches
  0 siblings, 1 reply; 2+ messages in thread
From: Matteo Croce @ 2019-07-10 23:19 UTC (permalink / raw)
  To: LKML; +Cc: Andy Whitcroft, Joe Perches

It can happen that a commit message refers to an invalid hash, because
the referenced hash changed following a rebase, or simply by mistake.
Add a check in checkpatch.pl which checks that an hash referenced by a Fixes
tag or just cited in the commit message is a valid commit hash.

    $ scripts/checkpatch.pl <<'EOF'
    Subject: [PATCH] test commit

    Sample test commit to test checkpatch.pl
    Commit 1da177e4c3f4 ("Linux-2.6.12-rc2") really exists,
    commit 0bba044c4ce7 ("tree") is valid but not a commit,
    while commit b4cc0b1c0cca ("unknown") is invalid.

    Fixes: f0cacc14cade ("unknown")
    Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
    EOF
    WARNING: Invalid hash 0bba044c4ce7
    WARNING: Invalid hash b4cc0b1c0cca
    WARNING: Invalid hash f0cacc14cade
    total: 0 errors, 3 warnings, 4 lines checked

Signed-off-by: Matteo Croce <mcroce@redhat.com>
---
 scripts/checkpatch.pl | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index a6d436809bf5..6fe15fbe876f 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2898,6 +2898,13 @@ sub process {
 			}
 		}
 
+# check for invalid hashes
+		if ($in_commit_log && $line =~ /(^fixes:|commit)\s+([0-9a-f]{6,40})\b/i) {
+			if (`git cat-file -t $2 2>/dev/null` ne "commit\n") {
+				WARN('INVALID_COMMIT_HASH', "Invalid commit hash $2");
+			}
+		}
+
 # ignore non-hunk lines and lines being removed
 		next if (!$hunk_line || $line =~ /^-/);
 
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-07-10 23:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-10 23:19 [PATCH] checkpatch.pl: warn on invalid commit hash Matteo Croce
2019-07-10 23:42 ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).