Skip to content

Commit 015e6b6

Browse files
lib/advisories: add function to get Git repo root
We want to expand the capabilities of `hsec-tools` so that the tool can be used to: - reserve advisories - prepare new advisory templates - assign IDs to placeholder IDs (e.g. in CI) - "fsck" the database For tolerable(?) user experience, it should be possible to run the tool anywhere within the *security-advisories* repository, or point it to (somewhere within) the repository. To help with this, add: ```haskell getRepoRoot :: FilePath -> IO (Either GitError FilePath) ``` CLI commands can apply this function to the current working directory, or a user-specified argument, to find the root of the repository.
1 parent 4fa6bcc commit 015e6b6

File tree

1 file changed

+20
-0
lines changed
  • code/hsec-tools/src/Security/Advisories

1 file changed

+20
-0
lines changed

code/hsec-tools/src/Security/Advisories/Git.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ module Security.Advisories.Git
77
( AdvisoryGitInfo(..)
88
, GitError(..)
99
, getAdvisoryGitInfo
10+
, getRepoRoot
1011
)
1112
where
1213

14+
import Data.Char (isSpace)
15+
import Data.List (dropWhileEnd)
1316
import Data.Time (ZonedTime)
1417
import Data.Time.Format.ISO8601 (iso8601ParseM)
1518
import System.Exit (ExitCode(ExitSuccess))
@@ -26,6 +29,23 @@ data GitError
2629
| GitTimeParseError String -- ^ unable to parse this input as a datetime
2730
deriving (Show)
2831

32+
-- | Get top-level directory of the working tree.
33+
--
34+
getRepoRoot :: FilePath -> IO (Either GitError FilePath)
35+
getRepoRoot path = do
36+
(status, stdout, stderr) <- readProcessWithExitCode
37+
"git"
38+
[ "-C", path
39+
, "rev-parse"
40+
, "--show-toplevel"
41+
]
42+
"" -- standard input
43+
pure $ case status of
44+
ExitSuccess -> Right $ trim stdout
45+
_ -> Left $ GitProcessError status stdout stderr
46+
where
47+
trim = dropWhileEnd isSpace . dropWhile isSpace
48+
2949
getAdvisoryGitInfo :: FilePath -> IO (Either GitError AdvisoryGitInfo)
3050
getAdvisoryGitInfo path = do
3151
let (dir, file) = splitFileName path

0 commit comments

Comments
 (0)