Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow selecting local branches in git_branch data source #24

Merged
merged 1 commit into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions internal/provider/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ func testCreateBranch(t *testing.T, repository *git.Repository, branch *config.B
if err != nil {
t.Fatal(err)
}
head, err := repository.Head()
if err != nil {
t.Fatal(err)
}
reference := plumbing.NewHashReference(plumbing.NewBranchReferenceName(branch.Name), head.Hash())
err = repository.Storer.SetReference(reference)
if err != nil {
t.Fatal(err)
}
}

func testWorktree(t *testing.T, repository *git.Repository) *git.Worktree {
Expand Down
47 changes: 30 additions & 17 deletions internal/provider/data_source_git_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package provider

import (
"context"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand Down Expand Up @@ -102,20 +103,6 @@ func (r *dataSourceGitBranch) Read(ctx context.Context, req datasource.ReadReque
return
}

branch, err := repository.Branch(name)
if err != nil {
resp.Diagnostics.AddError(
"Cannot read branch",
"Could not read branch ["+name+"] of ["+directory+"] because of: "+err.Error(),
)
return
}

tflog.Trace(ctx, "read branch", map[string]interface{}{
"directory": directory,
"branch": branch.Name,
})

branches, err := repository.Branches()
if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -124,9 +111,30 @@ func (r *dataSourceGitBranch) Read(ctx context.Context, req datasource.ReadReque
)
return
}
state.SHA1 = types.String{Unknown: true}
if err := branches.ForEach(func(ref *plumbing.Reference) error {
if ref.Name().Short() == branch.Name {
if ref.Name().Short() == name {
state.SHA1 = types.String{Value: ref.Hash().String()}

branch, err := repository.Branch(name)
if branch != nil {
state.Remote = types.String{Value: branch.Remote}
state.Rebase = types.String{Value: branch.Rebase}
} else if err == git.ErrBranchNotFound {
state.Remote = types.String{Null: true}
state.Rebase = types.String{Null: true}
} else if err != nil {
resp.Diagnostics.AddError(
"Cannot read branch",
"Could not read branch ["+name+"] of ["+directory+"] because of: "+err.Error(),
)
return err
}

tflog.Trace(ctx, "read branch", map[string]interface{}{
"directory": directory,
"branch": name,
})
}
return nil
}); err != nil {
Expand All @@ -137,11 +145,16 @@ func (r *dataSourceGitBranch) Read(ctx context.Context, req datasource.ReadReque
return
}

if state.SHA1.IsNull() || state.SHA1.IsUnknown() {
resp.Diagnostics.AddError(
"Cannot read branch",
"The branch ["+name+"] does not exist in ["+directory+"]",
)
}

state.Directory = inputs.Directory
state.Id = inputs.Name
state.Name = inputs.Name
state.Remote = types.String{Value: branch.Remote}
state.Rebase = types.String{Value: branch.Rebase}

diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/data_source_git_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func TestDataSourceGitBranch(t *testing.T) {
t.Parallel()
directory, repository := testRepository(t)
defer os.RemoveAll(directory)
worktree := testWorktree(t, repository)
testAddAndCommitNewFile(t, worktree, "some-file")
name := "name-of-branch"
remote := "origin"
rebase := "true"
Expand Down