From 99e114f5c598ce6321c81bbe41e212877a3c375c Mon Sep 17 00:00:00 2001
From: Ben Sima <ben@bsima.me>
Date: Mon, 1 Apr 2024 16:35:15 -0400
Subject: Make bild exception for git hooks

Apparently git hooks don't get called if they have a file extention, so these
weren't getting called at all since commit
904de577261e7024373e7a42fd763184764238f9. So this renames them back to the
extension-less versions, and adds an exception in bild for files in the
core.hooksPath directory.

Unfortunately this means Lint.hs will silently ignore these files, but I guess
that's okay for now.
---
 Biz/Bild.hs | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

(limited to 'Biz/Bild.hs')

diff --git a/Biz/Bild.hs b/Biz/Bild.hs
index 15f2700..15d9619 100644
--- a/Biz/Bild.hs
+++ b/Biz/Bild.hs
@@ -160,7 +160,8 @@ main = Cli.Plan help move test_ pure |> Cli.main
         "Biz.Bild"
         [ test_bildBild,
           test_bildExamples,
-          test_isGitIgnored
+          test_isGitIgnored,
+          test_isGitHook
         ]
 
 test_bildBild :: Test.Tree
@@ -201,6 +202,7 @@ move args =
       Cli.getAllArgs args (Cli.argument "target")
         |> filterM Dir.doesFileExist
         +> filterM (\x -> isGitIgnored x /> don't)
+        +> filterM (\x -> isGitHook root x /> don't)
         +> traverse Dir.makeAbsolute
         +> traverse (namespaceFromPathOrDie root)
         +> foldM analyze mempty
@@ -275,6 +277,27 @@ test_isGitIgnored =
           +> (@=? [False, True])
     ]
 
+isGitHook :: FilePath -> FilePath -> IO Bool
+isGitHook root path =
+  Process.readProcess "git" ["config", "--local", "core.hooksPath"] ""
+    /> strip
+    /> stripRoot root
+    /> flip List.isInfixOf path
+
+test_isGitHook :: Test.Tree
+test_isGitHook =
+  Test.group
+    "isGitHook"
+    [ Test.unit "filters pre-commit hook" <| do
+        root <- Env.getEnv "CODEROOT"
+        res <- isGitHook root <| root <> "/Biz/Ide/hooks/pre-commit"
+        res @=? True,
+      Test.unit "doesn't filter non-hooks" <| do
+        root <- Env.getEnv "CODEROOT"
+        res <- isGitHook root <| root <> "/Biz/Bild.hs"
+        res @=? False
+    ]
+
 namespaceFromPathOrDie :: FilePath -> FilePath -> IO Namespace
 namespaceFromPathOrDie root path =
   Namespace.fromPath root path |> \case
@@ -765,8 +788,6 @@ detectHaskellImports hmap contentLines =
                     (pkgs <> deps, map (stripRoot root) files |> Set.fromList)
                       |> pure
   where
-    stripRoot :: FilePath -> FilePath -> QualifiedPath
-    stripRoot root f = fromMaybe f (List.stripPrefix (root <> "/") f)
     filepaths :: [String] -> IO [FilePath]
     filepaths imports =
       imports
@@ -791,6 +812,9 @@ detectHaskellImports hmap contentLines =
     onlyHaskell :: Analysis -> Analysis
     onlyHaskell = Map.filterWithKey (\ns _ -> ext ns == Namespace.Hs)
 
+stripRoot :: FilePath -> FilePath -> FilePath
+stripRoot root f = fromMaybe f (List.stripPrefix (root <> "/") f)
+
 detectLispImports :: [Text] -> IO (Set Meta.Dep)
 detectLispImports contentLines =
   contentLines
-- 
cgit v1.2.3