diff options
| author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-05-26 20:33:37 +0200 | 
|---|---|---|
| committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-05-26 20:33:37 +0200 | 
| commit | 4534dffb865eb1a50bfbc291a5c3798183081caf (patch) | |
| tree | d5bd1a2d9912a6442e3be1511ffb1d99f12287b0 /pkg/git | |
| parent | 349a3d1ff36a436261b1b65b870f8f262f06584f (diff) | |
| download | cerrado-4534dffb865eb1a50bfbc291a5c3798183081caf.tar.gz cerrado-4534dffb865eb1a50bfbc291a5c3798183081caf.tar.bz2 cerrado-4534dffb865eb1a50bfbc291a5c3798183081caf.zip | |
feat: Add actual git listing implementation
Diffstat (limited to 'pkg/git')
| -rw-r--r-- | pkg/git/git.go | 82 | 
1 files changed, 82 insertions, 0 deletions
| diff --git a/pkg/git/git.go b/pkg/git/git.go new file mode 100644 index 0000000..85a3b95 --- /dev/null +++ b/pkg/git/git.go @@ -0,0 +1,82 @@ +package git + +import ( +	"errors" +	"os" +	"path" + +	"git.gabrielgio.me/cerrado/pkg/u" +	"github.com/go-git/go-git/v5" +	"github.com/go-git/go-git/v5/plumbing/object" +) + +var ( +	ScanPathErr    = errors.New("Scan path does not exist") +	RepoPathErr    = errors.New("Repository path does not exist") +	missingHeadErr = errors.New("Head not found") +) + +type ( +	GitServerRepository struct { +		scanPath string +	} + +	GitRepository struct { +		path string +	} +) + +func NewGitServerRepository(scanPath string) *GitServerRepository { +	return &GitServerRepository{scanPath} +} + +func NewGitRepository(dir string) *GitRepository { +	return &GitRepository{ +		path: dir, +	} +} + +func (g *GitServerRepository) List() ([]*GitRepository, error) { +	if !u.FileExist(g.scanPath) { +		return nil, ScanPathErr +	} + +	entries, err := os.ReadDir(g.scanPath) +	if err != nil { +		return nil, err +	} + +	repos := make([]*GitRepository, 0) +	for _, e := range entries { +		if !e.IsDir() { +			continue +		} + +		fullPath := path.Join(g.scanPath, e.Name()) +		repos = append(repos, NewGitRepository(fullPath)) +	} + +	return repos, nil +} + +func (g *GitRepository) Path() string { +	return g.path +} + +func (g *GitRepository) LastCommit() (*object.Commit, error) { +	repo, err := git.PlainOpen(g.path) +	if err != nil { +		return nil, err +	} + +	ref, err := repo.Head() +	if err != nil { +		return nil, errors.Join(missingHeadErr, err) +	} + +	c, err := repo.CommitObject(ref.Hash()) +	if err != nil { +		return nil, err +	} +	return c, nil +} | 
