Skip to content

Commit

Permalink
Merge pull request #29 from srevinsaju/feature/srev/git-tags
Browse files Browse the repository at this point in the history
ref, tag and branch for git provider
  • Loading branch information
srevinsaju authored Aug 5, 2023
2 parents de2beab + 5e658fa commit 1e5cd04
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
11 changes: 6 additions & 5 deletions docs/src/configuration/data_git.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ data "git" "this" {

## Arguments Reference

- [`url`](#url) - The URL of the repository to clone
- [`tag`](#tag) - The tag to checkout
- [`branch`](#branch) - The branch to checkout
- [`url`](#url) - The URL of the repository to clone.
- [`tag`](#tag) - The tag to checkout. `tag` takes precedence over `branch`.
- [`ref`](#ref) - The reference to checkout, in the format `refs/tags/v3` for example. `ref` takes precedence over `tag` and `branch`.
- [`branch`](#branch) - The branch to checkout. `ref` and `tag` takes precedence over `branch`.
- [`destination`](#destination) - The destination directory to clone the repository to, defaults to `"memory"`, which clones into a temporary directory managed by `togomak`
- [`commit`](#commit) - The commit to checkout
- [`depth`](#depth) - The depth of the clone
- [`commit`](#commit) - The commit to checkout.
- [`depth`](#depth) - The depth of the clone.
- [`ca_bundle`](#ca_bundle) - The path to a CA bundle file or directory, (deprecated, does nothing).
- [`auth`](#auth) - The authentication credentials to use when cloning the repository. Structure [documented below](#auth)
- [`files`](#files) - The files to checkout from the repository. Accepts an array of file paths.
Expand Down
17 changes: 17 additions & 0 deletions examples/git_tags/togomak.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
togomak {
version = 1
}

data "git" "repo" {
url = "https://github.com/srevinsaju/togomak"
files = ["togomak.hcl"]
tag = "v1.2.0"
}

stage "example" {
name = "example"
script = <<-EOT
echo '${data.git.repo.files["togomak.hcl"]}'
EOT

}
21 changes: 18 additions & 3 deletions pkg/blocks/data/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type gitProviderConfig struct {
repo string
tag string
branch string
ref string
destination string
commit string

Expand All @@ -42,6 +43,7 @@ const (
GitBlockArgumentUrl = "url"
GitBlockArgumentTag = "tag"
GitBlockArgumentBranch = "branch"
GitBlockArgumentRef = "ref"
GitBlockArgumentDestination = "destination"
GitBlockArgumentCommit = "commit"
GitBlockArgumentDepth = "depth"
Expand Down Expand Up @@ -114,6 +116,13 @@ func (e *GitProvider) DecodeBody(body hcl.Body) hcl.Diagnostics {
diags = diags.Extend(d)
}

refAttr, ok := content.Attributes[GitBlockArgumentRef]
ref := cty.StringVal("")
if ok {
ref, d = refAttr.Expr.Value(hclContext)
diags = diags.Extend(d)
}

commitAttr, ok := content.Attributes[GitBlockArgumentCommit]
commit := cty.StringVal("")
if ok {
Expand Down Expand Up @@ -187,6 +196,7 @@ func (e *GitProvider) DecodeBody(body hcl.Body) hcl.Diagnostics {
tag: tag.AsString(),
branch: branch.AsString(),
commit: commit.AsString(),
ref: ref.AsString(),
destination: destination.AsString(),
depth: int(depthInt),
caBundle: []byte(caBundle.AsString()),
Expand Down Expand Up @@ -294,15 +304,20 @@ func (e *GitProvider) Attributes(ctx context.Context, id string) (map[string]cty
var cloneComplete = make(chan bool)
go e.clonePassiveProgressBar(logger, cloneComplete)

ref := e.cfg.ref
if e.cfg.tag != "" {
ref = e.cfg.tag
} else {
ref = e.cfg.branch
}
opts := git.CloneRepoOptions{
Depth: e.cfg.depth,
Branch: e.cfg.branch,
Branch: ref,
Bare: false,
// TODO: SkipTLSVerify: e.cfg.skipTLSVerify,
// TODO: make it configurable
Quiet: true,
}

// TODO: implement git submodules

destination, d := e.resolveDestination(ctx, id)
Expand Down Expand Up @@ -410,7 +425,7 @@ func (e *GitProvider) Attributes(ctx context.Context, id string) (map[string]cty
})
}

ref, err := repo.ResolveReference("HEAD")
ref, err = repo.ResolveReference("HEAD")
if err != nil {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Expand Down

0 comments on commit 1e5cd04

Please sign in to comment.