summaryrefslogtreecommitdiffstats
path: root/modules/git
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-01-28 10:51:16 +0800
committerGitHub <noreply@github.com>2022-01-28 10:51:16 +0800
commit401e5c817474436218af08602da5135774d2c0eb (patch)
treeb8a468725a75ac0c723b80074aeb10928db8add3 /modules/git
parent668718c67ed16a7c7bc6000140b00e3de51cd480 (diff)
downloadgitea-401e5c817474436218af08602da5135774d2c0eb.tar.gz
gitea-401e5c817474436218af08602da5135774d2c0eb.zip
Fix broken when no commits and default branch is not master (#18422)
* Fix broken when no commits and default branch is not master * Fix IsEmpty check * Improve codes * Add timeout
Diffstat (limited to 'modules/git')
-rw-r--r--modules/git/repo.go20
1 files changed, 13 insertions, 7 deletions
diff --git a/modules/git/repo.go b/modules/git/repo.go
index 663e13dc18..881a76c87f 100644
--- a/modules/git/repo.go
+++ b/modules/git/repo.go
@@ -79,16 +79,22 @@ func InitRepository(ctx context.Context, repoPath string, bare bool) error {
// IsEmpty Check if repository is empty.
func (repo *Repository) IsEmpty() (bool, error) {
- var errbuf strings.Builder
- if err := NewCommandContext(repo.Ctx, "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