Skip to content

gh repo view fails on forked repo #9132

Description

@Shion1305

Describe the bug

I haven't figured out the reason, but I have failed to call gh repo view on forked repo.

Screenshot 2024-05-28 at 1 23 57

Steps to reproduce the behavior

  1. Folk repo
  2. Run gh repo view

Expected vs actual behavior

It should show info of forked repo.

Activity

  1. williammartin commented on May 28, 2024

    @williammartin
    Member

    Hey @Shion1305, thanks for creating this and sorry four your trouble. For forked repos we expect a default to be set so that we know whether to work with the upstream or the fork. For example, most of the time when running pr create you would expect it to be created against the upstream.

    However, your repo seems to be in an unusual state since it appears you only have one remote. How did you fork this repo and clone it onto your machine? Did you do anything with the remotes after cloning?

  2. Shion1305 commented on May 28, 2024

    @Shion1305
    ContributorAuthor

    @williammartin Thanks for checking this issue!
    I have forked the repo by fork button on the repository page, and cloned the folked repo by git clone ~
    I haven't done any operation with git after that, so this is strange.

  3. williammartin commented on May 28, 2024

    @williammartin
    Member

    Right, I can explain then. Since you didn't use gh repo fork --clone, or gh repo clone, gh is recognising that it is working on a fork because when working with a repository, we resolve the network for it, which includes the parents of any remotes. If there is more than one remote (and no default), we require gh repo set-default to make sure that gh is interacting with the correct repository.

    If you use gh repo clone, we set the default to the upstream:

    ➜ gh repo clone github-readme-stats
    Cloning into 'github-readme-stats'...
    remote: Enumerating objects: 7110, done.
    remote: Counting objects: 100% (1112/1112), done.
    remote: Compressing objects: 100% (60/60), done.
    remote: Total 7110 (delta 1084), reused 1058 (delta 1052), pack-reused 5998
    Receiving objects: 100% (7110/7110), 3.56 MiB | 3.44 MiB/s, done.
    Resolving deltas: 100% (4977/4977), done.
    From https://github.com/anuraghazra/github-readme-stats
     * [new branch]      master     -> upstream/master
     * [new tag]         v1.0.0     -> v1.0.0
    ! Repository anuraghazra/github-readme-stats set as the default repository. To learn more about the default repository, run: gh repo set-default --help
    

    So you will need to run gh repo set-default and choose the remote that you want API requests to work against i.e.

    ➜ gh repo set-default --help
    This command sets the default remote repository to use when querying the
    GitHub API for the locally cloned repository.
    
    gh uses the default repository for things like:
    
     - viewing and creating pull requests
     - viewing and creating issues
     - viewing and creating releases
     - working with GitHub Actions
     - adding repository and environment secrets
    

    Typically the upstream is the right choice. Functionally what this does is put an entry in your .git/config called gh-resolved. In the case of gh repo clone it sets the value to base on the upstream remote:

    ➜  github-readme-stats git:(master) cat .git/config
    ...
    [remote "origin"]
            url = https://github.com/williammartin/github-readme-stats.git
            fetch = +refs/heads/*:refs/remotes/origin/*
    [remote "upstream"]
            url = https://github.com/anuraghazra/github-readme-stats.git
            fetch = +refs/heads/*:refs/remotes/upstream/*
            gh-resolved = base
    

    And in your case where you only have one remote, it sets the value to the location of the upstream repo:

    ➜  github-readme-stats git:(master) cat .git/config
    ...
    [remote "origin"]
            url = https://github.com/williammartin/github-readme-stats.git
            fetch = +refs/heads/*:refs/remotes/origin/*
            gh-resolved = anuraghazra/github-readme-stats
    

    One improvement I can see is that when we do the automatic defaulting we include "To learn more about the default repository, run: gh repo set-default --help", which would probably be useful to include in your case as well.

    Hope this helps!

  4. changed the title [-]`gh repo view` fails on folked repo[/-] [+]`gh repo view` fails on forked repo[/+] on May 28, 2024
  5. arunsathiya commented on May 29, 2024

    @arunsathiya
    Contributor

    I stumbled upon this issue when researching a different issue: After moving repo to a new org, pr create adds a new remote named fork every time.

    About the current issue, do I have it right that the overall improvement being considered is the setting of the only remote (in the case of git clone clones) as the default repository, instead of prompting the user to manually set with gh repo set-default?

    That sounds like a sensible change to me, but I'm also a bit curious about why this wasn't considered so far. Are there any consequences of automatically setting it? 🤔

  6. williammartin commented on May 30, 2024

    @williammartin
    Member

    About the current issue, do I have it right that the overall improvement being considered is the setting of the only remote (in the case of git clone clones) as the default repository, instead of prompting the user to manually set with gh repo set-default?

    No, I'm not proposing a functional change here just a documentation one. There's a very real chance that even if you forked and cloned outside of gh, you still intend to work with the upstream repo, and I think it's just better to force the user to make a decision up front.

    I am open to being convinced otherwise though, I was not on the team when the concept of default was introduced so I don't have any particularly strong feelings except a bit of Chesterton's Fence related anxiety. 😅

  7. williammartin commented on May 31, 2024

    @williammartin
    Member

    Also relevant issue that I came across today which was surprising behaviour to me: #9152

  8. added
    coreThis issue is not accepting PRs from outside contributors
    and removed
    more-info-neededMore info needed from user/contributor
    on Jun 4, 2024
  9. williammartin commented on Jun 4, 2024

    @williammartin
    Member

    Labelling this help wanted with the action item being an update to ensure:

    One improvement I can see is that when we do the automatic defaulting we include "To learn more about the default repository, run: gh repo set-default --help", which would probably be useful to include in your case as well.

    Is addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcoreThis issue is not accepting PRs from outside contributorsdocsgh-reporelating to the gh repo commandhelp wantedContributions welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions