Skip to content

Instantly share code, notes, and snippets.

@prakashk
Created April 17, 2014 04:25
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/10952754 to your computer and use it in GitHub Desktop.
Save prakashk/10952754 to your computer and use it in GitHub Desktop.
rename files in git lost-found directory - v2
-- 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 = head $ fst $ head $ lex s
in case firstToken of
';' -> "clj"
'-' -> "hs"
'#' -> "pl"
_ -> "??"
problemId contents =
safeHead $ map snd $ problemIds $ pairwise $ words contents
where pairwise ws = zip ws $ tail ws
problemIds = filter (\(a, b) -> a `elem` ["Problem", "problem"])
safeHead [] = Nothing
safeHead (x:_) = Just x
findFileName contents =
fmap (\x -> ft ++ "/" ++ x ++ "." ++ ft) prid
where ft = fileType contents
prid = problemId contents
inferFileName path = do
contents <- readFile path
return $ findFileName contents
main = do
dirContents <- getDirectoryContents lostFilesDir
let lfiles = filter (\file -> not $ file `elem` [".", ".."]) dirContents
forM_ lfiles (\lfile -> do
fname <- inferFileName $ lostFilesDir ++ "/" ++ lfile
putStrLn $
case fname of
Just x -> lfile ++ " -> " ++ x
_ -> lfile ++ ": No program??"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment