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"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
@@ -44,6 +45,9 @@ type GitStatusLister 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)
GitCatFile(cwd, commitId, filename string) (data []byte, err error)
GetPath() string
@@ -66,6 +70,8 @@ type GitHandlerImpl struct {
GitPath string
GitCommiter string
GitEmail string
lock GitHandlerGenerator
}
func (s *GitHandlerImpl) GetPath() string {
@@ -73,34 +79,76 @@ func (s *GitHandlerImpl) GetPath() string {
}
type GitHandlerGenerator interface {
CreateGitHandler(git_author, email, prjName string) (Git, error)
ReadExistingPath(git_author, email, gitPath string) (Git, error)
CreateGitHandler(repo 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) {
gitPath, err := os.MkdirTemp("", prj_name)
if err != nil {
return nil, fmt.Errorf("Cannot create temp dir: %w", err)
}
if err = os.Chmod(gitPath, 0700); err != nil {
return nil, fmt.Errorf("Cannot fix permissions of temp dir: %w", err)
}
return s.ReadExistingPath(git_author, email, gitPath)
lock_lock sync.Mutex
lock map[string]sync.Mutex
}
func (*GitHandlerGeneratorImpl) ReadExistingPath(git_author, email, gitPath string) (Git, error) {
func AllocateGitWorkTree(basePath, org, branch, gitAuthor, email string) (GitHandlerGenerator, error) {
if fi, err := os.Stat(basePath); err != nil || !fi.IsDir() {
return nil, fmt.Errorf("Git basepath not a valid directory: %s %w", basePath, err)
}
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)
}
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{
GitCommiter: git_author,
GitPath: gitPath,
GitCommiter: s.git_author,
GitEmail: s.email,
GitPath: path.Join(s.path, s.org, s.branch, repo),
}
return git, nil
}
func (s *GitHandlerGeneratorImpl) ReleaseLock(repo string) {
}
//func (h *GitHandler) ProcessBranchList() []string {
// if h.HasError() {
// return make([]string, 0)
@@ -148,12 +196,8 @@ func (e *GitHandlerImpl) GitBranchHead(gitDir, branchName string) (string, error
return strings.TrimSpace(id), nil
}
func (e *GitHandlerImpl) Close() error {
if err := os.RemoveAll(e.GitPath); err != nil {
return err
}
e.GitPath = ""
return nil
func (e *GitHandlerImpl) Close() {
e.lock.ReleaseLock(e.GitPath)
}
type writeFunc func(data []byte) (int, error)