Skip to content

Commit

Permalink
Make abspath Split return an abspath
Browse files Browse the repository at this point in the history
  • Loading branch information
thockin committed Dec 9, 2023
1 parent 9a806c0 commit ad92ba6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions abspath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// necessarily), similar to filepath.Split. Unlike filepath.Split, the
// resulting stem part does not have any trailing path separators.
func (abs absPath) Split() (string, string) {
func (abs absPath) Split() (absPath, string) {
if abs == "" {
return "", ""
}
Expand All @@ -74,13 +74,13 @@ func (abs absPath) Split() (string, string) {
dir = string(os.PathSeparator)
}

return dir, base
return absPath(dir), base
}

// Dir returns the stem part of abs without the leaf, like filepath.Dir.
func (abs absPath) Dir() string {
dir, _ := abs.Split()
return dir
return string(dir)
}

// Base returns the leaf part of abs without the stem, like filepath.Base.
Expand Down
2 changes: 1 addition & 1 deletion abspath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestAbsPathJoin(t *testing.T) {
func TestAbsPathSplit(t *testing.T) {
testCases := []struct {
in absPath
expDir string
expDir absPath
expBase string
}{{
in: "",
Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func main() {
}
log := func() *logging.Logger {
dir, file := makeAbsPath(*flErrorFile, absRoot).Split()
return logging.New(dir, file, *flVerbose)
return logging.New(dir.String(), file, *flVerbose)
}()
cmdRunner := cmd.NewRunner(log)

Expand Down Expand Up @@ -1273,25 +1273,25 @@ func (git *repoSync) publishSymlink(ctx context.Context, worktree worktree) erro
linkDir, linkFile := git.link.Split()

// 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)
}

// 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.
targetRelative, err := filepath.Rel(linkDir, targetPath.String())
targetRelative, err := filepath.Rel(linkDir.String(), targetPath.String())
if err != nil {
return fmt.Errorf("error converting to relative path: %w", err)
}

const tmplink = "tmp-link"
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)
}

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)
}

Expand Down Expand Up @@ -1574,7 +1574,7 @@ func (git *repoSync) currentWorktree() (worktree, error) {
return worktree(target), nil
}
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,
Expand Down

0 comments on commit ad92ba6

Please sign in to comment.