diff --git a/pkg/git/GitCliManager.go b/pkg/git/GitCliManager.go index 92d4ddd6..79a5dfbd 100644 --- a/pkg/git/GitCliManager.go +++ b/pkg/git/GitCliManager.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "strconv" + "strings" ) type GitCliManager interface { @@ -172,11 +173,18 @@ func (impl *GitCliManagerImpl) processGitLogOutput(out string) ([]GitCommit, err for _, formattedCommit := range gitCommitFormattedList { + subject := strings.TrimSpace(formattedCommit.Subject) + body := strings.TrimSpace(formattedCommit.Body) + message := subject + if len(body) > 0 { + message = strings.Join([]string{subject, body}, "\n") + } + cm := GitCommitBase{ Commit: formattedCommit.Commit, Author: formattedCommit.Commiter.Name + " <" + formattedCommit.Commiter.Email + ">", Date: formattedCommit.Commiter.Date, - Message: formattedCommit.Subject + "\n" + formattedCommit.Body, + Message: message, } gitCommits = append(gitCommits, &GitCommitCli{ GitCommitBase: cm, diff --git a/pkg/git/GitFormatter.go b/pkg/git/GitFormatter.go index 76d65905..24ce973b 100644 --- a/pkg/git/GitFormatter.go +++ b/pkg/git/GitFormatter.go @@ -2,7 +2,6 @@ package git import ( "encoding/json" - "fmt" "strconv" "strings" "time" @@ -17,8 +16,8 @@ var GITFORMAT = "--pretty=format:{" + _dl_ + "commit" + _dl_ + ":" + _dl_ + "%H" + _dl_ + "," + _dl_ + "parent" + _dl_ + ":" + _dl_ + "%P" + _dl_ + "," + _dl_ + "refs" + _dl_ + ":" + _dl_ + "%D" + _dl_ + "," + - _dl_ + "subject" + _dl_ + ":" + _dl_ + "%s" + _dl_ + "," + - _dl_ + "body" + _dl_ + ":" + _dl_ + "%b" + _dl_ + "," + + _dl_ + "subject" + _dl_ + ":" + _dl_ + "%<(1024,trunc)%s" + _dl_ + "," + + _dl_ + "body" + _dl_ + ":" + _dl_ + "%<(1024,trunc)%b" + _dl_ + "," + _dl_ + "author" + _dl_ + ":{" + _dl_ + "name" + _dl_ + ":" + _dl_ + "%aN" + _dl_ + "," + @@ -52,13 +51,16 @@ func parseFormattedLogOutput(out string) ([]GitCommitFormat, error) { out = strings.ReplaceAll(out, "},\n", "},") // to escape the special characters like quotes and newline characters in the commit data - logOut := strconv.Quote(out) + var sb strings.Builder + buffer := strconv.AppendQuote(make([]byte, 0, len(out)), out) + sb.Write(buffer) + logOut := sb.String() //replace the delimiter with quotes to make it parsable json logOut = strings.ReplaceAll(logOut, _dl_, `"`) - logOut = logOut[1 : len(logOut)-2] // trim surround characters (surrounding quotes and trailing comma) - logOut = fmt.Sprintf("[%s]", logOut) // Add [] + logOut = logOut[1 : len(logOut)-2] // trim surround characters (surrounding quotes and trailing comma) + logOut = strings.Join([]string{"[", "]"}, logOut) var gitCommitFormattedList []GitCommitFormat err := json.Unmarshal([]byte(logOut), &gitCommitFormattedList) @@ -85,7 +87,7 @@ func (formattedCommit GitCommitFormat) transformToCommit() *Commit { }, Tag: &Tag{}, Tree: &Tree{}, - Subject: formattedCommit.Subject, - Body: formattedCommit.Body, + Subject: strings.TrimSpace(formattedCommit.Subject), + Body: strings.TrimSpace(formattedCommit.Body), } } diff --git a/pkg/git/Watcher.go b/pkg/git/Watcher.go index 86bcb1db..835af6e5 100644 --- a/pkg/git/Watcher.go +++ b/pkg/git/Watcher.go @@ -232,7 +232,12 @@ func (impl GitWatcherImpl) pollGitMaterialAndNotify(material *sql.GitMaterial) e impl.logger.Debugw("Running changesBySinceRepository for material - ", material) impl.logger.Debugw("---------------------------------------------------------- ") // parse env variables here, then search for the count field and pass here. - commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, material.Value, "", "", impl.configuration.GitHistoryCount) + lastSeenHash := "" + if len(material.LastSeenHash) > 0 { + // this might misbehave is the hash stored in table is corrupted somehow + lastSeenHash = material.LastSeenHash + } + commits, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, material.Value, lastSeenHash, "", impl.configuration.GitHistoryCount) if err != nil { material.Errored = true material.ErrorMsg = err.Error()