11package git
22
33import (
4+ "context"
45 "errors"
56 "fmt"
67 "os"
@@ -40,7 +41,7 @@ func (c *Command) excludeFiles() []string {
4041
4142// diffNames generates the git command to list the names of changed files.
4243// It includes options to handle amended commits and staged changes.
43- func (c * Command ) diffNames () * exec.Cmd {
44+ func (c * Command ) diffNames (ctx context. Context ) * exec.Cmd {
4445 args := []string {
4546 "diff" ,
4647 "--name-only" ,
@@ -55,7 +56,8 @@ func (c *Command) diffNames() *exec.Cmd {
5556 excludedFiles := c .excludeFiles ()
5657 args = append (args , excludedFiles ... )
5758
58- return exec .Command (
59+ return exec .CommandContext (
60+ ctx ,
5961 "git" ,
6062 args ... ,
6163 )
@@ -64,7 +66,7 @@ func (c *Command) diffNames() *exec.Cmd {
6466// diffFiles generates the git command to show the differences between files.
6567// It includes options to ignore whitespace changes, use minimal diff algorithm,
6668// and set the number of context lines.
67- func (c * Command ) diffFiles () * exec.Cmd {
69+ func (c * Command ) diffFiles (ctx context. Context ) * exec.Cmd {
6870 args := []string {
6971 "diff" ,
7072 "--ignore-all-space" ,
@@ -81,44 +83,47 @@ func (c *Command) diffFiles() *exec.Cmd {
8183 excludedFiles := c .excludeFiles ()
8284 args = append (args , excludedFiles ... )
8385
84- return exec .Command (
86+ return exec .CommandContext (
87+ ctx ,
8588 "git" ,
8689 args ... ,
8790 )
8891}
8992
9093// hookPath generates the git command to get the path of the hooks directory.
9194// This is used to locate where git hooks are stored.
92- func (c * Command ) hookPath () * exec.Cmd {
95+ func (c * Command ) hookPath (ctx context. Context ) * exec.Cmd {
9396 args := []string {
9497 "rev-parse" ,
9598 "--git-path" ,
9699 "hooks" ,
97100 }
98101
99- return exec .Command (
102+ return exec .CommandContext (
103+ ctx ,
100104 "git" ,
101105 args ... ,
102106 )
103107}
104108
105109// gitDir generates the git command to get the path of the git directory.
106110// This is used to determine the location of the .git directory.
107- func (c * Command ) gitDir () * exec.Cmd {
111+ func (c * Command ) gitDir (ctx context. Context ) * exec.Cmd {
108112 args := []string {
109113 "rev-parse" ,
110114 "--git-dir" ,
111115 }
112116
113- return exec .Command (
117+ return exec .CommandContext (
118+ ctx ,
114119 "git" ,
115120 args ... ,
116121 )
117122}
118123
119124// commit generates the git command to create a commit with the provided message.
120125// It includes options to skip pre-commit hooks, sign off the commit, and handle amendments.
121- func (c * Command ) commit (val string ) * exec.Cmd {
126+ func (c * Command ) commit (ctx context. Context , val string ) * exec.Cmd {
122127 args := []string {
123128 "commit" ,
124129 "--no-verify" ,
@@ -130,16 +135,17 @@ func (c *Command) commit(val string) *exec.Cmd {
130135 args = append (args , "--amend" )
131136 }
132137
133- return exec .Command (
138+ return exec .CommandContext (
139+ ctx ,
134140 "git" ,
135141 args ... ,
136142 )
137143}
138144
139145// Commit creates a git commit with the provided message and returns the output or an error.
140146// It uses the commit method to generate the git command and execute it.
141- func (c * Command ) Commit (val string ) (string , error ) {
142- output , err := c .commit (val ).Output ()
147+ func (c * Command ) Commit (ctx context. Context , val string ) (string , error ) {
148+ output , err := c .commit (ctx , val ).Output ()
143149 if err != nil {
144150 return "" , err
145151 }
@@ -148,8 +154,8 @@ func (c *Command) Commit(val string) (string, error) {
148154}
149155
150156// GitDir to show the (by default, absolute) path of the git directory of the working tree.
151- func (c * Command ) GitDir () (string , error ) {
152- output , err := c .gitDir ().Output ()
157+ func (c * Command ) GitDir (ctx context. Context ) (string , error ) {
158+ output , err := c .gitDir (ctx ).Output ()
153159 if err != nil {
154160 return "" , err
155161 }
@@ -163,16 +169,16 @@ func (c *Command) GitDir() (string, error) {
163169// DiffFiles compares the differences between two sets of data and returns the differences as a string and an error.
164170// It first lists the names of changed files and then shows the differences between them.
165171// If there are no staged changes, it returns an error message.
166- func (c * Command ) DiffFiles () (string , error ) {
167- output , err := c .diffNames ().Output ()
172+ func (c * Command ) DiffFiles (ctx context. Context ) (string , error ) {
173+ output , err := c .diffNames (ctx ).Output ()
168174 if err != nil {
169175 return "" , err
170176 }
171177 if string (output ) == "" {
172178 return "" , errors .New ("please add your staged changes using git add <files...>" )
173179 }
174180
175- output , err = c .diffFiles ().Output ()
181+ output , err = c .diffFiles (ctx ).Output ()
176182 if err != nil {
177183 return "" , err
178184 }
@@ -182,8 +188,8 @@ func (c *Command) DiffFiles() (string, error) {
182188
183189// InstallHook installs the prepare-commit-msg hook if it doesn't already exist.
184190// It retrieves the hooks directory path, checks if the hook file exists, and writes the hook file with executable permissions.
185- func (c * Command ) InstallHook () error {
186- hookPath , err := c .hookPath ().Output ()
191+ func (c * Command ) InstallHook (ctx context. Context ) error {
192+ hookPath , err := c .hookPath (ctx ).Output ()
187193 if err != nil {
188194 return err
189195 }
@@ -208,8 +214,8 @@ func (c *Command) InstallHook() error {
208214
209215// UninstallHook removes the prepare-commit-msg hook if it exists.
210216// It retrieves the hooks directory path, checks if the hook file exists, and removes the hook file.
211- func (c * Command ) UninstallHook () error {
212- hookPath , err := c .hookPath ().Output ()
217+ func (c * Command ) UninstallHook (ctx context. Context ) error {
218+ hookPath , err := c .hookPath (ctx ).Output ()
213219 if err != nil {
214220 return err
215221 }
0 commit comments