mirror of
https://gitea.com/gitea/tea.git
synced 2024-12-18 15:15:05 +01:00
819cc1ab21
Adds a new subcommand to clone repos: ``` tea clone --login try --depth 1 norwin/test tea clone gitea/tea tea clone noerw/tea # will set up `master` to track `upstream` remote tea clone try.gitea.io/noerw/test # will automatically set --login ``` This is just a replacement for `git clone` with small benefits: - [x] does not depend on `git`, as tea ships with go-git - [x] spares you typing of URLs and autoselects https/ssh based on your login config - [x] forked repos: set up origin + upstream remote Co-authored-by: Norwin <git@nroo.de> Co-authored-by: techknowlogick <techknowlogick@gitea.io> Reviewed-on: https://gitea.com/gitea/tea/pulls/411 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Norwin <noerw@noreply.gitea.io> Co-committed-by: Norwin <noerw@noreply.gitea.io>
94 lines
2.2 KiB
Go
94 lines
2.2 KiB
Go
// Copyright 2021 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 task
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
"code.gitea.io/tea/modules/config"
|
|
local_git "code.gitea.io/tea/modules/git"
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
git_config "github.com/go-git/go-git/v5/config"
|
|
"github.com/go-git/go-git/v5/plumbing"
|
|
)
|
|
|
|
// RepoClone creates a local git clone in the given path, and sets up upstream remote
|
|
// for fork repos, for good usability with tea.
|
|
func RepoClone(
|
|
path string,
|
|
login *config.Login,
|
|
repoOwner, repoName string,
|
|
callback func(string) (string, error),
|
|
depth int,
|
|
) (*local_git.TeaRepo, error) {
|
|
|
|
repoMeta, _, err := login.Client().GetRepo(repoOwner, repoName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
originURL, err := cloneURL(repoMeta, login)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
auth, err := local_git.GetAuthForURL(originURL, login.Token, login.SSHKey, callback)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// default path behaviour as native git
|
|
if path == "" {
|
|
path = repoName
|
|
}
|
|
|
|
repo, err := git.PlainClone(path, false, &git.CloneOptions{
|
|
URL: originURL.String(),
|
|
Auth: auth,
|
|
Depth: depth,
|
|
InsecureSkipTLS: login.Insecure,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// set up upstream remote for forks
|
|
if repoMeta.Fork && repoMeta.Parent != nil {
|
|
upstreamURL, err := cloneURL(repoMeta.Parent, login)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
upstreamBranch := repoMeta.Parent.DefaultBranch
|
|
repo.CreateRemote(&git_config.RemoteConfig{
|
|
Name: "upstream",
|
|
URLs: []string{upstreamURL.String()},
|
|
})
|
|
repoConf, err := repo.Config()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if b, ok := repoConf.Branches[upstreamBranch]; ok {
|
|
b.Remote = "upstream"
|
|
b.Merge = plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", upstreamBranch))
|
|
}
|
|
if err = repo.SetConfig(repoConf); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return &local_git.TeaRepo{Repository: repo}, nil
|
|
}
|
|
|
|
func cloneURL(repo *gitea.Repository, login *config.Login) (*url.URL, error) {
|
|
urlStr := repo.CloneURL
|
|
if login.SSHKey != "" {
|
|
urlStr = repo.SSHURL
|
|
}
|
|
return local_git.ParseURL(urlStr)
|
|
}
|