mirror of
https://gitea.com/gitea/tea.git
synced 2024-11-23 19:11:38 +01:00
4cda7e0299
Merge branch 'master' into issue-97/pulls-clean vendor terminal dependency pull/push: provide authentication method automatically select an AuthMethod according to the remote url type. If required, credentials are prompted for login: store username & optional keyfile refactor refactor GetRemote Merge branch 'master' into issue-97/pulls-clean adress code review add --ignore-sha flag When set, the local branch is not matched against the remote sha, but the remote branch name. This makes the command more flexible with diverging branches. add missing error check fix branch-not-found case Merge branch 'master' into issue-97/pulls-clean use directory namespaces for branches & remotes fix TeaCreateBranch() improve method of TeaFindBranch() now only checking .git/refs instead of looking up .git/config which may not list the branch add `tea pulls clean` fixes #97 add copyright to new files make linter happy refactor: use new git functions for old code add `tea pulls checkout` Co-authored-by: Norwin Roosen <git@nroo.de> Co-authored-by: Norwin <git@nroo.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/105 Reviewed-by: 6543 <6543@noreply.gitea.io> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package git
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
|
|
"gopkg.in/src-d/go-git.v4"
|
|
git_config "gopkg.in/src-d/go-git.v4/config"
|
|
)
|
|
|
|
// GetRemote tries to match a Remote of the repo via the given URL.
|
|
// Matching is based on the normalized URL, accepting different protocols.
|
|
func (r TeaRepo) GetRemote(remoteURL string) (*git.Remote, error) {
|
|
repoURL, err := ParseURL(remoteURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
remotes, err := r.Remotes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, r := range remotes {
|
|
for _, u := range r.Config().URLs {
|
|
remoteURL, err := ParseURL(u)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if remoteURL.Host == repoURL.Host && remoteURL.Path == repoURL.Path {
|
|
return r, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
// GetOrCreateRemote tries to match a Remote of the repo via the given URL.
|
|
// If no match is found, a new Remote with `newRemoteName` is created.
|
|
// Matching is based on the normalized URL, accepting different protocols.
|
|
func (r TeaRepo) GetOrCreateRemote(remoteURL, newRemoteName string) (*git.Remote, error) {
|
|
localRemote, err := r.GetRemote(remoteURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// if no match found, create a new remote
|
|
if localRemote == nil {
|
|
localRemote, err = r.CreateRemote(&git_config.RemoteConfig{
|
|
Name: newRemoteName,
|
|
URLs: []string{remoteURL},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return localRemote, nil
|
|
}
|
|
|
|
// TeaRemoteURL returns the first url entry for the given remote name
|
|
func (r TeaRepo) TeaRemoteURL(name string) (auth *url.URL, err error) {
|
|
remote, err := r.Remote(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
urls := remote.Config().URLs
|
|
if len(urls) == 0 {
|
|
return nil, fmt.Errorf("remote %s has no URL configured", name)
|
|
}
|
|
return ParseURL(remote.Config().URLs[0])
|
|
}
|