Last active
August 29, 2015 13:59
-
-
Save prakashk/10871712 to your computer and use it in GitHub Desktop.
rename files in git lost-found directory
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
-- rename Project Euler files in git lost-found directory | |
-- get the file type and the problem number from the initial comments | |
-- in each file | |
import System.Directory | |
import Text.Regex.Posix | |
import Control.Monad (forM_) | |
lostFilesDir = ".git/lost-found/other" | |
fileType s = | |
let firstToken = fst $ head $ lex s | |
in case firstToken of | |
";" -> "clj" | |
"--" -> "hs" | |
"#" -> "pl" | |
_ -> "??" | |
problemId s = snd $ head $ filter (\(a, b) -> a `elem` ["Problem", "problem"]) $ zip ws $ tail ws | |
where ws = words s | |
headerLine contents = | |
case filter (isHeaderLine) $ lines contents of | |
(x:_) -> Just x | |
[] -> Nothing | |
where isHeaderLine = (=~ "[pP]roblem [0-9]+") | |
findFileName line = | |
let ft = fileType line | |
in ft ++ "/" ++ problemId line ++ "." ++ ft | |
main = do | |
files <- getDirectoryContents lostFilesDir | |
forM_ (filter (\file -> not $ file `elem` [".", ".."]) files) | |
(\file -> do | |
let path = lostFilesDir ++ "/" ++ file | |
contents <- readFile $ path | |
case headerLine contents of | |
Just hl -> print $ path ++ " -> " ++ findFileName hl | |
_ -> print $ file ++ ": No program???" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment