git-delta 0.21.1

A syntax-highlighting pager for git
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
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`.
//
// A feature is enabled when the user's config or the command line turns it on, for example with
// side-by-side = 140 or features = side-by-side. A feature that another builtin feature brings in
// as a default, such as side-by-side's line numbers, can be in effect without being enabled. A
// feature is in effect when delta uses it for this output. delta decides that once per run, at
// the width it renders at (COLUMNS, or else the terminal's width), before it gathers the
// features: side-by-side = 140 enables side-by-side, but side-by-side is in effect, and gathered,
// only at 140 columns or more. So the feature list that this searches holds the features in
// effect.
//
// 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. Otherwise search the features in effect in these groups, and return the first value found:
//
//    2.1 The features from the command line (--features, DELTA_FEATURES, flags, and what they bring
//        in), in order of priority: for each, the user's git config section [delta "feature"],
//        or else the builtin feature's values; of a switch feature, only the value of its own
//        option, such as side-by-side's side-by-side.
//
//    2.2 The user's git config sections with the same name as a builtin feature that is in
//        effect, such as [delta "side-by-side"], in order of priority.
//
//    2.3 The other features, as in 2.1, in order of priority.
//
//    2.4 The values of the switch features that are in effect, such as side-by-side, in order of
//        priority.
//
// So what a switch feature sets besides its own option, such as side-by-side's line-number
// formats, is a default that anything the user sets beats, however the switch feature was turned
// on; a section with the same name as a builtin feature beats the other features in git config;
// and the command line beats git config.
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)
}

static GIT_CONFIG_THEME_REGEX: &str = r"^delta\.(.+)\.(light|dark)$";

pub fn get_themes(git_config: Option<git_config::GitConfig>) -> Vec<String> {
    let mut themes: Vec<String> = Vec::new();
    let git_config = git_config.unwrap();
    git_config.for_each(GIT_CONFIG_THEME_REGEX, |name, _| {
        if let Some(name) = name.strip_prefix("delta.") {
            if let Some((name, _)) = name.rsplit_once('.') {
                let name = name.to_owned();
                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);
            }
        }
        let features: Vec<&str> = opt.features.as_ref()?.split_whitespace().rev().collect();
        let is_builtin = |feature: &&str| builtin_features.contains_key(*feature);
        let is_switch_feature = |feature: &&str| crate::options::set::is_switch_feature(feature);
        let user_section = |feature: &str| {
            git_config
                .as_ref()?
                .get::<Self>(&format!("delta.{feature}.{option_name}"))
                .map(Into::into)
        };
        let builtin = |feature: &str| {
            let value_function = builtin_features.get(feature)?.get(option_name)?;
            match value_function(opt, git_config) {
                GitConfigValue(value) | DefaultValue(value) => Some(value),
            }
        };
        let in_order = || features.iter().copied();
        let from_command_line =
            |feature: &&str| opt.computed.command_line_features.contains(*feature);
        // The value a feature gives at its place in the list: its user section, or else a builtin
        // style feature's value, or a switch feature's value for its own option only.
        let in_place = |feature: &str| {
            user_section(feature).or_else(|| {
                let keeps_place = is_builtin(&feature)
                    && (!is_switch_feature(&feature) || feature == option_name);
                keeps_place.then(|| builtin(feature)).flatten()
            })
        };
        in_order()
            .filter(from_command_line)
            .find_map(in_place)
            .or_else(|| {
                in_order()
                    .filter(|feature| is_builtin(feature) && !from_command_line(feature))
                    .find_map(user_section)
            })
            .or_else(|| {
                // A builtin feature's section was looked up just above, and gives nothing here.
                in_order()
                    .filter(|feature| !from_command_line(feature))
                    .find_map(in_place)
            })
            .or_else(|| in_order().filter(is_switch_feature).find_map(builtin))
            .map(Into::into)
    }
}

impl GetOptionValue for Option<String> {}
impl GetOptionValue for String {}
impl GetOptionValue for bool {}
impl GetOptionValue for f64 {}
impl GetOptionValue for usize {}
impl GetOptionValue for cli::SideBySide {}

#[cfg(test)]
pub mod tests {
    use std::fs::remove_file;

    use crate::cli::Opt;
    use crate::env::DeltaEnv;
    use crate::options::get::get_themes;
    use crate::tests::integration_test_utils;

    // fn generic<T>(_s: SGen<T>) {}
    fn _test_env_var_overrides_git_config_generic(
        git_config_contents: &[u8],
        git_config_path: &str,
        env_value: String,
        fn_cmp_before: &dyn Fn(Opt),
        fn_cmp_after: &dyn Fn(Opt),
    ) {
        let opt = integration_test_utils::make_options_from_args_and_git_config(
            &[],
            Some(git_config_contents),
            Some(git_config_path),
        );
        fn_cmp_before(opt);

        let opt = integration_test_utils::make_options_from_args_and_git_config_honoring_env_var_with_custom_env(
            DeltaEnv {
                git_config_parameters: Some(env_value),
                ..DeltaEnv::default()
            },
            &[],
            Some(git_config_contents),
            Some(git_config_path),
        );
        fn_cmp_after(opt);

        remove_file(git_config_path).unwrap();
    }
    #[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";
        _test_env_var_overrides_git_config_generic(
            git_config_contents,
            git_config_path,
            "'delta.plus-style=green'".into(),
            &|opt: Opt| assert_eq!(opt.plus_style, "blue"),
            &|opt: Opt| assert_eq!(opt.plus_style, "green"),
        );
    }

    #[test]
    fn test_env_var_overrides_git_config_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";
        _test_env_var_overrides_git_config_generic(
            git_config_contents,
            git_config_path,
            r##"'delta.minus-style=magenta italic ol "#aabbcc"'"##.into(),
            &|opt: Opt| assert_eq!(opt.minus_style, r##"red bold ul #ffeeee"##),
            &|opt: Opt| assert_eq!(opt.minus_style, r##"magenta italic ol "#aabbcc""##,),
        );
    }

    #[test]
    fn test_env_var_overrides_git_config_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";
        _test_env_var_overrides_git_config_generic(
            git_config_contents,
            git_config_path,
            "'delta.plus-style=green'".into(),
            &|opt: Opt| assert_eq!(opt.plus_style, "blue"),
            &|opt: Opt| assert_eq!(opt.plus_style, "green"),
        );
    }

    #[test]
    fn test_env_var_overrides_git_config_bool() {
        let git_config_contents = b"
[delta]
    side-by-side = true
";
        let git_config_path = "delta__test_bool_env_var_overrides_git_config.gitconfig";
        _test_env_var_overrides_git_config_generic(
            git_config_contents,
            git_config_path,
            "'delta.side-by-side=false'".into(),
            &|opt: Opt| assert!(opt.computed.side_by_side),
            &|opt: Opt| assert!(!opt.computed.side_by_side),
        );
    }

    #[test]
    fn test_env_var_overrides_git_config_int() {
        let git_config_contents = b"
[delta]
    max-line-length = 1
";
        let git_config_path = "delta__test_int_env_var_overrides_git_config.gitconfig";
        _test_env_var_overrides_git_config_generic(
            git_config_contents,
            git_config_path,
            "'delta.max-line-length=2'".into(),
            &|opt: Opt| assert_eq!(opt.max_line_length, 1),
            &|opt: Opt| assert_eq!(opt.max_line_length, 2),
        );
    }

    #[test]
    fn test_env_var_overrides_git_config_float() {
        let git_config_contents = b"
[delta]
    max-line-distance = 0.6
";
        let git_config_path = "delta__test_float_env_var_overrides_git_config.gitconfig";
        _test_env_var_overrides_git_config_generic(
            git_config_contents,
            git_config_path,
            "'delta.max-line-distance=0.7'".into(),
            &|opt: Opt| assert_eq!(opt.max_line_distance, 0.6),
            &|opt: Opt| assert_eq!(opt.max_line_distance, 0.7),
        );
    }

    #[test]
    fn test_side_by_side_value_in_git_config_takes_precedence_over_side_by_side_feature() {
        // The side-by-side feature sets side-by-side to "true", but like any option, a value in
        // the main [delta] section wins over the feature turned on in git config: booleans,
        // minimum widths and hunk alike. Adding side-by-side to the feature list on the command
        // line is the same as --side-by-side, which beats git config.
        for (i, value) in ["false", "99999", "hunk", "hunk,99999", "true"]
            .iter()
            .enumerate()
        {
            let git_config_contents = format!("[delta]\n    side-by-side = {value}\n");
            let with_feature = format!("{git_config_contents}    features = side-by-side\n");
            let git_config_path =
                format!("delta__test_side_by_side_feature_precedence_{i}.gitconfig");
            let side_by_side =
                |git_config_contents: &str, features: Option<&str>, args: &[&str]| {
                    let opt =
                    integration_test_utils::make_options_from_args_and_git_config_with_custom_env(
                        DeltaEnv {
                            features: features.map(str::to_string),
                            ..DeltaEnv::default()
                        },
                        args,
                        Some(git_config_contents.as_bytes()),
                        Some(&git_config_path),
                    );
                    (
                        opt.side_by_side,
                        opt.computed.side_by_side,
                        opt.computed.side_by_side_scope,
                    )
                };
            let without_feature = side_by_side(&git_config_contents, None, &[]);
            assert_eq!(without_feature.0, value.parse().unwrap());
            assert_eq!(side_by_side(&with_feature, None, &[]), without_feature);

            let with_flag = side_by_side(&git_config_contents, None, &["--side-by-side"]);
            assert_eq!(with_flag.0, "true".parse().unwrap());
            assert_eq!(
                side_by_side(&git_config_contents, Some("+side-by-side"), &[]),
                with_flag
            );
            assert_eq!(
                side_by_side(&git_config_contents, Some("side-by-side"), &[]),
                with_flag
            );
            remove_file(git_config_path).unwrap();
        }
    }

    #[test]
    fn test_delta_features_env_var() {
        let git_config_contents = b"
[delta]
    features = feature-from-gitconfig
";
        let git_config_path = "delta__test_delta_features_env_var.gitconfig";

        let opt = integration_test_utils::make_options_from_args_and_git_config(
            &[],
            Some(git_config_contents),
            Some(git_config_path),
        );
        assert_eq!(opt.features.unwrap(), "feature-from-gitconfig");
        assert!(!opt.computed.side_by_side);

        let opt = integration_test_utils::make_options_from_args_and_git_config_with_custom_env(
            DeltaEnv {
                features: Some("side-by-side".into()),
                ..DeltaEnv::default()
            },
            &[],
            Some(git_config_contents),
            Some(git_config_path),
        );
        // `line-numbers` is a builtin feature induced by side-by-side
        assert_eq!(opt.features.unwrap(), "line-numbers side-by-side");
        assert!(opt.computed.side_by_side);

        let opt = integration_test_utils::make_options_from_args_and_git_config_with_custom_env(
            DeltaEnv {
                features: Some("+side-by-side".into()),
                ..DeltaEnv::default()
            },
            &[],
            Some(git_config_contents),
            Some(git_config_path),
        );
        assert_eq!(
            opt.features.unwrap(),
            "feature-from-gitconfig line-numbers side-by-side"
        );
        assert!(opt.computed.side_by_side);

        remove_file(git_config_path).unwrap();
    }

    #[test]
    fn test_get_themes_from_config() {
        let git_config_contents = r#"
[delta "dark-theme"]
    max-line-distance = 0.6
    dark = true

[delta "light-theme"]
    max-line-distance = 0.6
    light = true

[delta "light-and-dark-theme"]
    max-line-distance = 0.6
    light = true
    dark = true

[delta "Uppercase-Theme"]
    light = true

[delta "not-a-theme"]
    max-line-distance = 0.6
"#;
        let git_config_path = "delta__test_get_themes_git_config.gitconfig";

        let git_config = Some(integration_test_utils::make_git_config(
            &DeltaEnv::default(),
            git_config_contents.as_bytes(),
            git_config_path,
            false,
        ));

        let themes = get_themes(git_config);

        assert_eq!(
            themes,
            [
                "dark-theme",
                "light-and-dark-theme",
                "light-theme",
                "Uppercase-Theme"
            ]
        );

        remove_file(git_config_path).unwrap();
    }
}