-
Notifications
You must be signed in to change notification settings - Fork 1
/
git_worktree.go
55 lines (50 loc) · 1.35 KB
/
git_worktree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* SPDX-FileCopyrightText: The terraform-provider-git Authors
* SPDX-License-Identifier: 0BSD
*/
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) {
worktree, err := repository.Worktree()
if err == git.ErrIsBareRepository {
return nil, nil
} else if err == nil {
return worktree, nil
}
diag.AddError(
"Cannot read worktree",
"Could not read worktree because of: "+err.Error(),
)
return nil, err
}
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 create commit",
"Could not create commit because of: "+err.Error(),
)
return nil
}
return &hash
}
func getStatus(ctx context.Context, worktree *git.Worktree, diag *diag.Diagnostics) git.Status {
status, err := worktree.Status()
if err != nil {
diag.AddError(
"Cannot read status",
"Could not read status because of: "+err.Error(),
)
return nil
}
tflog.Trace(ctx, "read status", map[string]interface{}{
"status": status.String(),
})
return status
}