mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-10 13:37:08 +08:00
gitutil: find default remote by tracking branch
Using this command resolves remote based on remote tracking branch of the curently selected branch and should be more precise in case we can't predict if user uses origin to mark upstream or their fork. Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
@ -78,7 +78,13 @@ func (c *Git) GitDir() (string, error) {
|
||||
}
|
||||
|
||||
func (c *Git) RemoteURL() (string, error) {
|
||||
// Try to get the remote URL from the origin remote first
|
||||
// Try default remote based on remote tracking branch
|
||||
if remote, err := c.currentRemote(); err == nil && remote != "" {
|
||||
if ru, err := c.clean(c.run("remote", "get-url", remote)); err == nil && ru != "" {
|
||||
return stripCredentials(ru), nil
|
||||
}
|
||||
}
|
||||
// Next try to get the remote URL from the origin remote first
|
||||
if ru, err := c.clean(c.run("remote", "get-url", "origin")); err == nil && ru != "" {
|
||||
return stripCredentials(ru), nil
|
||||
}
|
||||
@ -149,6 +155,22 @@ func (c *Git) clean(out string, err error) (string, error) {
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (c *Git) currentRemote() (string, error) {
|
||||
symref, err := c.clean(c.run("symbolic-ref", "-q", "HEAD"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if symref == "" {
|
||||
return "", nil
|
||||
}
|
||||
// git for-each-ref --format='%(upstream:remotename)'
|
||||
remote, err := c.clean(c.run("for-each-ref", "--format=%(upstream:remotename)", symref))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return remote, nil
|
||||
}
|
||||
|
||||
func IsUnknownRevision(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
|
Reference in New Issue
Block a user