mirror of
https://github.com/Theodor-Springmann-Stiftung/lenz-web.git
synced 2025-10-29 01:05:32 +00:00
Server
This commit is contained in:
255
git/git.go
255
git/git.go
@@ -4,7 +4,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
@@ -13,232 +12,202 @@ import (
|
||||
|
||||
var InvalidBranchError = errors.New("The currently checked out branch does not match the requested branch. Please checkout the correct branch first.")
|
||||
var InvalidStateError = errors.New("The GitProvider is not in a valid state. Fix the issues or continue without Git data.")
|
||||
var NoURLProvidedError = errors.New("No URL provided for GitProvider.")
|
||||
var NoPathProvidedError = errors.New("No path or branch provided for GitProvider.")
|
||||
var NoURLProvidedError = errors.New("Missing URL.")
|
||||
var NoPathProvidedError = errors.New("Missing path.")
|
||||
var NoBranchProvidedError = errors.New("Missing branch name.")
|
||||
|
||||
// NOTE: GitProvider does not open any worktree files, it only
|
||||
// - reads in information from the repo, given a path
|
||||
// - clones a repo, given an URL & a path
|
||||
// - pulls a repo, given a path
|
||||
// In case of success it updates it's state: the commit hash and date. Then it closes the repo.
|
||||
type GitProvider struct {
|
||||
mu sync.Mutex
|
||||
|
||||
URL string
|
||||
type Commit struct {
|
||||
Path string
|
||||
URL string
|
||||
Branch string
|
||||
Commit string
|
||||
Hash string
|
||||
Date time.Time
|
||||
}
|
||||
|
||||
func NewGitProvider(url string, path string, branch string) (*GitProvider, error) {
|
||||
// TODO: check if directory is empty
|
||||
// TODO: force clone
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return GitProviderFromPath(path, branch)
|
||||
}
|
||||
|
||||
return GitProviderFromURL(url, path, branch)
|
||||
func IsValidRepository(path, url, branch string) *Commit {
|
||||
commit, _ := Read(path, branch, url)
|
||||
return commit
|
||||
}
|
||||
|
||||
func GitProviderFromPath(path string, branch string) (*GitProvider, error) {
|
||||
if branch == "" || path == "" {
|
||||
return nil, NoPathProvidedError
|
||||
func OpenOrClone(path, url, branch string) (*Commit, error) {
|
||||
commit := IsValidRepository(path, url, branch)
|
||||
if commit == nil {
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
err := os.RemoveAll(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
c, err := Clone(path, url, branch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
gp := GitProvider{Path: path, Branch: branch}
|
||||
if err := gp.Read(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &gp, nil
|
||||
return commit, nil
|
||||
}
|
||||
|
||||
func GitProviderFromURL(url string, path string, branch string) (*GitProvider, error) {
|
||||
func Pull(path, url, branch string) (*Commit, error) {
|
||||
if url == "" {
|
||||
return nil, NoURLProvidedError
|
||||
}
|
||||
|
||||
if branch == "" || path == "" {
|
||||
if branch == "" {
|
||||
return nil, NoPathProvidedError
|
||||
}
|
||||
|
||||
gp := GitProvider{URL: url, Path: path, Branch: branch}
|
||||
if err := gp.Clone(); err != nil {
|
||||
return nil, err
|
||||
if path == "" {
|
||||
return nil, NoPathProvidedError
|
||||
}
|
||||
|
||||
return &gp, nil
|
||||
}
|
||||
|
||||
// Returs true if the repo was updated remotely, false otherwise
|
||||
func (g *GitProvider) Pull() (error, bool) {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
|
||||
branch := plumbing.NewBranchReferenceName(g.Branch)
|
||||
repo, err := git.PlainOpen(g.Path)
|
||||
br := plumbing.NewBranchReferenceName(branch)
|
||||
repo, err := git.PlainOpen(path)
|
||||
if err != nil {
|
||||
return err, false
|
||||
return nil, err
|
||||
}
|
||||
|
||||
wt, err := repo.Worktree()
|
||||
if err != nil {
|
||||
return err, false
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := wt.Pull(&git.PullOptions{
|
||||
RemoteName: "origin",
|
||||
ReferenceName: branch,
|
||||
ReferenceName: br,
|
||||
Progress: os.Stdout,
|
||||
}); err != nil {
|
||||
if err == git.NoErrAlreadyUpToDate {
|
||||
return nil, false
|
||||
}
|
||||
return err, false
|
||||
}); err != nil && err != git.NoErrAlreadyUpToDate {
|
||||
return nil, err
|
||||
}
|
||||
defer wt.Clean(&git.CleanOptions{Dir: true})
|
||||
|
||||
oldCommit := g.Commit
|
||||
if err := g.setValues(repo); err != nil {
|
||||
return err, false
|
||||
}
|
||||
|
||||
if oldCommit == g.Commit {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return nil, true
|
||||
return latestCommit(repo, path, branch, url)
|
||||
}
|
||||
|
||||
func (g *GitProvider) Clone() error {
|
||||
if g.URL == "" {
|
||||
return NoURLProvidedError
|
||||
func Read(path, branch, url string) (*Commit, error) {
|
||||
if branch == "" {
|
||||
return nil, NoBranchProvidedError
|
||||
}
|
||||
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
if path == "" {
|
||||
return nil, NoPathProvidedError
|
||||
}
|
||||
|
||||
branch := plumbing.NewBranchReferenceName(g.Branch)
|
||||
if url == "" {
|
||||
return nil, NoURLProvidedError
|
||||
}
|
||||
|
||||
repo, err := git.PlainClone(g.Path, false, &git.CloneOptions{
|
||||
URL: g.URL,
|
||||
repo, err := git.PlainOpen(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := ValidateBranch(repo, branch); err != nil {
|
||||
br := plumbing.NewBranchReferenceName(branch)
|
||||
wt, err := repo.Worktree()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer wt.Clean(&git.CleanOptions{Dir: true})
|
||||
|
||||
if err := wt.Checkout(&git.CheckoutOptions{
|
||||
Branch: br,
|
||||
Force: true,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := ValidateBranch(repo, branch); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return latestCommit(repo, path, branch, url)
|
||||
}
|
||||
|
||||
func Clone(path, url, branch string) (*Commit, error) {
|
||||
if url == "" {
|
||||
return nil, NoURLProvidedError
|
||||
}
|
||||
|
||||
if branch == "" {
|
||||
return nil, NoBranchProvidedError
|
||||
}
|
||||
|
||||
if path == "" {
|
||||
return nil, NoPathProvidedError
|
||||
}
|
||||
|
||||
br := plumbing.NewBranchReferenceName(branch)
|
||||
|
||||
repo, err := git.PlainClone(path, false, &git.CloneOptions{
|
||||
URL: url,
|
||||
Progress: os.Stdout,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
wt, err := repo.Worktree()
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
defer wt.Clean(&git.CleanOptions{Dir: true})
|
||||
|
||||
if err := wt.Checkout(&git.CheckoutOptions{
|
||||
Branch: branch,
|
||||
Branch: br,
|
||||
Force: true,
|
||||
}); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return g.setValues(repo)
|
||||
return latestCommit(repo, path, branch, url)
|
||||
}
|
||||
|
||||
// Implement String Interface
|
||||
func (g *GitProvider) String() string {
|
||||
return fmt.Sprintf("GitProvider\nURL: %s\nPath: %s\nBranch: %s\nCommit: %s\nDate: %s\n", g.URL, g.Path, g.Branch, g.Commit, g.Date)
|
||||
func (g Commit) String() string {
|
||||
return fmt.Sprintf("Path: %s\nURL: %s\nBranch: %s\nHash: %s\nDate: %s", g.Path, g.URL, g.Branch, g.Hash, g.Date)
|
||||
}
|
||||
|
||||
func (g *GitProvider) setValues(repo *git.Repository) error {
|
||||
func (g Commit) Pull() (*Commit, error) {
|
||||
return Pull(g.Path, g.URL, g.Branch)
|
||||
}
|
||||
|
||||
func latestCommit(repo *git.Repository, path, branch, url string) (*Commit, error) {
|
||||
log, err := repo.Log(&git.LogOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
defer log.Close()
|
||||
|
||||
commit, err := log.Next()
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
g.Commit = commit.Hash.String()
|
||||
g.Date = commit.Author.When
|
||||
c := commit.Hash.String()
|
||||
d := commit.Author.When
|
||||
|
||||
return nil
|
||||
return &Commit{Path: path, URL: url, Branch: branch, Hash: c, Date: d}, nil
|
||||
}
|
||||
|
||||
func (g *GitProvider) Read() error {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
|
||||
repo, err := git.PlainOpen(g.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.ValidateBranch(repo); err != nil {
|
||||
branch := plumbing.NewBranchReferenceName(g.Branch)
|
||||
wt, err := repo.Worktree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer wt.Clean(&git.CleanOptions{Dir: true})
|
||||
|
||||
if err := wt.Checkout(&git.CheckoutOptions{
|
||||
Branch: branch,
|
||||
Force: true,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.ValidateBranch(repo); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return g.setValues(repo)
|
||||
}
|
||||
|
||||
func (g *GitProvider) Validate() error {
|
||||
repo, err := git.PlainOpen(g.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.ValidateBranch(repo); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.ValidateCommit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *GitProvider) ValidateBranch(repo *git.Repository) error {
|
||||
func ValidateBranch(repo *git.Repository, branch string) error {
|
||||
head, err := repo.Head()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cbranch := head.Name().Short()
|
||||
if cbranch != g.Branch {
|
||||
if cbranch != branch {
|
||||
return InvalidBranchError
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *GitProvider) Wait() {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
}
|
||||
|
||||
func (g *GitProvider) ValidateCommit() error {
|
||||
if g.Commit == "" || g.Date.IsZero() {
|
||||
func (g Commit) ValidateCommit() error {
|
||||
if g.Hash == "" || g.Date.IsZero() {
|
||||
return InvalidStateError
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user