github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/remote.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package git 7 8 import ( 9 "context" 10 11 giturl "github.com/gitbundle/modules/git/url" 12 ) 13 14 // GetRemoteAddress returns remote url of git repository in the repoPath with special remote name 15 func GetRemoteAddress(ctx context.Context, repoPath, remoteName string) (string, error) { 16 var cmd *Command 17 if CheckGitVersionAtLeast("2.7") == nil { 18 cmd = NewCommand(ctx, "remote", "get-url", remoteName) 19 } else { 20 cmd = NewCommand(ctx, "config", "--get", "remote."+remoteName+".url") 21 } 22 23 result, _, err := cmd.RunStdString(&RunOpts{Dir: repoPath}) 24 if err != nil { 25 return "", err 26 } 27 28 if len(result) > 0 { 29 result = result[:len(result)-1] 30 } 31 return result, nil 32 } 33 34 // GetRemoteURL returns the url of a specific remote of the repository. 35 func GetRemoteURL(ctx context.Context, repoPath, remoteName string) (*giturl.GitURL, error) { 36 addr, err := GetRemoteAddress(ctx, repoPath, remoteName) 37 if err != nil { 38 return nil, err 39 } 40 return giturl.Parse(addr) 41 }