Skip to content

Commit 8e5c83c

Browse files
committed
Handle file paths in a cross-platform compliant way
1 parent 81a227e commit 8e5c83c

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

Diff for: internal/repo_sync/repo_handler.go

+38-27
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const (
2525
// Otherwise, it clones it.
2626
func HandleRepository(dryRun bool, output, organization string, repositoryInfo github.RepositoryInfo, protocol CloneProtocol) error {
2727
repository := repositoryInfo.Name
28-
repoPath, err := filepath.Abs(filepath.FromSlash(fmt.Sprintf("%s/%s", output, repository)))
28+
repoPath, err := safeAbsPath(fmt.Sprintf("%s/%s", output, repository))
2929
if err != nil {
3030
return err
3131
}
@@ -37,7 +37,7 @@ func HandleRepository(dryRun bool, output, organization string, repositoryInfo g
3737
return nil
3838
}
3939
log.Println("[debug] cloning repo because local folder not found:", repoPath)
40-
if err := clone(output, organization, repository, protocol); err != nil {
40+
if err := clone(repoPath, organization, repository, protocol); err != nil {
4141
return err
4242
}
4343
return nil
@@ -52,11 +52,10 @@ func HandleRepository(dryRun bool, output, organization string, repositoryInfo g
5252
return nil
5353
}
5454
log.Println("[debug] updating local clone for repo:", repoPath)
55-
return updateLocalClone(output, organization, repositoryInfo)
55+
return updateLocalClone(repoPath, organization, repositoryInfo)
5656
}
5757

5858
func clone(output, organization string, repository string, protocol CloneProtocol) error {
59-
repoPath := fmt.Sprintf("%s/%s", output, repository)
6059
var repoUrl string
6160
if protocol == SystemProtocol {
6261
repoUrl = fmt.Sprintf("%s/%s", organization, repository)
@@ -67,6 +66,10 @@ func clone(output, organization string, repository string, protocol CloneProtoco
6766
} else {
6867
return fmt.Errorf("unknown protocol for cloning: %s", protocol)
6968
}
69+
repoPath, err := safeAbsPath(output)
70+
if err != nil {
71+
return err
72+
}
7073
args := []string{"repo", "clone", repoUrl, repoPath}
7174
_, stdErr, err := gh.Exec(args...)
7275
if stdErrString := stdErr.String(); stdErrString != "" {
@@ -75,10 +78,13 @@ func clone(output, organization string, repository string, protocol CloneProtoco
7578
return err
7679
}
7780

78-
func updateLocalClone(output, organization string, repositoryInfo github.RepositoryInfo) error {
81+
func updateLocalClone(outputPath, organization string, repositoryInfo github.RepositoryInfo) error {
7982
repository := repositoryInfo.Name
80-
repoPath := fmt.Sprintf("%s/%s", output, repository)
81-
err := fetchAllRemotes(repoPath)
83+
repoPath, err := safeAbsPath(outputPath)
84+
if err != nil {
85+
return err
86+
}
87+
err = fetchAllRemotes(repoPath)
8288
if err != nil {
8389
log.Println("[warn]", err)
8490
}
@@ -91,27 +97,32 @@ func updateLocalClone(output, organization string, repositoryInfo github.Reposit
9197
return err
9298
}
9399

94-
func fetchAllRemotes(repoPath string) error {
100+
func fetchAllRemotes(outputPath string) error {
101+
repoPath, err := safeAbsPath(outputPath)
102+
if err != nil {
103+
return err
104+
}
95105
r, err := git.PlainOpen(repoPath)
96-
var errorReturned error
97106
if err != nil {
98-
errorReturned = err
99-
} else {
100-
remotes, err := r.Remotes()
101-
if err != nil {
102-
errorReturned = err
103-
} else {
104-
var wg sync.WaitGroup
105-
wg.Add(len(remotes))
106-
for _, remote := range remotes {
107-
go func(rem *git.Remote) {
108-
defer wg.Done()
109-
log.Printf("[debug] fetching remote '%s' in %s", rem.Config().Name, repoPath)
110-
_ = rem.Fetch(&git.FetchOptions{Depth: 0, Tags: git.AllTags})
111-
}(remote)
112-
}
113-
wg.Wait()
114-
}
107+
return err
115108
}
116-
return errorReturned
109+
remotes, err := r.Remotes()
110+
if err != nil {
111+
return err
112+
}
113+
var wg sync.WaitGroup
114+
wg.Add(len(remotes))
115+
for _, remote := range remotes {
116+
go func(rem *git.Remote) {
117+
defer wg.Done()
118+
log.Printf("[debug] fetching remote '%s' in %s", rem.Config().Name, repoPath)
119+
_ = rem.Fetch(&git.FetchOptions{Depth: 0, Tags: git.AllTags})
120+
}(remote)
121+
}
122+
wg.Wait()
123+
return nil
124+
}
125+
126+
func safeAbsPath(p string) (string, error) {
127+
return filepath.Abs(filepath.FromSlash(p))
117128
}

0 commit comments

Comments
 (0)