summaryrefslogtreecommitdiffstats
path: root/src/config/git_config.rs
blob: c496759fec89e9277282845a1c54ecc445cbf390 (plain)
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
use anyhow::{anyhow, Result};
use git2::Config;

use crate::config::utils::{editor_from_env, get_string, get_unsigned_integer};

#[derive(Clone, Debug)]
pub struct GitConfig {
	pub(crate) comment_char: String,
	pub(crate) diff_context: u32,
	pub(crate) diff_interhunk_lines: u32,
	pub(crate) diff_rename_limit: u32,
	pub(crate) diff_renames: bool,
	pub(crate) diff_copies: bool,
	pub(crate) editor: String,
}

impl GitConfig {
	pub(super) fn new(git_config: &Config) -> Result<Self> {
		let comment_char = get_string(git_config, "core.commentChar", "#")?;
		let comment_char = if comment_char.as_str().eq("auto") {
			String::from("#")
		}
		else {
			comment_char
		};

		let git_diff_renames = get_string(git_config, "diff.renames", "true")?.to_lowercase();
		let (diff_renames, diff_copies) = match git_diff_renames.to_lowercase().as_str() {
			"true" => (true, false),
			"false" => (false, false),
			"copy" | "copies" => (true, true),
			v => {
				return Err(anyhow!(
					"\"{}\" does not match one of \"true\", \"false\", \"copy\" or \"copies\"",
					v
				)
				.context("\"diff.renames\" is not valid"));
			},
		};

		Ok(Self {
			comment_char,
			diff_context: get_unsigned_integer(git_config, "diff.context", 3)?,
			diff_interhunk_lines: get_unsigned_integer(git_config, "diff.interHunkContext", 0)?,
			diff_rename_limit: get_unsigned_integer(git_config, "diff.renameLimit", 200)?,
			diff_renames,
			diff_copies,
			editor: get_string(git_config, "core.editor", editor_from_env().as_str())?,
		})
	}
}