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

fix git_add resource #46

Merged
merged 4 commits into from Sep 9, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 17 additions & 15 deletions docs/resources/add.md
Expand Up @@ -3,38 +3,42 @@
page_title: "git_add Resource - terraform-provider-git"
subcategory: ""
description: |-
Add file contents to the index using git add.
Add file contents to the index similar to git add.
---

# git_add (Resource)

Add file contents to the index using `git add`.
Add file contents to the index similar to `git add`.

## Example Usage

```terraform
# add single file
resource "git_add" "file" {
resource "git_add" "single_file" {
directory = "/path/to/git/repository"
exact_path = "path/to/file/in/repository"
add_paths = ["path/to/file/in/repository"]
}

# add all files in directory and its subdirectory recursively
resource "git_add" "directory" {
resource "git_add" "single_directory" {
directory = "/path/to/git/repository"
exact_path = "path/to/directory/in/repository"
add_paths = ["path/to/directory/in/repository"]
}

# add files matching pattern
resource "git_add" "glob" {
resource "git_add" "glob_pattern" {
directory = "/path/to/git/repository"
glob_path = "path/*/in/repo*"
add_paths = ["path/*/in/repo*"]
}

# add all modified files
resource "git_add" "all" {
# mix exact paths and glob patterns
resource "git_add" "glob_pattern" {
directory = "/path/to/git/repository"
all = true
add_paths = [
"path/*/in/repo*",
"another/path/to/file/here",
"this/could/be/a/directory",
]
}
```

Expand All @@ -47,12 +51,10 @@ resource "git_add" "all" {

### Optional

- `all` (Boolean) Update the index not only where the working tree has a file matching `exact_path` or `glob_path` but also where the index already has an entry. This adds, modifies, and removes index entries to match the working tree. If no paths are given, all files in the entire working tree are updated. Defaults to `true`.
- `exact_path` (String) The exact filepath to the file or directory to be added. Conflicts with `glob_path`.
- `glob_path` (String) The glob pattern of files or directories to be added. Conflicts with `exact_path`.
- `add_paths` (List of String) The paths to add to the Git index. Values can be exact paths or glob patterns.

### Read-Only

- `id` (String) The same value as the `directory` attribute.
- `id` (Number) The timestamp of the last addition in Unix nanoseconds.


22 changes: 13 additions & 9 deletions examples/resources/git_add/resource.tf
@@ -1,23 +1,27 @@
# add single file
resource "git_add" "file" {
resource "git_add" "single_file" {
directory = "/path/to/git/repository"
exact_path = "path/to/file/in/repository"
add_paths = ["path/to/file/in/repository"]
}

# add all files in directory and its subdirectory recursively
resource "git_add" "directory" {
resource "git_add" "single_directory" {
directory = "/path/to/git/repository"
exact_path = "path/to/directory/in/repository"
add_paths = ["path/to/directory/in/repository"]
}

# add files matching pattern
resource "git_add" "glob" {
resource "git_add" "glob_pattern" {
directory = "/path/to/git/repository"
glob_path = "path/*/in/repo*"
add_paths = ["path/*/in/repo*"]
}

# add all modified files
resource "git_add" "all" {
# mix exact paths and glob patterns
resource "git_add" "glob_pattern" {
directory = "/path/to/git/repository"
all = true
add_paths = [
"path/*/in/repo*",
"another/path/to/file/here",
"this/could/be/a/directory",
]
}
Expand Up @@ -7,7 +7,6 @@ package modifiers

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)
Expand All @@ -22,7 +21,7 @@ type defaultValueAttributePlanModifier struct {
defaultValue attr.Value
}

func (d *defaultValueAttributePlanModifier) Description(ctx context.Context) string {
func (d *defaultValueAttributePlanModifier) Description(_ context.Context) string {
return "If the config does not contain a value, a default will be set using defaultValue."
}

Expand All @@ -33,9 +32,9 @@ func (d *defaultValueAttributePlanModifier) MarkdownDescription(ctx context.Cont
// Modify checks that the value of the attribute in the configuration and assigns the default value if
// the value in the config is null. This is a destructive operation in that it will overwrite any value
// present in the plan.
func (d *defaultValueAttributePlanModifier) Modify(ctx context.Context, req tfsdk.ModifyAttributePlanRequest, resp *tfsdk.ModifyAttributePlanResponse) {
// If the attribute configuration is not null, we are done here
func (d *defaultValueAttributePlanModifier) Modify(_ context.Context, req tfsdk.ModifyAttributePlanRequest, resp *tfsdk.ModifyAttributePlanResponse) {
if !req.AttributeConfig.IsNull() {
// If the attribute configuration is not null, we are done here
return
}

Expand Down
27 changes: 16 additions & 11 deletions internal/provider/git_worktree.go
Expand Up @@ -6,9 +6,11 @@
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/diag"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

func getWorktree(repository *git.Repository, diag *diag.Diagnostics) (*git.Worktree, error) {
Expand All @@ -25,26 +27,29 @@ func getWorktree(repository *git.Repository, diag *diag.Diagnostics) (*git.Workt
return nil, err
}

func addPaths(worktree *git.Worktree, options *git.AddOptions, diag *diag.Diagnostics) error {
err := worktree.AddWithOptions(options)
func createCommit(worktree *git.Worktree, message string, options *git.CommitOptions, diag *diag.Diagnostics) *plumbing.Hash {
hash, err := worktree.Commit(message, options)
if err != nil {
diag.AddError(
"Cannot add paths to worktree",
"The given paths cannot be added to the worktree because of: "+err.Error(),
"Cannot create commit",
"Could not create commit because of: "+err.Error(),
)
return err
return nil
}
return nil
return &hash
}

func createCommit(worktree *git.Worktree, message string, options *git.CommitOptions, diag *diag.Diagnostics) *plumbing.Hash {
hash, err := worktree.Commit(message, options)
func getStatus(ctx context.Context, worktree *git.Worktree, diag *diag.Diagnostics) git.Status {
status, err := worktree.Status()
if err != nil {
diag.AddError(
"Cannot create commit",
"Could not create commit because of: "+err.Error(),
"Cannot read status",
"Could not read status because of: "+err.Error(),
)
return nil
}
return &hash
tflog.Trace(ctx, "read status", map[string]interface{}{
"status": status.String(),
})
return status
}