summaryrefslogtreecommitdiff
path: root/linters/lint-submodule-refs
diff options
context:
space:
mode:
authorMatthew Pickering <matthewtpickering@gmail.com>2022-02-18 17:27:32 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2022-02-24 20:25:06 -0500
commit6555b68ca0678827b89c5624db071f5a485d18b7 (patch)
tree9dbcd231add48a179d7751606523865029d2fc1a /linters/lint-submodule-refs
parent06c18990fb6f10aaf1907ba8f0fe3f1a138da159 (diff)
downloadhaskell-6555b68ca0678827b89c5624db071f5a485d18b7.tar.gz
Move linters into the tree
This MR moves the GHC linters into the tree, so that they can be run directly using Hadrian. * Query all files tracked by Git instead of using changed files, so that we can run the exact same linting step locally and in a merge request. * Only check that the changelogs don't contain TBA when RELEASE=YES. * Add hadrian/lint script, which runs all the linting steps. * Ensure the hlint job exits with a failure if hlint is not installed (otherwise we were ignoring the failure). Given that hlint doesn't seem to be available in CI at the moment, I've temporarily allowed failure in the hlint job. * Run all linting tests in CI using hadrian.
Diffstat (limited to 'linters/lint-submodule-refs')
-rw-r--r--linters/lint-submodule-refs/Main.hs77
-rw-r--r--linters/lint-submodule-refs/cabal.project2
-rw-r--r--linters/lint-submodule-refs/lint-submodule-refs.cabal28
3 files changed, 107 insertions, 0 deletions
diff --git a/linters/lint-submodule-refs/Main.hs b/linters/lint-submodule-refs/Main.hs
new file mode 100644
index 0000000000..f99f066da0
--- /dev/null
+++ b/linters/lint-submodule-refs/Main.hs
@@ -0,0 +1,77 @@
+{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE OverloadedStrings #-}
+
+module Main where
+
+-- base
+import Control.Monad
+ ( forM, forM_, unless, when )
+import Data.List
+ ( partition )
+import Data.Maybe
+ ( mapMaybe )
+import System.Environment
+ ( getArgs )
+import System.Exit
+ ( ExitCode(..), exitWith )
+
+-- text
+import qualified Data.Text as T
+import qualified Data.Text.IO as T
+ ( putStrLn )
+
+-- linters-common
+import Linters.Common
+ ( GitType(..)
+ , gitBranchesContain, gitCatCommit, gitDiffTree, gitNormCid
+ )
+
+--------------------------------------------------------------------------------
+
+main :: IO ()
+main = do
+ dir:refs <- getArgs >>= \case
+ [] -> fail "usage: lint-submodule-refs <git-repo> [<commit-id>+]"
+ x -> return x
+
+ forM_ (map T.pack refs) $ \ref -> do
+ (cid,deltas) <- gitDiffTree dir ref
+
+ let smDeltas = [ (smPath, smCid) | (_, (GitTypeGitLink, smCid), smPath) <- deltas ]
+
+ unless (null smDeltas) $ do
+ T.putStrLn $ "Submodule update(s) detected in " <> cid <> ":"
+
+ (_, msg) <- gitCatCommit dir cid
+
+ unless ("submodule" `T.isInfixOf` msg) $ do
+ T.putStrLn "*FAIL* commit message does not contain magic 'submodule' word."
+ T.putStrLn "This lint avoids accidental changes to git submodules."
+ T.putStrLn "Include the word 'submodule' in your commit message to silence this warning, e.g. 'Update submodule'."
+ exitWith (ExitFailure 1)
+
+ bad <- fmap or $ forM smDeltas $ \(smPath,smCid) -> do
+ T.putStrLn $ " - " <> smPath <> " => " <> smCid
+
+ let smAbsPath = dir ++ "/" ++ T.unpack smPath
+ remoteBranches <- gitBranchesContain smAbsPath smCid
+
+ let (wip, nonWip) = partition ("wip/" `T.isPrefixOf`) originBranches
+ originBranches = mapMaybe isOriginTracking remoteBranches
+ isOriginTracking = T.stripPrefix "origin/"
+ let bad = null nonWip
+ when bad $ do
+ T.putStrLn $ " *FAIL* commit not found in submodule repo"
+ T.putStrLn " or not reachable from persistent branches"
+ T.putStrLn ""
+ when (not $ null wip) $ do
+ T.putStrLn " Found the following non-mirrored WIP branches:"
+ forM_ wip $ \branch -> do
+ commit <- gitNormCid smAbsPath branch
+ T.putStrLn $ " - " <> branch <> " -> " <> commit
+ T.putStrLn ""
+ pure bad
+
+ if bad
+ then exitWith (ExitFailure 1)
+ else T.putStrLn " OK"
diff --git a/linters/lint-submodule-refs/cabal.project b/linters/lint-submodule-refs/cabal.project
new file mode 100644
index 0000000000..444b636706
--- /dev/null
+++ b/linters/lint-submodule-refs/cabal.project
@@ -0,0 +1,2 @@
+packages: .,
+ ../linters-common
diff --git a/linters/lint-submodule-refs/lint-submodule-refs.cabal b/linters/lint-submodule-refs/lint-submodule-refs.cabal
new file mode 100644
index 0000000000..ce4012adfc
--- /dev/null
+++ b/linters/lint-submodule-refs/lint-submodule-refs.cabal
@@ -0,0 +1,28 @@
+cabal-version: 3.0
+name: lint-submodule-refs
+version: 0.1.0.0
+synopsis: Lint submodule references
+license: GPL-3.0-only
+author: The GHC team
+build-type: Simple
+
+executable lint-submodule-refs
+
+ default-language:
+ Haskell2010
+
+ build-depends:
+ base
+ >= 4.14 && < 5,
+ text
+ >= 1.2 && < 2.1,
+ linters-common
+
+ ghc-options:
+ -Wall
+
+ hs-source-dirs:
+ .
+
+ main-is:
+ Main.hs