tea pr checkout: dont create local branches #314

Merged
lunny merged 8 commits from noerw/tea:checkout-use-remote-tracking-branch into master 2021-03-02 13:50:12 +00:00
4 changed files with 64 additions and 24 deletions
Showing only changes of commit 1eba3345e3 - Show all commits

View File

@ -6,6 +6,7 @@ package cmd
import (
"fmt"
"io/ioutil"
"strings"
"code.gitea.io/tea/modules/interact"
@ -45,12 +46,23 @@ func runAddComment(cmd *cli.Context) error {
}
body := strings.Join(ctx.Args().Tail(), " ")
if len(body) == 0 {
if interact.IsStdinPiped() {
// custom solution until https://github.com/AlecAivazis/survey/issues/328 is fixed
if bodyStdin, err := ioutil.ReadAll(ctx.App.Reader); err != nil {
return err
} else if len(bodyStdin) != 0 {
body = strings.Join([]string{body, string(bodyStdin)}, "\n\n")
}
} else if len(body) == 0 {
if body, err = interact.PromptMultiline("Content"); err != nil {
return err
}
}
if len(body) == 0 {
return fmt.Errorf("No comment body provided")
}
client := ctx.Login.Client()
comment, _, err := client.CreateIssueComment(ctx.Owner, ctx.Repo, idx, gitea.CreateIssueCommentOption{
Body: body,

View File

@ -75,13 +75,16 @@ func InitCommand(ctx *cli.Context) *TeaContext {
loginFlag := ctx.String("login")
remoteFlag := ctx.String("remote")
var repoSlug string
var repoPath string // empty means PWD
var repoFlagPathExists bool
var (
c TeaContext
err error
repoPath string // empty means PWD
repoFlagPathExists bool
)
// check if repoFlag can be interpreted as path to local repo.
if len(repoFlag) != 0 {
repoFlagPathExists, err := utils.PathExists(repoFlag)
repoFlagPathExists, err = utils.DirExists(repoFlag)
if err != nil {
log.Fatal(err.Error())
}
@ -90,38 +93,42 @@ func InitCommand(ctx *cli.Context) *TeaContext {
}
}
// try to read git repo & extract context, ignoring if PWD is not a repo
localRepo, login, repoSlug, err := contextFromLocalRepo(repoPath, remoteFlag)
if err != nil && err != gogit.ErrRepositoryNotExists {
log.Fatal(err.Error())
if len(repoFlag) == 0 || repoFlagPathExists {
// try to read git repo & extract context, ignoring if PWD is not a repo
c.LocalRepo, c.Login, c.RepoSlug, err = contextFromLocalRepo(repoPath, remoteFlag)
if err != nil && err != gogit.ErrRepositoryNotExists {
log.Fatal(err.Error())
}
}
// if repoFlag is not a path, use it to override repoSlug
if len(repoFlag) != 0 && !repoFlagPathExists {
repoSlug = repoFlag
// if repoFlag is not a valid path, use it to override repoSlug
c.RepoSlug = repoFlag
}
// override login from flag, or use default login if repo based detection failed
if len(loginFlag) != 0 {
login = config.GetLoginByName(loginFlag)
if login == nil {
c.Login = config.GetLoginByName(loginFlag)
if c.Login == nil {
log.Fatalf("Login name '%s' does not exist", loginFlag)
}
} else if login == nil {
if login, err = config.GetDefaultLogin(); err != nil {
} else if c.Login == nil {
if c.Login, err = config.GetDefaultLogin(); err != nil {
log.Fatal(err.Error())
}
}
// parse reposlug (owner falling back to login owner if reposlug contains only repo name)
owner, reponame := utils.GetOwnerAndRepo(repoSlug, login.User)
c.Owner, c.Repo = utils.GetOwnerAndRepo(c.RepoSlug, c.Login.User)
return &TeaContext{ctx, login, repoSlug, owner, reponame, ctx.String("output"), localRepo}
c.Context = ctx
c.Output = ctx.String("output")
return &c
}
// contextFromLocalRepo discovers login & repo slug from the default branch remote of the given local repo
func contextFromLocalRepo(repoValue, remoteValue string) (*git.TeaRepo, *config.Login, string, error) {
repo, err := git.RepoFromPath(repoValue)
func contextFromLocalRepo(repoPath, remoteValue string) (*git.TeaRepo, *config.Login, string, error) {
repo, err := git.RepoFromPath(repoPath)
if err != nil {
return nil, nil, "", err
}

View File

@ -26,7 +26,7 @@ func ShowCommentsMaybeInteractive(ctx *context.TeaContext, idx int64, totalComme
return err
}
print.Comments(comments)
} else if isInteractive() && !ctx.IsSet("comments") {
} else if IsInteractive() && !ctx.IsSet("comments") {
// if we're interactive, but --comments hasn't been explicitly set to false
if err := ShowCommentsPaginated(ctx, idx, totalComments); err != nil {
fmt.Printf("error while loading comments: %v\n", err)
@ -70,6 +70,11 @@ func ShowCommentsPaginated(ctx *context.TeaContext, idx int64, totalComments int
}
// IsInteractive checks if the output is piped, but NOT if the session is run interactively..
func isInteractive() bool {
func IsInteractive() bool {
return terminal.IsTerminal(int(os.Stdout.Fd()))
}
// IsStdinPiped checks if stdin is piped
func IsStdinPiped() bool {
return !terminal.IsTerminal(int(os.Stdin.Fd()))
}

View File

@ -26,15 +26,31 @@ func PathExists(path string) (bool, error) {
// FileExist returns whether the given file exists or not
func FileExist(fileName string) (bool, error) {
f, err := os.Stat(fileName)
return exists(fileName, false)
}
// DirExists returns whether the given file exists or not
func DirExists(path string) (bool, error) {
return exists(path, true)
}
func exists(path string, expectDir bool) (bool, error) {
f, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
return false, nil
} else if err.(*os.PathError).Err.Error() == "not a directory" {
// some middle segment of path is a file, cannot traverse
// FIXME: catches error on linux; go does not provide a way to catch this properly..
return false, nil
}
return false, err
}
if f.IsDir() {
isDir := f.IsDir()
if isDir && !expectDir {
return false, errors.New("A directory with the same name exists")
} else if !isDir && expectDir {
return false, errors.New("A file with the same name exists")
}
return true, nil
}