Skip to content

Commit

Permalink
[#37] Add git remote name as an argument to git VCS initializer
Browse files Browse the repository at this point in the history
- add checker warning when git remote name is not found
- rollback adding ot SetRemoteName() to VCS interface
- remove constant DefaultRemoteName from git implementation
- update git.SessionSummary() so that it also provides the remote name when available
  • Loading branch information
mengdaming committed Oct 2, 2024
1 parent c99c2ba commit 1411eb3
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 87 deletions.
3 changes: 3 additions & 0 deletions src/checker/check_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ func checkGitRemote(_ params.Params) (cp []model.CheckPoint) {
}

if !checkEnv.vcs.IsRemoteEnabled() {
if checkEnv.vcs.GetRemoteName() != "" {
cp = append(cp, model.WarningCheckPoint("git remote not found: ", checkEnv.vcs.GetRemoteName()))
}
cp = append(cp, model.OkCheckPoint("git remote is disabled: all operations will be done locally"))
return cp
}
Expand Down
3 changes: 2 additions & 1 deletion src/checker/check_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ func Test_check_git_remote(t *testing.T) {
"VCS not initialized", nil, []model.CheckPoint{},
},
{
"git remote disabled",
"git remote disabled due to undefined remote name",
fake.NewVCSFake(fake.Settings{RemoteEnabled: false}),
[]model.CheckPoint{
model.WarningCheckPoint("git remote not found: vcs-fake-remote-name"),
model.OkCheckPoint("git remote is disabled: all operations will be done locally"),
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/checker/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ func initCheckEnv(p params.Params) {
checkEnv.workDir = toolchain.GetWorkDir()

if checkEnv.sourceTreeErr == nil {
checkEnv.vcs, checkEnv.vcsErr = factory.InitVCS(p.VCS, checkEnv.sourceTree.GetBaseDir())
checkEnv.vcs, checkEnv.vcsErr = factory.InitVCS(p.VCS, checkEnv.sourceTree.GetBaseDir(), p.GitRemote)
}
}
2 changes: 1 addition & 1 deletion src/checker/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (

func initTestCheckEnv(params params.Params) {
// Replace VCS factory initializer in order to use a VCS fake instead of the real thing
factory.InitVCS = func(_ string, _ string) (vcs.Interface, error) {
factory.InitVCS = func(_ string, _ string, _ string) (vcs.Interface, error) {
return fake.NewVCSFake(fake.Settings{}), nil
}
initCheckEnv(params)
Expand Down
9 changes: 4 additions & 5 deletions src/engine/tcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,9 @@ func (tcr *TCREngine) Init(p params.Params) {
tcr.handleError(err, true, status.ConfigError)
report.PostInfo("Work directory is ", toolchain.GetWorkDir())

tcr.initVCS(p.VCS, p.Trace)
tcr.initVCS(p.VCS, p.GitRemote, p.Trace)
tcr.setMessageSuffix(p.MessageSuffix)
tcr.vcs.EnableAutoPush(p.AutoPush)
tcr.vcs.SetRemoteName(p.GitRemote)

tcr.SetVariant(p.Variant)
tcr.setMobTimerDuration(p.MobTurnDuration)
Expand Down Expand Up @@ -241,7 +240,7 @@ func tcrLogsToEvents(tcrLogs vcs.LogItems) (tcrEvents events.TcrEvents) {

func (tcr *TCREngine) queryVCSLogs(p params.Params) vcs.LogItems {
tcr.initSourceTree(p)
tcr.initVCS(p.VCS, p.Trace)
tcr.initVCS(p.VCS, "", p.Trace)

logs, err := tcr.vcs.Log(isTCRCommitMessage)
if err != nil {
Expand Down Expand Up @@ -315,14 +314,14 @@ func (tcr *TCREngine) wrapCommitMessages(statusMessage string, event *events.TCR
return messages
}

func (tcr *TCREngine) initVCS(vcsName string, trace string) {
func (tcr *TCREngine) initVCS(vcsName string, remoteName string, trace string) {
if tcr.vcs != nil {
return // VCS should be initialized only once
}
// Set VCS trace flag
vcs.SetTrace(trace == "vcs")
var err error
tcr.vcs, err = factory.InitVCS(vcsName, tcr.sourceTree.GetBaseDir())
tcr.vcs, err = factory.InitVCS(vcsName, tcr.sourceTree.GetBaseDir(), remoteName)
var unsupportedVCSError *factory.UnsupportedVCSError
switch {
case errors.As(err, &unsupportedVCSError):
Expand Down
3 changes: 2 additions & 1 deletion src/engine/tcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,15 @@ func initTCREngineWithFakesWithFileDiffs(
params.WithPollingPeriod(p.PollingPeriod),
params.WithRunMode(p.Mode),
params.WithVCS(p.VCS),
params.WithGitRemote(p.GitRemote),
params.WithMessageSuffix(p.MessageSuffix),
)
}

tcr := NewTCREngine()
// Replace VCS factory initializer in order to use a VCS fake instead of the real thing
var vcsFake *fake.VCSFake
factory.InitVCS = func(_ string, _ string) (vcs.Interface, error) {
factory.InitVCS = func(_ string, _ string, _ string) (vcs.Interface, error) {
fakeSettings := fake.Settings{
FailingCommands: vcsFailures,
ChangedFiles: fileDiffs,
Expand Down
6 changes: 3 additions & 3 deletions src/vcs/factory/vcs_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

// InitVCS returns the VCS instance of type defined by name, working on the provided directory
var InitVCS func(name string, dir string) (vcs.Interface, error)
var InitVCS func(name string, dir string, remoteName string) (vcs.Interface, error)

func init() {
// InitVCS is set by default to real VCS implementation factory.
Expand All @@ -51,10 +51,10 @@ func (e *UnsupportedVCSError) Error() string {
}

// initVCS returns the VCS instance of type defined by name, working on the provided directory
func initVCS(name string, dir string) (vcs.Interface, error) {
func initVCS(name string, dir string, remoteName string) (vcs.Interface, error) {
switch strings.ToLower(name) {
case git.Name:
return git.New(dir)
return git.New(dir, remoteName)
case p4.Name:
return p4.New(dir)
default:
Expand Down
4 changes: 2 additions & 2 deletions src/vcs/factory/vcs_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ import (
func Test_supported_vcs(t *testing.T) {
for _, name := range []string{"git", "p4"} {
t.Run(name, func(t *testing.T) {
_, err := initVCS(name, "")
_, err := initVCS(name, "", "")
assert.NotEqual(t, reflect.TypeOf(&UnsupportedVCSError{}), reflect.TypeOf(err))
})
}
}

func Test_vcs_factory_returns_an_error_when_vcs_is_not_supported(t *testing.T) {
v, err := initVCS("unknown-vcs", "")
v, err := initVCS("unknown-vcs", "", "")
assert.IsType(t, &UnsupportedVCSError{}, err)
assert.Zero(t, v)
}
Expand Down
5 changes: 0 additions & 5 deletions src/vcs/fake/vcs_test_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,6 @@ func (vf *VCSFake) GetRemoteName() string {
return vf.remoteName
}

// SetRemoteName sets the current VCS remote name
func (vf *VCSFake) SetRemoteName(name string) {
vf.remoteName = name
}

// GetWorkingBranch returns the current VCS working branch
func (vf *VCSFake) GetWorkingBranch() string {
return "vcs-fake-working-branch"
Expand Down
35 changes: 20 additions & 15 deletions src/vcs/git/git_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ import (
// Name provides the name for this VCS implementation
const Name = "git"

// DefaultRemoteName is the alias used by default for the git remote repository
const DefaultRemoteName = "origin"

// gitImpl provides the implementation of the git interface
type gitImpl struct {
baseDir string
Expand All @@ -62,13 +59,17 @@ type gitImpl struct {
}

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

func newGitImpl(initRepo func(string) (*git.Repository, billy.Filesystem, error), dir string) (*gitImpl, error) {
func newGitImpl(
initRepo func(string) (*git.Repository, billy.Filesystem, error),
dir string,
remoteName string) (*gitImpl, error) {
var g = gitImpl{
baseDir: dir,
remoteName: remoteName,
autoPushEnabled: vcs.DefaultAutoPushEnabled,
runGitFunction: runGitCommand,
traceGitFunction: traceGitCommand,
Expand All @@ -87,10 +88,12 @@ func newGitImpl(initRepo func(string) (*git.Repository, billy.Filesystem, error)
return nil, fmt.Errorf("%s - %s", Name, err.Error())
}

if isRemoteDefined(DefaultRemoteName, g.repository) {
if isRemoteDefined(remoteName, g.repository) {
report.PostInfo("Git remote name is ", g.remoteName)
g.remoteEnabled = true
g.remoteName = DefaultRemoteName
g.workingBranchExistsOnRemote, err = g.isWorkingBranchOnRemote()
} else {
report.PostWarning("Git remote name not found: ", g.remoteName)
}

return &g, err
Expand All @@ -103,7 +106,11 @@ func (*gitImpl) Name() string {

// SessionSummary provides a short description related to current VCS session summary
func (g *gitImpl) SessionSummary() string {
return fmt.Sprintf("%s branch \"%s\"", g.Name(), g.GetWorkingBranch())
var branch = g.GetWorkingBranch()
if g.IsRemoteEnabled() {
branch = fmt.Sprintf("%s/%s", g.GetRemoteName(), g.GetWorkingBranch())
}
return fmt.Sprintf("%s branch \"%s\"", g.Name(), branch)
}

// plainOpen is the regular function used to open a repository
Expand All @@ -126,6 +133,9 @@ func plainOpen(dir string) (*git.Repository, billy.Filesystem, error) {

// isRemoteDefined returns true is the provided remote is defined in the repository
func isRemoteDefined(remoteName string, repo *git.Repository) bool {
if remoteName == "" {
return false
}
_, err := repo.Remote(remoteName)
return err == nil
}
Expand Down Expand Up @@ -172,7 +182,7 @@ func retrieveWorkingBranch(repository *git.Repository) (string, error) {
return head.Name().Short(), nil
}

// Brand new repo: nothing is committed yet
// Brand-new repo: nothing is committed yet
head, err = repository.Reference(plumbing.HEAD, false)
if err != nil {
return "", err
Expand All @@ -190,11 +200,6 @@ func (g *gitImpl) GetRemoteName() string {
return g.remoteName
}

// SetRemoteName sets the current git remote name
func (g *gitImpl) SetRemoteName(name string) {
g.remoteName = name
}

// IsRemoteEnabled indicates if git remote operations are enabled
func (g *gitImpl) IsRemoteEnabled() bool {
return g.remoteEnabled
Expand Down
Loading

0 comments on commit 1411eb3

Please sign in to comment.