Skip to content

Commit 3bc8b97

Browse files
committed
[#200] Add methods Name() and SessionInfo() to vcs interface
- evolving towards a more generic VCS interface
1 parent fdf59a2 commit 3bc8b97

12 files changed

+104
-45
lines changed

src/cli/terminal_ui.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,10 @@ func (term *TerminalUI) ShowSessionInfo() {
274274
term.ReportInfo(false, "Language=", info.LanguageName, ", Toolchain=", info.ToolchainName)
275275

276276
autoPush := "disabled"
277-
if info.AutoPush {
277+
if info.GitAutoPush {
278278
autoPush = "enabled"
279279
}
280-
term.ReportInfo(false,
281-
"Running on branch \"", info.BranchName,
282-
"\" with auto-push ", autoPush)
280+
term.ReportInfo(false, "Running on ", info.VCSSessionSummary, " with auto-push ", autoPush)
283281
}
284282

285283
// Confirm asks the user for confirmation

src/cli/terminal_ui_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ func Test_show_session_info(t *testing.T) {
471471
expected := asCyanTraceWithSeparatorLine("Base Directory: fake") +
472472
asCyanTrace("Work Directory: fake") +
473473
asCyanTrace("Language=fake, Toolchain=fake") +
474-
asCyanTrace("Running on branch \"fake\" with auto-push disabled")
474+
asCyanTrace("Running on VCS session \"fake\" with auto-push disabled")
475475

476476
assert.Equal(t, expected, capturer.CaptureStdout(func() {
477477
term, _, _ := terminalSetup(*params.AParamSet())

src/engine/session_info.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ package engine
2424

2525
// SessionInfo contains TCR session information. Used mainly to exchange information between TCR engine and UI
2626
type SessionInfo struct {
27-
BaseDir string
28-
WorkDir string
29-
LanguageName string
30-
ToolchainName string
31-
AutoPush bool
32-
CommitOnFail bool
33-
BranchName string
27+
BaseDir string
28+
WorkDir string
29+
LanguageName string
30+
ToolchainName string
31+
VCSName string
32+
VCSSessionSummary string
33+
CommitOnFail bool
34+
GitAutoPush bool
3435
}

src/engine/tcr.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (*TCREngine) RunCheck(p params.Params) {
181181
// PrintLog prints the TCR VCS commit history
182182
func (tcr *TCREngine) PrintLog(p params.Params) {
183183
tcrLogs := tcr.queryVCSLogs(p)
184-
report.PostInfo("Printing TCR log for branch ", tcr.vcs.GetWorkingBranch())
184+
report.PostInfo("Printing TCR log for ", tcr.vcs.SessionSummary())
185185
for _, log := range tcrLogs {
186186
report.PostTitle("commit: ", log.Hash)
187187
report.PostInfo("timestamp: ", log.Timestamp)
@@ -194,7 +194,7 @@ func (tcr *TCREngine) PrintLog(p params.Params) {
194194
// PrintStats prints the TCR execution stats
195195
func (tcr *TCREngine) PrintStats(p params.Params) {
196196
tcrLogs := tcr.queryVCSLogs(p)
197-
stats.Print(tcr.vcs.GetWorkingBranch(), tcrLogsToEvents(tcrLogs))
197+
stats.Print(tcr.vcs.SessionSummary(), tcrLogsToEvents(tcrLogs))
198198
}
199199

200200
func tcrLogsToEvents(tcrLogs vcs.LogItems) (tcrEvents events.TcrEvents) {
@@ -214,7 +214,7 @@ func (tcr *TCREngine) queryVCSLogs(p params.Params) vcs.LogItems {
214214
report.PostError(err)
215215
}
216216
if len(logs) == 0 {
217-
report.PostWarning("no TCR commit found in branch ", tcr.vcs.GetWorkingBranch(), "'s history")
217+
report.PostWarning("no TCR commit found in ", tcr.vcs.SessionSummary(), "'s history")
218218
}
219219
return logs
220220
}
@@ -252,9 +252,9 @@ func (tcr *TCREngine) initVCS(vcsName string) {
252252

253253
var err error
254254
switch strings.ToLower(vcsName) {
255-
case "git":
255+
case git.Name:
256256
tcr.vcs, err = git.New(tcr.sourceTree.GetBaseDir())
257-
case "p4", "perforce":
257+
case p4.Name:
258258
tcr.vcs, err = p4.New(tcr.sourceTree.GetBaseDir())
259259
default:
260260
tcr.handleError(fmt.Errorf("VCS not supported: %s", vcsName), true, status.ConfigError)
@@ -277,7 +277,7 @@ func (tcr *TCREngine) setVCS(vcsInterface vcs.Interface) {
277277
func (tcr *TCREngine) warnIfOnRootBranch(interactive bool) {
278278
if tcr.vcs.IsOnRootBranch() {
279279
message := "Running " + settings.ApplicationName +
280-
" on branch \"" + tcr.vcs.GetWorkingBranch() + "\" is not recommended"
280+
" on " + tcr.vcs.SessionSummary() + " is not recommended"
281281
if interactive {
282282
if !tcr.ui.Confirm(message, false) {
283283
tcr.Quit()
@@ -457,7 +457,7 @@ func (tcr *TCREngine) test() (result toolchain.TestCommandResult) {
457457
}
458458

459459
func (tcr *TCREngine) commit(event events.TCREvent) {
460-
report.PostInfo("Committing changes on branch ", tcr.vcs.GetWorkingBranch())
460+
report.PostInfo("Committing changes on ", tcr.vcs.SessionSummary())
461461
var err error
462462
err = tcr.vcs.Add()
463463
tcr.handleError(err, false, status.VCSError)
@@ -550,13 +550,14 @@ func (tcr *TCREngine) revertFile(file string) error {
550550
// Used mainly by the user interface packages to retrieve and display this information
551551
func (tcr *TCREngine) GetSessionInfo() SessionInfo {
552552
return SessionInfo{
553-
BaseDir: tcr.sourceTree.GetBaseDir(),
554-
WorkDir: toolchain.GetWorkDir(),
555-
LanguageName: tcr.language.GetName(),
556-
ToolchainName: tcr.toolchain.GetName(),
557-
AutoPush: tcr.vcs.IsPushEnabled(),
558-
CommitOnFail: tcr.commitOnFail,
559-
BranchName: tcr.vcs.GetWorkingBranch(),
553+
BaseDir: tcr.sourceTree.GetBaseDir(),
554+
WorkDir: toolchain.GetWorkDir(),
555+
LanguageName: tcr.language.GetName(),
556+
ToolchainName: tcr.toolchain.GetName(),
557+
VCSName: tcr.vcs.Name(),
558+
VCSSessionSummary: tcr.vcs.SessionSummary(),
559+
GitAutoPush: tcr.vcs.IsPushEnabled(),
560+
CommitOnFail: tcr.commitOnFail,
560561
}
561562
}
562563

src/engine/tcr_test.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ func Test_set_auto_push(t *testing.T) {
439439
t.Run(tt.desc, func(t *testing.T) {
440440
tcr, _ = initTCREngineWithFakes(nil, nil, nil, nil)
441441
tcr.SetAutoPush(tt.state)
442-
assert.Equal(t, tt.state, tcr.GetSessionInfo().AutoPush)
442+
assert.Equal(t, tt.state, tcr.GetSessionInfo().GitAutoPush)
443443
})
444444
}
445445
}
@@ -497,23 +497,24 @@ func Test_vcs_push_highlights_errors(t *testing.T) {
497497
func Test_toggle_auto_push(t *testing.T) {
498498
tcr, _ := initTCREngineWithFakes(nil, nil, nil, nil)
499499
tcr.SetAutoPush(false)
500-
assert.Equal(t, false, tcr.GetSessionInfo().AutoPush)
500+
assert.Equal(t, false, tcr.GetSessionInfo().GitAutoPush)
501501
tcr.ToggleAutoPush()
502-
assert.Equal(t, true, tcr.GetSessionInfo().AutoPush)
502+
assert.Equal(t, true, tcr.GetSessionInfo().GitAutoPush)
503503
tcr.ToggleAutoPush()
504-
assert.Equal(t, false, tcr.GetSessionInfo().AutoPush)
504+
assert.Equal(t, false, tcr.GetSessionInfo().GitAutoPush)
505505
}
506506

507507
func Test_get_session_info(t *testing.T) {
508508
tcr, _ := initTCREngineWithFakes(nil, nil, nil, nil)
509509
currentDir, _ := os.Getwd()
510510
expected := SessionInfo{
511-
BaseDir: currentDir,
512-
WorkDir: currentDir,
513-
LanguageName: "fake-language",
514-
ToolchainName: "fake-toolchain",
515-
AutoPush: false,
516-
BranchName: "master",
511+
BaseDir: currentDir,
512+
WorkDir: currentDir,
513+
LanguageName: "fake-language",
514+
ToolchainName: "fake-toolchain",
515+
VCSName: "fake-vcs",
516+
VCSSessionSummary: "VCS session \"fake\"",
517+
GitAutoPush: false,
517518
}
518519
assert.Equal(t, expected, tcr.GetSessionInfo())
519520
}
@@ -637,7 +638,7 @@ func Test_tcr_print_log(t *testing.T) {
637638
{
638639
desc: "warning when no record found",
639640
filter: func(msg report.Message) bool {
640-
return msg.Type.Severity == report.Warning && strings.Index(msg.Text, "no TCR commit found in branch") == 0
641+
return msg.Type.Severity == report.Warning && strings.Index(msg.Text, "no TCR commit found in ") == 0
641642
},
642643
logItems: nil,
643644
expectedMatches: 1,

src/engine/tcr_test_fake.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ func NewFakeTCREngine() *FakeTCREngine {
6565
return &FakeTCREngine{
6666
returnCode: 0,
6767
info: &SessionInfo{
68-
BaseDir: "fake",
69-
WorkDir: "fake",
70-
LanguageName: "fake",
71-
ToolchainName: "fake",
72-
AutoPush: false,
73-
CommitOnFail: false,
74-
BranchName: "fake",
68+
BaseDir: "fake",
69+
WorkDir: "fake",
70+
LanguageName: "fake",
71+
ToolchainName: "fake",
72+
VCSName: "fake",
73+
VCSSessionSummary: "VCS session \"fake\"",
74+
GitAutoPush: false,
75+
CommitOnFail: false,
7576
},
7677
}
7778
}

src/vcs/git/git_impl.go

+13
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ import (
4040
"strings"
4141
)
4242

43+
// Name provides the name for this VCS implementation
44+
const Name = "git"
45+
4346
// DefaultRemoteName is the alias used by default for the git remote repository
4447
const DefaultRemoteName = "origin"
4548

@@ -93,6 +96,16 @@ func newGitImpl(initRepo func(string) (*git.Repository, billy.Filesystem, error)
9396
return &g, err
9497
}
9598

99+
// Name returns VCS name
100+
func (*gitImpl) Name() string {
101+
return Name
102+
}
103+
104+
// SessionSummary provides a short description related to current VCS session summary
105+
func (g *gitImpl) SessionSummary() string {
106+
return fmt.Sprintf("%s branch \"%s\"", g.Name(), g.GetWorkingBranch())
107+
}
108+
96109
// plainOpen is the regular function used to open a repository
97110
func plainOpen(dir string) (*git.Repository, billy.Filesystem, error) {
98111
repo, err := git.PlainOpenWithOptions(dir, &git.PlainOpenOptions{

src/vcs/git/git_impl_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ func slowTestTag(t *testing.T) {
4040
}
4141
}
4242

43+
func Test_get_vcs_name(t *testing.T) {
44+
g, _ := newGitImpl(inMemoryRepoInit, "")
45+
assert.Equal(t, "git", g.Name())
46+
}
47+
48+
func Test_get_vcs_session_summary(t *testing.T) {
49+
g, _ := newGitImpl(inMemoryRepoInit, "")
50+
assert.Equal(t, "git branch \"master\"", g.SessionSummary())
51+
}
52+
4353
func Test_git_auto_push_is_disabled_default(t *testing.T) {
4454
g, _ := newGitImpl(inMemoryRepoInit, "")
4555
assert.Zero(t, g.IsPushEnabled())
@@ -61,6 +71,7 @@ func Test_init_fails_when_working_dir_is_not_in_a_git_repo(t *testing.T) {
6171

6272
func Test_can_retrieve_working_branch_on_in_memory_repo(t *testing.T) {
6373
g, _ := newGitImpl(inMemoryRepoInit, "")
74+
// go-git's in memory repository default branch is "master"
6475
assert.Equal(t, "master", g.GetWorkingBranch())
6576
}
6677

src/vcs/git/git_test_fake.go

+8
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ func NewFake(settings FakeSettings) (*Fake, error) {
102102
return &Fake{impl: impl, settings: settings}, err
103103
}
104104

105+
func (gf *Fake) Name() string {
106+
return "fake-vcs"
107+
}
108+
109+
func (gf *Fake) SessionSummary() string {
110+
return "VCS session \"fake\""
111+
}
112+
105113
// GetLastCommand returns the last command called
106114
func (gf *Fake) GetLastCommand() Command {
107115
return gf.lastCommand

src/vcs/p4/p4_impl.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ import (
3939
"strings"
4040
)
4141

42+
// Name provides the name for this VCS implementation
43+
const Name = "p4"
44+
4245
// p4Impl provides the implementation of the Perforce interface
4346
type p4Impl struct {
4447
baseDir string
@@ -68,7 +71,7 @@ func newP4Impl(initDepotFs func() afero.Fs, dir string, testFlag bool) (*p4Impl,
6871

6972
if testFlag {
7073
// For test purpose only: tests should run and pass without having p4 installed and with no p4 server available
71-
p.clientName = ""
74+
p.clientName = "test"
7275
p.rootDir = dir
7376
} else {
7477
p.clientName = GetP4ClientName()
@@ -85,6 +88,16 @@ func newP4Impl(initDepotFs func() afero.Fs, dir string, testFlag bool) (*p4Impl,
8588
return &p, nil
8689
}
8790

91+
// Name returns VCS name
92+
func (*p4Impl) Name() string {
93+
return Name
94+
}
95+
96+
// SessionSummary provides a short description related to current VCS session summary
97+
func (p *p4Impl) SessionSummary() string {
98+
return fmt.Sprintf("%s client \"%s\"", p.Name(), p.clientName)
99+
}
100+
88101
// plainOpen is the regular function used to open a p4 depot
89102
func plainOpen() afero.Fs {
90103
return afero.NewOsFs()

src/vcs/p4/p4_impl_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ import (
3333
"testing"
3434
)
3535

36+
func Test_get_vcs_name(t *testing.T) {
37+
p, _ := newP4Impl(inMemoryDepotInit, "", true)
38+
assert.Equal(t, "p4", p.Name())
39+
}
40+
41+
func Test_get_vcs_session_summary(t *testing.T) {
42+
p, _ := newP4Impl(inMemoryDepotInit, "", true)
43+
assert.Equal(t, "p4 client \"test\"", p.SessionSummary())
44+
}
45+
3646
func Test_p4_auto_push_is_always_enabled(t *testing.T) {
3747
p, _ := newP4Impl(inMemoryDepotInit, "", true)
3848
assert.True(t, p.IsPushEnabled())

src/vcs/vcs.go

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const (
3030
// Interface provides the interface that a VCS implementation must satisfy for TCR engine to be
3131
// able to interact with it
3232
type Interface interface {
33+
Name() string
34+
SessionSummary() string
3335
GetRootDir() string
3436
GetRemoteName() string
3537
GetWorkingBranch() string

0 commit comments

Comments
 (0)