Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 44 additions & 28 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"sort"
"strings"

"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/git"
Expand Down Expand Up @@ -85,36 +86,18 @@ func (r *ResolvedRemotes) BaseRepo(io *iostreams.IOStreams, p iprompter) (ghrepo
return r.remotes[0], nil
}

// from here on, consult the API
if r.network == nil {
err := resolveNetwork(r)
if err != nil {
return nil, err
}
repos, err := r.NetworkRepos()
if err != nil {
return nil, err
}

var repoNames []string
repoMap := map[string]*api.Repository{}
add := func(r *api.Repository) {
fn := ghrepo.FullName(r)
if _, ok := repoMap[fn]; !ok {
repoMap[fn] = r
repoNames = append(repoNames, fn)
}
}

for _, repo := range r.network.Repositories {
if repo == nil {
continue
}
if repo.Parent != nil {
add(repo.Parent)
}
add(repo)
if len(repos) == 0 {
return r.remotes[0], nil
}

if len(repoNames) == 0 {
return r.remotes[0], nil
var repoNames []string
for _, r := range repos {
repoNames = append(repoNames, ghrepo.FullName(r))
}

baseName := repoNames[0]
Expand All @@ -130,7 +113,8 @@ func (r *ResolvedRemotes) BaseRepo(io *iostreams.IOStreams, p iprompter) (ghrepo
}

// determine corresponding git remote
selectedRepo := repoMap[baseName]
owner, repo, _ := strings.Cut(baseName, "/")
selectedRepo := ghrepo.New(owner, repo)
resolution := "base"
remote, _ := r.RemoteForRepo(selectedRepo)
if remote == nil {
Expand All @@ -140,7 +124,7 @@ func (r *ResolvedRemotes) BaseRepo(io *iostreams.IOStreams, p iprompter) (ghrepo

// cache the result to git config
c := &git.Client{}
err := c.SetRemoteResolution(context.Background(), remote.Name, resolution)
err = c.SetRemoteResolution(context.Background(), remote.Name, resolution)
return selectedRepo, err
}

Expand All @@ -161,6 +145,38 @@ func (r *ResolvedRemotes) HeadRepos() ([]*api.Repository, error) {
return results, nil
}

func (r *ResolvedRemotes) NetworkRepos() ([]*api.Repository, error) {
if r.network == nil {
err := resolveNetwork(r)
if err != nil {
return nil, err
}
}

var repos []*api.Repository
repoMap := map[string]bool{}

add := func(r *api.Repository) {
fn := ghrepo.FullName(r)
if _, ok := repoMap[fn]; !ok {
repoMap[fn] = true
repos = append(repos, r)
}
}

for _, repo := range r.network.Repositories {
if repo == nil {
continue
}
if repo.Parent != nil {
add(repo.Parent)
}
add(repo)
}

return repos, nil
}

// RemoteForRepo finds the git remote that points to a repository
func (r *ResolvedRemotes) RemoteForRepo(repo ghrepo.Interface) (*Remote, error) {
for _, remote := range r.remotes {
Expand Down
39 changes: 24 additions & 15 deletions context/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (r Remotes) FindByName(names ...string) (*Remote, error) {
}
}
}
return nil, fmt.Errorf("no GitHub remotes found")
return nil, fmt.Errorf("no matching remote found")
}

// FindByRepo returns the first Remote that points to a specific GitHub repository
Expand All @@ -34,6 +34,29 @@ func (r Remotes) FindByRepo(owner, name string) (*Remote, error) {
return nil, fmt.Errorf("no matching remote found")
}

// Filter remotes by given hostnames, maintains original order
func (r Remotes) FilterByHosts(hosts []string) Remotes {
filtered := make(Remotes, 0)
for _, rr := range r {
for _, host := range hosts {
if strings.EqualFold(rr.RepoHost(), host) {
filtered = append(filtered, rr)
break
}
}
}
return filtered
}

func (r Remotes) ResolvedRemote() (*Remote, error) {
for _, rr := range r {
if rr.Resolved != "" {
return rr, nil
}
}
return nil, fmt.Errorf("no resolved remote found")
}

func remoteNameSortScore(name string) int {
switch strings.ToLower(name) {
case "upstream":
Expand All @@ -54,20 +77,6 @@ func (r Remotes) Less(i, j int) bool {
return remoteNameSortScore(r[i].Name) > remoteNameSortScore(r[j].Name)
}

// Filter remotes by given hostnames, maintains original order
func (r Remotes) FilterByHosts(hosts []string) Remotes {
filtered := make(Remotes, 0)
for _, rr := range r {
for _, host := range hosts {
if strings.EqualFold(rr.RepoHost(), host) {
filtered = append(filtered, rr)
break
}
}
}
return filtered
}

// Remote represents a git remote mapped to a GitHub repository
type Remote struct {
*git.Remote
Expand Down
30 changes: 30 additions & 0 deletions git/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,36 @@ func (c *Client) AddRemote(ctx context.Context, name, urlStr string, trackingBra
return remote, nil
}

func (c *Client) InGitDirectory(ctx context.Context) bool {
showCmd, err := c.Command(ctx, "rev-parse", "--is-inside-work-tree")
if err != nil {
return false
}
out, err := showCmd.Output()
if err != nil {
return false
}

split := strings.Split(string(out), "\n")
if len(split) > 0 {
return split[0] == "true"
}
return false
}

func (c *Client) UnsetRemoteResolution(ctx context.Context, name string) error {
args := []string{"config", "--unset", fmt.Sprintf("remote.%s.gh-resolved", name)}
cmd, err := c.Command(ctx, args...)
if err != nil {
return err
}
_, err = cmd.Output()
if err != nil {
return err
}
return nil
}

func resolveGitPath() (string, error) {
path, err := safeexec.LookPath("git")
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
gardenCmd "github.com/cli/cli/v2/pkg/cmd/repo/garden"
repoListCmd "github.com/cli/cli/v2/pkg/cmd/repo/list"
repoRenameCmd "github.com/cli/cli/v2/pkg/cmd/repo/rename"
repoDefaultCmd "github.com/cli/cli/v2/pkg/cmd/repo/setdefault"
repoSyncCmd "github.com/cli/cli/v2/pkg/cmd/repo/sync"
repoViewCmd "github.com/cli/cli/v2/pkg/cmd/repo/view"
"github.com/cli/cli/v2/pkg/cmdutil"
Expand Down Expand Up @@ -52,6 +53,7 @@ func NewCmdRepo(f *cmdutil.Factory) *cobra.Command {
cmd.AddCommand(repoRenameCmd.NewCmdRename(f, nil))
cmd.AddCommand(repoDeleteCmd.NewCmdDelete(f, nil))
cmd.AddCommand(repoArchiveCmd.NewCmdArchive(f, nil))
cmd.AddCommand(repoDefaultCmd.NewCmdSetDefault(f, nil))

return cmd
}
Loading