Skip to content

Commit e88223c

Browse files
agletgregorriegler
authored andcommitted
Fix: Handle filenames with spaces in mob next
Git's porcelain output quotes filenames containing spaces. The getPathOfLastModifiedFile function was passing these quoted filenames directly to os.Stat, causing failures on mob next. - Unquote git-quoted filenames before calling os.Stat. - Handle unquoting errors gracefully with warning messages. - Bump version to 5.4.1.
1 parent 1284b27 commit e88223c

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 5.4.1
2+
- Fix: `mob next` now correctly handles filenames with spaces when determining the last modified file for the open command.
3+
14
# 5.4.0
25
- Feature: Add shortcut for `mob start --create` as `mob start -c`.
36

mob.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
const (
24-
versionNumber = "5.4.0"
24+
versionNumber = "5.4.1"
2525
minimumGitVersion = "2.13.0"
2626
)
2727

@@ -881,7 +881,17 @@ func getPathOfLastModifiedFile() string {
881881
}
882882

883883
for _, file := range files {
884-
absoluteFilepath := rootDir + "/" + file
884+
unquotedFile := file
885+
if strings.HasPrefix(file, "\"") {
886+
var err error
887+
unquotedFile, err = strconv.Unquote(file)
888+
if err != nil {
889+
say.Warning("Could not unquote filename from git: " + file)
890+
say.Warning(err.Error())
891+
continue
892+
}
893+
}
894+
absoluteFilepath := rootDir + "/" + unquotedFile
885895
say.Debug(absoluteFilepath)
886896
info, err := os.Stat(absoluteFilepath)
887897
if err != nil {

mob_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,19 @@ func TestStartNextStay_WriteLastModifiedFileInCommit_WhenFileIsModifiedAndWorkin
859859
equals(t, silentgit("log", "--format=%B", "-n", "1", "HEAD"), configuration.WipCommitMessage+"\n\nlastFile:file1.txt")
860860
}
861861

862+
func TestStartNextStay_WriteLastModifiedFileInCommit_WhenFilenameContainsSpaces(t *testing.T) {
863+
_, configuration := setup(t)
864+
configuration.NextStay = true
865+
start(configuration)
866+
createFile(t, "file with spaces.txt", "contentIrrelevant")
867+
assertOnBranch(t, "mob-session")
868+
869+
next(configuration)
870+
871+
equals(t, silentgit("log", "--format=%B", "-n", "1", "HEAD"), configuration.WipCommitMessage+"\n\nlastFile:\"file with spaces.txt\"")
872+
assertOnBranch(t, "mob-session")
873+
}
874+
862875
func TestStartNextStay_DoNotWriteLastModifiedFileInCommit_WhenFileIsDeleted(t *testing.T) {
863876
_, configuration := setup(t)
864877
configuration.NextStay = true
@@ -2416,3 +2429,18 @@ func getSymlinkGitDirectory(path string) string {
24162429
func getSymlinkDirectory(path string) string {
24172430
return path + "/local-symlink"
24182431
}
2432+
2433+
func TestGetPathOfLastModifiedFile_HandlesFilenamesWithSpaces(t *testing.T) {
2434+
_, configuration := setup(t)
2435+
configuration.NextStay = true
2436+
2437+
start(configuration)
2438+
createFile(t, "file with spaces.txt", "content")
2439+
2440+
git("add", "--all")
2441+
lastFile := getPathOfLastModifiedFile()
2442+
2443+
if !strings.Contains(lastFile, "file with spaces.txt") {
2444+
t.Errorf("Expected lastFile to contain the spaced filename, got: %s", lastFile)
2445+
}
2446+
}

0 commit comments

Comments
 (0)