mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-12 04:45:47 +02:00
Use filepath instead of path for file path operations
In practice, using path seems to work too, since Windows seems to be capable of dealing with a path like C:/x/y instead of C:\x\y; but it's cleaner to do this properly.
This commit is contained in:
parent
07dd8a2b07
commit
75a95865ff
3 changed files with 78 additions and 25 deletions
|
@ -3,7 +3,6 @@ package git_commands
|
||||||
import (
|
import (
|
||||||
ioFs "io/fs"
|
ioFs "io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -64,9 +63,9 @@ func (self *RepoPaths) IsBareRepo() bool {
|
||||||
func MockRepoPaths(currentPath string) *RepoPaths {
|
func MockRepoPaths(currentPath string) *RepoPaths {
|
||||||
return &RepoPaths{
|
return &RepoPaths{
|
||||||
worktreePath: currentPath,
|
worktreePath: currentPath,
|
||||||
worktreeGitDirPath: path.Join(currentPath, ".git"),
|
worktreeGitDirPath: filepath.Join(currentPath, ".git"),
|
||||||
repoPath: currentPath,
|
repoPath: currentPath,
|
||||||
repoGitDirPath: path.Join(currentPath, ".git"),
|
repoGitDirPath: filepath.Join(currentPath, ".git"),
|
||||||
repoName: "lazygit",
|
repoName: "lazygit",
|
||||||
isBareRepo: false,
|
isBareRepo: false,
|
||||||
}
|
}
|
||||||
|
@ -116,9 +115,9 @@ func GetRepoPathsForDir(
|
||||||
if isSubmodule {
|
if isSubmodule {
|
||||||
repoPath = worktreePath
|
repoPath = worktreePath
|
||||||
} else {
|
} else {
|
||||||
repoPath = path.Dir(repoGitDirPath)
|
repoPath = filepath.Dir(repoGitDirPath)
|
||||||
}
|
}
|
||||||
repoName := path.Base(repoPath)
|
repoName := filepath.Base(repoPath)
|
||||||
|
|
||||||
return &RepoPaths{
|
return &RepoPaths{
|
||||||
worktreePath: worktreePath,
|
worktreePath: worktreePath,
|
||||||
|
@ -154,7 +153,7 @@ func linkedWortkreePaths(fs afero.Fs, repoGitDirPath string) []string {
|
||||||
result := []string{}
|
result := []string{}
|
||||||
// For each directory in this path we're going to cat the `gitdir` file and append its contents to our result
|
// For each directory in this path we're going to cat the `gitdir` file and append its contents to our result
|
||||||
// That file points us to the `.git` file in the worktree.
|
// That file points us to the `.git` file in the worktree.
|
||||||
worktreeGitDirsPath := path.Join(repoGitDirPath, "worktrees")
|
worktreeGitDirsPath := filepath.Join(repoGitDirPath, "worktrees")
|
||||||
|
|
||||||
// ensure the directory exists
|
// ensure the directory exists
|
||||||
_, err := fs.Stat(worktreeGitDirsPath)
|
_, err := fs.Stat(worktreeGitDirsPath)
|
||||||
|
@ -171,7 +170,7 @@ func linkedWortkreePaths(fs afero.Fs, repoGitDirPath string) []string {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
gitDirPath := path.Join(currPath, "gitdir")
|
gitDirPath := filepath.Join(currPath, "gitdir")
|
||||||
gitDirBytes, err := afero.ReadFile(fs, gitDirPath)
|
gitDirBytes, err := afero.ReadFile(fs, gitDirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// ignoring error
|
// ignoring error
|
||||||
|
@ -179,7 +178,7 @@ func linkedWortkreePaths(fs afero.Fs, repoGitDirPath string) []string {
|
||||||
}
|
}
|
||||||
trimmedGitDir := strings.TrimSpace(string(gitDirBytes))
|
trimmedGitDir := strings.TrimSpace(string(gitDirBytes))
|
||||||
// removing the .git part
|
// removing the .git part
|
||||||
worktreeDir := path.Dir(trimmedGitDir)
|
worktreeDir := filepath.Dir(trimmedGitDir)
|
||||||
result = append(result, worktreeDir)
|
result = append(result, worktreeDir)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,11 +2,13 @@ package git_commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/go-errors/errors"
|
"github.com/go-errors/errors"
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
||||||
|
"github.com/samber/lo"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,7 +31,17 @@ func TestGetRepoPaths(t *testing.T) {
|
||||||
Name: "typical case",
|
Name: "typical case",
|
||||||
BeforeFunc: func(runner *oscommands.FakeCmdObjRunner, getRevParseArgs argFn) {
|
BeforeFunc: func(runner *oscommands.FakeCmdObjRunner, getRevParseArgs argFn) {
|
||||||
// setup for main worktree
|
// setup for main worktree
|
||||||
expectedOutput := []string{
|
mockOutput := lo.Ternary(runtime.GOOS == "windows", []string{
|
||||||
|
// --show-toplevel
|
||||||
|
`C:\path\to\repo`,
|
||||||
|
// --git-dir
|
||||||
|
`C:\path\to\repo\.git`,
|
||||||
|
// --git-common-dir
|
||||||
|
`C:\path\to\repo\.git`,
|
||||||
|
// --is-bare-repository
|
||||||
|
"false",
|
||||||
|
// --show-superproject-working-tree
|
||||||
|
}, []string{
|
||||||
// --show-toplevel
|
// --show-toplevel
|
||||||
"/path/to/repo",
|
"/path/to/repo",
|
||||||
// --git-dir
|
// --git-dir
|
||||||
|
@ -39,28 +51,45 @@ func TestGetRepoPaths(t *testing.T) {
|
||||||
// --is-bare-repository
|
// --is-bare-repository
|
||||||
"false",
|
"false",
|
||||||
// --show-superproject-working-tree
|
// --show-superproject-working-tree
|
||||||
}
|
})
|
||||||
runner.ExpectGitArgs(
|
runner.ExpectGitArgs(
|
||||||
append(getRevParseArgs(), "--show-toplevel", "--absolute-git-dir", "--git-common-dir", "--is-bare-repository", "--show-superproject-working-tree"),
|
append(getRevParseArgs(), "--show-toplevel", "--absolute-git-dir", "--git-common-dir", "--is-bare-repository", "--show-superproject-working-tree"),
|
||||||
strings.Join(expectedOutput, "\n"),
|
strings.Join(mockOutput, "\n"),
|
||||||
nil)
|
nil)
|
||||||
},
|
},
|
||||||
Path: "/path/to/repo",
|
Path: "/path/to/repo",
|
||||||
Expected: &RepoPaths{
|
Expected: lo.Ternary(runtime.GOOS == "windows", &RepoPaths{
|
||||||
|
worktreePath: `C:\path\to\repo`,
|
||||||
|
worktreeGitDirPath: `C:\path\to\repo\.git`,
|
||||||
|
repoPath: `C:\path\to\repo`,
|
||||||
|
repoGitDirPath: `C:\path\to\repo\.git`,
|
||||||
|
repoName: `repo`,
|
||||||
|
isBareRepo: false,
|
||||||
|
}, &RepoPaths{
|
||||||
worktreePath: "/path/to/repo",
|
worktreePath: "/path/to/repo",
|
||||||
worktreeGitDirPath: "/path/to/repo/.git",
|
worktreeGitDirPath: "/path/to/repo/.git",
|
||||||
repoPath: "/path/to/repo",
|
repoPath: "/path/to/repo",
|
||||||
repoGitDirPath: "/path/to/repo/.git",
|
repoGitDirPath: "/path/to/repo/.git",
|
||||||
repoName: "repo",
|
repoName: "repo",
|
||||||
isBareRepo: false,
|
isBareRepo: false,
|
||||||
},
|
}),
|
||||||
Err: nil,
|
Err: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "bare repo",
|
Name: "bare repo",
|
||||||
BeforeFunc: func(runner *oscommands.FakeCmdObjRunner, getRevParseArgs argFn) {
|
BeforeFunc: func(runner *oscommands.FakeCmdObjRunner, getRevParseArgs argFn) {
|
||||||
// setup for main worktree
|
// setup for main worktree
|
||||||
expectedOutput := []string{
|
mockOutput := lo.Ternary(runtime.GOOS == "windows", []string{
|
||||||
|
// --show-toplevel
|
||||||
|
`C:\path\to\repo`,
|
||||||
|
// --git-dir
|
||||||
|
`C:\path\to\bare_repo\bare.git`,
|
||||||
|
// --git-common-dir
|
||||||
|
`C:\path\to\bare_repo\bare.git`,
|
||||||
|
// --is-bare-repository
|
||||||
|
`true`,
|
||||||
|
// --show-superproject-working-tree
|
||||||
|
}, []string{
|
||||||
// --show-toplevel
|
// --show-toplevel
|
||||||
"/path/to/repo",
|
"/path/to/repo",
|
||||||
// --git-dir
|
// --git-dir
|
||||||
|
@ -70,27 +99,45 @@ func TestGetRepoPaths(t *testing.T) {
|
||||||
// --is-bare-repository
|
// --is-bare-repository
|
||||||
"true",
|
"true",
|
||||||
// --show-superproject-working-tree
|
// --show-superproject-working-tree
|
||||||
}
|
})
|
||||||
runner.ExpectGitArgs(
|
runner.ExpectGitArgs(
|
||||||
append(getRevParseArgs(), "--show-toplevel", "--absolute-git-dir", "--git-common-dir", "--is-bare-repository", "--show-superproject-working-tree"),
|
append(getRevParseArgs(), "--show-toplevel", "--absolute-git-dir", "--git-common-dir", "--is-bare-repository", "--show-superproject-working-tree"),
|
||||||
strings.Join(expectedOutput, "\n"),
|
strings.Join(mockOutput, "\n"),
|
||||||
nil)
|
nil)
|
||||||
},
|
},
|
||||||
Path: "/path/to/repo",
|
Path: "/path/to/repo",
|
||||||
Expected: &RepoPaths{
|
Expected: lo.Ternary(runtime.GOOS == "windows", &RepoPaths{
|
||||||
|
worktreePath: `C:\path\to\repo`,
|
||||||
|
worktreeGitDirPath: `C:\path\to\bare_repo\bare.git`,
|
||||||
|
repoPath: `C:\path\to\bare_repo`,
|
||||||
|
repoGitDirPath: `C:\path\to\bare_repo\bare.git`,
|
||||||
|
repoName: `bare_repo`,
|
||||||
|
isBareRepo: true,
|
||||||
|
}, &RepoPaths{
|
||||||
worktreePath: "/path/to/repo",
|
worktreePath: "/path/to/repo",
|
||||||
worktreeGitDirPath: "/path/to/bare_repo/bare.git",
|
worktreeGitDirPath: "/path/to/bare_repo/bare.git",
|
||||||
repoPath: "/path/to/bare_repo",
|
repoPath: "/path/to/bare_repo",
|
||||||
repoGitDirPath: "/path/to/bare_repo/bare.git",
|
repoGitDirPath: "/path/to/bare_repo/bare.git",
|
||||||
repoName: "bare_repo",
|
repoName: "bare_repo",
|
||||||
isBareRepo: true,
|
isBareRepo: true,
|
||||||
},
|
}),
|
||||||
Err: nil,
|
Err: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "submodule",
|
Name: "submodule",
|
||||||
BeforeFunc: func(runner *oscommands.FakeCmdObjRunner, getRevParseArgs argFn) {
|
BeforeFunc: func(runner *oscommands.FakeCmdObjRunner, getRevParseArgs argFn) {
|
||||||
expectedOutput := []string{
|
mockOutput := lo.Ternary(runtime.GOOS == "windows", []string{
|
||||||
|
// --show-toplevel
|
||||||
|
`C:\path\to\repo\submodule1`,
|
||||||
|
// --git-dir
|
||||||
|
`C:\path\to\repo\.git\modules\submodule1`,
|
||||||
|
// --git-common-dir
|
||||||
|
`C:\path\to\repo\.git\modules\submodule1`,
|
||||||
|
// --is-bare-repository
|
||||||
|
`false`,
|
||||||
|
// --show-superproject-working-tree
|
||||||
|
`C:\path\to\repo`,
|
||||||
|
}, []string{
|
||||||
// --show-toplevel
|
// --show-toplevel
|
||||||
"/path/to/repo/submodule1",
|
"/path/to/repo/submodule1",
|
||||||
// --git-dir
|
// --git-dir
|
||||||
|
@ -101,21 +148,28 @@ func TestGetRepoPaths(t *testing.T) {
|
||||||
"false",
|
"false",
|
||||||
// --show-superproject-working-tree
|
// --show-superproject-working-tree
|
||||||
"/path/to/repo",
|
"/path/to/repo",
|
||||||
}
|
})
|
||||||
runner.ExpectGitArgs(
|
runner.ExpectGitArgs(
|
||||||
append(getRevParseArgs(), "--show-toplevel", "--absolute-git-dir", "--git-common-dir", "--is-bare-repository", "--show-superproject-working-tree"),
|
append(getRevParseArgs(), "--show-toplevel", "--absolute-git-dir", "--git-common-dir", "--is-bare-repository", "--show-superproject-working-tree"),
|
||||||
strings.Join(expectedOutput, "\n"),
|
strings.Join(mockOutput, "\n"),
|
||||||
nil)
|
nil)
|
||||||
},
|
},
|
||||||
Path: "/path/to/repo/submodule1",
|
Path: "/path/to/repo/submodule1",
|
||||||
Expected: &RepoPaths{
|
Expected: lo.Ternary(runtime.GOOS == "windows", &RepoPaths{
|
||||||
|
worktreePath: `C:\path\to\repo\submodule1`,
|
||||||
|
worktreeGitDirPath: `C:\path\to\repo\.git\modules\submodule1`,
|
||||||
|
repoPath: `C:\path\to\repo\submodule1`,
|
||||||
|
repoGitDirPath: `C:\path\to\repo\.git\modules\submodule1`,
|
||||||
|
repoName: `submodule1`,
|
||||||
|
isBareRepo: false,
|
||||||
|
}, &RepoPaths{
|
||||||
worktreePath: "/path/to/repo/submodule1",
|
worktreePath: "/path/to/repo/submodule1",
|
||||||
worktreeGitDirPath: "/path/to/repo/.git/modules/submodule1",
|
worktreeGitDirPath: "/path/to/repo/.git/modules/submodule1",
|
||||||
repoPath: "/path/to/repo/submodule1",
|
repoPath: "/path/to/repo/submodule1",
|
||||||
repoGitDirPath: "/path/to/repo/.git/modules/submodule1",
|
repoGitDirPath: "/path/to/repo/.git/modules/submodule1",
|
||||||
repoName: "submodule1",
|
repoName: "submodule1",
|
||||||
isBareRepo: false,
|
isBareRepo: false,
|
||||||
},
|
}),
|
||||||
Err: nil,
|
Err: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -3,7 +3,7 @@ package git_commands
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/go-errors/errors"
|
"github.com/go-errors/errors"
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||||
|
@ -233,7 +233,7 @@ func (self *WorkingTreeCommands) Ignore(filename string) error {
|
||||||
|
|
||||||
// Exclude adds a file to the .git/info/exclude for the repo
|
// Exclude adds a file to the .git/info/exclude for the repo
|
||||||
func (self *WorkingTreeCommands) Exclude(filename string) error {
|
func (self *WorkingTreeCommands) Exclude(filename string) error {
|
||||||
excludeFile := path.Join(self.repoPaths.repoGitDirPath, "info", "exclude")
|
excludeFile := filepath.Join(self.repoPaths.repoGitDirPath, "info", "exclude")
|
||||||
return self.os.AppendLineToFile(excludeFile, filename)
|
return self.os.AppendLineToFile(excludeFile, filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue