diff --git a/bitbucket/bitbucket.go b/bitbucket/bitbucket.go index aecaab8..d3f6ff9 100644 --- a/bitbucket/bitbucket.go +++ b/bitbucket/bitbucket.go @@ -386,3 +386,7 @@ func (s *bitbucketRepoService) ActionsURL(repoHTMLURL string) string { func (s *bitbucketRepoService) ReleasesURL(repoHTMLURL string) string { return repoHTMLURL + "/downloads" } + +func (s *bitbucketRepoService) BlobURL(repoHTMLURL, ref, path string) string { + return repoHTMLURL + "/src/" + ref + "/" + path +} diff --git a/bitbucket/urls_test.go b/bitbucket/urls_test.go new file mode 100644 index 0000000..2e3b638 --- /dev/null +++ b/bitbucket/urls_test.go @@ -0,0 +1,12 @@ +package bitbucket + +import "testing" + +func TestBitbucketRepoBlobURL(t *testing.T) { + s := &bitbucketRepoService{} + got := s.BlobURL("https://bitbucket.org/owner/repo", "master", "README.md") + want := "https://bitbucket.org/owner/repo/src/master/README.md" + if got != want { + t.Errorf("BlobURL = %q, want %q", got, want) + } +} diff --git a/forges_test.go b/forges_test.go index cae5fc9..f903b82 100644 --- a/forges_test.go +++ b/forges_test.go @@ -618,6 +618,10 @@ func (m *mockRepoService) ReleasesURL(repoHTMLURL string) string { return repoHTMLURL + "/releases" } +func (m *mockRepoService) BlobURL(repoHTMLURL, ref, path string) string { + return repoHTMLURL + "/blob/" + ref + "/" + path +} + type mockIssueService struct { issue *Issue issues []Issue diff --git a/gitea/gitea.go b/gitea/gitea.go index 07e01b6..62d62d9 100644 --- a/gitea/gitea.go +++ b/gitea/gitea.go @@ -416,4 +416,8 @@ func (s *giteaRepoService) ReleasesURL(repoHTMLURL string) string { return repoHTMLURL + "/releases" } +func (s *giteaRepoService) BlobURL(repoHTMLURL, ref, path string) string { + return repoHTMLURL + "/src/branch/" + ref + "/" + path +} + func boolPtr(b bool) *bool { return &b } diff --git a/gitea/urls_test.go b/gitea/urls_test.go new file mode 100644 index 0000000..5ab1719 --- /dev/null +++ b/gitea/urls_test.go @@ -0,0 +1,12 @@ +package gitea + +import "testing" + +func TestGiteaRepoBlobURL(t *testing.T) { + s := &giteaRepoService{} + got := s.BlobURL("https://codeberg.org/owner/repo", "main", "README.md") + want := "https://codeberg.org/owner/repo/src/branch/main/README.md" + if got != want { + t.Errorf("BlobURL = %q, want %q", got, want) + } +} diff --git a/github/github.go b/github/github.go index 2430ea5..bb7299a 100644 --- a/github/github.go +++ b/github/github.go @@ -445,3 +445,7 @@ func (s *gitHubRepoService) ActionsURL(repoHTMLURL string) string { func (s *gitHubRepoService) ReleasesURL(repoHTMLURL string) string { return repoHTMLURL + "/releases" } + +func (s *gitHubRepoService) BlobURL(repoHTMLURL, ref, path string) string { + return repoHTMLURL + "/blob/" + ref + "/" + path +} diff --git a/github/urls_test.go b/github/urls_test.go new file mode 100644 index 0000000..b5628a1 --- /dev/null +++ b/github/urls_test.go @@ -0,0 +1,12 @@ +package github + +import "testing" + +func TestGitHubRepoBlobURL(t *testing.T) { + s := &gitHubRepoService{} + got := s.BlobURL("https://github.com/owner/repo", "main", "cmd/main.go") + want := "https://github.com/owner/repo/blob/main/cmd/main.go" + if got != want { + t.Errorf("BlobURL = %q, want %q", got, want) + } +} diff --git a/gitlab/gitlab.go b/gitlab/gitlab.go index f42c730..7847d1d 100644 --- a/gitlab/gitlab.go +++ b/gitlab/gitlab.go @@ -432,3 +432,7 @@ func (s *gitLabRepoService) ActionsURL(repoHTMLURL string) string { func (s *gitLabRepoService) ReleasesURL(repoHTMLURL string) string { return repoHTMLURL + "/-/releases" } + +func (s *gitLabRepoService) BlobURL(repoHTMLURL, ref, path string) string { + return repoHTMLURL + "/-/blob/" + ref + "/" + path +} diff --git a/gitlab/urls_test.go b/gitlab/urls_test.go new file mode 100644 index 0000000..01f1084 --- /dev/null +++ b/gitlab/urls_test.go @@ -0,0 +1,12 @@ +package gitlab + +import "testing" + +func TestGitLabRepoBlobURL(t *testing.T) { + s := &gitLabRepoService{} + got := s.BlobURL("https://gitlab.com/group/project", "main", "src/app.go") + want := "https://gitlab.com/group/project/-/blob/main/src/app.go" + if got != want { + t.Errorf("BlobURL = %q, want %q", got, want) + } +} diff --git a/internal/cli/browse.go b/internal/cli/browse.go index bc5c7ff..00fda81 100644 --- a/internal/cli/browse.go +++ b/internal/cli/browse.go @@ -56,7 +56,7 @@ var browseCmd = &cobra.Command{ if branch == "" { branch = repo.DefaultBranch } - url = repoURL + fmt.Sprintf("/blob/%s/%s", branch, args[0]) + url = forge.Repos().BlobURL(repoURL, branch, args[0]) } } else { url = repoURL diff --git a/services.go b/services.go index 11893d2..412bfb1 100644 --- a/services.go +++ b/services.go @@ -23,6 +23,7 @@ type RepoService interface { WikiURL(repoHTMLURL string) string ActionsURL(repoHTMLURL string) string ReleasesURL(repoHTMLURL string) string + BlobURL(repoHTMLURL, ref, path string) string } // PullRequestService provides operations on pull requests (merge requests on GitLab).