mirror of
https://gitea.com/gitea/tea.git
synced 2024-11-23 19:11:38 +01:00
tea open: allow usage outside of git repo
when called where PWD is not a repo, first arg will be interpreted as repository slug, or a user-scope view, eg /issues or /pulls
This commit is contained in:
parent
3461d96d08
commit
ba9f927d3a
@ -199,6 +199,7 @@ func saveConfig(ymlPath string) error {
|
||||
return ioutil.WriteFile(ymlPath, bs, 0660)
|
||||
}
|
||||
|
||||
// extracts a matching login and repo slug for a given local path or PWD
|
||||
func curGitRepoPath(path string) (*Login, string, error) {
|
||||
var err error
|
||||
var repo *git.TeaRepo
|
||||
|
@ -100,7 +100,7 @@ func initCommand() (*Login, string, string) {
|
||||
}
|
||||
|
||||
if exist || len(repoValue) == 0 {
|
||||
login, repoValue, err = curGitRepoPath(repoValue)
|
||||
login, repoValue, err = curGitRepoPath("")
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
132
cmd/open.go
132
cmd/open.go
@ -7,6 +7,7 @@ package cmd
|
||||
import (
|
||||
"log"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
local_git "code.gitea.io/tea/modules/git"
|
||||
@ -22,56 +23,54 @@ var CmdOpen = cli.Command{
|
||||
Description: `Open something of the repository on web browser`,
|
||||
Action: runOpen,
|
||||
Flags: append([]cli.Flag{}, LoginRepoFlags...),
|
||||
ArgsUsage: "[<owner>/<repo>] [<issue index> | issues | pulls | releases | commits | branches | wiki | activity | settings | labels | milestones]",
|
||||
}
|
||||
|
||||
func runOpen(ctx *cli.Context) error {
|
||||
login, owner, repo := initCommand()
|
||||
login := initCommandLoginOnly()
|
||||
arg1 := ctx.Args().Get(0)
|
||||
arg2 := ctx.Args().Get(1)
|
||||
owner, repo, view, index := argsToIndexOrRepo(arg1, arg2, repoValue)
|
||||
|
||||
var suffix string
|
||||
number := ctx.Args().Get(0)
|
||||
switch {
|
||||
case strings.EqualFold(number, "issues"):
|
||||
suffix = "issues"
|
||||
case strings.EqualFold(number, "pulls"):
|
||||
suffix = "pulls"
|
||||
case strings.EqualFold(number, "releases"):
|
||||
suffix = "releases"
|
||||
case strings.EqualFold(number, "commits"):
|
||||
repo, err := local_git.RepoForWorkdir()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
// if no repo specified via args / flags, try to extract from .git in PWD
|
||||
if owner == "" {
|
||||
l, ownerAndRepo, err := curGitRepoPath(repoValue)
|
||||
if err == nil {
|
||||
owner, repo = getOwnerAndRepo(ownerAndRepo, login.User)
|
||||
login = l
|
||||
}
|
||||
b, err := repo.Head()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
name := b.Name()
|
||||
switch {
|
||||
case name.IsBranch():
|
||||
suffix = "commits/branch/" + name.Short()
|
||||
case name.IsTag():
|
||||
suffix = "commits/tag/" + name.Short()
|
||||
}
|
||||
case strings.EqualFold(number, "branches"):
|
||||
suffix = "branches"
|
||||
case strings.EqualFold(number, "wiki"):
|
||||
suffix = "wiki"
|
||||
case strings.EqualFold(number, "activity"):
|
||||
suffix = "activity"
|
||||
case strings.EqualFold(number, "settings"):
|
||||
suffix = "settings"
|
||||
case strings.EqualFold(number, "labels"):
|
||||
suffix = "labels"
|
||||
case strings.EqualFold(number, "milestones"):
|
||||
suffix = "milestones"
|
||||
case number != "":
|
||||
suffix = "issues/" + number
|
||||
default:
|
||||
suffix = number
|
||||
}
|
||||
|
||||
u := path.Join(login.URL, owner, repo, suffix)
|
||||
if owner != "" && repo != "" {
|
||||
switch {
|
||||
case strings.EqualFold(arg1, "issues"):
|
||||
view = "issues"
|
||||
case strings.EqualFold(arg1, "pulls"):
|
||||
view = "pulls"
|
||||
case strings.EqualFold(arg1, "releases"):
|
||||
view = "releases"
|
||||
case strings.EqualFold(arg1, "commits"):
|
||||
view = getCommitView()
|
||||
case strings.EqualFold(arg1, "branches"):
|
||||
view = "branches"
|
||||
case strings.EqualFold(arg1, "wiki"):
|
||||
view = "wiki"
|
||||
case strings.EqualFold(arg1, "activity"):
|
||||
view = "activity"
|
||||
case strings.EqualFold(arg1, "settings"):
|
||||
view = "settings"
|
||||
case strings.EqualFold(arg1, "labels"):
|
||||
view = "labels"
|
||||
case strings.EqualFold(arg1, "milestones"):
|
||||
view = "milestones"
|
||||
case index != "":
|
||||
view = "issues/" + index
|
||||
}
|
||||
} else if index != "" {
|
||||
log.Fatal("no repository specified")
|
||||
}
|
||||
|
||||
u := path.Join(login.URL, owner, repo, view)
|
||||
err := open.Start(u)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@ -79,3 +78,50 @@ func runOpen(ctx *cli.Context) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// parses an argument either to an issue index, or a owner/repo combination
|
||||
func argsToIndexOrRepo(arg1, arg2, overrideRepoSlug string) (owner, repo, view, idx string) {
|
||||
owner, repo = getOwnerAndRepo(overrideRepoSlug, "")
|
||||
|
||||
_, err := strconv.ParseInt(arg1, 10, 64)
|
||||
arg1IsIndex := err == nil
|
||||
_, err = strconv.ParseInt(arg2, 10, 64)
|
||||
arg2IsIndex := err == nil
|
||||
|
||||
if arg1IsIndex {
|
||||
idx = arg1
|
||||
return
|
||||
} else if repo != "" {
|
||||
view = arg1 // view was already provided by overrideRepoSlug, so we use arg1 as view param
|
||||
} else {
|
||||
owner, repo = getOwnerAndRepo(arg1, "")
|
||||
}
|
||||
|
||||
if arg2IsIndex {
|
||||
idx = arg2
|
||||
} else {
|
||||
view = arg2
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func getCommitView() string {
|
||||
repo, err := local_git.RepoForWorkdir()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
b, err := repo.Head()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
name := b.Name()
|
||||
switch {
|
||||
case name.IsBranch():
|
||||
return "commits/branch/" + name.Short()
|
||||
case name.IsTag():
|
||||
return "commits/tag/" + name.Short()
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user