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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
use lazy_static::lazy_static;
use regex::Regex;
use std::collections::HashMap;
use crate::cli;
use crate::features;
use crate::git_config::{self, GitConfigGet};
use crate::options::option_value::{OptionValue, ProvenancedOptionValue};
use ProvenancedOptionValue::*;
// Look up a value of type `T` associated with `option name`. The search rules are:
//
// 1. If there is a value associated with `option_name` in the main [delta] git config
// section, then stop searching and return that value.
//
// 2. For each feature in the ordered list of enabled features:
//
// 2.1 Look-up the value, treating `feature` as a custom feature.
// I.e., if there is a value associated with `option_name` in a git config section
// named [delta "`feature`"] then stop searching and return that value.
//
// 2.2 Look-up the value, treating `feature` as a builtin feature.
// I.e., if there is a value (not a default value) associated with `option_name` in a
// builtin feature named `feature`, then stop searching and return that value.
// Otherwise, record the default value and continue searching.
//
// 3. Return the last default value that was encountered.
pub fn get_option_value<T>(
option_name: &str,
builtin_features: &HashMap<String, features::BuiltinFeature>,
opt: &cli::Opt,
git_config: &mut Option<git_config::GitConfig>,
) -> Option<T>
where
T: GitConfigGet,
T: GetOptionValue,
T: From<OptionValue>,
T: Into<OptionValue>,
{
T::get_option_value(option_name, builtin_features, opt, git_config)
}
lazy_static! {
static ref GIT_CONFIG_THEME_REGEX: Regex = Regex::new(r"^delta\.(.+)\.(light|dark)$").unwrap();
}
pub fn get_themes(git_config: Option<git_config::GitConfig>) -> Vec<String> {
let mut themes: Vec<String> = Vec::new();
for e in &git_config.unwrap().config.entries(None).unwrap() {
let entry = e.unwrap();
let entry_name = entry.name().unwrap();
let caps = GIT_CONFIG_THEME_REGEX.captures(entry_name);
if let Some(caps) = caps {
let name = caps.get(1).map_or("", |m| m.as_str()).to_string();
if !themes.contains(&name) {
themes.push(name)
}
}
}
themes.sort_by_key(|a| a.to_lowercase());
themes
}
pub trait GetOptionValue {
fn get_option_value(
option_name: &str,
builtin_features: &HashMap<String, features::BuiltinFeature>,
opt: &cli::Opt,
git_config: &mut Option<git_config::GitConfig>,
) -> Option<Self>
where
Self: Sized,
Self: GitConfigGet,
Self: From<OptionValue>,
Self: Into<OptionValue>,
{
if let Some(git_config) = git_config {
if let Some(value) = git_config.get::<Self>(&format!("delta.{}", option_name)) {
return Some(value);
}
}
for feature in opt.features.split_whitespace().rev() {
match Self::get_provenanced_value_for_feature(
option_name,
feature,
builtin_features,
opt,
git_config,
) {
Some(GitConfigValue(value)) | Some(DefaultValue(value)) => {
return Some(value.into());
}
None => {}
}
}
None
}
/// Return the value, or default value, associated with `option_name` under feature name
/// `feature`. This may refer to a custom feature, or a builtin feature, or both. Only builtin
/// features have defaults. See `GetOptionValue::get_option_value`.
fn get_provenanced_value_for_feature(
option_name: &str,
feature: &str,
builtin_features: &HashMap<String, features::BuiltinFeature>,
opt: &cli::Opt,
git_config: &mut Option<git_config::GitConfig>,
) -> Option<ProvenancedOptionValue>
where
Self: Sized,
Self: GitConfigGet,
Self: Into<OptionValue>,
{
if let Some(git_config) = git_config {
if let Some(value) =
git_config.get::<Self>(&format!("delta.{}.{}", feature, option_name))
{
return Some(GitConfigValue(value.into()));
}
}
if let Some(builtin_feature) = builtin_features.get(feature) {
if let Some(value_function) = builtin_feature.get(option_name) {
return Some(value_function(opt, git_config));
}
}
None
}
}
impl GetOptionValue for Option<String> {}
impl GetOptionValue for String {}
impl GetOptionValue for bool {}
impl GetOptionValue for f64 {}
impl GetOptionValue for usize {}
#[cfg(test)]
pub mod tests {
use std::env;
use std::fs::remove_file;
use crate::options::get::get_themes;
use crate::tests::integration_test_utils;
// TODO: the followig tests are collapsed into one since they all set the same env var and thus
// could affect each other if allowed to run concurrently.
#[test]
fn test_env_var_overrides_git_config() {
// ----------------------------------------------------------------------------------------
// simple string
let git_config_contents = b"
[delta]
plus-style = blue
";
let git_config_path = "delta__test_simple_string_env_var_overrides_git_config.gitconfig";
let opt = integration_test_utils::make_options_from_args_and_git_config(
&[],
Some(git_config_contents),
Some(git_config_path),
);
assert_eq!(opt.plus_style, "blue");
env::set_var("GIT_CONFIG_PARAMETERS", "'delta.plus-style=green'");
let opt = integration_test_utils::make_options_from_args_and_git_config_honoring_env_var(
&[],
Some(git_config_contents),
Some(git_config_path),
);
assert_eq!(opt.plus_style, "green");
remove_file(git_config_path).unwrap();
// ----------------------------------------------------------------------------------------
// complex string
let git_config_contents = br##"
[delta]
minus-style = red bold ul "#ffeeee"
"##;
let git_config_path = "delta__test_complex_string_env_var_overrides_git_config.gitconfig";
let opt = integration_test_utils::make_options_from_args_and_git_config(
&[],
Some(git_config_contents),
Some(git_config_path),
);
assert_eq!(opt.minus_style, r##"red bold ul #ffeeee"##);
env::set_var(
"GIT_CONFIG_PARAMETERS",
r##"'delta.minus-style=magenta italic ol "#aabbcc"'"##,
);
let opt = integration_test_utils::make_options_from_args_and_git_config_honoring_env_var(
&[],
Some(git_config_contents),
Some(git_config_path),
);
assert_eq!(opt.minus_style, r##"magenta italic ol "#aabbcc""##,);
remove_file(git_config_path).unwrap();
// ----------------------------------------------------------------------------------------
// option string
let git_config_contents = b"
[delta]
plus-style = blue
";
let git_config_path = "delta__test_option_string_env_var_overrides_git_config.gitconfig";
let opt = integration_test_utils::make_options_from_args_and_git_config(
&[],
Some(git_config_contents),
Some(git_config_path),
);
assert_eq!(opt.plus_style, "blue");
env::set_var("GIT_CONFIG_PARAMETERS", "'delta.plus-style=green'");
|