// isValidGitRepository checks to see if there is a git repository in the // directory and if the repository is valid -- i.e. it has remotes or commits func isValidGitRepository(fs util.FileSystem, dir string) (bool, error) { gitPath := filepath.Join(strings.TrimPrefix(dir, "file://"), ".git") fi, err := fs.Stat(gitPath) if os.IsNotExist(err) { // The directory is not a git repo, no error return false, nil } if err != nil { return false, err } if !fi.IsDir() { gitPath, err = followGitSubmodule(fs, gitPath) if err != nil { return false, err } } // Search the content of the .git directory for content directories := [2]string{ filepath.Join(gitPath, "objects"), filepath.Join(gitPath, "refs"), } // For the directories we search, if the git repo has been used, there will // be some file. We don't just search the base git repository because of the // hook samples that are normally generated with `git init` isEmpty := true for _, dir := range directories { err := fs.Walk(dir, func(path string, info os.FileInfo, err error) error { // If we find a file, the git directory is "not empty" // We're looking for object blobs, and ref files if info != nil && !info.IsDir() { isEmpty = false return filepath.SkipDir } return err }) if err != nil && err != filepath.SkipDir { // There is a .git, but we've encountered an error return true, err } if !isEmpty { return true, nil } } // Since we know there's a .git directory, but there is nothing in it, we // throw an error return true, errors.NewEmptyGitRepositoryError(dir) }