Skip to content

Instantly share code, notes, and snippets.

@prakashk
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prakashk/10871712 to your computer and use it in GitHub Desktop.
Save prakashk/10871712 to your computer and use it in GitHub Desktop.
rename files in git lost-found directory
-- 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