Permalink
Browse files

path: add pathExistOr, simplify show-pack, show-idx

Remove similar helper functions fPack, fIdx
  • Loading branch information...
1 parent fc4ff29 commit 674efcae78723a287f0d23a050a7156d2da3f066 @kfish committed May 11, 2011
Showing with 18 additions and 14 deletions.
  1. +16 −0 Git/Path.hs
  2. +2 −14 tools/ght.hs
View
@@ -1,9 +1,13 @@
{-# OPTIONS -Wall #-}
module Git.Path (
+ -- * Generate paths in git dir
gitPath
, gitRoot
, gitDeref
+
+ -- * General path handling
+ , pathExistOr
) where
import Control.Monad ((<=<))
@@ -67,6 +71,18 @@ gitDeref = deref <=< L.readFile <=< gitPath
chomp = C.takeWhile (/= '\n')
------------------------------------------------------------
+-- pathExistOr
+
+-- | Return the given path if it exists, else the result of applying the
+-- modifier function
+pathExistOr :: (FilePath -> IO FilePath) -> FilePath -> IO FilePath
+pathExistOr f path = do
+ exists <- doesFileExist path
+ if exists
+ then return path
+ else f path
+
+------------------------------------------------------------
-- normalise
--
View
@@ -139,13 +139,7 @@ ghtShowPack = defCmd {
cmdExamples = [("Show raw contents of pack pack-abcd.pack", "abcd")]
}
-ghtShowPackHandler = mapM_ (liftIO . (putStrLn . show <=< packRead <=< fPack)) =<< appArgs
-
-fPack pack = do
- exists <- doesFileExist pack
- if exists
- then return pack
- else packPath pack
+ghtShowPackHandler = mapM_ (liftIO . (putStrLn . show <=< packRead <=< pathExistOr packPath)) =<< appArgs
------------------------------------------------------------
-- show-idx
@@ -159,13 +153,7 @@ ghtShowIdx = defCmd {
cmdExamples = [("Show raw contents of pack pack-abcd.idx", "abcd")]
}
-ghtShowIdxHandler = mapM_ (liftIO . (putStrLn <=< dumpRawPackIndex <=< fIdx)) =<< appArgs
-
-fIdx idx = do
- exists <- doesFileExist idx
- if exists
- then return idx
- else idxPath idx
+ghtShowIdxHandler = mapM_ (liftIO . (putStrLn <=< dumpRawPackIndex <=< pathExistOr idxPath)) =<< appArgs
------------------------------------------------------------
-- find-idx

0 comments on commit 674efca

Please sign in to comment.