package cache import ( "context" "time" git "github.com/go-git/go-git/v5" "gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects" ) const gitCloneTimeoutSecs = 60 // 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 func (c *Cache) OpenProject(ctx context.Context, project *projects.Project) *git.Repository { 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 if err := project.CheckHost(projects.GitProtoSSH); err != nil { 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 }