arxanas / git-branchless

High-velocity, monorepo-scale workflow for Git
Apache License 2.0
3.45k stars 87 forks source link

If there is no history, an error occurs. #698

Open black7375 opened 1 year ago

black7375 commented 1 year ago

Description of the bug

After the initialization of the git-branchless, panic occurs without any commit.

↳ git init           
Alias tip: g init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty Git repository in /tmp/branchless/.git

↳ git branchless init
Alias tip: g branchless init
Created config file at /tmp/branchless/.git/branchless/config
Your main branch name could not be auto-detected!
Examples of a main branch: master, main, trunk, etc.
See https://github.com/arxanas/git-branchless/wiki/Concepts#main-branch
Enter the name of your main branch: master
Installing hook: post-commit
Installing hook: post-merge
Installing hook: post-rewrite
Installing hook: post-checkout
Installing hook: pre-auto-gc
Installing hook: reference-transaction
Successfully installed git-branchless.
To uninstall, run: git branchless init --uninstall

↳ git sl             
Alias tip: g sl
The application panicked (crashed).
Message:  A fatal error occurred: 
   0: Could not find repository main branch

Location:
   /home/black7375/.cargo/registry/src/github.com-1ecc6299db9ec823/git-branchless-lib-0.6.0/src/core/repo_ext.rs:74

Suggestion: 
The main branch "master" could not be found in your repository
at path: "/tmp/branchless/.git/".
These branches exist: []
Either create it, or update the main branch setting by running:

    git branchless init --main-branch <branch>

Note that remote main branches are no longer supported as of v0.6.0. See
https://github.com/arxanas/git-branchless/discussions/595 for more details.

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.
Location: /home/black7375/.cargo/registry/src/github.com-1ecc6299db9ec823/git-branchless-0.6.0/src/commands/mod.rs:464

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.

Expected behavior

Instead of panic, it is better to get a message that there is no commit.


Sample message:

your current branch 'master' does not have any commits yet

Actual behavior

No response

Version of rustc

rustc 1.67.0-nightly (01fbc5ae7 2022-12-07)

Automated bug report

Software version

git-branchless 0.6.0

Operating system

Linux 6.0.11-zen1-1-zen

Command-line

/home/black7375/.cargo/bin/git-branchless bug-report 

Environment variables

SHELL=/usr/bin/zsh
EDITOR=vim

Git version

> git version 
git version 2.38.1

Hooks

Show 6 hooks ##### Hook `post-commit` ``` #!/bin/sh ## START BRANCHLESS CONFIG git branchless hook-post-commit "$@" ## END BRANCHLESS CONFIG ``` ##### Hook `post-merge` ``` #!/bin/sh ## START BRANCHLESS CONFIG git branchless hook-post-merge "$@" ## END BRANCHLESS CONFIG ``` ##### Hook `post-rewrite` ``` #!/bin/sh ## START BRANCHLESS CONFIG git branchless hook-post-rewrite "$@" ## END BRANCHLESS CONFIG ``` ##### Hook `post-checkout` ``` #!/bin/sh ## START BRANCHLESS CONFIG git branchless hook-post-checkout "$@" ## END BRANCHLESS CONFIG ``` ##### Hook `pre-auto-gc` ``` #!/bin/sh ## START BRANCHLESS CONFIG git branchless hook-pre-auto-gc "$@" ## END BRANCHLESS CONFIG ``` ##### Hook `reference-transaction` ``` #!/bin/sh ## START BRANCHLESS CONFIG # Avoid canceling the reference transaction in the case that `branchless` fails # for whatever reason. git branchless hook-reference-transaction "$@" || ( echo 'branchless: Failed to process reference transaction!' echo 'branchless: Some events (e.g. branch updates) may have been lost.' echo 'branchless: This is a bug. Please report it.' ) ## END BRANCHLESS CONFIG ```

Events

Error: Could not find repository main branch

Version of git-branchless

No response

Version of git

No response

arxanas commented 1 year ago

Hi @black7375, thanks for reporting. I think this would be fairly straightforward to fix; I just haven't gotten around to it yet.

Relevant reading: https://stackoverflow.com/questions/28852902/how-to-detect-if-head-points-to-an-unborn-branch

Parsifal-M commented 1 year ago

Hey :wave:!

Kind of new to Rust, but I wouldn't mind helping out if you'd like? This should be a fix in the init.rs file right? Please let me know :smile:

arxanas commented 1 year ago

Hi @Parsifal-M, sorry for the late reply. This error message can occur without having run git branchless init, and it probably shouldn't be an error to init in a repo before having made the first commit.

The best thing to here could be to refactor the get_main_branch function to write a message to stdout and return an ExitCode, or to catch the raised error at a higher level by attempting to downcast it. These are probably straightforward but maybe not easy (for a Rust beginner). You can try anyways, if you like, and post in the Discord chat if you have trouble.

There are a couple of issues marked as "good first issue" which you could try instead; #494 is very similar. I'll remove the "good first issue" label from this one.