feat(git): add remote URL for hyperlink

This commit is contained in:
Jan De Dobbeleer 2022-01-14 22:38:05 +01:00 committed by Jan De Dobbeleer
parent 37e96ef968
commit d71422a35b
3 changed files with 30 additions and 10 deletions

View file

@ -20,6 +20,11 @@ This can be used in templates and icons/text inside your config.
The engine has the ability to render hyperlinks. Your terminal has to support it and the option
has to be enabled at the segment level. Hyperlink generation is disabled by default.
When using in a template, the syntax is like markdown:
- url: `[text](https://link)`
- file: `[file](file://path)`
### Supported segments
- [Path][path-segment]

View file

@ -106,6 +106,7 @@ You can set the following properties to `true` to enable fetching additional inf
- `.BranchStatus`: `string` - the current branch context (ahead/behind string representation)
- `.Upstream`: `string` - the upstream name (remote)
- `.UpstreamIcon`: `string` - the upstream icon (based on the icons above)
- `.UpstreamURL`: `string` - the upstream URL for use in [hyperlinks][hyperlinks] in templates: `[{{ .UpstreamIcon }} {{ .HEAD }}]({{ .UpstreamURL }})`
- `.StashCount`: `int` - the stash count
- `.WorktreeCount`: `int` - the worktree count
- `.IsWorkTree`: `boolean` - if in a worktree repo or not
@ -122,3 +123,4 @@ You can set the following properties to `true` to enable fetching additional inf
[poshgit]: /docs/poshgit
[go-text-template]: https://golang.org/pkg/text/template/
[sprig]: https://masterminds.github.io/sprig/
[hyperlinks]: /docs/config-text-style#hyperlinks

View file

@ -41,6 +41,7 @@ type git struct {
BranchStatus string
Upstream string
UpstreamIcon string
UpstreamURL string
StashCount int
WorktreeCount int
IsWorkTree bool
@ -230,17 +231,17 @@ func (g *git) setBranchStatus() {
func (g *git) getUpstreamIcon() string {
upstream := replaceAllString("/.*", g.Upstream, "")
url := g.getOriginURL(upstream)
if strings.Contains(url, "github") {
g.UpstreamURL = g.getOriginURL(upstream)
if strings.Contains(g.UpstreamURL, "github") {
return g.props.getString(GithubIcon, "\uF408 ")
}
if strings.Contains(url, "gitlab") {
if strings.Contains(g.UpstreamURL, "gitlab") {
return g.props.getString(GitlabIcon, "\uF296 ")
}
if strings.Contains(url, "bitbucket") {
if strings.Contains(g.UpstreamURL, "bitbucket") {
return g.props.getString(BitbucketIcon, "\uF171 ")
}
if strings.Contains(url, "dev.azure.com") || strings.Contains(url, "visualstudio.com") {
if strings.Contains(g.UpstreamURL, "dev.azure.com") || strings.Contains(g.UpstreamURL, "visualstudio.com") {
return g.props.getString(AzureDevOpsIcon, "\uFD03 ")
}
return g.props.getString(GitIcon, "\uE5FB ")
@ -496,15 +497,27 @@ func (g *git) getWorktreeContext() int {
}
func (g *git) getOriginURL(upstream string) string {
cleanSSHURL := func(url string) string {
if strings.HasPrefix(url, "http") {
return url
}
url = strings.TrimPrefix(url, "git://")
url = strings.TrimPrefix(url, "git@")
url = strings.TrimSuffix(url, ".git")
url = strings.ReplaceAll(url, ":", "/")
return fmt.Sprintf("https://%s", url)
}
var url string
cfg, err := ini.Load(g.gitRootFolder + "/config")
if err != nil {
return g.getGitCommandOutput("remote", "get-url", upstream)
url = g.getGitCommandOutput("remote", "get-url", upstream)
return cleanSSHURL(url)
}
keyVal := cfg.Section("remote \"" + upstream + "\"").Key("url").String()
if keyVal == "" {
return g.getGitCommandOutput("remote", "get-url", upstream)
url = cfg.Section("remote \"" + upstream + "\"").Key("url").String()
if url == "" {
url = g.getGitCommandOutput("remote", "get-url", upstream)
}
return keyVal
return cleanSSHURL(url)
}
func (g *git) convertToWindowsPath(path string) string {