Make abspath Split return an abspath

This commit is contained in:
Tim Hockin 2023-12-08 11:33:20 -08:00
parent 9a806c09cb
commit ad92ba62c4
3 changed files with 10 additions and 10 deletions

View File

@ -60,7 +60,7 @@ func (abs absPath) Join(elems ...string) absPath {
// Split breaks abs into stem and leaf parts (often directory and file, but not // Split breaks abs into stem and leaf parts (often directory and file, but not
// necessarily), similar to filepath.Split. Unlike filepath.Split, the // necessarily), similar to filepath.Split. Unlike filepath.Split, the
// resulting stem part does not have any trailing path separators. // resulting stem part does not have any trailing path separators.
func (abs absPath) Split() (string, string) { func (abs absPath) Split() (absPath, string) {
if abs == "" { if abs == "" {
return "", "" return "", ""
} }
@ -74,13 +74,13 @@ func (abs absPath) Split() (string, string) {
dir = string(os.PathSeparator) dir = string(os.PathSeparator)
} }
return dir, base return absPath(dir), base
} }
// Dir returns the stem part of abs without the leaf, like filepath.Dir. // Dir returns the stem part of abs without the leaf, like filepath.Dir.
func (abs absPath) Dir() string { func (abs absPath) Dir() string {
dir, _ := abs.Split() dir, _ := abs.Split()
return dir return string(dir)
} }
// Base returns the leaf part of abs without the stem, like filepath.Base. // Base returns the leaf part of abs without the stem, like filepath.Base.

View File

@ -130,7 +130,7 @@ func TestAbsPathJoin(t *testing.T) {
func TestAbsPathSplit(t *testing.T) { func TestAbsPathSplit(t *testing.T) {
testCases := []struct { testCases := []struct {
in absPath in absPath
expDir string expDir absPath
expBase string expBase string
}{{ }{{
in: "", in: "",

12
main.go
View File

@ -347,7 +347,7 @@ func main() {
} }
log := func() *logging.Logger { log := func() *logging.Logger {
dir, file := makeAbsPath(*flErrorFile, absRoot).Split() dir, file := makeAbsPath(*flErrorFile, absRoot).Split()
return logging.New(dir, file, *flVerbose) return logging.New(dir.String(), file, *flVerbose)
}() }()
cmdRunner := cmd.NewRunner(log) cmdRunner := cmd.NewRunner(log)
@ -1273,25 +1273,25 @@ func (git *repoSync) publishSymlink(ctx context.Context, worktree worktree) erro
linkDir, linkFile := git.link.Split() linkDir, linkFile := git.link.Split()
// Make sure the link directory exists. // Make sure the link directory exists.
if err := os.MkdirAll(linkDir, defaultDirMode); err != nil { if err := os.MkdirAll(linkDir.String(), defaultDirMode); err != nil {
return fmt.Errorf("error making symlink dir: %w", err) return fmt.Errorf("error making symlink dir: %w", err)
} }
// linkDir is absolute, so we need to change it to a relative path. This is // linkDir is absolute, so we need to change it to a relative path. This is
// so it can be volume-mounted at another path and the symlink still works. // so it can be volume-mounted at another path and the symlink still works.
targetRelative, err := filepath.Rel(linkDir, targetPath.String()) targetRelative, err := filepath.Rel(linkDir.String(), targetPath.String())
if err != nil { if err != nil {
return fmt.Errorf("error converting to relative path: %w", err) return fmt.Errorf("error converting to relative path: %w", err)
} }
const tmplink = "tmp-link" const tmplink = "tmp-link"
git.log.V(2).Info("creating tmp symlink", "dir", linkDir, "link", tmplink, "target", targetRelative) git.log.V(2).Info("creating tmp symlink", "dir", linkDir, "link", tmplink, "target", targetRelative)
if err := os.Symlink(targetRelative, filepath.Join(linkDir, tmplink)); err != nil { if err := os.Symlink(targetRelative, filepath.Join(linkDir.String(), tmplink)); err != nil {
return fmt.Errorf("error creating symlink: %w", err) return fmt.Errorf("error creating symlink: %w", err)
} }
git.log.V(2).Info("renaming symlink", "root", linkDir, "oldName", tmplink, "newName", linkFile) git.log.V(2).Info("renaming symlink", "root", linkDir, "oldName", tmplink, "newName", linkFile)
if err := os.Rename(filepath.Join(linkDir, tmplink), git.link.String()); err != nil { if err := os.Rename(filepath.Join(linkDir.String(), tmplink), git.link.String()); err != nil {
return fmt.Errorf("error replacing symlink: %w", err) return fmt.Errorf("error replacing symlink: %w", err)
} }
@ -1574,7 +1574,7 @@ func (git *repoSync) currentWorktree() (worktree, error) {
return worktree(target), nil return worktree(target), nil
} }
linkDir, _ := git.link.Split() linkDir, _ := git.link.Split()
return worktree(absPath(linkDir).Join(target)), nil return worktree(linkDir.Join(target)), nil
} }
// SyncRepo syncs the repository to the desired ref, publishes it via the link, // SyncRepo syncs the repository to the desired ref, publishes it via the link,