Skip to content

Commit

Permalink
feat: clean ~ from path
Browse files Browse the repository at this point in the history
  • Loading branch information
juev committed Apr 28, 2024
1 parent bc87c2b commit 23c5909
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ func main() {
func normalize(repo string) string {
r := regexp.MustCompile(`^(?:.*://)?(?:[^@]+@)?([^:/]+)(?::\d+)?(?:/|:)?(.*)$`)
match := r.FindStringSubmatch(repo)
if len(match) == 3 {
return match[1] + "/" + strings.TrimSuffix(strings.TrimSuffix(match[2], ".git"), "/")
if len(match) != 3 {
return ""
}
return ""
path := match[2]
path = strings.TrimSuffix(path, "/")
path = strings.TrimSuffix(path, ".git")
path = strings.TrimPrefix(path, "~")
return filepath.Join(match[1], path)
}

// getProjectDir return directory from GIT_PROJECT_DIR variable and
Expand Down
6 changes: 3 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func Test_getProjectDir(t *testing.T) {
repository: "ssh://[email protected]:443/~user/path/to/repo.git/",
homeVar: "/home/test",
gitProjectDir: "src",
want: "src/host.xz/~user/path/to/repo.git",
want: "src/host.xz/user/path/to/repo",
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -144,14 +144,14 @@ func Test_normalize(t *testing.T) {
args: args{
repository: "ssh://[email protected]:443/~user/path/to/repo.git/",
},
wantRepo: "host.xz/~user/path/to/repo.git",
wantRepo: "host.xz/user/path/to/repo",
},
{
name: "git",
args: args{
repository: "[email protected]:~libreboot/lbmk",
},
wantRepo: "git.sr.ht/~libreboot/lbmk",
wantRepo: "git.sr.ht/libreboot/lbmk",
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 23c5909

Please sign in to comment.