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

repo create failing for repository dirs other than .git #8472

Open
claui opened this issue Dec 17, 2023 · 3 comments
Open

repo create failing for repository dirs other than .git #8472

claui opened this issue Dec 17, 2023 · 3 comments
Labels
bug Something isn't working gh-repo relating to the gh repo command help wanted Contributions welcome p3 Affects a small number of users or is largely cosmetic

Comments

@claui
Copy link

claui commented Dec 17, 2023

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:

$ gh --version
gh version 2.40.1 (2023-12-13)
https://github.com/cli/cli/releases/tag/v2.40.1

Steps to reproduce the behavior

  1. Create a local repository in a non-standard location, e.g. .git2:

    cd "$(mktemp -d)"
    export GIT_DIR="$(pwd)/.git2"
    export GIT_WORK_TREE="$(pwd)"
    git init
    git commit --allow-empty -m 'Initial commit'
  2. Create a GitHub repository:

    # Note: substitute `myowner/myrepo` with an appropriate value
    gh repo create --private myowner/myrepo -s .

Expected vs actual behavior

I expect the gh repo create invocation to succeed.

Instead, it prints the following error message:

current directory is not a git repository. Run git init to initialize it

Why the behavior is incorrect

I think that this error message is misleading, because:

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:

check if path is the top level directory of a git repo

In an attempt to figure out whether we’re on the top level directory, the code insideisLocalRepo first calls the equivalent of git 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, the isLocalRepo function use the ToplevelDir and PathFromRoot functions instead.

The proposed functions already exist in the code base; they’re used by the browse and pr modules. Internally, they run git rev-parse --show-toplevel and git 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?

@claui claui added the bug Something isn't working label Dec 17, 2023
@cliAutomation cliAutomation added the needs-triage needs to be reviewed label Dec 17, 2023
claui added a commit to claui/cli that referenced this issue Dec 17, 2023
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
@samcoe
Copy link
Contributor

samcoe commented Jan 11, 2024

@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.

@samcoe samcoe added p3 Affects a small number of users or is largely cosmetic help wanted Contributions welcome gh-repo relating to the gh repo command and removed needs-triage needs to be reviewed labels Jan 11, 2024
claui added a commit to claui/cli that referenced this issue Jan 11, 2024
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
Copy link
Author

claui commented Jan 11, 2024

@samcoe Thank you. Pull request filed as #8563. Would appreciate your review!

@stefanct
Copy link

I just got bit by this too. It also affects submodules where .git has been absorbed into the parent repo's git-dir. Thus some very useful things like moving a whole tree including its submodules onto github with something like git submodule foreach 'gh repo create -s . someprojectname/$(basename -s .git $(git config --get remote.origin.url))' doesn't work (and should probably be a test case?). Thank you Claudia for your efforts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working gh-repo relating to the gh repo command help wanted Contributions welcome p3 Affects a small number of users or is largely cosmetic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants