Skip to content

Commit 063bb1c

Browse files
committed
repo: manipulate paths with 'filepath.Join()'
Replace manual path joining via string concatenation with use of 'filepath.Join()'. The path normalization provided by the function helps avoid issues caused by missing (or extra) path separators and aligns paths to the to the appropriate filesystem's conventions. Signed-off-by: Victoria Dye <[email protected]>
1 parent b5144ff commit 063bb1c

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

internal/core/paths.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ package core
22

33
import (
44
"os/user"
5+
"path/filepath"
56
)
67

78
func bundleroot(user *user.User) string {
8-
return user.HomeDir + "/git-bundle-server/"
9+
return filepath.Join(user.HomeDir, "git-bundle-server")
910
}
1011

1112
func webroot(user *user.User) string {
12-
return bundleroot(user) + "www/"
13+
return filepath.Join(bundleroot(user), "www")
1314
}
1415

1516
func reporoot(user *user.User) string {
16-
return bundleroot(user) + "git/"
17+
return filepath.Join(bundleroot(user), "git")
1718
}
1819

1920
func CrontabFile(user *user.User) string {
20-
return bundleroot(user) + "cron-schedule"
21+
return filepath.Join(bundleroot(user), "cron-schedule")
2122
}

internal/core/repo.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"path/filepath"
78

89
"github.com/github/git-bundle-server/internal/common"
910
"github.com/github/git-bundle-server/internal/log"
@@ -57,8 +58,8 @@ func (r *repoProvider) CreateRepository(ctx context.Context, route string) (*Rep
5758
return &repo, nil
5859
}
5960

60-
repodir := reporoot(user) + route
61-
web := webroot(user) + route
61+
repodir := filepath.Join(reporoot(user), route)
62+
web := filepath.Join(webroot(user), route)
6263

6364
mkdirErr := os.MkdirAll(web, os.ModePerm)
6465
if mkdirErr != nil {
@@ -105,8 +106,7 @@ func (r *repoProvider) writeRouteFile(repos map[string]Repository) error {
105106
if err != nil {
106107
return err
107108
}
108-
dir := bundleroot(user)
109-
routefile := dir + "/routes"
109+
routefile := filepath.Join(bundleroot(user), "routes")
110110

111111
contents := ""
112112

@@ -128,8 +128,7 @@ func (r *repoProvider) GetRepositories(ctx context.Context) (map[string]Reposito
128128

129129
repos := make(map[string]Repository)
130130

131-
dir := bundleroot(user)
132-
routefile := dir + "/routes"
131+
routefile := filepath.Join(bundleroot(user), "routes")
133132

134133
lines, err := r.fileSystem.ReadFileLines(routefile)
135134
if err != nil {
@@ -142,8 +141,8 @@ func (r *repoProvider) GetRepositories(ctx context.Context) (map[string]Reposito
142141

143142
repo := Repository{
144143
Route: route,
145-
RepoDir: reporoot(user) + route,
146-
WebDir: webroot(user) + route,
144+
RepoDir: filepath.Join(reporoot(user), route),
145+
WebDir: filepath.Join(webroot(user), route),
147146
}
148147
repos[route] = repo
149148
}

internal/core/repo_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"os/user"
7+
"path/filepath"
78
"testing"
89

910
"github.com/github/git-bundle-server/internal/core"
@@ -115,8 +116,8 @@ func TestRepos_GetRepositories(t *testing.T) {
115116
a := actual[repo.Route]
116117

117118
assert.Equal(t, repo.Route, a.Route)
118-
assert.Equal(t, repo.RepoDir, a.RepoDir)
119-
assert.Equal(t, repo.WebDir, a.WebDir)
119+
assert.Equal(t, filepath.Clean(repo.RepoDir), a.RepoDir)
120+
assert.Equal(t, filepath.Clean(repo.WebDir), a.WebDir)
120121
}
121122
}
122123
})

0 commit comments

Comments
 (0)