This keeps git operations working even when PATH is modified, e.g. `brew
update` will work even though Homebrew runs the command explicitly
without `/usr/local/bin` in PATH.
Additionally, this inserts a blank value for `credential.*.helper` to
instruct git to ignore previously configured credential helpers, i.e.
those that might have been set up in system configuration files. We do
this because otherwise, git will store the credential obtained from gh
in every other credential helper in the chain, which we want to avoid.
Before:
git config --global credential.https://github.com.helper '!gh auth git-credential'
After:
git config --global credential.https://github.com.helper ''
git config --global --add credential.https://github.com.helper '!/path/to/gh auth git-credential'
80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
package factory
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/cli/cli/git"
|
|
"github.com/cli/cli/internal/config"
|
|
"github.com/cli/cli/internal/ghinstance"
|
|
"github.com/cli/cli/internal/ghrepo"
|
|
"github.com/cli/cli/pkg/cmdutil"
|
|
"github.com/cli/cli/pkg/iostreams"
|
|
)
|
|
|
|
func New(appVersion string) *cmdutil.Factory {
|
|
io := iostreams.System()
|
|
|
|
var cachedConfig config.Config
|
|
var configError error
|
|
configFunc := func() (config.Config, error) {
|
|
if cachedConfig != nil || configError != nil {
|
|
return cachedConfig, configError
|
|
}
|
|
cachedConfig, configError = config.ParseDefaultConfig()
|
|
if errors.Is(configError, os.ErrNotExist) {
|
|
cachedConfig = config.NewBlankConfig()
|
|
configError = nil
|
|
}
|
|
cachedConfig = config.InheritEnv(cachedConfig)
|
|
return cachedConfig, configError
|
|
}
|
|
|
|
hostOverride := ""
|
|
if !strings.EqualFold(ghinstance.Default(), ghinstance.OverridableDefault()) {
|
|
hostOverride = ghinstance.OverridableDefault()
|
|
}
|
|
|
|
rr := &remoteResolver{
|
|
readRemotes: git.Remotes,
|
|
getConfig: configFunc,
|
|
}
|
|
remotesFunc := rr.Resolver(hostOverride)
|
|
|
|
ghExecutable := "gh"
|
|
if exe, err := os.Executable(); err == nil {
|
|
ghExecutable = exe
|
|
}
|
|
|
|
return &cmdutil.Factory{
|
|
IOStreams: io,
|
|
Config: configFunc,
|
|
Remotes: remotesFunc,
|
|
HttpClient: func() (*http.Client, error) {
|
|
cfg, err := configFunc()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return NewHTTPClient(io, cfg, appVersion, true), nil
|
|
},
|
|
BaseRepo: func() (ghrepo.Interface, error) {
|
|
remotes, err := remotesFunc()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return remotes[0], nil
|
|
},
|
|
Branch: func() (string, error) {
|
|
currentBranch, err := git.CurrentBranch()
|
|
if err != nil {
|
|
return "", fmt.Errorf("could not determine current branch: %w", err)
|
|
}
|
|
return currentBranch, nil
|
|
},
|
|
Executable: ghExecutable,
|
|
}
|
|
}
|