mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Fix isempty detection of git repository (#18746)
* Fix isempty detection of git repository * Fix IsEmpty check
This commit is contained in:
		@@ -80,21 +80,20 @@ func InitRepository(ctx context.Context, repoPath string, bare bool) error {
 | 
				
			|||||||
// IsEmpty Check if repository is empty.
 | 
					// IsEmpty Check if repository is empty.
 | 
				
			||||||
func (repo *Repository) IsEmpty() (bool, error) {
 | 
					func (repo *Repository) IsEmpty() (bool, error) {
 | 
				
			||||||
	var errbuf, output strings.Builder
 | 
						var errbuf, output strings.Builder
 | 
				
			||||||
	if err := NewCommand(repo.Ctx, "rev-list", "--all", "--count", "--max-count=1").
 | 
						if err := NewCommand(repo.Ctx, "show-ref", "--head", "^HEAD$").
 | 
				
			||||||
		RunWithContext(&RunContext{
 | 
							RunWithContext(&RunContext{
 | 
				
			||||||
			Timeout: -1,
 | 
								Timeout: -1,
 | 
				
			||||||
			Dir:     repo.Path,
 | 
								Dir:     repo.Path,
 | 
				
			||||||
			Stdout:  &output,
 | 
								Stdout:  &output,
 | 
				
			||||||
			Stderr:  &errbuf,
 | 
								Stderr:  &errbuf,
 | 
				
			||||||
		}); err != nil {
 | 
							}); err != nil {
 | 
				
			||||||
 | 
							if err.Error() == "exit status 1" && errbuf.String() == "" {
 | 
				
			||||||
 | 
								return true, nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		return true, fmt.Errorf("check empty: %v - %s", err, errbuf.String())
 | 
							return true, fmt.Errorf("check empty: %v - %s", err, errbuf.String())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	c, err := strconv.Atoi(strings.TrimSpace(output.String()))
 | 
						return strings.TrimSpace(output.String()) == "", nil
 | 
				
			||||||
	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
 | 
					// CloneRepoOptions options when clone a repository
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user