Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add ref option to stage options
  • Loading branch information
MissingRoberto committed Jul 24, 2025
commit 6d5fa3a49ebf3d254bf2e6a2e467d731052c4ac9
7 changes: 6 additions & 1 deletion pkg/registry/apis/provisioning/repository/git/staged.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ func NewStagedGitRepository(ctx context.Context, repo *gitRepository, opts repos
defer cancel()
}

ref, err := repo.client.GetRef(ctx, "refs/heads/"+repo.gitConfig.Branch)
branch := opts.Ref
if branch == "" {
branch = repo.gitConfig.Branch
}

ref, err := repo.client.GetRef(ctx, "refs/heads/"+branch)
if err != nil {
// TODO: opts.CreateIfNotExists doesn't make sense in the context of the staged repository
// because we only support the branch that is passed in.
Expand Down
40 changes: 33 additions & 7 deletions pkg/registry/apis/provisioning/repository/git/staged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (

func TestNewStagedGitRepository(t *testing.T) {
tests := []struct {
name string
setupMock func(*mocks.FakeClient)
opts repository.StageOptions
wantError error
name string
setupMock func(*mocks.FakeClient)
opts repository.StageOptions
wantError error
expectedRef string
}{
{
name: "succeeds with default options",
Expand All @@ -31,11 +32,29 @@ func TestNewStagedGitRepository(t *testing.T) {
mockWriter := &mocks.FakeStagedWriter{}
mockClient.NewStagedWriterReturns(mockWriter, nil)
},
expectedRef: "refs/heads/main",
opts: repository.StageOptions{
PushOnWrites: false,
},
wantError: nil,
},
{
name: "succeeds with custom ref option",
setupMock: func(mockClient *mocks.FakeClient) {
mockClient.GetRefReturns(nanogit.Ref{
Name: "refs/heads/custom",
Hash: hash.Hash{1, 2, 3},
}, nil)
mockWriter := &mocks.FakeStagedWriter{}
mockClient.NewStagedWriterReturns(mockWriter, nil)
},
expectedRef: "refs/heads/custom",
opts: repository.StageOptions{
Ref: "custom",
PushOnWrites: false,
},
wantError: nil,
},
{
name: "succeeds with BeforeFn",
setupMock: func(mockClient *mocks.FakeClient) {
Expand All @@ -49,7 +68,8 @@ func TestNewStagedGitRepository(t *testing.T) {
opts: repository.StageOptions{
PushOnWrites: false,
},
wantError: nil,
wantError: nil,
expectedRef: "refs/heads/main",
},
{
name: "succeeds with timeout",
Expand All @@ -65,7 +85,8 @@ func TestNewStagedGitRepository(t *testing.T) {
PushOnWrites: false,
Timeout: time.Second * 5,
},
wantError: nil,
expectedRef: "refs/heads/main",
wantError: nil,
},
{
name: "fails with GetRef error",
Expand All @@ -89,7 +110,8 @@ func TestNewStagedGitRepository(t *testing.T) {
opts: repository.StageOptions{
PushOnWrites: false,
},
wantError: errors.New("build staged writer: failed to create writer"),
wantError: errors.New("build staged writer: failed to create writer"),
expectedRef: "refs/heads/main",
},
}

Expand Down Expand Up @@ -123,6 +145,10 @@ func TestNewStagedGitRepository(t *testing.T) {
actualOpts := stagedRepo.(*stagedGitRepository).opts
require.Equal(t, tt.opts.PushOnWrites, actualOpts.PushOnWrites)
require.Equal(t, tt.opts.Timeout, actualOpts.Timeout)

// Verify the expected ref
_, ref := mockClient.GetRefArgsForCall(0)
require.Equal(t, tt.expectedRef, ref)
}
})
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/registry/apis/provisioning/repository/staged.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
)

type StageOptions struct {
// Ref custom ref
Ref string
// Push on every write
PushOnWrites bool
// Maximum time allowed for clone operation in seconds (0 means no limit)
Expand Down