daytonaio / daytona

The Open Source Dev Environment Manager.
https://daytona.io
Apache License 2.0
5.66k stars 220 forks source link

Git service doesn't clone branch when it should #553

Closed Tpuljak closed 1 month ago

Tpuljak commented 1 month ago

Describe the bug An example of a valid git repository passed to a project would be:

"repository": {
        "branch": "branch-test",
        "id": "",
        "name": "",
        "owner": "daytonaio",
        "prNumber": 8,
        "sha": "693eebceea8d7590eb965c08645500e31a477bdb",
        "source": "github.com",
        "url": "https://github.com/daytonaio/daytona.git"
      }

But because of the shouldCloneBranch method of the git service, the branch is not cloned.

func (s *Service) shouldCloneBranch(project *workspace.Project) bool {
    if project.Repository.Branch == nil || *project.Repository.Branch == "" {
        return false
    }

    if project.Repository.Sha == "" {
        return true
    }

    return *project.Repository.Branch == project.Repository.Sha
}

To Reproduce Steps to reproduce the behavior:

  1. Create a workspace with a project that has a branch attached.
  2. Observe that the repo will not be cloned on the appropriate branch

Expected behavior The repo should be cloned on the appropriate branch. The shouldCloneBranch method should be changed to:

func (s *Service) shouldCloneBranch(project *workspace.Project) bool {
    if project.Repository.Branch == nil || *project.Repository.Branch == "" {
        return false
    }

    if project.Repository.Sha == "" {
        return true
    }

    return *project.Repository.Branch != project.Repository.Sha
}

We need to check that the branch is different from the SHA because in the case where a user wants to checkout a specific commit, the SHA and branch will be the same and in that case the shouldCheckoutSha function should return true.

Desktop (please complete the following information):