This commit is contained in:
2025-04-08 19:03:33 +02:00
parent d89c77e22d
commit 9dcd25b69a

View File

@@ -27,6 +27,7 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
@@ -44,6 +45,9 @@ type GitStatusLister interface {
} }
type Git interface { type Git interface {
// error if git, but wrong remote
Clone(remoteUrl string) error // clone, or chekc if path is already checked out remote, error otherwise
GitParseCommits(cwd string, commitIDs []string) (parsedCommits []GitCommit, err error) GitParseCommits(cwd string, commitIDs []string) (parsedCommits []GitCommit, err error)
GitCatFile(cwd, commitId, filename string) (data []byte, err error) GitCatFile(cwd, commitId, filename string) (data []byte, err error)
GetPath() string GetPath() string
@@ -66,6 +70,8 @@ type GitHandlerImpl struct {
GitPath string GitPath string
GitCommiter string GitCommiter string
GitEmail string GitEmail string
lock GitHandlerGenerator
} }
func (s *GitHandlerImpl) GetPath() string { func (s *GitHandlerImpl) GetPath() string {
@@ -73,34 +79,76 @@ func (s *GitHandlerImpl) GetPath() string {
} }
type GitHandlerGenerator interface { type GitHandlerGenerator interface {
CreateGitHandler(git_author, email, prjName string) (Git, error) CreateGitHandler(repo string) (Git, error)
ReadExistingPath(git_author, email, gitPath string) (Git, error) ReadExistingPath(repo string) (Git, error)
ReleaseLock(path string)
} }
type GitHandlerGeneratorImpl struct{} type GitHandlerGeneratorImpl struct {
// TODO: add mutex to lock paths so only one workflow per path and others wait
path string
org, branch string
git_author string
email string
func (s *GitHandlerGeneratorImpl) CreateGitHandler(git_author, email, prj_name string) (Git, error) { lock_lock sync.Mutex
gitPath, err := os.MkdirTemp("", prj_name) lock map[string]sync.Mutex
if err != nil {
return nil, fmt.Errorf("Cannot create temp dir: %w", err)
} }
if err = os.Chmod(gitPath, 0700); err != nil { func AllocateGitWorkTree(basePath, org, branch, gitAuthor, email string) (GitHandlerGenerator, error) {
return nil, fmt.Errorf("Cannot fix permissions of temp dir: %w", err) if fi, err := os.Stat(basePath); err != nil || !fi.IsDir() {
return nil, fmt.Errorf("Git basepath not a valid directory: %s %w", basePath, err)
} }
return s.ReadExistingPath(git_author, email, gitPath) path := path.Join(basePath, org, branch)
if fi, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
if err = os.MkdirAll(path, 0o700); err != nil {
return nil, fmt.Errorf("Cannot create git directory structure: %s: %w", path, err)
}
} else {
return nil, fmt.Errorf("Error checking git directory strcture: %s: %w", path, err)
}
} else if !fi.IsDir() {
return nil, fmt.Errorf("Invalid git directory structure: %s != directory", path)
} }
func (*GitHandlerGeneratorImpl) ReadExistingPath(git_author, email, gitPath string) (Git, error) { return &GitHandlerGeneratorImpl{
path: basePath,
org: org,
branch: branch,
git_author: gitAuthor,
email: email,
lock: make(map[string]sync.Mutex),
}, nil
}
func (s *GitHandlerGeneratorImpl) CreateGitHandler(repoName string) (Git, error) {
path := path.Join(s.path, repoName)
if err := os.Mkdir(path, 0o777); err != nil {
return nil, err
}
return s.ReadExistingPath(repoName)
}
func (s *GitHandlerGeneratorImpl) ReadExistingPath(repo string) (Git, error) {
git := &GitHandlerImpl{ git := &GitHandlerImpl{
GitCommiter: git_author, GitCommiter: s.git_author,
GitPath: gitPath, GitEmail: s.email,
GitPath: path.Join(s.path, s.org, s.branch, repo),
} }
return git, nil return git, nil
} }
func (s *GitHandlerGeneratorImpl) ReleaseLock(repo string) {
}
//func (h *GitHandler) ProcessBranchList() []string { //func (h *GitHandler) ProcessBranchList() []string {
// if h.HasError() { // if h.HasError() {
// return make([]string, 0) // return make([]string, 0)
@@ -148,12 +196,8 @@ func (e *GitHandlerImpl) GitBranchHead(gitDir, branchName string) (string, error
return strings.TrimSpace(id), nil return strings.TrimSpace(id), nil
} }
func (e *GitHandlerImpl) Close() error { func (e *GitHandlerImpl) Close() {
if err := os.RemoveAll(e.GitPath); err != nil { e.lock.ReleaseLock(e.GitPath)
return err
}
e.GitPath = ""
return nil
} }
type writeFunc func(data []byte) (int, error) type writeFunc func(data []byte) (int, error)