-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from fujimura/follow-mv-and-count-commits
Follow file move and count changes
- Loading branch information
Showing
10 changed files
with
280 additions
and
73 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import qualified Data.Text as T | ||
import qualified Data.Text.Encoding as T | ||
import qualified Data.Text.IO as T | ||
import qualified Text.Trifecta.Parser as P |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,10 @@ jobs: | |
with: | ||
ghc-version: ${{ matrix.ghc }} | ||
cabal-version: ${{ matrix.cabal }} | ||
- run: cabal test --test-show-details=direct | ||
- run: | | ||
git config --global user.email "[email protected]" | ||
git config --global user.name "John Doe" | ||
cabal test --test-show-details=direct | ||
build: | ||
name: Build on GHC ${{ matrix.ghc }} | ||
runs-on: ubuntu-latest | ||
|
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module Git.NumStat (numstat) where | ||
|
||
import Control.Applicative | ||
import qualified Data.Text as T | ||
import Text.Trifecta hiding (spaces) | ||
import Text.Parser.LookAhead (LookAheadParsing(lookAhead)) | ||
|
||
import Types | ||
|
||
numstat :: Parser (Maybe NumStat) | ||
numstat = do | ||
hyphen <- lookAhead $ optional (char '-') | ||
case hyphen of | ||
Just _ -> return Nothing | ||
Nothing -> do | ||
added <- read <$> manyTill digit tab | ||
skipMany tab | ||
deleted <- read <$> manyTill digit tab | ||
skipMany tab | ||
fm <- optional (try filemoveWithBrace <|> try filemove) | ||
case fm of | ||
Just (old, current) -> return $ Just (T.pack old, Delta { added = added, deleted = deleted } , Just $ T.pack current) | ||
Nothing -> do | ||
current <- manyTill anyChar eof | ||
return $ Just (T.pack current, Delta { added = added, deleted = deleted }, Nothing) | ||
|
||
filemoveWithBrace :: Parser (String, String) | ||
filemoveWithBrace = do | ||
before <- manyTill anyChar (char '{') | ||
from <- manyTill anyChar space | ||
skipSome $ string "=> " | ||
to <- manyTill anyChar (char '}') | ||
after <- manyTill anyChar eof | ||
|
||
return (before ++ from ++ after, before ++ to ++ after) | ||
|
||
filemove :: Parser (String, String) | ||
filemove = do | ||
from <- manyTill anyChar space | ||
skipSome $ string "=> " | ||
to <- manyTill anyChar eof | ||
|
||
return (from, to) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{-# LANGUAGE OverloadedRecordDot #-} | ||
|
||
module Types where | ||
|
||
import Data.Text (Text) | ||
import Data.Map.Strict (Map) | ||
|
||
type FileName = Text | ||
data Delta = Delta | ||
{ added::Int | ||
, deleted:: Int | ||
} deriving (Show, Eq) | ||
|
||
instance Semigroup Delta where | ||
x <> y = Delta { added = x.added + y.added, deleted = x.deleted + y.deleted } | ||
|
||
instance Monoid Delta where | ||
mempty = Delta { added = 0, deleted = 0 } | ||
|
||
instance Ord Delta where | ||
x `compare` y = (x.added + x.deleted) `compare` (y.added + y.deleted) | ||
|
||
type NumStat = (FileName, Delta, Maybe FileName) | ||
|
||
data Changes = Changes | ||
{ delta :: Delta | ||
, commits :: Int | ||
} deriving (Show, Eq) | ||
|
||
instance Semigroup Changes where | ||
x <> y = Changes { delta = x.delta <> y.delta, commits = x.commits + y.commits } | ||
|
||
instance Monoid Changes where | ||
mempty = Changes { delta = Delta { added = 0, deleted = 0 }, commits = 0 } | ||
|
||
type Result = Map FileName Changes |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,70 @@ | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Git.FreqSpec ( spec ) where | ||
|
||
import Data.ByteString (ByteString) | ||
import qualified Data.ByteString.Char8 as BS | ||
import qualified Data.Map.Strict as Map | ||
import System.IO.Streams (InputStream) | ||
import qualified System.IO.Streams as Streams | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE QuasiQuotes #-} | ||
|
||
module Git.FreqSpec (spec) where | ||
|
||
import Control.Exception (catch) | ||
import Data.ByteString (ByteString) | ||
import qualified Data.ByteString.Char8 as BS | ||
import qualified Data.Map.Strict as Map | ||
import Data.String.Interpolate (i) | ||
import System.Exit (ExitCode (ExitSuccess)) | ||
import System.IO (stdout) | ||
import System.IO.Silently (capture, hSilence) | ||
import System.IO.Streams (InputStream) | ||
import qualified System.IO.Streams as Streams | ||
import System.Process (system) | ||
|
||
import Test.Hspec | ||
|
||
import Git.Freq | ||
import Git.Freq (freq, freq') | ||
import Types | ||
import Helper (commitFile, inTempRepo) | ||
|
||
createMockStream :: [String] -> IO (InputStream ByteString) | ||
createMockStream = Streams.fromByteString . BS.pack . unlines | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "freq'" $ do | ||
describe "freq" $ do | ||
around_ (hSilence [stdout] . inTempRepo) $ do | ||
|
||
it "should summarize changes" $ do | ||
source <- createMockStream [ "100\t0\tgit-freq.cabal" | ||
, "20\t10\tgit-freq.cabal" | ||
, "0\t120\tgit-freq.cabal" | ||
, "2\t5\tREADME.md" | ||
, "%0" | ||
, "4\t3\t" | ||
, "4\t\t3\tfoo" | ||
, "2\t9\tREADME.md" | ||
] | ||
freq' source `shouldReturn` Map.fromList [ ("git-freq.cabal", (120, 130)) | ||
, ("README.md", (4, 14)) | ||
] | ||
commitFile "foo.hs" [i| | ||
putStrLn "Foo" | ||
|] | ||
|
||
commitFile "foo.hs" [i| | ||
putStrLn "Foo" | ||
putStrLn "Bar" | ||
|] | ||
|
||
commitFile "foo.hs" [i| | ||
putStrLn "Bar" | ||
|] | ||
|
||
system "git mv foo.hs bar.hs" | ||
system "git commit -a -m 'moved'" | ||
system "git log --numstat" | ||
|
||
let run paths = fst <$> capture (freq paths `catch` (\ExitSuccess -> return ())) | ||
|
||
run ["."] `shouldReturn` unlines ["bar.hs,2,1,4"] | ||
|
||
describe "freq'" $ do | ||
it "should summarize changes" $ do | ||
source <- | ||
createMockStream | ||
[ "100\t0\tgit-freq.cabal", | ||
"20\t10\tgit-freq.cabal", | ||
"0\t120\tgit-freq.cabal", | ||
"2\t5\tREADME.md", | ||
--"4\t\t3\tfoo", | ||
"2\t9\tREADME.md" | ||
] | ||
freq' source | ||
`shouldReturn` Map.fromList | ||
[ ("git-freq.cabal", Changes { delta = Delta {added =120, deleted =130}, commits = 3 }), | ||
("README.md", Changes { delta = Delta {added = 4, deleted =14 }, commits = 2 }) | ||
] |
Oops, something went wrong.