From c14d4628079b059cd1394598388ab4c5a4cf7650 Mon Sep 17 00:00:00 2001
From: Fabian Kirchner <kirchner@posteo.de>
Date: Sat, 14 Oct 2023 18:51:47 +0200
Subject: also consider issues changed in current working tree

---
 app/History.hs | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)

diff --git a/app/History.hs b/app/History.hs
index c5953b6..b6cef76 100644
--- a/app/History.hs
+++ b/app/History.hs
@@ -39,8 +39,8 @@ listIssues filters paths = do
     hashFirst : hashesRest -> do
       issuesInitial <- cached (append hashFirst (pack ".all")) (\_ -> getIssuesCommitAll hashFirst)
       commitInfos <- mapM (\hash -> cached (append hash (pack ".changed")) (\_ -> getCommitInfo hash)) hashesRest
-      -- TODO We also have to get the issues which changed in the working tree
-      let eventses = getEvents hashFirst issuesInitial commitInfos
+      commitInfoWorkingTree <- getCommitInfoWorkingTree paths
+      let eventses = getEvents hashFirst issuesInitial (commitInfos ++ [commitInfoWorkingTree])
       let issues = mapMaybe issueFromIssueEvents eventses
           issuesFiltered = filter (applyFilter filters) issues
       -- FIXME We have to only consider issues in the specified paths
@@ -52,15 +52,15 @@ getCommitHashes =
 
 data IssueEvent
   = IssueCreated
-      { hash :: Text,
+      { hash :: Maybe Text,
         issue :: Issue
       }
   | IssueChanged
-      { hash :: Text,
+      { hash :: Maybe Text,
         issue :: Issue
       }
   | IssueDeleted
-      { hash :: Text
+      { hash :: Maybe Text
       }
   deriving (Show)
 
@@ -76,7 +76,7 @@ issueFromIssueEvent issueEvent =
 
 data CommitInfo = CommitInfo
   -- TODO Extact CommitInfo so we can change hash' -> hash
-  { hash' :: Text,
+  { hash' :: Maybe Text,
     filesChanged :: [FilePath],
     issues :: [Issue]
   }
@@ -87,18 +87,28 @@ getCommitInfo hash = do
   (issuesCommitChanged, filesChanged) <- getIssuesAndFilesCommitChanged hash
   pure $
     CommitInfo
-      { hash' = hash,
+      { hash' = Just hash,
         filesChanged = filesChanged,
         issues = issuesCommitChanged
       }
 
+getCommitInfoWorkingTree :: [FilePath] -> IO CommitInfo
+getCommitInfoWorkingTree paths = do
+  (issuesWorkingTreeChanged, filesChanged) <- getIssuesAndFilesWorkingTreeChanged paths
+  pure $
+    CommitInfo
+      { hash' = Nothing,
+        filesChanged = filesChanged,
+        issues = issuesWorkingTreeChanged
+      }
+
 getEvents :: Text -> [Issue] -> [CommitInfo] -> [[IssueEvent]]
 getEvents hashInitial issuesInitial commitInfos =
   let issueEventsesInitial =
         map
           ( \issueInitial ->
               [ IssueCreated
-                  { hash = hashInitial,
+                  { hash = Just hashInitial,
                     issue = issueInitial
                   }
               ]
@@ -179,11 +189,12 @@ getIssuesWorkingTreeAll paths = do
 -- | Gets issues in all files which have been changed in your current
 -- [working
 -- - tree](https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefworkingtreeaworkingtree)
-getIssuesWorkingTreeChanged :: [FilePath] -> IO [Issue]
-getIssuesWorkingTreeChanged paths = do
+getIssuesAndFilesWorkingTreeChanged :: [FilePath] -> IO ([Issue], [FilePath])
+getIssuesAndFilesWorkingTreeChanged paths = do
   cwd <- getCurrentDirectory
   files <- gitLsFilesModifiedIn cwd paths
-  concat <$> catch (getIssuesPar cwd files) dieOfInvalidTreeGrepperResult
+  issues <- concat <$> catch (getIssuesPar cwd files) dieOfInvalidTreeGrepperResult
+  pure (issues, files)
 
 -- | Given the hash of a commit, get all issues in all files at the
 -- [tree](https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddeftreeatree)
-- 
cgit v1.2.3