Skip to content

Commit 383327a

Browse files
committed
add new api for getting total commit logs
1 parent 5c7176d commit 383327a

File tree

5 files changed

+117
-107
lines changed

5 files changed

+117
-107
lines changed

git/commit/commit.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Commit interface {
1010

1111
type Operation struct {
1212
Changes Changes
13+
Total Total
1314
}
1415

1516
func (c Operation) GitCommitChange() (string, error) {
@@ -22,3 +23,7 @@ func (c Operation) GitCommitChange() (string, error) {
2223

2324
return global.CommitChangeSuccess, nil
2425
}
26+
27+
func (c Operation) GitTotalCommits() int {
28+
return c.Total.Get()
29+
}

git/commit/git_total_commits.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package commit
2+
3+
import (
4+
"fmt"
5+
git2go "github.com/libgit2/git2go/v31"
6+
"github.com/neel1996/gitconvex/global"
7+
)
8+
9+
type Total interface {
10+
Get() int
11+
}
12+
13+
type totalCommits struct {
14+
repo *git2go.Repository
15+
}
16+
17+
// Get function returns the total number of commits from the repo and commit message of the most recent commit
18+
func (t totalCommits) Get() int {
19+
var total = 0
20+
repo := t.repo
21+
22+
logItr, itrErr := repo.Walk()
23+
if itrErr != nil {
24+
logger.Log(fmt.Sprintf("Repo has no logs -> %s", itrErr.Error()), global.StatusError)
25+
return total
26+
}
27+
28+
commits, err := t.iterateCommitLogs(logItr)
29+
if err != nil {
30+
logger.Log(fmt.Sprintf("Unable to obtain commits for the repo"), global.StatusError)
31+
return total
32+
33+
}
34+
35+
total = len(commits)
36+
if total == 0 {
37+
logger.Log("Repo has no commit logs", global.StatusError)
38+
return total
39+
}
40+
41+
logger.Log(fmt.Sprintf("Total commits in the repo -> %v", total), global.StatusInfo)
42+
return total
43+
}
44+
45+
func (t totalCommits) iterateCommitLogs(logItr *git2go.RevWalk) ([]git2go.Commit, error) {
46+
var commits []git2go.Commit
47+
_ = logItr.PushHead()
48+
49+
err := logItr.Iterate(func(commit *git2go.Commit) bool {
50+
if commit != nil {
51+
commits = append(commits, *commit)
52+
return true
53+
} else {
54+
return false
55+
}
56+
})
57+
58+
return commits, err
59+
}
60+
61+
func NewTotalCommits(repo *git2go.Repository) Total {
62+
return totalCommits{repo: repo}
63+
}

git/commit/git_total_commits_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package commit
2+
3+
import (
4+
"fmt"
5+
git2go "github.com/libgit2/git2go/v31"
6+
"github.com/stretchr/testify/suite"
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
)
11+
12+
type TotalCommitsTestSuite struct {
13+
suite.Suite
14+
total Total
15+
repo *git2go.Repository
16+
noHeadRepo *git2go.Repository
17+
}
18+
19+
func TestTotalCommitsTestSuite(t *testing.T) {
20+
suite.Run(t, new(TotalCommitsTestSuite))
21+
}
22+
23+
func (suite *TotalCommitsTestSuite) SetupTest() {
24+
r, err := git2go.OpenRepository(os.Getenv("GITCONVEX_TEST_REPO"))
25+
if err != nil {
26+
fmt.Println(err)
27+
}
28+
29+
noHeadPath := os.Getenv("GITCONVEX_TEST_REPO") + string(filepath.Separator) + "no_head"
30+
noHeadRepo, _ := git2go.OpenRepository(noHeadPath)
31+
32+
suite.repo = r
33+
suite.noHeadRepo = noHeadRepo
34+
suite.total = NewTotalCommits(suite.repo)
35+
}
36+
37+
func (suite *TotalCommitsTestSuite) TestGet_WhenLogsAreAvailable_ShouldReturnTotal() {
38+
got := suite.total.Get()
39+
40+
suite.NotZero(got)
41+
}
42+
43+
func (suite *TotalCommitsTestSuite) TestGet_WhenRepoHasNoLogs_ShouldReturnZero() {
44+
suite.total = NewTotalCommits(suite.noHeadRepo)
45+
46+
got := suite.total.Get()
47+
48+
suite.Zero(got)
49+
}

git/git_total_commits.go

Lines changed: 0 additions & 65 deletions
This file was deleted.

tests/git_total_commits_test.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)