Skip to content

Commit

Permalink
fix: don't load non-git repos
Browse files Browse the repository at this point in the history
Fixes: #108
  • Loading branch information
aymanbagabas committed Apr 7, 2022
1 parent 11cf86f commit e256fed
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions git/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ var (
ErrReferenceNotFound = errors.New("reference not found")
// ErrRevisionNotExist is returned when a revision is not found.
ErrRevisionNotExist = git.ErrRevisionNotExist
// ErrNotAGitRepository is returned when the given path is not a Git repository.
ErrNotAGitRepository = errors.New("not a git repository")
)
22 changes: 21 additions & 1 deletion git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var (
// Repository is a wrapper around git.Repository with helper methods.
type Repository struct {
*git.Repository
Path string
Path string
IsBare bool
}

// Clone clones a repository.
Expand All @@ -35,15 +36,34 @@ func Init(path string, bare bool) (*Repository, error) {
return Open(path)
}

func isInsideWorkTree(r *git.Repository) bool {
out, err := r.RevParse("--is-inside-work-tree")
return err == nil && out == "true"
}

func isInsideGitDir(r *git.Repository) bool {
out, err := r.RevParse("--is-inside-git-dir")
return err == nil && out == "true"
}

func gitDir(r *git.Repository) (string, error) {
return r.RevParse("--git-dir")
}

// Open opens a git repository at the given path.
func Open(path string) (*Repository, error) {
repo, err := git.Open(path)
if err != nil {
return nil, err
}
gp, err := gitDir(repo)
if err != nil || (gp != "." && gp != ".git") {
return nil, ErrNotAGitRepository
}
return &Repository{
Repository: repo,
Path: path,
IsBare: gp == ".",
}, nil
}

Expand Down
10 changes: 7 additions & 3 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type Repo struct {
patchCache *lru.Cache
}

// Open opens a Git repository.
func (rs *RepoSource) Open(path string) (*Repo, error) {
// open opens a Git repository.
func (rs *RepoSource) open(path string) (*Repo, error) {
rg, err := git.Open(path)
if err != nil {
return nil, err
Expand Down Expand Up @@ -199,8 +199,9 @@ func (rs *RepoSource) LoadRepo(name string) error {
rs.mtx.Lock()
defer rs.mtx.Unlock()
rp := filepath.Join(rs.Path, name)
r, err := rs.Open(rp)
r, err := rs.open(rp)
if err != nil {
log.Printf("error opening repository %s: %s", name, err)
return err
}
rs.repos[name] = r
Expand All @@ -215,6 +216,9 @@ func (rs *RepoSource) LoadRepos() error {
}
for _, de := range rd {
err = rs.LoadRepo(de.Name())
if err == git.ErrNotAGitRepository {
continue
}
if err != nil {
return err
}
Expand Down

0 comments on commit e256fed

Please sign in to comment.