]> source.dussan.org Git - gitea.git/commitdiff
Fix broken when no commits and default branch is not master (#18424)
authorLunny Xiao <xiaolunwen@gmail.com>
Fri, 28 Jan 2022 06:48:18 +0000 (14:48 +0800)
committerGitHub <noreply@github.com>
Fri, 28 Jan 2022 06:48:18 +0000 (14:48 +0800)
* Fix broken when no commits and default branch is not master

* Fix IsEmpty check

* Improve codes

modules/git/repo.go

index 43f329f4487d66732325c59b4f9d3a19fbe72248..53a8666d8b0403b82e83cc0bb2f8f21961b202ab 100644 (file)
@@ -75,16 +75,16 @@ 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 := NewCommand("rev-list", "--all", "--count", "--max-count=1").RunInDirPipeline(repo.Path, &output, &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