<commit_before>{-# LANGUAGE OverloadedStrings, DeriveGeneric, TemplateHaskell #-}

module Git where

import qualified System.Process as Process
import Data.Time.Calendar
import Data.Time.LocalTime
import Text.ParserCombinators.Parsec
import qualified Text.Parsec.Text as T
import qualified Text.Parsec as T
import qualified Data.Text as T
import qualified Data.Text.IO as IO
import Data.List (isInfixOf, intercalate)
import Data.Aeson.TH (deriveJSON, defaultOptions)
import Data.Maybe

import Event as Event
import qualified Util
import EventProvider

data GitRecord = GitRecord
	{
		gitUser :: T.Text,
		gitRepo :: T.Text
	} deriving Show
deriveJSON defaultOptions ''GitRecord

getGitProvider :: EventProvider GitRecord
getGitProvider = EventProvider
	{
		getModuleName = "Git",
		getEvents = getRepoCommits,
		getConfigType = members $(thGetTypeDesc ''GitRecord)
	}

getRepoCommits :: GitRecord -> GlobalSettings -> Day -> IO [Event.Event]
getRepoCommits (GitRecord _username _projectPath) _ date = do
	let username = T.unpack _username
	let projectPath = T.unpack _projectPath
	(inh, Just outh, errh, pid) <- Process.createProcess
		(Process.proc "git" [
			"log", "--since", formatDate $ addDays (-1) date,
			"--until", formatDate $ addDays 1 date,
	--		"--author=\"" ++ username ++ "\"",
			"--stat", "--all"])
		{
			Process.std_out = Process.CreatePipe,
			Process.cwd = Just projectPath
		}
	output <- IO.hGetContents outh
	timezone <- getCurrentTimeZone
	let parseResult = parseCommitsParsec $ T.concat [output, "\n"]
	case parseResult of
		Left pe -> do
			print $ T.unpack output
			putStrLn $ "GIT: parse error: " ++ Util.displayErrors pe
			error "GIT parse error, aborting"
			--return []
		Right x -> do
			let myCommits = filter ((isInfixOf username) . commitAuthor) x
			let myCommitsInInterval = filter (inRange . localDay . commitDate) myCommits
			let myNonMergeCommitsInInterval = filter (not . commitIsMerge) myCommitsInInterval
			return $ map (toEvent _projectPath timezone) myNonMergeCommitsInInterval<commit_msg>didn't really test, but should work and definitely better than before<commit_after>{-# LANGUAGE OverloadedStrings, DeriveGeneric, TemplateHaskell #-}

module Git where

import qualified System.Process as Process
import Data.Time.Calendar
import Data.Time.LocalTime
import Text.ParserCombinators.Parsec
import qualified Text.Parsec.Text as T
import qualified Text.Parsec as T
import qualified Data.Text as T
import qualified Data.Text.IO as IO
import Data.List (isInfixOf, intercalate)
import Data.Aeson.TH (deriveJSON, defaultOptions)
import Data.Maybe

import Event as Event
import qualified Util
import EventProvider

data GitRecord = GitRecord
	{
		gitUser :: T.Text,
		gitRepo :: T.Text
	} deriving Show
deriveJSON defaultOptions ''GitRecord

getGitProvider :: EventProvider GitRecord
getGitProvider = EventProvider
	{
		getModuleName = "Git",
		getEvents = getRepoCommits,
		getConfigType = members $(thGetTypeDesc ''GitRecord)
	}

getRepoCommits :: GitRecord -> GlobalSettings -> Day -> IO [Event.Event]
getRepoCommits (GitRecord _username _projectPath) _ date = do
	let username = T.unpack _username
	let projectPath = T.unpack _projectPath
	(inh, Just outh, errh, pid) <- Process.createProcess
		(Process.proc "git" [
			"log", "--since", formatDate $ addDays (-1) date,
			"--until", formatDate $ addDays 1 date,
	--		"--author=\"" ++ username ++ "\"",
			"--stat", "--all"])
		{
			Process.std_out = Process.CreatePipe,
			Process.cwd = Just projectPath
		}
	output <- IO.hGetContents outh
	timezone <- getCurrentTimeZone
	let parseResult = parseCommitsParsec $ T.concat [output, "\n"]
	case parseResult of
		Left pe -> do
			print $ T.unpack output
			putStrLn $ "GIT: parse error: " ++ Util.displayErrors pe
			error "GIT parse error, aborting"
			--return []
		Right allCommits -> do
			let relevantCommits = filter (isCommitRelevant date username) allCommits
			return $ map (toEvent _projectPath timezone) relevantCommits

isCommitRelevant :: Day -> String -> Commit -> Bool
isCommitRelevant date username commit = and $ map ($ commit) [
			(isInfixOf username) . commitAuthor,
			inRange . localDay . commitDate,
			not . commitIsMerge]
{'fraction_non_alphanumeric': 0.10045335242185637, 'fraction_numerical': 0.000954426151276545, 'mean_word_length': 4.246558197747184, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5598321', 'n_tokens_mistral': 1391, 'n_tokens_neox': 1289, 'n_words': 400}