diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-01-28 14:48:36 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-28 14:48:36 +0800 |
commit | 2f22337125f496ada3a4d17148bb6b2a82c639e5 (patch) | |
tree | e8988271748790f97001ee58ccf4d0aa40eb9aa0 /modules | |
parent | 781ad8a79e22344e4b5d79ba29d8dac4b39f1ff4 (diff) | |
download | gitea-2f22337125f496ada3a4d17148bb6b2a82c639e5.tar.gz gitea-2f22337125f496ada3a4d17148bb6b2a82c639e5.zip |
Fix broken when no commits and default branch is not master (#18423)
* Fix broken when no commits and default branch is not master
* Fix IsEmpty check
* Improve codes
Diffstat (limited to 'modules')
-rw-r--r-- | modules/git/repo.go | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/modules/git/repo.go b/modules/git/repo.go index 47e46de3aa..5dca619ef6 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -79,16 +79,21 @@ func InitRepository(repoPath string, bare bool) error { // IsEmpty Check if repository is empty. func (repo *Repository) IsEmpty() (bool, error) { - var errbuf strings.Builder - if err := NewCommand("log", "-1").RunInDirPipeline(repo.Path, nil, &errbuf); err != nil { - if strings.Contains(errbuf.String(), "fatal: bad default revision 'HEAD'") || - strings.Contains(errbuf.String(), "fatal: your current branch 'master' does not have any commits yet") { - return true, nil - } + var errbuf, output strings.Builder + if err := NewCommandContext(repo.Ctx, "rev-list", "--all", "--count", "--max-count=1").RunWithContext(&RunContext{ + Timeout: -1, + Dir: repo.Path, + Stdout: &output, + Stderr: &errbuf, + }); err != nil { return true, fmt.Errorf("check empty: %v - %s", err, errbuf.String()) } - return false, nil + c, err := strconv.Atoi(strings.TrimSpace(output.String())) + if err != nil { + return true, fmt.Errorf("check empty: convert %s to count failed: %v", output.String(), err) + } + return c == 0, nil } // CloneRepoOptions options when clone a repository |