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 to specify nested folder for git repository directories #316

Merged
merged 4 commits into from
Dec 15, 2024
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
3 changes: 2 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: The terraform-provider-git Authors
# SPDX-License-Identifier: 0BSD

version: 2
before:
hooks:
- go mod tidy
Expand Down Expand Up @@ -50,5 +51,5 @@ release:
- glob: 'terraform-registry-manifest.json'
name_template: '{{ .ProjectName }}_{{ .Version }}_manifest.json'
changelog:
skip: false
disable: false
use: github-native
34 changes: 34 additions & 0 deletions internal/provider/data_source_remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"fmt"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/metio/terraform-provider-git/internal/testutils"
"os"
"path/filepath"
"regexp"
"testing"
)
Expand Down Expand Up @@ -111,3 +113,35 @@ func TestDataSourceGitRemote_MissingRepository(t *testing.T) {
},
})
}

func TestDataSourceGitRemote_Issue314(t *testing.T) {
t.Parallel()
directory, repository := testutils.CreateRepository(t)
nested := filepath.Join(directory, "nested")
err := os.MkdirAll(nested, os.ModePerm)
if err != nil {
t.Errorf("cannot create nested directory: %v", err)
}
remote := "example"
testutils.CreateRemote(t, repository, remote)

resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testutils.ProviderFactories(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
data "git_remote" "test" {
directory = "%s"
name = "%s"
}
`, filepath.ToSlash(nested), remote),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.git_remote.test", "directory", filepath.ToSlash(nested)),
resource.TestCheckResourceAttr("data.git_remote.test", "id", remote),
resource.TestCheckResourceAttr("data.git_remote.test", "name", remote),
resource.TestCheckResourceAttr("data.git_remote.test", "urls.#", "1"),
),
},
},
})
}
24 changes: 18 additions & 6 deletions internal/provider/git_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@ import (
)

func openRepository(ctx context.Context, directory string, diag *diag.Diagnostics) *git.Repository {
repository, err := git.PlainOpen(directory)
repository, err := git.PlainOpenWithOptions(directory, &git.PlainOpenOptions{
DetectDotGit: false,
EnableDotGitCommonDir: false,
})
if err != nil {
diag.AddError(
"Cannot open repository",
"Could not open git repository ["+directory+"] because of: "+err.Error(),
)
return nil
// we are trying to open the repository again, this time by searching upward for a .git folder
// this is necessary to support 'directory' values that point to any location within a git repository
// we cannot always enable this detection mechanism because it fails for bare repositories.
repository, err = git.PlainOpenWithOptions(directory, &git.PlainOpenOptions{
DetectDotGit: true,
EnableDotGitCommonDir: false,
})
if err != nil {
diag.AddError(
"Cannot open repository",
"Could not open git repository ["+directory+"] because of: "+err.Error(),
)
return nil
}
}
tflog.Trace(ctx, "opened repository", map[string]interface{}{
"directory": directory,
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestResourceGitPush_Directory_Invalid(t *testing.T) {
{
Config: fmt.Sprintf(`
resource "git_push" "test" {
directory = "does/not/exist"
directory = "/does/not/exist"
refspecs = ["%s"]
}
`, "master:master"),
Expand Down
Loading