Skip to content

Commit

Permalink
support empty repositories w/o a single commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Hoß authored and sebhoss committed Aug 25, 2022
1 parent e7f2ac7 commit 3c8f0bc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
4 changes: 2 additions & 2 deletions docs/data-sources/repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ data "git_repository" "repository" {

### Read-Only

- `branch` (String) The current branch of the given Git repository. Note that repositories in detached state might not have a branch associated with them.
- `branch` (String) The name of the current branch of the given Git repository. Note that repositories in detached state might not have a branch associated with them.
- `id` (String) The same value as the `directory` attribute.
- `sha1` (String) The current SHA1 of the `HEAD` of the given Git repository.
- `sha1` (String) The SHA1 of the current `HEAD` of the given Git repository.


40 changes: 22 additions & 18 deletions internal/provider/data_source_git_repository.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/plumbing"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
Expand Down Expand Up @@ -47,12 +48,12 @@ func (r *dataSourceGitRepositoryType) GetSchema(_ context.Context) (tfsdk.Schema
Computed: true,
},
"branch": {
Description: "The current branch of the given Git repository. Note that repositories in detached state might not have a branch associated with them.",
Description: "The name of the current branch of the given Git repository. Note that repositories in detached state might not have a branch associated with them.",
Type: types.StringType,
Computed: true,
},
"sha1": {
MarkdownDescription: "The current SHA1 of the `HEAD` of the given Git repository.",
MarkdownDescription: "The SHA1 of the current `HEAD` of the given Git repository.",
Type: types.StringType,
Computed: true,
},
Expand All @@ -67,10 +68,9 @@ func (r *dataSourceGitRepositoryType) NewDataSource(_ context.Context, p provide
}

func (r *dataSourceGitRepository) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
tflog.Debug(ctx, "Reading Git repository")
tflog.Debug(ctx, "Read git_repository")

var inputs dataSourceGitRepositorySchema
var state dataSourceGitRepositorySchema

diags := req.Config.Get(ctx, &inputs)
resp.Diagnostics.Append(diags...)
Expand All @@ -85,27 +85,31 @@ func (r *dataSourceGitRepository) Read(ctx context.Context, req datasource.ReadR
return
}

var state dataSourceGitRepositorySchema
state.Directory = inputs.Directory
state.Id = inputs.Directory

head, err := repository.Head()
if err != nil {
if err == plumbing.ErrReferenceNotFound {
state.SHA1 = types.String{Null: true}
state.Branch = types.String{Null: true}
} else if err != nil {
resp.Diagnostics.AddError(
"Error reading HEAD reference",
"Could not read HEAD of ["+directory+"] because of: "+err.Error(),
)
return
}

tflog.Trace(ctx, "read HEAD reference", map[string]interface{}{
"directory": directory,
"head": head.String(),
})

state.Directory = inputs.Directory
state.Id = inputs.Directory
state.SHA1 = types.String{Value: head.Hash().String()}
if head.Name().IsBranch() {
state.Branch = types.String{Value: head.Name().Short()}
} else {
state.Branch = types.String{Null: true}
tflog.Trace(ctx, "read HEAD reference", map[string]interface{}{
"directory": directory,
"head": head.String(),
})
state.SHA1 = types.String{Value: head.Hash().String()}
if head.Name().IsBranch() {
state.Branch = types.String{Value: head.Name().Short()}
} else {
state.Branch = types.String{Null: true}
}
}

diags = resp.State.Set(ctx, &state)
Expand Down
25 changes: 25 additions & 0 deletions internal/provider/data_source_git_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,31 @@ func TestDataSourceGitRepository_Detached(t *testing.T) {
})
}

func TestDataSourceGitRepository_Empty(t *testing.T) {
t.Parallel()
directory, _ := testRepository(t)
defer os.RemoveAll(directory)

resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testProviderFactories(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
data "git_repository" "test" {
directory = "%s"
}
`, directory),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.git_repository.test", "directory", directory),
resource.TestCheckResourceAttr("data.git_repository.test", "id", directory),
resource.TestCheckNoResourceAttr("data.git_repository.test", "branch"),
resource.TestCheckNoResourceAttr("data.git_repository.test", "sha1"),
),
},
},
})
}

func TestDataSourceGitRepository_InvalidRepository(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
Expand Down

0 comments on commit 3c8f0bc

Please sign in to comment.