Skip to content

Commit

Permalink
fixes-cli (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
subhashish-devtron authored Jan 24, 2024
1 parent bd235ac commit 25c3eb6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 22 deletions.
16 changes: 5 additions & 11 deletions pkg/git/GitCliManager.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package git

import (
"encoding/json"
"fmt"
"go.uber.org/zap"
"gopkg.in/src-d/go-billy.v4/osfs"
"os"
Expand Down Expand Up @@ -149,20 +147,16 @@ func (impl *GitCliManagerImpl) GitShow(rootDir string, hash string) (GitCommit,
}

func (impl *GitCliManagerImpl) processGitLogOutput(out string, rootDir string) ([]GitCommit, error) {

gitCommits := make([]GitCommit, 0)
if len(out) == 0 {
return make([]GitCommit, 0), nil
return gitCommits, nil
}
logOut := out
logOut = logOut[:len(logOut)-1] // Remove the last ","
logOut = fmt.Sprintf("[%s]", logOut) // Add []

var gitCommitFormattedList []GitCommitFormat
err := json.Unmarshal([]byte(logOut), &gitCommitFormattedList)
gitCommitFormattedList, err := parseFormattedLogOutput(out)
if err != nil {
return nil, err
return gitCommits, err
}

gitCommits := make([]GitCommit, 0)
for _, formattedCommit := range gitCommitFormattedList {

cm := GitCommitBase{
Expand Down
86 changes: 75 additions & 11 deletions pkg/git/GitFormatter.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
package git

import "time"

var GITFORMAT = `--pretty=format:{
"commit": "%H",
"parent": "%P",
"refs": "%D",
"subject": "%s",
"body": "%b",
"author": { "name": "%aN", "email": "%aE", "date": "%ad" },
"commiter": { "name": "%cN", "email": "%cE", "date": "%cd" }
},`
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
)

const _dl_ = "devtron_delimiter"

// GITFORMAT Refer git official doc for supported placeholders to add new fields
// Need to make sure the output does not break the json structure which is ensured
// by having the _dl_ delimiter which is later replaced by quotes
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_ + "author" + _dl_ +
":{" +
_dl_ + "name" + _dl_ + ":" + _dl_ + "%aN" + _dl_ + "," +
_dl_ + "email" + _dl_ + ":" + _dl_ + "%aE" + _dl_ + "," +
_dl_ + "date" + _dl_ + ":" + _dl_ + "%ad" + _dl_ +
"}," +
_dl_ + "commiter" + _dl_ +
":{" +
_dl_ + "name" + _dl_ + ":" + _dl_ + "%cN" + _dl_ + "," +
_dl_ + "email" + _dl_ + ":" + _dl_ + "%cE" + _dl_ + "," +
_dl_ + "date" + _dl_ + ":" + _dl_ + "%cd" + _dl_ +
"}},"

type GitPerson struct {
Name string `json:"name"`
Expand All @@ -23,5 +43,49 @@ type GitCommitFormat struct {
Refs string `json:"refs"`
Subject string `json:"subject"`
Commiter GitPerson `json:"commiter"`
Author GitPerson `json:"author"`
Body string `json:"body"`
}

func parseFormattedLogOutput(out string) ([]GitCommitFormat, error) {
//remove the new line character which is after each terminal comma
out = strings.ReplaceAll(out, "},\n", "},")

// to escape the special characters like quotes and newline characters in the commit data
logOut := strconv.Quote(out)

//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 []

var gitCommitFormattedList []GitCommitFormat
err := json.Unmarshal([]byte(logOut), &gitCommitFormattedList)
if err != nil {
return nil, err
}
return gitCommitFormattedList, nil
}

func (formattedCommit GitCommitFormat) transformToCommit() *Commit {
return &Commit{
Hash: &Hash{
Long: formattedCommit.Commit,
},
Author: &Author{
Name: formattedCommit.Author.Name,
Email: formattedCommit.Author.Email,
Date: formattedCommit.Author.Date,
},
Committer: &Committer{
Name: formattedCommit.Commiter.Name,
Email: formattedCommit.Commiter.Email,
Date: formattedCommit.Commiter.Date,
},
Tag: &Tag{},
Tree: &Tree{},
Subject: formattedCommit.Subject,
Body: formattedCommit.Body,
}
}

0 comments on commit 25c3eb6

Please sign in to comment.