Skip to content

Commit

Permalink
[#200] Move the GitCommand, GitImpl and GitTestFake to the subpackage…
Browse files Browse the repository at this point in the history
… "git"

- Make variables under FileDiff public
- Make GitLogItem.Len public
  • Loading branch information
Ahmad ATWI authored and mengdaming committed Jan 3, 2023
1 parent 86be1cd commit 5103177
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 73 deletions.
3 changes: 2 additions & 1 deletion src/checker/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/murex/tcr/status"
"github.com/murex/tcr/toolchain"
"github.com/murex/tcr/vcs"
"github.com/murex/tcr/vcs/git"
"os"
)

Expand Down Expand Up @@ -130,7 +131,7 @@ func initCheckEnv(p params.Params) {
checkEnv.workDir = toolchain.GetWorkDir()

if checkEnv.sourceTreeErr == nil {
checkEnv.git, checkEnv.gitErr = vcs.New(checkEnv.sourceTree.GetBaseDir())
checkEnv.git, checkEnv.gitErr = git.New(checkEnv.sourceTree.GetBaseDir())
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/checker/check_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package checker
import (
"github.com/murex/tcr/params"
"github.com/murex/tcr/vcs"
"github.com/murex/tcr/vcs/git"
)

func checkGitEnvironment(_ params.Params) (cr *CheckResults) {
Expand All @@ -37,18 +38,18 @@ func checkGitEnvironment(_ params.Params) (cr *CheckResults) {
}

func checkGitCommand() (cp []CheckPoint) {
if !vcs.IsGitCommandAvailable() {
if !git.IsGitCommandAvailable() {
cp = append(cp, errorCheckPoint("git command was not found on path"))
return cp
}
cp = append(cp, okCheckPoint("git command path is ", vcs.GetGitCommandPath()))
cp = append(cp, okCheckPoint("git version is ", vcs.GetGitCommandVersion()))
cp = append(cp, okCheckPoint("git command path is ", git.GetGitCommandPath()))
cp = append(cp, okCheckPoint("git version is ", git.GetGitCommandVersion()))
// We could add here a check on git minimum version. No specific need for now.
return cp
}

func checkGitConfig() (cp []CheckPoint) {
cp = append(cp, okCheckPoint("git username is ", vcs.GetGitUserName()))
cp = append(cp, okCheckPoint("git username is ", git.GetGitUserName()))
return cp
}

Expand Down
4 changes: 2 additions & 2 deletions src/checker/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package checker
import (
"github.com/murex/tcr/params"
"github.com/murex/tcr/status"
"github.com/murex/tcr/vcs"
"github.com/murex/tcr/vcs/git"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
Expand All @@ -46,7 +46,7 @@ var (
func initTestCheckEnv(params params.Params) {
initCheckEnv(params)
// We replace git implementation with a fake so that we bypass real git access
checkEnv.git, checkEnv.gitErr = vcs.NewGitFake(vcs.GitFakeSettings{})
checkEnv.git, checkEnv.gitErr = git.NewGitFake(git.GitFakeSettings{})
}

func assertStatus(t *testing.T, expected CheckStatus, checker func(params params.Params) (cr *CheckResults), params params.Params) {
Expand Down
3 changes: 2 additions & 1 deletion src/engine/tcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/murex/tcr/toolchain"
"github.com/murex/tcr/ui"
"github.com/murex/tcr/vcs"
"github.com/murex/tcr/vcs/git"
"gopkg.in/tomb.v2"
"os"
"strings"
Expand Down Expand Up @@ -245,7 +246,7 @@ func parseCommitMessage(message string) (event events.TcrEvent) {
func (tcr *TcrEngine) initVcs() {
if tcr.vcs == nil {
var err error
tcr.vcs, err = vcs.New(tcr.sourceTree.GetBaseDir())
tcr.vcs, err = git.New(tcr.sourceTree.GetBaseDir())
tcr.handleError(err, true, status.GitError)
}
}
Expand Down
47 changes: 24 additions & 23 deletions src/engine/tcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/murex/tcr/toolchain"
"github.com/murex/tcr/ui"
"github.com/murex/tcr/vcs"
"github.com/murex/tcr/vcs/git"
"github.com/stretchr/testify/assert"
"os"
"strings"
Expand Down Expand Up @@ -180,15 +181,15 @@ func Test_tcr_operation_end_state(t *testing.T) {
{
"commit with git commit failure",
func() {
tcr, _ := initTcrEngineWithFakes(nil, nil, vcs.GitCommands{vcs.CommitCommand}, nil)
tcr, _ := initTcrEngineWithFakes(nil, nil, git.GitCommands{git.CommitCommand}, nil)
tcr.commit(events.TcrEvent{})
},
status.GitError,
},
{
"commit with git push failure",
func() {
tcr, _ := initTcrEngineWithFakes(nil, nil, vcs.GitCommands{vcs.PushCommand}, nil)
tcr, _ := initTcrEngineWithFakes(nil, nil, git.GitCommands{git.PushCommand}, nil)
tcr.commit(events.TcrEvent{})
},
status.GitError,
Expand All @@ -204,15 +205,15 @@ func Test_tcr_operation_end_state(t *testing.T) {
{
"revert with git diff failure",
func() {
tcr, _ := initTcrEngineWithFakes(nil, nil, vcs.GitCommands{vcs.DiffCommand}, nil)
tcr, _ := initTcrEngineWithFakes(nil, nil, git.GitCommands{git.DiffCommand}, nil)
tcr.revert(events.TcrEvent{})
},
status.GitError,
},
{
"revert with git restore failure",
func() {
tcr, _ := initTcrEngineWithFakes(nil, nil, vcs.GitCommands{vcs.RestoreCommand}, nil)
tcr, _ := initTcrEngineWithFakes(nil, nil, git.GitCommands{git.RestoreCommand}, nil)
tcr.revert(events.TcrEvent{})
},
status.GitError,
Expand All @@ -231,7 +232,7 @@ func Test_tcr_operation_end_state(t *testing.T) {
func Test_tcr_revert_end_state_with_commit_on_fail_enabled(t *testing.T) {
testFlags := []struct {
desc string
gitFailures vcs.GitCommands
gitFailures git.GitCommands
expectedStatus status.Status
}{
{
Expand All @@ -241,27 +242,27 @@ func Test_tcr_revert_end_state_with_commit_on_fail_enabled(t *testing.T) {
},
{
"git stash failure",
vcs.GitCommands{vcs.StashCommand},
git.GitCommands{git.StashCommand},
status.GitError,
},
{
"git un-stash failure",
vcs.GitCommands{vcs.UnStashCommand},
git.GitCommands{git.UnStashCommand},
status.GitError,
},
{
"git add failure",
vcs.GitCommands{vcs.AddCommand},
git.GitCommands{git.AddCommand},
status.GitError,
},
{
"git commit failure",
vcs.GitCommands{vcs.CommitCommand},
git.GitCommands{git.CommitCommand},
status.GitError,
},
{
"git revert failure",
vcs.GitCommands{vcs.RevertCommand},
git.GitCommands{git.RevertCommand},
status.GitError,
},
}
Expand All @@ -281,7 +282,7 @@ func Test_tcr_cycle_end_state(t *testing.T) {
testFlags := []struct {
desc string
toolchainFailures toolchain.Operations
gitFailures vcs.GitCommands
gitFailures git.GitCommands
expectedStatus status.Status
}{
{
Expand All @@ -301,27 +302,27 @@ func Test_tcr_cycle_end_state(t *testing.T) {
},
{
"with git add failure",
nil, vcs.GitCommands{vcs.AddCommand},
nil, git.GitCommands{git.AddCommand},
status.GitError,
},
{
"with git commit failure",
nil, vcs.GitCommands{vcs.CommitCommand},
nil, git.GitCommands{git.CommitCommand},
status.GitError,
},
{
"with git push failure",
nil, vcs.GitCommands{vcs.PushCommand},
nil, git.GitCommands{git.PushCommand},
status.GitError,
},
{
"with test and git diff failure",
toolchain.Operations{toolchain.TestOperation}, vcs.GitCommands{vcs.DiffCommand},
toolchain.Operations{toolchain.TestOperation}, git.GitCommands{git.DiffCommand},
status.GitError,
},
{
"with test and git restore failure",
toolchain.Operations{toolchain.TestOperation}, vcs.GitCommands{vcs.RestoreCommand},
toolchain.Operations{toolchain.TestOperation}, git.GitCommands{git.RestoreCommand},
status.GitError,
},
}
Expand All @@ -336,7 +337,7 @@ func Test_tcr_cycle_end_state(t *testing.T) {
}
}

func initTcrEngineWithFakes(p *params.Params, toolchainFailures toolchain.Operations, gitFailures vcs.GitCommands, gitLogItems vcs.GitLogItems) (TcrInterface, *vcs.GitFake) {
func initTcrEngineWithFakes(p *params.Params, toolchainFailures toolchain.Operations, gitFailures git.GitCommands, gitLogItems vcs.GitLogItems) (TcrInterface, *git.GitFake) {
tchn := registerFakeToolchain(toolchainFailures)
lang := registerFakeLanguage(tchn)

Expand Down Expand Up @@ -384,13 +385,13 @@ func registerFakeLanguage(toolchainName string) string {
return fake.GetName()
}

func replaceGitImplWithFake(tcr TcrInterface, failures vcs.GitCommands, gitLogItems vcs.GitLogItems) *vcs.GitFake {
fakeSettings := vcs.GitFakeSettings{
func replaceGitImplWithFake(tcr TcrInterface, failures git.GitCommands, gitLogItems vcs.GitLogItems) *git.GitFake {
fakeSettings := git.GitFakeSettings{
FailingCommands: failures,
ChangedFiles: vcs.FileDiffs{vcs.NewFileDiff("fake-src", 1, 1)},
Logs: gitLogItems,
}
fake, _ := vcs.NewGitFake(fakeSettings)
fake, _ := git.NewGitFake(fakeSettings)
tcr.setVcs(fake)
return fake
}
Expand Down Expand Up @@ -453,7 +454,7 @@ func Test_set_commit_on_fail(t *testing.T) {
func Test_git_pull_calls_git_command(t *testing.T) {
tcr, gitFake := initTcrEngineWithFakes(nil, nil, nil, nil)
tcr.GitPull()
assert.Equal(t, vcs.PullCommand, gitFake.GetLastCommand())
assert.Equal(t, git.PullCommand, gitFake.GetLastCommand())
}

func Test_git_pull_highlights_errors(t *testing.T) {
Expand All @@ -462,7 +463,7 @@ func Test_git_pull_highlights_errors(t *testing.T) {
return msg.Type.Severity == report.Error && msg.Text == "git pull command failed!"
},
)
tcr, _ := initTcrEngineWithFakes(nil, nil, vcs.GitCommands{vcs.PullCommand}, nil)
tcr, _ := initTcrEngineWithFakes(nil, nil, git.GitCommands{git.PullCommand}, nil)
tcr.GitPull()
time.Sleep(1 * time.Millisecond)
sniffer.Stop()
Expand All @@ -475,7 +476,7 @@ func Test_git_push_highlights_errors(t *testing.T) {
return msg.Type.Severity == report.Error && msg.Text == "git push command failed!"
},
)
tcr, _ := initTcrEngineWithFakes(nil, nil, vcs.GitCommands{vcs.PushCommand}, nil)
tcr, _ := initTcrEngineWithFakes(nil, nil, git.GitCommands{git.PushCommand}, nil)
tcr.GitPush()
time.Sleep(1 * time.Millisecond)
sniffer.Stop()
Expand Down
7 changes: 4 additions & 3 deletions src/vcs/git_command.go → src/vcs/git/git_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,25 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package vcs
package git

import (
"bufio"
"bytes"
"github.com/codeskyblue/go-sh"
"github.com/murex/tcr/report"
"github.com/murex/tcr/vcs"
"strings"
)

// IsGitCommandAvailable indicates if git command is available on local machine
func IsGitCommandAvailable() bool {
return IsCommandAvailable("git")
return vcs.IsCommandAvailable("git")
}

// GetGitCommandPath returns the path to git command on this machine
func GetGitCommandPath() string {
return GetCommandPath("git")
return vcs.GetCommandPath("git")
}

// GetGitCommandVersion returns the version of git command on this machine
Expand Down
20 changes: 10 additions & 10 deletions src/vcs/git_impl.go → src/vcs/git/git_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package vcs
package git

import (
"bufio"
Expand All @@ -34,6 +34,7 @@ import (
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/murex/tcr/report"
"github.com/murex/tcr/vcs"
"path/filepath"
"strconv"
"strings"
Expand All @@ -55,15 +56,14 @@ type gitImpl struct {
}

// New initializes the git implementation based on the provided directory from local clone
func New(dir string) (GitInterface, error) {
func New(dir string) (vcs.GitInterface, error) {
return newGitImpl(plainOpen, dir)
}

// newGitImpl initializes a gitImpl instance
func newGitImpl(initRepo func(string) (*git.Repository, billy.Filesystem, error), dir string) (*gitImpl, error) {
var g = gitImpl{
baseDir: dir,
pushEnabled: DefaultPushEnabled,
pushEnabled: vcs.DefaultPushEnabled,
runGitFunction: runGitCommand,
traceGitFunction: traceGitCommand,
}
Expand All @@ -81,9 +81,9 @@ func newGitImpl(initRepo func(string) (*git.Repository, billy.Filesystem, error)
return nil, err
}

if isRemoteDefined(DefaultRemoteName, g.repository) {
if isRemoteDefined(vcs.DefaultRemoteName, g.repository) {
g.remoteEnabled = true
g.remoteName = DefaultRemoteName
g.remoteName = vcs.DefaultRemoteName
g.workingBranchExistsOnRemote, err = g.isWorkingBranchOnRemote()
}

Expand Down Expand Up @@ -286,7 +286,7 @@ func (g *gitImpl) UnStash(keep bool) error {

// Diff returns the list of files modified since last commit with diff info for each file
// Current implementation uses a direct call to git
func (g *gitImpl) Diff() (diffs FileDiffs, err error) {
func (g *gitImpl) Diff() (diffs vcs.FileDiffs, err error) {
var gitOutput []byte
gitOutput, err = g.runGit("diff", "--numstat", "--ignore-cr-at-eol",
"--ignore-all-space", "--ignore-blank-lines", "HEAD")
Expand All @@ -301,7 +301,7 @@ func (g *gitImpl) Diff() (diffs FileDiffs, err error) {
added, _ := strconv.Atoi(fields[0])
removed, _ := strconv.Atoi(fields[1])
filename := filepath.Join(g.rootDir, fields[2])
diffs = append(diffs, NewFileDiff(filename, added, removed))
diffs = append(diffs, vcs.NewFileDiff(filename, added, removed))
}
}
return diffs, nil
Expand All @@ -310,7 +310,7 @@ func (g *gitImpl) Diff() (diffs FileDiffs, err error) {
// Log returns the list of git log items compliant with the provided msgFilter.
// When no msgFilter is provided, returns all git log items unfiltered.
// Current implementation uses go-git's Log() function
func (g *gitImpl) Log(msgFilter func(msg string) bool) (logs GitLogItems, err error) {
func (g *gitImpl) Log(msgFilter func(msg string) bool) (logs vcs.GitLogItems, err error) {
plainOpenOptions := git.PlainOpenOptions{
DetectDotGit: true,
EnableDotGitCommonDir: false,
Expand All @@ -333,7 +333,7 @@ func (g *gitImpl) Log(msgFilter func(msg string) bool) (logs GitLogItems, err er
}
_ = cIter.ForEach(func(c *object.Commit) error {
if msgFilter == nil || msgFilter(c.Message) {
logs.Add(NewGitLogItem(c.Hash.String(), c.Committer.When.UTC(), c.Message))
logs.Add(vcs.NewGitLogItem(c.Hash.String(), c.Committer.When.UTC(), c.Message))
}
return nil
})
Expand Down
Loading

0 comments on commit 5103177

Please sign in to comment.