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
repo create
failing for repository dirs other than .git
#8472
Comments
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 Thanks for the bug report and thorough investigation! I agree with your analysis about the underlying cause of this bug. We would definitely be open to a PR to fix this bug. |
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
I just got bit by this too. It also affects submodules where |
Describe the bug
The
gh repo create --source DIR
command may fail with a misleading and incorrect error message.This happens if, for example,
DIR
is a local work tree that uses a non-standard location for its.git
directory.Version info:
Steps to reproduce the behavior
Create a local repository in a non-standard location, e.g.
.git2
:Create a GitHub repository:
Expected vs actual behavior
I expect the
gh repo create
invocation to succeed.Instead, it prints the following error message:
Why the behavior is incorrect
I think that this error message is misleading, because:
Contrary to what the error message says, the current directory actually is a perfectly good Git repository.
Throughout the project, the code appears to be generally aware that repository paths are configurable.
In one instance though, it has the
.git
string hard coded, causing the above error message. (See Analysis for details.)Why this is important
Git repositories that live at some other path than
.git
can be part of several useful workflows, even if they’re not bare Git repositories.(For example, a developer may want to keep textual notes as Markdown files next to their code, and be able to track these notes even while checking out an earlier commit, during an interactive rebase, or during a
git bisect
session. To that end, they might want to keep those notes in a secondary Git repository named e.g..git2
or.notes
.)Analysis
According to the comment next to the
isLocalRepo
function, the purpose of the function is to:In an attempt to figure out whether we’re on the top level directory, the code inside
isLocalRepo
first calls the equivalent ofgit rev-parse --git-dir
. It then 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.Both assumptions a) and b) are wrong. This leads to false alarms in several cases, including this bug.
Suggested fix
To fix the issue, I suggest that rather than call
GitDir
, theisLocalRepo
function use theToplevelDir
andPathFromRoot
functions instead.The proposed functions already exist in the code base; they’re used by the
browse
andpr
modules. Internally, they rungit rev-parse --show-toplevel
andgit rev-parse --show-prefix
, respectively, which represents the standard, Git-supported way to figure out where we are.Would you consider reviewing a PR for this?
The text was updated successfully, but these errors were encountered: