Last active
March 19, 2020 00:21
-
-
Save lehins/fd36a8cc8bf853173437b17f6b6426ad to your computer and use it in GitHub Desktop.
Restore file and directory modification time to the commit time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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