2020-12-16 01:38:22 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2023-09-08 09:40:02 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-16 01:38:22 +08:00
|
|
|
|
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2022-09-27 04:35:59 +08:00
|
|
|
"path"
|
2020-12-16 01:38:22 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"code.gitea.io/tea/modules/config"
|
|
|
|
"code.gitea.io/tea/modules/git"
|
|
|
|
"code.gitea.io/tea/modules/utils"
|
|
|
|
|
|
|
|
gogit "github.com/go-git/go-git/v5"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2021-03-05 16:56:15 +08:00
|
|
|
var (
|
|
|
|
errNotAGiteaRepo = errors.New("No Gitea login found. You might want to specify --repo (and --login) to work outside of a repository")
|
|
|
|
)
|
|
|
|
|
2020-12-16 01:38:22 +08:00
|
|
|
// TeaContext contains all context derived during command initialization and wraps cli.Context
|
|
|
|
type TeaContext struct {
|
|
|
|
*cli.Context
|
2021-03-05 16:56:15 +08:00
|
|
|
Login *config.Login // config data & client for selected login
|
|
|
|
RepoSlug string // <owner>/<repo>, optional
|
|
|
|
Owner string // repo owner as derived from context or provided in flag, optional
|
|
|
|
Repo string // repo name as derived from context or provided in flag, optional
|
|
|
|
Output string // value of output flag
|
|
|
|
LocalRepo *git.TeaRepo // is set if flags specified a local repo via --repo, or if $PWD is a git repo
|
2020-12-16 01:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetListOptions return ListOptions based on PaginationFlags
|
|
|
|
func (ctx *TeaContext) GetListOptions() gitea.ListOptions {
|
|
|
|
page := ctx.Int("page")
|
|
|
|
limit := ctx.Int("limit")
|
2021-05-15 22:16:24 +08:00
|
|
|
if limit < 0 {
|
|
|
|
limit = 0
|
|
|
|
}
|
2020-12-16 01:38:22 +08:00
|
|
|
if limit != 0 && page == 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
return gitea.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: limit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 04:35:59 +08:00
|
|
|
// GetRemoteRepoHTMLURL returns the web-ui url of the remote repo,
|
|
|
|
// after ensuring a remote repo is present in the context.
|
|
|
|
func (ctx *TeaContext) GetRemoteRepoHTMLURL() string {
|
|
|
|
ctx.Ensure(CtxRequirement{RemoteRepo: true})
|
|
|
|
return path.Join(ctx.Login.URL, ctx.Owner, ctx.Repo)
|
|
|
|
}
|
|
|
|
|
2020-12-16 01:38:22 +08:00
|
|
|
// Ensure checks if requirements on the context are set, and terminates otherwise.
|
|
|
|
func (ctx *TeaContext) Ensure(req CtxRequirement) {
|
|
|
|
if req.LocalRepo && ctx.LocalRepo == nil {
|
|
|
|
fmt.Println("Local repository required: Execute from a repo dir, or specify a path with --repo.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.RemoteRepo && len(ctx.RepoSlug) == 0 {
|
|
|
|
fmt.Println("Remote repository required: Specify ID via --repo or execute from a local git repo.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CtxRequirement specifies context needed for operation
|
|
|
|
type CtxRequirement struct {
|
|
|
|
// ensures a local git repo is available & ctx.LocalRepo is set. Implies .RemoteRepo
|
|
|
|
LocalRepo bool
|
|
|
|
// ensures ctx.RepoSlug, .Owner, .Repo are set
|
|
|
|
RemoteRepo bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitCommand resolves the application context, and returns the active login, and if
|
|
|
|
// available the repo slug. It does this by reading the config file for logins, parsing
|
|
|
|
// the remotes of the .git repo specified in repoFlag or $PWD, and using overrides from
|
|
|
|
// command flags. If a local git repo can't be found, repo slug values are unset.
|
|
|
|
func InitCommand(ctx *cli.Context) *TeaContext {
|
|
|
|
// these flags are used as overrides to the context detection via local git repo
|
|
|
|
repoFlag := ctx.String("repo")
|
|
|
|
loginFlag := ctx.String("login")
|
|
|
|
remoteFlag := ctx.String("remote")
|
|
|
|
|
2021-03-01 06:29:26 +08:00
|
|
|
var (
|
|
|
|
c TeaContext
|
|
|
|
err error
|
|
|
|
repoPath string // empty means PWD
|
|
|
|
repoFlagPathExists bool
|
|
|
|
)
|
2020-12-16 01:38:22 +08:00
|
|
|
|
|
|
|
// check if repoFlag can be interpreted as path to local repo.
|
|
|
|
if len(repoFlag) != 0 {
|
2021-03-05 16:56:15 +08:00
|
|
|
if repoFlagPathExists, err = utils.DirExists(repoFlag); err != nil {
|
2020-12-16 01:38:22 +08:00
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
if repoFlagPathExists {
|
|
|
|
repoPath = repoFlag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 07:34:14 +08:00
|
|
|
if len(remoteFlag) == 0 {
|
|
|
|
remoteFlag = config.GetPreferences().FlagDefaults.Remote
|
|
|
|
}
|
|
|
|
|
2021-09-23 00:12:56 +08:00
|
|
|
// try to read local git repo & extract context: if repoFlag specifies a valid path, read repo in that dir,
|
|
|
|
// otherwise attempt PWD. if no repo is found, continue with default login
|
|
|
|
if c.LocalRepo, c.Login, c.RepoSlug, err = contextFromLocalRepo(repoPath, remoteFlag); err != nil {
|
|
|
|
if err == errNotAGiteaRepo || err == gogit.ErrRepositoryNotExists {
|
|
|
|
// we can deal with that, commands needing the optional values use ctx.Ensure()
|
|
|
|
} else {
|
|
|
|
log.Fatal(err.Error())
|
2021-03-01 06:29:26 +08:00
|
|
|
}
|
2020-12-16 01:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(repoFlag) != 0 && !repoFlagPathExists {
|
2021-03-01 06:29:26 +08:00
|
|
|
// if repoFlag is not a valid path, use it to override repoSlug
|
|
|
|
c.RepoSlug = repoFlag
|
2020-12-16 01:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// override login from flag, or use default login if repo based detection failed
|
|
|
|
if len(loginFlag) != 0 {
|
2021-03-01 06:29:26 +08:00
|
|
|
c.Login = config.GetLoginByName(loginFlag)
|
|
|
|
if c.Login == nil {
|
2020-12-16 01:38:22 +08:00
|
|
|
log.Fatalf("Login name '%s' does not exist", loginFlag)
|
|
|
|
}
|
2021-03-01 06:29:26 +08:00
|
|
|
} else if c.Login == nil {
|
|
|
|
if c.Login, err = config.GetDefaultLogin(); err != nil {
|
2021-07-03 22:39:05 +08:00
|
|
|
if err.Error() == "No available login" {
|
|
|
|
// TODO: maybe we can directly start interact.CreateLogin() (only if
|
|
|
|
// we're sure we can interactively!), as gh cli does.
|
|
|
|
fmt.Println(`No gitea login configured. To start using tea, first run
|
|
|
|
tea login add
|
|
|
|
and then run your command again.`)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
2020-12-16 01:38:22 +08:00
|
|
|
}
|
2021-07-03 22:39:05 +08:00
|
|
|
fmt.Printf("NOTE: no gitea login detected, falling back to login '%s'\n", c.Login.Name)
|
2020-12-16 01:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse reposlug (owner falling back to login owner if reposlug contains only repo name)
|
2021-03-01 06:29:26 +08:00
|
|
|
c.Owner, c.Repo = utils.GetOwnerAndRepo(c.RepoSlug, c.Login.User)
|
2020-12-16 01:38:22 +08:00
|
|
|
|
2021-03-01 06:29:26 +08:00
|
|
|
c.Context = ctx
|
|
|
|
c.Output = ctx.String("output")
|
|
|
|
return &c
|
2020-12-16 01:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// contextFromLocalRepo discovers login & repo slug from the default branch remote of the given local repo
|
2021-03-01 06:29:26 +08:00
|
|
|
func contextFromLocalRepo(repoPath, remoteValue string) (*git.TeaRepo, *config.Login, string, error) {
|
|
|
|
repo, err := git.RepoFromPath(repoPath)
|
2020-12-16 01:38:22 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, "", err
|
|
|
|
}
|
|
|
|
gitConfig, err := repo.Config()
|
|
|
|
if err != nil {
|
|
|
|
return repo, nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(gitConfig.Remotes) == 0 {
|
2022-03-29 08:01:37 +08:00
|
|
|
return repo, nil, "", errNotAGiteaRepo
|
2020-12-16 01:38:22 +08:00
|
|
|
}
|
|
|
|
|
2022-03-29 07:34:14 +08:00
|
|
|
// When no preferred value is given, choose a remote to find a
|
|
|
|
// matching login based on its URL.
|
|
|
|
if len(gitConfig.Remotes) > 1 && len(remoteValue) == 0 {
|
|
|
|
// if master branch is present, use it as the default remote
|
|
|
|
mainBranches := []string{"main", "master", "trunk"}
|
|
|
|
for _, b := range mainBranches {
|
|
|
|
masterBranch, ok := gitConfig.Branches[b]
|
2022-03-09 09:35:51 +08:00
|
|
|
if ok {
|
2022-03-29 07:34:14 +08:00
|
|
|
if len(masterBranch.Remote) > 0 {
|
|
|
|
remoteValue = masterBranch.Remote
|
2020-12-16 01:38:22 +08:00
|
|
|
}
|
2022-03-29 07:34:14 +08:00
|
|
|
break
|
2020-12-16 01:38:22 +08:00
|
|
|
}
|
|
|
|
}
|
2022-03-29 07:34:14 +08:00
|
|
|
// if no branch has matched, default to origin or upstream remote.
|
|
|
|
if len(remoteValue) == 0 {
|
|
|
|
if _, ok := gitConfig.Remotes["upstream"]; ok {
|
|
|
|
remoteValue = "upstream"
|
|
|
|
} else if _, ok := gitConfig.Remotes["origin"]; ok {
|
|
|
|
remoteValue = "origin"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// make sure a remote is selected
|
|
|
|
if len(remoteValue) == 0 {
|
|
|
|
for remote := range gitConfig.Remotes {
|
|
|
|
remoteValue = remote
|
|
|
|
break
|
|
|
|
}
|
2020-12-16 01:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
remoteConfig, ok := gitConfig.Remotes[remoteValue]
|
|
|
|
if !ok || remoteConfig == nil {
|
|
|
|
return repo, nil, "", fmt.Errorf("Remote '%s' not found in this Git repository", remoteValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
logins, err := config.GetLogins()
|
|
|
|
if err != nil {
|
|
|
|
return repo, nil, "", err
|
|
|
|
}
|
|
|
|
for _, l := range logins {
|
2021-09-06 18:52:34 +08:00
|
|
|
sshHost := l.GetSSHHost()
|
2020-12-16 01:38:22 +08:00
|
|
|
for _, u := range remoteConfig.URLs {
|
2021-09-06 18:52:34 +08:00
|
|
|
p, err := git.ParseURL(u)
|
2020-12-16 01:38:22 +08:00
|
|
|
if err != nil {
|
|
|
|
return repo, nil, "", fmt.Errorf("Git remote URL parse failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
if strings.EqualFold(p.Scheme, "http") || strings.EqualFold(p.Scheme, "https") {
|
|
|
|
if strings.HasPrefix(u, l.URL) {
|
|
|
|
ps := strings.Split(p.Path, "/")
|
|
|
|
path := strings.Join(ps[len(ps)-2:], "/")
|
|
|
|
return repo, &l, strings.TrimSuffix(path, ".git"), nil
|
|
|
|
}
|
|
|
|
} else if strings.EqualFold(p.Scheme, "ssh") {
|
2021-09-06 18:52:34 +08:00
|
|
|
if sshHost == p.Host {
|
|
|
|
return repo, &l, strings.TrimLeft(p.Path, "/"), nil
|
2020-12-16 01:38:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:56:15 +08:00
|
|
|
return repo, nil, "", errNotAGiteaRepo
|
2020-12-16 01:38:22 +08:00
|
|
|
}
|