Skip to content

Instantly share code, notes, and snippets.

@lehins
Last active March 19, 2020 00:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lehins/fd36a8cc8bf853173437b17f6b6426ad to your computer and use it in GitHub Desktop.
Save lehins/fd36a8cc8bf853173437b17f6b6426ad to your computer and use it in GitHub Desktop.
Restore file and directory modification time to the commit time
{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ < 802
main :: IO ()
main = do
let (ghcMaj, ghcMin) = divMod (__GLASGOW_HASKELL__ :: Int) 100
ghcVer = show ghcMaj ++ "." ++ show ghcMin
putStrLn $ "GHC version: " ++ ghcVer ++ " is not supported by git-modtime script."
#else
import Data.Time.Format (parseTimeM, iso8601DateFormat, defaultTimeLocale)
import System.Directory (setModificationTime)
import System.Environment (getArgs)
import System.Process (readProcess)
main :: IO ()
main = do
args <- getArgs
let rev = case args of
[] -> "HEAD"
(x:_) -> x
fs <- readProcess "git" ["ls-tree", "-r", "-t", "--full-name", "--name-only", rev] ""
let iso8601 = iso8601DateFormat (Just "%H:%M:%S%z")
restoreFileModtime fp = do
modTimeStr <- readProcess "git" ["log", "--pretty=format:%cI", "-1", rev, "--", fp] ""
modTime <- parseTimeM True defaultTimeLocale iso8601 modTimeStr
setModificationTime fp modTime
putStrLn $ "[" ++ modTimeStr ++ "] " ++ fp
putStrLn "Restoring modification time for all these files:"
mapM_ restoreFileModtime $ lines fs
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment