Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix repo create for repo dirs other than .git #8563

Closed
wants to merge 1 commit into from

Conversation

claui
Copy link

@claui claui commented Jan 11, 2024

This pull request replaces the current implementation of the isLocalRepo function in repo/create/create.go, which is based on --git-dir and wrongly assumes that if and only if the current directory is on the top level of a Git repository, then the output is a) a relative path, which b) points to the .git directory.

For an example that demonstrates why these assumptions are incorrect, see #8472.

To figure out whether we’re a work tree at all, the proposed implementation uses TopLevelDir instead of GitDir.
(Calling ToplevelDir is equivalent to git rev-parse --show-toplevel.)

It also introduces an isToplevel function, which calls PathFromRoot internally.
(Calling PathFromRoot is equivalent to git rev-parse --show-prefix.)

Fixes #8472.

In `create.go`, rather than invoke `GitDir`, change the `isLocalRepo`
function so it only calls `ToplevelDir` to figure out whether we’re a
work tree at all.
(This is equivalent to `git rev-parse --show-toplevel`.)

Also introduce an `isToplevel` function, which calls `PathFromRoot`
internally.
(This is equivalent to `git rev-parse --show-prefix`.)

This replaces the current implementation, which is based on `--git-dir`
and wrongly assumes that if and only if the current directory is on the
top level, then the output is a) a relative path, which b) points to
the .git directory.

Fixes cli#8472 [1].

[1]: cli#8472
@claui claui requested a review from a team as a code owner January 11, 2024 20:23
@claui claui requested review from andyfeller and removed request for a team January 11, 2024 20:23
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Jan 11, 2024
@cliAutomation cliAutomation added this to Needs review 🤔 in The GitHub CLI Jan 11, 2024
Copy link
Contributor

@andyfeller andyfeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@claui : thank you for contributing to the GitHub CLI and your patience! 🙇

Reviewing the code changes, I think they make sense given the context. The tests are failing because the new Git commands used weren't mocked out, which appear to be in 3 places:

  • execStubs: func(cs *run.CommandStubber) {
    cs.Register(`git -C . rev-parse --git-dir`, 0, ".git")
    cs.Register(`git -C . rev-parse HEAD`, 0, "commithash")
    },
  • execStubs: func(cs *run.CommandStubber) {
    cs.Register(`git -C . rev-parse --git-dir`, 0, ".git")
    cs.Register(`git -C . rev-parse HEAD`, 0, "commithash")
    cs.Register(`git -C . remote add origin https://github.com/OWNER/REPO`, 0, "")
    cs.Register(`git -C . push --set-upstream origin HEAD`, 0, "")
    },
  • execStubs: func(cs *run.CommandStubber) {
    cs.Register(`git -C . rev-parse --git-dir`, 0, ".git")
    cs.Register(`git -C . rev-parse HEAD`, 0, "commithash")
    cs.Register(`git -C . remote add origin https://github.com/OWNER/REPO`, 0, "")
    },

Additionally since this scenario is focused on non-standard .git directories, what do you think about creating a new test case where we specifically ensure that something like .git2 is matched as expected?

@williammartin
Copy link
Member

Gentle nudge on this review @claui

@williammartin
Copy link
Member

This PR has gone stale so I'm going to close it. I would welcome anyone who wants to pick it up to start from here and address the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external pull request originating outside of the CLI core team
Projects
No open projects
The GitHub CLI
  
Needs review 🤔
Development

Successfully merging this pull request may close these issues.

repo create failing for repository dirs other than .git
4 participants