From 72c757b7a44dc5d19d821f7c2fa36bb082e4f2e3 Mon Sep 17 00:00:00 2001 From: Sandro Jäckel Date: Mon, 17 Apr 2023 13:28:47 +0200 Subject: Save rtree with go 1.20 --- internal/rtree/remote.go | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) (limited to 'internal/rtree/remote.go') diff --git a/internal/rtree/remote.go b/internal/rtree/remote.go index 8435979..2973b8c 100644 --- a/internal/rtree/remote.go +++ b/internal/rtree/remote.go @@ -25,18 +25,18 @@ import ( "strings" ) -//RemoteURL is the URL of a remote of a Git repository. +// RemoteURL is the URL of a remote of a Git repository. type RemoteURL string -//ParseRemoteURL parses the given remote URL by substituting aliases defined in -//the system-wide and user-global Git config. For example, with +// ParseRemoteURL parses the given remote URL by substituting aliases defined in +// the system-wide and user-global Git config. For example, with // -// $ cat /etc/gitconfig -// [url "git://github.com/"] -// insteadOf = gh: +// $ cat /etc/gitconfig +// [url "git://github.com/"] +// insteadOf = gh: // -//and the input "gh:foo/bar", the result has a canonical URL of -//"git://github.com/foo/bar". +// and the input "gh:foo/bar", the result has a canonical URL of +// "git://github.com/foo/bar". func ParseRemoteURL(input string) RemoteURL { var best *RemoteAlias for _, current := range RemoteAliases { @@ -52,15 +52,15 @@ func ParseRemoteURL(input string) RemoteURL { return RemoteURL(best.Replacement + strings.TrimPrefix(input, best.Alias)) } -//CanonicalURL returns the URL where the remote will be fetched from. +// CanonicalURL returns the URL where the remote will be fetched from. func (u RemoteURL) CanonicalURL() string { return string(u) } -//CompactURL returns the most compact representation of this remote URL, -//obtained by substituting the longest matching alias defined in the -//system-wide or user-global Git config. This function is mostly the reverse of -//ParseRemoteURL(). +// CompactURL returns the most compact representation of this remote URL, +// obtained by substituting the longest matching alias defined in the +// system-wide or user-global Git config. This function is mostly the reverse of +// ParseRemoteURL(). func (u RemoteURL) CompactURL() string { var best *RemoteAlias for _, current := range RemoteAliases { @@ -76,16 +76,15 @@ func (u RemoteURL) CompactURL() string { return best.Alias + strings.TrimPrefix(string(u), best.Replacement) } -//This regex recognizes the scp-like syntax for git remotes -//(i.e. "[user@]example.org:path/to/repo") as specified by the "GIT URLS" -//section of man:git-clone(1). +// This regex recognizes the scp-like syntax for git remotes +// (i.e. "[user@]example.org:path/to/repo") as specified by the "GIT URLS" +// section of man:git-clone(1). var scpSyntaxRx = regexp.MustCompile(`^(?:[^/@:]+@)?([^/:]+\.[^/:]+):(.+)$`) -//CheckoutPath derives the checkout path for a remote URL. -// -// RemoteURL("https://example.org/foo/bar") -> "example.org/foo/bar" -// RemoteURL("git@example.org:foo/bar.git") -> "example.org/foo/bar" +// CheckoutPath derives the checkout path for a remote URL. // +// RemoteURL("https://example.org/foo/bar") -> "example.org/foo/bar" +// RemoteURL("git@example.org:foo/bar.git") -> "example.org/foo/bar" func (u RemoteURL) CheckoutPath() (string, error) { stripped := strings.TrimSuffix(u.CanonicalURL(), ".git") @@ -102,13 +101,13 @@ func (u RemoteURL) CheckoutPath() (string, error) { return filepath.Join(parsed.Hostname(), parsed.Path), nil } -//MarshalYAML implements the yaml.Marshaler interface. +// MarshalYAML implements the yaml.Marshaler interface. func (u RemoteURL) MarshalYAML() (interface{}, error) { //store URLs in the index in the compact format return u.CompactURL(), nil } -//UnmarshalYAML implements the yaml.Unmarshaler interface. +// UnmarshalYAML implements the yaml.Unmarshaler interface. func (u *RemoteURL) UnmarshalYAML(unmarshal func(interface{}) error) error { var s string err := unmarshal(&s) -- cgit v1.3.1 From d0059fcb87312d296a31d6965d6db84e499fe020 Mon Sep 17 00:00:00 2001 From: Sandro Jäckel Date: Mon, 17 Apr 2023 14:43:15 +0200 Subject: Only use compact URL in UI, not in written files many tools like eg some vscode extensions can parse the git remotes but do not respect git aliases and fail to understand shorthands like gh:majewsky/gofu --- internal/rtree/get_test.go | 8 ++++---- internal/rtree/remote.go | 2 +- internal/rtree/repo.go | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) (limited to 'internal/rtree/remote.go') diff --git a/internal/rtree/get_test.go b/internal/rtree/get_test.go index a438234..789e903 100644 --- a/internal/rtree/get_test.go +++ b/internal/rtree/get_test.go @@ -35,7 +35,7 @@ var testIndexWithTwoRepos = Index{ { CheckoutPath: "github.com/git/git", Remotes: []Remote{ - {Name: "origin", URL: "gh:git/git"}, + {Name: "origin", URL: "https://github.com/git/git"}, }, }, }, @@ -65,14 +65,14 @@ func TestGetNewRepo(t *testing.T) { Args: []string{"get", remoteURL}, Index: testIndexWithTwoRepos, ExpectOutput: target + "\n", - ExpectExecution: Recorded("git clone gh:another/repo " + target), + ExpectExecution: Recorded("git clone https://github.com/another/repo " + target), ExpectIndex: &Index{ Repos: []*Repo{ { CheckoutPath: "github.com/another/repo", Remotes: []Remote{ //regardless of the remote URL used, we expect the contracted form to be used - {Name: "origin", URL: "gh:another/repo"}, + {Name: "origin", URL: "https://github.com/another/repo"}, }, }, testIndexWithTwoRepos.Repos[0], @@ -106,7 +106,7 @@ func TestGetNewForkAsRemote(t *testing.T) { { CheckoutPath: "github.com/git/git", Remotes: []Remote{ - {Name: "origin", URL: "gh:git/git"}, + {Name: "origin", URL: "https://github.com/git/git"}, {Name: "myfork", URL: "https://example.com/git"}, }, }, diff --git a/internal/rtree/remote.go b/internal/rtree/remote.go index 2973b8c..36e12dd 100644 --- a/internal/rtree/remote.go +++ b/internal/rtree/remote.go @@ -104,7 +104,7 @@ func (u RemoteURL) CheckoutPath() (string, error) { // MarshalYAML implements the yaml.Marshaler interface. func (u RemoteURL) MarshalYAML() (interface{}, error) { //store URLs in the index in the compact format - return u.CompactURL(), nil + return u.CanonicalURL(), nil } // UnmarshalYAML implements the yaml.Unmarshaler interface. diff --git a/internal/rtree/repo.go b/internal/rtree/repo.go index 5cdd39f..2267666 100644 --- a/internal/rtree/repo.go +++ b/internal/rtree/repo.go @@ -153,7 +153,7 @@ func (r Repo) Checkout() error { cli.Interface.ShowWarning(`will not checkout anything since there is no remote named "origin"`) } else { err := cli.Interface.Run(cli.Command{ - Program: []string{"git", "clone", originURL.CompactURL(), r.AbsolutePath()}, + Program: []string{"git", "clone", originURL.CanonicalURL(), r.AbsolutePath()}, }) if err != nil { return err @@ -164,7 +164,7 @@ func (r Repo) Checkout() error { for _, remote := range r.Remotes { if remote.Name != "origin" { err := cli.Interface.Run(cli.Command{ - Program: []string{"git", "remote", "add", remote.Name, remote.URL.CompactURL()}, + Program: []string{"git", "remote", "add", remote.Name, remote.URL.CanonicalURL()}, WorkDir: r.AbsolutePath(), }) if err != nil { @@ -234,7 +234,7 @@ func (r *Repo) Move(checkoutPath string, makeSymlink bool) error { func (r Repo) ReformatRemoteURLs() error { for _, remote := range r.Remotes { err := cli.Interface.Run(cli.Command{ - Program: []string{"git", "remote", "set-url", remote.Name, remote.URL.CompactURL()}, + Program: []string{"git", "remote", "set-url", remote.Name, remote.URL.CanonicalURL()}, WorkDir: r.AbsolutePath(), }) if err != nil { -- cgit v1.3.1