44 "bytes"
55 "errors"
66 "fmt"
7+ "os/exec"
78 "strconv"
89 "strings"
910)
@@ -114,14 +115,18 @@ func configKeyMatchesPrefix(key, prefix string) (bool, string) {
114115}
115116
116117func (repo * Repository ) ConfigStringDefault (key string , defaultValue string ) (string , error ) {
118+ // Note that `git config --get` didn't get `--default` until Git
119+ // 2.18 (released 2018-06-21).
117120 cmd := repo .GitCommand (
118- "config" ,
119- "--default" , defaultValue ,
120- key ,
121+ "config" , "--get" , key ,
121122 )
122123
123124 out , err := cmd .Output ()
124125 if err != nil {
126+ if err , ok := err .(* exec.ExitError ); ok && err .ExitCode () == 1 {
127+ // This indicates that the value was not found.
128+ return defaultValue , nil
129+ }
125130 return defaultValue , fmt .Errorf ("running 'git config': %w" , err )
126131 }
127132
@@ -133,15 +138,18 @@ func (repo *Repository) ConfigStringDefault(key string, defaultValue string) (st
133138}
134139
135140func (repo * Repository ) ConfigBoolDefault (key string , defaultValue bool ) (bool , error ) {
141+ // Note that `git config --get` didn't get `--type=bool` or
142+ // `--default` until Git 2.18 (released 2018-06-21).
136143 cmd := repo .GitCommand (
137- "config" ,
138- "--type" , "bool" ,
139- "--default" , strconv .FormatBool (defaultValue ),
140- key ,
144+ "config" , "--get" , "--bool" , key ,
141145 )
142146
143147 out , err := cmd .Output ()
144148 if err != nil {
149+ if err , ok := err .(* exec.ExitError ); ok && err .ExitCode () == 1 {
150+ // This indicates that the value was not found.
151+ return defaultValue , nil
152+ }
145153 return defaultValue , fmt .Errorf ("running 'git config': %w" , err )
146154 }
147155
@@ -155,15 +163,18 @@ func (repo *Repository) ConfigBoolDefault(key string, defaultValue bool) (bool,
155163}
156164
157165func (repo * Repository ) ConfigIntDefault (key string , defaultValue int ) (int , error ) {
166+ // Note that `git config --get` didn't get `--type=int` or
167+ // `--default` until Git 2.18 (released 2018-06-21).
158168 cmd := repo .GitCommand (
159- "config" ,
160- "--type" , "int" ,
161- "--default" , strconv .Itoa (defaultValue ),
162- key ,
169+ "config" , "--get" , "--int" , key ,
163170 )
164171
165172 out , err := cmd .Output ()
166173 if err != nil {
174+ if err , ok := err .(* exec.ExitError ); ok && err .ExitCode () == 1 {
175+ // This indicates that the value was not found.
176+ return defaultValue , nil
177+ }
167178 return defaultValue , fmt .Errorf ("running 'git config': %w" , err )
168179 }
169180
0 commit comments