Skip to content

Commit

Permalink
Merge pull request #33 from nlewo/fix-testing-back-to-main
Browse files Browse the repository at this point in the history
Allowing to go back to main from testing on no change
  • Loading branch information
nlewo authored Apr 27, 2024
2 parents fb0e1ff + d55102c commit 9ad1d95
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 12 deletions.
18 changes: 12 additions & 6 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func (r *repository) Fetch(remoteName string) (err error) {
}

func (r *repository) Update() error {
selectedCommitId := ""

// We first walk on all Main branches in order to get a commit
// from a Main branch. Once found, we could then walk on all
// Testing branches to get a testing commit on top of the Main
Expand Down Expand Up @@ -116,15 +118,15 @@ func (r *repository) Update() error {
remote.Main.CommitMsg = msg
remote.Main.OnTopOf = r.RepositoryStatus.MainCommitId

if r.RepositoryStatus.SelectedCommitId == "" {
r.RepositoryStatus.SelectedCommitId = head.String()
if selectedCommitId == "" {
selectedCommitId = head.String()
r.RepositoryStatus.SelectedCommitMsg = msg
r.RepositoryStatus.SelectedBranchName = remote.Main.Name
r.RepositoryStatus.SelectedRemoteName = remote.Name
r.RepositoryStatus.SelectedBranchIsTesting = false
}
if head.String() != r.RepositoryStatus.MainCommitId {
r.RepositoryStatus.SelectedCommitId = head.String()
selectedCommitId = head.String()
r.RepositoryStatus.SelectedCommitMsg = msg
r.RepositoryStatus.SelectedBranchName = remote.Main.Name
r.RepositoryStatus.SelectedBranchIsTesting = false
Expand Down Expand Up @@ -166,8 +168,8 @@ func (r *repository) Update() error {
remote.Testing.CommitMsg = msg
remote.Testing.OnTopOf = r.RepositoryStatus.MainCommitId

if head.String() != r.RepositoryStatus.SelectedCommitId && head.String() != r.RepositoryStatus.MainCommitId {
r.RepositoryStatus.SelectedCommitId = head.String()
if head.String() != selectedCommitId && head.String() != r.RepositoryStatus.MainCommitId {
selectedCommitId = head.String()
r.RepositoryStatus.SelectedCommitMsg = msg
r.RepositoryStatus.SelectedBranchName = remote.Testing.Name
r.RepositoryStatus.SelectedBranchIsTesting = true
Expand All @@ -176,7 +178,11 @@ func (r *repository) Update() error {
}
}

if err := hardReset(*r, plumbing.NewHash(r.RepositoryStatus.SelectedCommitId)); err != nil {
if selectedCommitId != "" {
r.RepositoryStatus.SelectedCommitId = selectedCommitId
}

if err := hardReset(*r, plumbing.NewHash(selectedCommitId)); err != nil {
r.RepositoryStatus.Error = err
r.RepositoryStatus.ErrorMsg = err.Error()
return err
Expand Down
72 changes: 66 additions & 6 deletions internal/repository/repository_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package repository

import (
"testing"

"github.com/go-git/go-git/v5/plumbing"
"github.com/nlewo/comin/internal/types"
"github.com/stretchr/testify/assert"
"testing"
)

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -272,16 +273,15 @@ func TestMultipleRemote(t *testing.T) {
assert.Equal(t, "main", r.RepositoryStatus.SelectedBranchName)
assert.Equal(t, "r2", r.RepositoryStatus.SelectedRemoteName)

// r1/main: c1 - c2 - c3 - c4 - c5
// r2/main: c1 - c2 - c3 - c4 - *c5
// FIXME: maybe it could be better to switch to the first remote, ie r1/main
// r1/main: c1 - c2 - c3 - c4 - *c5
// r2/main: c1 - c2 - c3 - c4 - c5
newCommitId, err = commitFile(r1, r1Dir, "main", "file-5")
assert.Nil(t, err)
_ = r.Fetch("")
_ = r.Update()
assert.Equal(t, newCommitId, r.RepositoryStatus.SelectedCommitId)
assert.Equal(t, "main", r.RepositoryStatus.SelectedBranchName)
assert.Equal(t, "r2", r.RepositoryStatus.SelectedRemoteName)
assert.Equal(t, "r1", r.RepositoryStatus.SelectedRemoteName)

// r1/main: c1 - c2 - c3 - c4 - c5 - c6
// r2/main: c1 - c2 - c3 - c4 - c5 - c6
Expand Down Expand Up @@ -413,9 +413,9 @@ func TestTestingSwitch(t *testing.T) {
// r2/testing: c1 - c2 - c3 - *c4
_ = r.Fetch("")
_ = r.Update()
assert.Equal(t, c4, r.RepositoryStatus.SelectedCommitId)
assert.Equal(t, "testing", r.RepositoryStatus.SelectedBranchName)
assert.Equal(t, "r2", r.RepositoryStatus.SelectedRemoteName)
assert.Equal(t, c4, r.RepositoryStatus.SelectedCommitId)

// r1/main: c1 - c2 - c3
// r1/testing: c1 - c2 - c3
Expand Down Expand Up @@ -647,3 +647,63 @@ func TestRepositoryUpdateTesting(t *testing.T) {
assert.Equal(t, "main", r.RepositoryStatus.SelectedBranchName)
assert.Equal(t, "origin", r.RepositoryStatus.SelectedRemoteName)
}

func TestTestingHardReset(t *testing.T) {
var err error
r1Dir := t.TempDir()
cominRepositoryDir := t.TempDir()
r1, err := initRemoteRepostiory(r1Dir, true)
cMain := HeadCommitId(r1)
gitConfig := types.GitConfig{
Path: cominRepositoryDir,
Remotes: []types.Remote{
{
Name: "r1",
URL: r1Dir,
Branches: types.Branches{
Main: types.Branch{
Name: "main",
},
Testing: types.Branch{
Name: "testing",
},
},
Timeout: 30,
},
},
}
r, err := New(gitConfig, RepositoryStatus{})
assert.Nil(t, err)
// r1/main: c1 - c2 - *c3
// r1/testing: c1 - c2 - c3
err = r.Fetch("")
assert.Nil(t, err)
err = r.Update()
assert.Nil(t, err)
assert.Equal(t, cMain, r.RepositoryStatus.SelectedCommitId)
assert.Equal(t, "main", r.RepositoryStatus.SelectedBranchName)
assert.Equal(t, "r1", r.RepositoryStatus.SelectedRemoteName)

// r1/main: c1 - c2 - c3
// r1/testing: c1 - c2 - c3 - *c4
c4, err := commitFile(r1, r1Dir, "testing", "file-4")
err = r.Fetch("")
assert.Nil(t, err)
err = r.Update()
assert.Nil(t, err)
assert.Equal(t, c4, r.RepositoryStatus.SelectedCommitId)
assert.Equal(t, "testing", r.RepositoryStatus.SelectedBranchName)
assert.Equal(t, "r1", r.RepositoryStatus.SelectedRemoteName)

// r1/main: c1 - c2 - *c3
// r1/testing: c1 - c2 - c3
ref := plumbing.NewHashReference("refs/heads/testing", plumbing.NewHash(cMain))
r1.Storer.SetReference(ref)
err = r.Fetch("")
assert.Nil(t, err)
err = r.Update()
assert.Nil(t, err)
assert.Equal(t, cMain, r.RepositoryStatus.SelectedCommitId)
assert.Equal(t, "main", r.RepositoryStatus.SelectedBranchName)
assert.Equal(t, "r1", r.RepositoryStatus.SelectedRemoteName)
}

0 comments on commit 9ad1d95

Please sign in to comment.