Skip to content

Commit 545b144

Browse files
committed
Extract WorkingDir from git.Client into workdir package
The working directory is not git-specific — it's the directory where the entire application operates. Move it to a shared workdir.Path variable so all packages (git, test, mob) use the same source of truth. https://claude.ai/code/session_0162Mf6jkLfhrQP3PAWWyhgr
1 parent fc82254 commit 545b144

5 files changed

Lines changed: 32 additions & 30 deletions

File tree

git/git.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ import (
1010
config "github.com/remotemobprogramming/mob/v5/configuration"
1111
"github.com/remotemobprogramming/mob/v5/exit"
1212
"github.com/remotemobprogramming/mob/v5/say"
13+
"github.com/remotemobprogramming/mob/v5/workdir"
1314
)
1415

1516
type Client struct {
16-
WorkingDir string
1717
PassthroughStderrStdout bool
1818
}
1919

2020
func (g *Client) runCommandSilent(name string, args ...string) (string, string, error) {
2121
command := exec.Command(name, args...)
22-
if len(g.WorkingDir) > 0 {
23-
command.Dir = g.WorkingDir
22+
if len(workdir.Path) > 0 {
23+
command.Dir = workdir.Path
2424
}
2525
commandString := strings.Join(command.Args, " ")
2626
say.Debug("Running command <" + commandString + "> in silent mode, capturing combined output")
@@ -32,8 +32,8 @@ func (g *Client) runCommandSilent(name string, args ...string) (string, string,
3232

3333
func (g *Client) runCommand(name string, args ...string) (string, string, error) {
3434
command := exec.Command(name, args...)
35-
if len(g.WorkingDir) > 0 {
36-
command.Dir = g.WorkingDir
35+
if len(workdir.Path) > 0 {
36+
command.Dir = workdir.Path
3737
}
3838
commandString := strings.Join(command.Args, " ")
3939
say.Debug("Running command <" + commandString + "> passing output through")

mob.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/remotemobprogramming/mob/v5/help"
2222
"github.com/remotemobprogramming/mob/v5/open"
2323
"github.com/remotemobprogramming/mob/v5/say"
24+
"github.com/remotemobprogramming/mob/v5/workdir"
2425
)
2526

2627
const (
@@ -252,7 +253,7 @@ func run(osArgs []string) {
252253
say.Debug("command '" + command + "'")
253254
say.Debug("parameters '" + strings.Join(parameters, " ") + "'")
254255
say.Debug("version " + versionNumber)
255-
say.Debug("workingDir '" + gitClient.WorkingDir + "'")
256+
say.Debug("workingDir '" + workdir.Path + "'")
256257

257258
// workaround until we have a better design
258259
if configuration.GitHooksEnabled {
@@ -877,10 +878,10 @@ func getPathOfLastModifiedFile() string {
877878
// uses git status --porcelain. To work properly files have to be staged.
878879
func getModifiedFiles(rootDir string) []string {
879880
say.Debug("Find modified files")
880-
oldWorkingDir := gitClient.WorkingDir
881-
gitClient.WorkingDir = rootDir
881+
oldWorkingDir := workdir.Path
882+
workdir.Path = rootDir
882883
gitstatus := silentgit("status", "--porcelain")
883-
gitClient.WorkingDir = oldWorkingDir
884+
workdir.Path = oldWorkingDir
884885
lines := strings.Split(gitstatus, "\n")
885886
files := []string{}
886887
for _, line := range lines {
@@ -1114,8 +1115,8 @@ func isGit() bool {
11141115

11151116
func startCommand(name string, args ...string) (string, error) {
11161117
command := exec.Command(name, args...)
1117-
if len(gitClient.WorkingDir) > 0 {
1118-
command.Dir = gitClient.WorkingDir
1118+
if len(workdir.Path) > 0 {
1119+
command.Dir = workdir.Path
11191120
}
11201121
commandString := strings.Join(command.Args, " ")
11211122
say.Debug("Starting command " + commandString)

mob_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/remotemobprogramming/mob/v5/exit"
99
"github.com/remotemobprogramming/mob/v5/open"
1010
"github.com/remotemobprogramming/mob/v5/say"
11+
"github.com/remotemobprogramming/mob/v5/workdir"
1112
"path/filepath"
1213
"reflect"
1314
"runtime"
@@ -209,7 +210,7 @@ func TestStart(t *testing.T) {
209210

210211
func TestStartDespiteGitHook(t *testing.T) {
211212
_, configuration := setup(t)
212-
createExecutableFileInPath(t, gitClient.WorkingDir+"/.git/hooks", "pre-commit", "#!/bin/sh\necho 'boo'\nexit 1\n")
213+
createExecutableFileInPath(t, workdir.Path+"/.git/hooks", "pre-commit", "#!/bin/sh\necho 'boo'\nexit 1\n")
213214

214215
start(configuration)
215216

@@ -854,7 +855,7 @@ func TestStartNextStay_WriteLastModifiedFileInCommit_WhenFileIsModifiedAndWorkin
854855
start(configuration)
855856
createDirectory(t, "dir")
856857
createFile(t, "file1.txt", "contentIrrelevantButModified")
857-
setWorkingDir(gitClient.WorkingDir + "/dir")
858+
setWorkingDir(workdir.Path + "/dir")
858859
next(configuration)
859860

860861
assertOnBranch(t, "mob-session")
@@ -884,7 +885,7 @@ func TestStartNextStay_DoNotWriteLastModifiedFileInCommit_WhenFileIsDeleted(t *t
884885
next(configuration)
885886

886887
start(configuration)
887-
removeFile(t, filepath.Join(gitClient.WorkingDir, "file1.txt"))
888+
removeFile(t, filepath.Join(workdir.Path, "file1.txt"))
888889
next(configuration)
889890

890891
assertOnBranch(t, "mob-session")
@@ -901,7 +902,7 @@ func TestStartNextStay_DoNotWriteLastModifiedFileInCommit_WhenFileIsMoved(t *tes
901902

902903
start(configuration)
903904
createDirectory(t, "dir")
904-
moveFile(t, filepath.Join(gitClient.WorkingDir, "file1.txt"), filepath.Join(gitClient.WorkingDir, "dir", "file1.txt"))
905+
moveFile(t, filepath.Join(workdir.Path, "file1.txt"), filepath.Join(workdir.Path, "dir", "file1.txt"))
905906
next(configuration)
906907

907908
assertOnBranch(t, "mob-session")
@@ -2090,7 +2091,7 @@ func resetExit() {
20902091
}
20912092

20922093
func createTestbed(t *testing.T, configuration config.Configuration) {
2093-
gitClient.WorkingDir = ""
2094+
workdir.Path = ""
20942095

20952096
tempDir = t.TempDir()
20962097

@@ -2121,7 +2122,7 @@ func createTestbedIn(t *testing.T, temporaryDirectory string) {
21212122
cloneRepository(localDirectory, remoteDirectory)
21222123

21232124
say.Debug("Populate, initial import and push")
2124-
gitClient.WorkingDir = localDirectory
2125+
workdir.Path = localDirectory
21252126
createFile(t, "test.txt", "test")
21262127
createDirectory(t, "subdir")
21272128
createFileInPath(t, localDirectory+"/subdir", "subdir.txt", "subdir")
@@ -2153,7 +2154,7 @@ func createTestbedIn(t *testing.T, temporaryDirectory string) {
21532154
}
21542155

21552156
func setWorkingDir(dir string) {
2156-
gitClient.WorkingDir = dir
2157+
workdir.Path = dir
21572158
say.Say("\n===== cd " + dir)
21582159
}
21592160

@@ -2181,7 +2182,7 @@ func assertCommitsOnBranch(t *testing.T, commits int, branchName string) {
21812182
result := silentgit("rev-list", "--count", branchName)
21822183
number, _ := strconv.Atoi(result)
21832184
if number != commits {
2184-
failWithFailure(t, strconv.Itoa(commits)+" commits in "+gitClient.WorkingDir, strconv.Itoa(number)+" commits in "+gitClient.WorkingDir)
2185+
failWithFailure(t, strconv.Itoa(commits)+" commits in "+workdir.Path, strconv.Itoa(number)+" commits in "+workdir.Path)
21852186
}
21862187
}
21872188

@@ -2200,7 +2201,7 @@ func assertCommitLogNotContainsMessage(t *testing.T, branchName string, commitMe
22002201
}
22012202

22022203
func assertFileExist(t *testing.T, filename string) {
2203-
path := gitClient.WorkingDir + "/" + filename
2204+
path := workdir.Path + "/" + filename
22042205
if strings.Index(filename, "/") == 0 {
22052206
path = filename
22062207
}
@@ -2216,7 +2217,7 @@ func createFileAndCommitIt(t *testing.T, filename string, content string, commit
22162217
}
22172218

22182219
func createFile(t *testing.T, filename string, content string) (pathToFile string) {
2219-
return createFileInPath(t, gitClient.WorkingDir, filename, content)
2220+
return createFileInPath(t, workdir.Path, filename, content)
22202221
}
22212222

22222223
func createFileInPath(t *testing.T, path, filename, content string) (pathToFile string) {
@@ -2242,7 +2243,7 @@ func createExecutableFileInPath(t *testing.T, path, filename, content string) (p
22422243
}
22432244

22442245
func createDirectory(t *testing.T, directory string) (pathToDirectory string) {
2245-
return ensureDirectoryExists(t, gitClient.WorkingDir+"/"+directory)
2246+
return ensureDirectoryExists(t, workdir.Path+"/"+directory)
22462247
}
22472248

22482249
func ensureDirectoryExists(t *testing.T, path string) (pathToDirectory string) {
@@ -2378,7 +2379,7 @@ func createRemoteRepository(path string) {
23782379
say.Error(err.Error())
23792380
return
23802381
}
2381-
gitClient.WorkingDir = path
2382+
workdir.Path = path
23822383
say.Debug("before git init")
23832384
git("--bare", "init")
23842385
say.Debug("before symbolic-ref")
@@ -2396,7 +2397,7 @@ func cloneRepository(path, remoteDirectory string) {
23962397
say.Error(err.Error())
23972398
return
23982399
}
2399-
gitClient.WorkingDir = path
2400+
workdir.Path = path
24002401
name := basename(path)
24012402
git("clone", "--origin", "origin", "file://"+remoteDirectory, ".")
24022403
git("config", "--local", "user.name", name)

test/test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package test
33
import (
44
"fmt"
55
"github.com/remotemobprogramming/mob/v5/say"
6+
"github.com/remotemobprogramming/mob/v5/workdir"
67
"os"
78
"path/filepath"
89
"reflect"
@@ -13,10 +14,6 @@ import (
1314
"time"
1415
)
1516

16-
var (
17-
workingDir string
18-
)
19-
2017
const (
2118
AWAIT_DEFAULT_POLL_INTERVAL = 100 * time.Millisecond
2219
AWAIT_DEFAULT_AT_MOST = 2 * time.Second
@@ -50,7 +47,7 @@ func failWithFailureMessage(t *testing.T, message string) {
5047

5148
func CreateFile(t *testing.T, filename string, content string) (pathToFile string) {
5249
contentAsBytes := []byte(content)
53-
pathToFile = workingDir + "/" + filename
50+
pathToFile = workdir.Path + "/" + filename
5451
err := os.WriteFile(pathToFile, contentAsBytes, 0644)
5552
if err != nil {
5653
failWithFailure(t, "creating file "+filename+" with content "+content, "error")
@@ -59,7 +56,7 @@ func CreateFile(t *testing.T, filename string, content string) (pathToFile strin
5956
}
6057

6158
func SetWorkingDir(dir string) {
62-
workingDir = dir
59+
workdir.Path = dir
6360
say.Say("\n===== cd " + dir)
6461
}
6562

workdir/workdir.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package workdir
2+
3+
var Path string

0 commit comments

Comments
 (0)