Write git clone progress only if terminal is a TTY (#670)

This commit is contained in:
Markus Wolf 2021-05-06 15:55:23 +02:00 committed by GitHub
parent 37a98f134a
commit a07cb440c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 20 deletions

1
go.mod
View file

@ -20,6 +20,7 @@ require (
github.com/joho/godotenv v1.3.0 github.com/joho/godotenv v1.3.0
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/mattn/go-isatty v0.0.12
github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/go-homedir v1.1.0
github.com/moby/buildkit v0.8.1 github.com/moby/buildkit v0.8.1
github.com/moby/sys/mount v0.2.0 // indirect github.com/moby/sys/mount v0.2.0 // indirect

View file

@ -17,6 +17,7 @@ import (
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-ini/ini" "github.com/go-ini/ini"
"github.com/mattn/go-isatty"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -238,6 +239,7 @@ func CloneIfRequired(ctx context.Context, refName plumbing.ReferenceName, input
r, err := git.PlainOpen(input.Dir) r, err := git.PlainOpen(input.Dir)
if err != nil { if err != nil {
var progressWriter io.Writer var progressWriter io.Writer
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
if entry, ok := logger.(*log.Entry); ok { if entry, ok := logger.(*log.Entry); ok {
progressWriter = entry.WriterLevel(log.DebugLevel) progressWriter = entry.WriterLevel(log.DebugLevel)
} else if lgr, ok := logger.(*log.Logger); ok { } else if lgr, ok := logger.(*log.Logger); ok {
@ -246,21 +248,16 @@ func CloneIfRequired(ctx context.Context, refName plumbing.ReferenceName, input
log.Errorf("Unable to get writer from logger (type=%T)", logger) log.Errorf("Unable to get writer from logger (type=%T)", logger)
progressWriter = os.Stdout progressWriter = os.Stdout
} }
}
var cloneOptions git.CloneOptions cloneOptions := git.CloneOptions{
if input.Token != "" {
cloneOptions = git.CloneOptions{
URL: input.URL, URL: input.URL,
Progress: progressWriter, Progress: progressWriter,
Auth: &http.BasicAuth{ }
if input.Token != "" {
cloneOptions.Auth = &http.BasicAuth{
Username: "token", Username: "token",
Password: input.Token, Password: input.Token,
},
}
} else {
cloneOptions = git.CloneOptions{
URL: input.URL,
Progress: progressWriter,
} }
} }