Skip to content

Commit

Permalink
refactor: git service
Browse files Browse the repository at this point in the history
move all git operations into the git service

Signed-off-by: Toma Puljak <[email protected]>
  • Loading branch information
Tpuljak committed Dec 13, 2024
1 parent c2561af commit 12c0039
Show file tree
Hide file tree
Showing 20 changed files with 652 additions and 522 deletions.
27 changes: 4 additions & 23 deletions pkg/agent/toolbox/git/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
package git

import (
"github.com/daytonaio/daytona/pkg/git"
"github.com/gin-gonic/gin"
"github.com/go-git/go-git/v5"
)

func AddFiles(c *gin.Context) {
Expand All @@ -15,33 +15,14 @@ func AddFiles(c *gin.Context) {
return
}

repo, err := git.PlainOpen(req.Path)
if err != nil {
c.AbortWithError(400, err)
return
gitService := git.Service{
ProjectDir: req.Path,
}

w, err := repo.Worktree()
if err != nil {
if err := gitService.Add(req.Files); err != nil {
c.AbortWithError(400, err)
return
}

if len(req.Files) == 1 && req.Files[0] == "." {
_, err = w.Add(".")
if err != nil {
c.AbortWithError(400, err)
return
}
} else {
for _, file := range req.Files {
_, err = w.Add(file)
if err != nil {
c.AbortWithError(400, err)
return
}
}
}

c.Status(200)
}
57 changes: 23 additions & 34 deletions pkg/agent/toolbox/git/clone_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
package git

import (
"fmt"

"github.com/daytonaio/daytona/pkg/git"
"github.com/daytonaio/daytona/pkg/gitprovider"
"github.com/gin-gonic/gin"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)

Expand All @@ -19,49 +17,40 @@ func CloneRepository(c *gin.Context) {
return
}

options := &git.CloneOptions{
URL: req.URL,
Progress: nil,
branch := "main"
if req.Branch != nil {
branch = *req.Branch
}

repo := gitprovider.GitRepository{
Url: req.URL,
Branch: branch,
}

if req.CommitID != nil {
repo.Target = gitprovider.CloneTargetCommit
repo.Sha = *req.CommitID
}

gitService := git.Service{
ProjectDir: req.Path,
}

var auth *http.BasicAuth

// Set authentication if provided
if req.Username != nil && req.Password != nil {
options.Auth = &http.BasicAuth{
auth = &http.BasicAuth{
Username: *req.Username,
Password: *req.Password,
}
}

// Handle branch or commit specification
if req.Branch != nil {
options.ReferenceName = plumbing.NewBranchReferenceName(*req.Branch)
options.SingleBranch = true
}

// Clone the repository
repo, err := git.PlainClone(req.Path, false, options)
err := gitService.CloneRepository(&repo, auth)
if err != nil {
c.AbortWithError(400, err)
return
}

// If a specific commit is requested, checkout that commit
if req.CommitID != nil {
worktree, err := repo.Worktree()
if err != nil {
c.AbortWithError(400, fmt.Errorf("failed to get worktree: %w", err))
return
}

hash := plumbing.NewHash(*req.CommitID)
err = worktree.Checkout(&git.CheckoutOptions{
Hash: hash,
})
if err != nil {
c.AbortWithError(400, fmt.Errorf("failed to checkout commit: %w", err))
return
}
}

c.Status(200)
}
19 changes: 6 additions & 13 deletions pkg/agent/toolbox/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package git
import (
"time"

"github.com/daytonaio/daytona/pkg/git"
"github.com/gin-gonic/gin"
"github.com/go-git/go-git/v5"
go_git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
)

Expand All @@ -18,19 +19,11 @@ func CommitChanges(c *gin.Context) {
return
}

repo, err := git.PlainOpen(req.Path)
if err != nil {
c.AbortWithError(400, err)
return
}

worktree, err := repo.Worktree()
if err != nil {
c.AbortWithError(400, err)
return
gitService := git.Service{
ProjectDir: req.Path,
}

commit, err := worktree.Commit(req.Message, &git.CommitOptions{
commitSha, err := gitService.Commit(req.Message, &go_git.CommitOptions{
Author: &object.Signature{
Name: req.Author,
Email: req.Email,
Expand All @@ -44,6 +37,6 @@ func CommitChanges(c *gin.Context) {
}

c.JSON(200, GitCommitResponse{
Hash: commit.String(),
Hash: commitSha,
})
}
22 changes: 4 additions & 18 deletions pkg/agent/toolbox/git/create_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
package git

import (
"github.com/daytonaio/daytona/pkg/git"
"github.com/gin-gonic/gin"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)

func CreateBranch(c *gin.Context) {
Expand All @@ -16,24 +15,11 @@ func CreateBranch(c *gin.Context) {
return
}

repo, err := git.PlainOpen(req.Path)
if err != nil {
c.AbortWithError(400, err)
return
gitService := git.Service{
ProjectDir: req.Path,
}

worktree, err := repo.Worktree()
if err != nil {
c.AbortWithError(400, err)
return
}

err = worktree.Checkout(&git.CheckoutOptions{
Create: true,
Branch: plumbing.NewBranchReferenceName(req.Name),
})

if err != nil {
if err := gitService.CreateBranch(req.Name); err != nil {
c.AbortWithError(400, err)
return
}
Expand Down
35 changes: 5 additions & 30 deletions pkg/agent/toolbox/git/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ package git
import (
"errors"

"github.com/daytonaio/daytona/pkg/git"
"github.com/gin-gonic/gin"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
)

func GetCommitHistory(c *gin.Context) {
Expand All @@ -18,39 +17,15 @@ func GetCommitHistory(c *gin.Context) {
return
}

repo, err := git.PlainOpen(path)
if err != nil {
c.AbortWithError(400, err)
return
}

ref, err := repo.Head()
if err != nil {
c.AbortWithError(400, err)
return
}

commits, err := repo.Log(&git.LogOptions{From: ref.Hash()})
if err != nil {
c.AbortWithError(400, err)
return
gitService := git.Service{
ProjectDir: path,
}

var history []GitCommitInfo
err = commits.ForEach(func(commit *object.Commit) error {
history = append(history, GitCommitInfo{
Hash: commit.Hash.String(),
Author: commit.Author.Name,
Email: commit.Author.Email,
Message: commit.Message,
Timestamp: commit.Author.When,
})
return nil
})
log, err := gitService.Log()
if err != nil {
c.AbortWithError(400, err)
return
}

c.JSON(200, history)
c.JSON(200, log)
}
21 changes: 4 additions & 17 deletions pkg/agent/toolbox/git/list_branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ package git
import (
"errors"

"github.com/daytonaio/daytona/pkg/git"
"github.com/gin-gonic/gin"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)

func ListBranches(c *gin.Context) {
Expand All @@ -18,23 +17,11 @@ func ListBranches(c *gin.Context) {
return
}

repo, err := git.PlainOpen(path)
if err != nil {
c.AbortWithError(400, err)
return
gitService := git.Service{
ProjectDir: path,
}

branches, err := repo.Branches()
if err != nil {
c.AbortWithError(400, err)
return
}

var branchList []string
err = branches.ForEach(func(ref *plumbing.Reference) error {
branchList = append(branchList, ref.Name().Short())
return nil
})
branchList, err := gitService.ListBranches()
if err != nil {
c.AbortWithError(400, err)
return
Expand Down
30 changes: 10 additions & 20 deletions pkg/agent/toolbox/git/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
package git

import (
"github.com/daytonaio/daytona/pkg/git"
"github.com/gin-gonic/gin"
"github.com/go-git/go-git/v5"
go_git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)

Expand All @@ -16,31 +17,20 @@ func PullChanges(c *gin.Context) {
return
}

repo, err := git.PlainOpen(req.Path)
if err != nil {
c.AbortWithError(400, err)
return
}

w, err := repo.Worktree()
if err != nil {
c.AbortWithError(400, err)
return
}

options := &git.PullOptions{
RemoteName: "origin",
}

var auth *http.BasicAuth
if req.Username != nil && req.Password != nil {
options.Auth = &http.BasicAuth{
auth = &http.BasicAuth{
Username: *req.Username,
Password: *req.Password,
}
}

err = w.Pull(options)
if err != nil && err != git.NoErrAlreadyUpToDate {
gitService := git.Service{
ProjectDir: req.Path,
}

err := gitService.Pull(auth)
if err != nil && err != go_git.NoErrAlreadyUpToDate {
c.AbortWithError(400, err)
return
}
Expand Down
21 changes: 10 additions & 11 deletions pkg/agent/toolbox/git/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
package git

import (
"github.com/daytonaio/daytona/pkg/git"
"github.com/gin-gonic/gin"
"github.com/go-git/go-git/v5"
go_git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)

Expand All @@ -16,22 +17,20 @@ func PushChanges(c *gin.Context) {
return
}

repo, err := git.PlainOpen(req.Path)
if err != nil {
c.AbortWithError(400, err)
return
}

options := &git.PushOptions{}
var auth *http.BasicAuth
if req.Username != nil && req.Password != nil {
options.Auth = &http.BasicAuth{
auth = &http.BasicAuth{
Username: *req.Username,
Password: *req.Password,
}
}

err = repo.Push(options)
if err != nil && err != git.NoErrAlreadyUpToDate {
gitService := git.Service{
ProjectDir: req.Path,
}

err := gitService.Push(auth)
if err != nil && err != go_git.NoErrAlreadyUpToDate {
c.AbortWithError(400, err)
return
}
Expand Down
Loading

0 comments on commit 12c0039

Please sign in to comment.