-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage.go
89 lines (74 loc) · 2.98 KB
/
stage.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Copyright (c) 2023 Purple Clay
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package git
import "strings"
// StageOption provides a way for setting specific options during a stage
// operation. Each supported option can customize the way files are staged
// within the current repository (working directory)
type StageOption func(*stageOptions)
type stageOptions struct {
PathSpecs []string
}
// WithPathSpecs permits a series of [PathSpecs] (or globs) to be defined
// that will stage any matching files within the current repository
// (working directory). Paths to files and folders are relative to the
// root of the repository. All leading and trailing whitespace will be
// trimmed from the file paths, allowing empty paths to be ignored
//
// [PathSpecs]: https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefpathspecapathspec
func WithPathSpecs(specs ...string) StageOption {
return func(opts *stageOptions) {
opts.PathSpecs = trim(specs...)
}
}
// Stage changes to any file or folder within the current repository
// (working directory) ready for inclusion in the next commit. Options
// can be provided to further configure stage semantics. By default,
// all changes will be staged ready for the next commit.
func (c *Client) Stage(opts ...StageOption) (string, error) {
options := &stageOptions{}
for _, opt := range opts {
opt(options)
}
// Build command based on the provided options
var stageCmd strings.Builder
stageCmd.WriteString("git add ")
if len(options.PathSpecs) > 0 {
stageCmd.WriteString("--")
for _, spec := range options.PathSpecs {
stageCmd.WriteString(" ")
stageCmd.WriteString(spec)
}
} else {
stageCmd.WriteString("--all")
}
return c.exec(stageCmd.String())
}
// Staged retrieves a list of all currently staged file changes within the
// current repository
func (c *Client) Staged() ([]string, error) {
diff, err := c.exec("git diff --staged --name-only")
if err != nil {
return nil, err
}
if diff == "" {
return nil, nil
}
return strings.Split(diff, "\n"), nil
}