diff --git a/main.go b/main.go index a87db50..e19bf1c 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/main_test.go b/main_test.go index 8d8ae25..f24ce73 100644 --- a/main_test.go +++ b/main_test.go @@ -60,7 +60,7 @@ func Test_getProjectDir(t *testing.T) { repository: "ssh://user@host.xz: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 { @@ -144,14 +144,14 @@ func Test_normalize(t *testing.T) { args: args{ repository: "ssh://user@host.xz: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: "git@git.sr.ht:~libreboot/lbmk", }, - wantRepo: "git.sr.ht/~libreboot/lbmk", + wantRepo: "git.sr.ht/libreboot/lbmk", }, } for _, tt := range tests {