4
4
"bytes"
5
5
"errors"
6
6
"fmt"
7
+ "os/exec"
7
8
"strconv"
8
9
"strings"
9
10
)
@@ -114,14 +115,18 @@ func configKeyMatchesPrefix(key, prefix string) (bool, string) {
114
115
}
115
116
116
117
func (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).
117
120
cmd := repo .GitCommand (
118
- "config" ,
119
- "--default" , defaultValue ,
120
- key ,
121
+ "config" , "--get" , key ,
121
122
)
122
123
123
124
out , err := cmd .Output ()
124
125
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
+ }
125
130
return defaultValue , fmt .Errorf ("running 'git config': %w" , err )
126
131
}
127
132
@@ -133,15 +138,18 @@ func (repo *Repository) ConfigStringDefault(key string, defaultValue string) (st
133
138
}
134
139
135
140
func (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).
136
143
cmd := repo .GitCommand (
137
- "config" ,
138
- "--type" , "bool" ,
139
- "--default" , strconv .FormatBool (defaultValue ),
140
- key ,
144
+ "config" , "--get" , "--bool" , key ,
141
145
)
142
146
143
147
out , err := cmd .Output ()
144
148
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
+ }
145
153
return defaultValue , fmt .Errorf ("running 'git config': %w" , err )
146
154
}
147
155
@@ -155,15 +163,18 @@ func (repo *Repository) ConfigBoolDefault(key string, defaultValue bool) (bool,
155
163
}
156
164
157
165
func (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).
158
168
cmd := repo .GitCommand (
159
- "config" ,
160
- "--type" , "int" ,
161
- "--default" , strconv .Itoa (defaultValue ),
162
- key ,
169
+ "config" , "--get" , "--int" , key ,
163
170
)
164
171
165
172
out , err := cmd .Output ()
166
173
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
+ }
167
178
return defaultValue , fmt .Errorf ("running 'git config': %w" , err )
168
179
}
169
180
0 commit comments