Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: optimizations in git flows #83

Open
wants to merge 2 commits into
base: gitclictx
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkg/git/GitCliManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
)

type GitCliManager interface {
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 10 additions & 8 deletions pkg/git/GitFormatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package git

import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
Expand All @@ -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_ + "," +
Expand Down Expand Up @@ -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)
Expand All @@ -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),
}
}
7 changes: 6 additions & 1 deletion pkg/git/Watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down