Skip to content

Commit 5232ee9

Browse files
committed
docs: document methods and initialization in the git package
- Add comments to describe the purpose and functionality of various methods in the `Command` struct - Document the `excludeFiles`, `diffNames`, `diffFiles`, `hookPath`, `gitDir`, `commit`, `Commit`, `DiffFiles`, `InstallHook`, `UninstallHook`, and `New` methods - Add package-level documentation for the `git` package in `hook.go` - Add comments for `HookPrepareCommitMessageTemplate` and `CommitMessageTemplate` constants - Document the initialization process for Git hook templates in the `init` function Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 3ea74f9 commit 5232ee9

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

git/git.go

+24
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type Command struct {
2828
isAmend bool
2929
}
3030

31+
// excludeFiles returns a list of files to be excluded from git operations.
32+
// It prepends each file in the excludeList with the exclude and top options.
3133
func (c *Command) excludeFiles() []string {
3234
var excludedFiles []string
3335
for _, f := range c.excludeList {
@@ -36,6 +38,8 @@ func (c *Command) excludeFiles() []string {
3638
return excludedFiles
3739
}
3840

41+
// diffNames generates the git command to list the names of changed files.
42+
// It includes options to handle amended commits and staged changes.
3943
func (c *Command) diffNames() *exec.Cmd {
4044
args := []string{
4145
"diff",
@@ -57,6 +61,9 @@ func (c *Command) diffNames() *exec.Cmd {
5761
)
5862
}
5963

64+
// diffFiles generates the git command to show the differences between files.
65+
// It includes options to ignore whitespace changes, use minimal diff algorithm,
66+
// and set the number of context lines.
6067
func (c *Command) diffFiles() *exec.Cmd {
6168
args := []string{
6269
"diff",
@@ -80,6 +87,8 @@ func (c *Command) diffFiles() *exec.Cmd {
8087
)
8188
}
8289

90+
// hookPath generates the git command to get the path of the hooks directory.
91+
// This is used to locate where git hooks are stored.
8392
func (c *Command) hookPath() *exec.Cmd {
8493
args := []string{
8594
"rev-parse",
@@ -93,6 +102,8 @@ func (c *Command) hookPath() *exec.Cmd {
93102
)
94103
}
95104

105+
// gitDir generates the git command to get the path of the git directory.
106+
// This is used to determine the location of the .git directory.
96107
func (c *Command) gitDir() *exec.Cmd {
97108
args := []string{
98109
"rev-parse",
@@ -105,6 +116,8 @@ func (c *Command) gitDir() *exec.Cmd {
105116
)
106117
}
107118

119+
// commit generates the git command to create a commit with the provided message.
120+
// It includes options to skip pre-commit hooks, sign off the commit, and handle amendments.
108121
func (c *Command) commit(val string) *exec.Cmd {
109122
args := []string{
110123
"commit",
@@ -123,6 +136,8 @@ func (c *Command) commit(val string) *exec.Cmd {
123136
)
124137
}
125138

139+
// Commit creates a git commit with the provided message and returns the output or an error.
140+
// It uses the commit method to generate the git command and execute it.
126141
func (c *Command) Commit(val string) (string, error) {
127142
output, err := c.commit(val).Output()
128143
if err != nil {
@@ -145,6 +160,9 @@ func (c *Command) GitDir() (string, error) {
145160
// Diff compares the differences between two sets of data.
146161
// It returns a string representing the differences and an error.
147162
// If there are no differences, it returns an empty string and an error.
163+
// DiffFiles compares the differences between two sets of data and returns the differences as a string and an error.
164+
// It first lists the names of changed files and then shows the differences between them.
165+
// If there are no staged changes, it returns an error message.
148166
func (c *Command) DiffFiles() (string, error) {
149167
output, err := c.diffNames().Output()
150168
if err != nil {
@@ -162,6 +180,8 @@ func (c *Command) DiffFiles() (string, error) {
162180
return string(output), nil
163181
}
164182

183+
// InstallHook installs the prepare-commit-msg hook if it doesn't already exist.
184+
// It retrieves the hooks directory path, checks if the hook file exists, and writes the hook file with executable permissions.
165185
func (c *Command) InstallHook() error {
166186
hookPath, err := c.hookPath().Output()
167187
if err != nil {
@@ -182,6 +202,8 @@ func (c *Command) InstallHook() error {
182202
return os.WriteFile(target, content, 0o755) //nolint:gosec
183203
}
184204

205+
// UninstallHook removes the prepare-commit-msg hook if it exists.
206+
// It retrieves the hooks directory path, checks if the hook file exists, and removes the hook file.
185207
func (c *Command) UninstallHook() error {
186208
hookPath, err := c.hookPath().Output()
187209
if err != nil {
@@ -195,6 +217,8 @@ func (c *Command) UninstallHook() error {
195217
return os.Remove(target)
196218
}
197219

220+
// New creates a new Command object with the provided options.
221+
// It applies each option to the config object and initializes the Command object with the configurations.
198222
func New(opts ...Option) *Command {
199223
// Instantiate a new config object with default values
200224
cfg := &config{}

git/hook.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
// Package git provides functionality for working with git repositories.
2+
//
3+
// This package includes constants defining template names for git hooks and
4+
// initializes the templates from embedded files on package initialization.
5+
//
6+
// The package embeds files from a 'templates/' directory which contain hook templates
7+
// that can be used for git operations such as preparing commit messages.
18
package git
29

310
import (
@@ -11,10 +18,14 @@ import (
1118
var files embed.FS
1219

1320
const (
21+
// HookPrepareCommitMessageTemplate is the template for the prepare-commit-msg hook
1422
HookPrepareCommitMessageTemplate = "prepare-commit-msg"
15-
CommitMessageTemplate = "commit-msg.tmpl"
23+
// CommitMessageTemplate is the template for the commit message
24+
CommitMessageTemplate = "commit-msg.tmpl"
1625
)
1726

27+
// init initializes the Git hook templates by loading them from embedded files.
28+
// If there's an error loading the templates, the function logs a fatal error and terminates the program.
1829
func init() { //nolint:gochecknoinits
1930
if err := util.LoadTemplates(files); err != nil {
2031
log.Fatal(err)

0 commit comments

Comments
 (0)