git-project-manager/internal/cache/projects_git.go

61 lines
1.7 KiB
Go
Raw Normal View History

2024-01-15 21:02:15 +00:00
package cache
2023-12-10 04:19:19 +00:00
import (
"context"
"time"
git "github.com/go-git/go-git/v5"
2024-01-15 20:39:35 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
2023-12-10 04:19:19 +00:00
)
2023-12-12 21:30:33 +00:00
const gitCloneTimeoutSecs = 30
2023-12-10 04:19:19 +00:00
// Will either read in the current repo, preparing a report
// on its current state, or will clone the project if it has not
// already been cloned in its path
2024-01-15 20:39:35 +00:00
func (c *Cache) OpenProject(ctx context.Context, project *projects.Project) *git.Repository {
2023-12-10 04:19:19 +00:00
path := c.GetProjectPath(project)
cloneCtx, cncl := context.WithDeadline(ctx, time.Now().Add(gitCloneTimeoutSecs*time.Second))
defer cncl()
var repo *git.Repository
var err error
if repo, err = git.PlainOpen(path); err != nil {
if err == git.ErrRepositoryNotExists {
c.log.Debug("Project not yet cloned")
}
}
if repo == nil {
// Check to make sure we can connect before we time out
// shouldn't be necessary, but go-git does not properly
// honor its context
2024-01-16 17:48:42 +00:00
if err := project.CheckHost(projects.GitProtoSSH); err != nil {
2023-12-10 04:19:19 +00:00
c.log.Fatal("Git remote unreachable, giving up", c.log.Args("error", err))
}
c.log.Info("Cloning project from remote", c.log.Args("remote", project.SSHURLToRepo))
repo, err = git.PlainCloneContext(cloneCtx, path, false, &git.CloneOptions{
URL: project.SSHURLToRepo,
})
if err == git.ErrRepositoryAlreadyExists {
c.log.Debug("Skipping clone, already exists")
} else if err != nil {
c.log.Fatal("Failed to open git project", c.log.Args(
"error", err,
))
}
}
head, err := repo.Head()
if err != nil {
c.log.Fatal("Something went wrong, unable to fetch HEAD from repo", c.log.Args(
"error", err,
))
}
c.log.Debug("Repository ready", c.log.Args("branch", head.Name().String()))
return repo
}