summaryrefslogtreecommitdiffstats
path: root/vendor/gix/src/repository/identity.rs
blob: 61a4b4a98be495bd844f803889d028b058025bd1 (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
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
use std::time::SystemTime;

use crate::{
    bstr::BString,
    config,
    config::tree::{gitoxide, keys, Author, Committer, Key, User},
};

/// Identity handling.
///
/// # Deviation
///
/// There is no notion of a default user like in git, and instead failing to provide a user
/// is fatal. That way, we enforce correctness and force application developers to take care
/// of this issue which can be done in various ways, for instance by setting
/// `gitoxide.committer.nameFallback` and similar.
impl crate::Repository {
    /// Return the committer as configured by this repository, which is determined by…
    ///
    /// * …the git configuration `committer.name|email`…
    /// * …the `GIT_COMMITTER_(NAME|EMAIL|DATE)` environment variables…
    /// * …the configuration for `user.name|email` as fallback…
    ///
    /// …and in that order, or `None` if no committer name or email was configured, or `Some(Err(…))`
    /// if the committer date could not be parsed.
    ///
    /// # Note
    ///
    /// The values are cached when the repository is instantiated.
    pub fn committer(&self) -> Option<Result<gix_actor::SignatureRef<'_>, config::time::Error>> {
        let p = self.config.personas();

        Ok(gix_actor::SignatureRef {
            name: p.committer.name.as_ref().or(p.user.name.as_ref()).map(|v| v.as_ref())?,
            email: p
                .committer
                .email
                .as_ref()
                .or(p.user.email.as_ref())
                .map(|v| v.as_ref())?,
            time: match extract_time_or_default(p.committer.time.as_ref(), &gitoxide::Commit::COMMITTER_DATE) {
                Ok(t) => t,
                Err(err) => return Some(Err(err)),
            },
        })
        .into()
    }

    /// Return the author as configured by this repository, which is determined by…
    ///
    /// * …the git configuration `author.name|email`…
    /// * …the `GIT_AUTHOR_(NAME|EMAIL|DATE)` environment variables…
    /// * …the configuration for `user.name|email` as fallback…
    ///
    /// …and in that order, or `None` if there was nothing configured.
    ///
    /// # Note
    ///
    /// The values are cached when the repository is instantiated.
    pub fn author(&self) -> Option<Result<gix_actor::SignatureRef<'_>, config::time::Error>> {
        let p = self.config.personas();

        Ok(gix_actor::SignatureRef {
            name: p.author.name.as_ref().or(p.user.name.as_ref()).map(|v| v.as_ref())?,
            email: p.author.email.as_ref().or(p.user.email.as_ref()).map(|v| v.as_ref())?,
            time: match extract_time_or_default(p.author.time.as_ref(), &gitoxide::Commit::AUTHOR_DATE) {
                Ok(t) => t,
                Err(err) => return Some(Err(err)),
            },
        })
        .into()
    }
}

fn extract_time_or_default(
    time: Option<&Result<gix_actor::Time, gix_date::parse::Error>>,
    config_key: &'static keys::Time,
) -> Result<gix_actor::Time, config::time::Error> {
    match time {
        Some(Ok(t)) => Ok(*t),
        None => Ok(gix_date::Time::now_local_or_utc()),
        Some(Err(err)) => Err(config::time::Error::from(config_key).with_source(err.clone())),
    }
}

#[derive(Debug, Clone)]
pub(crate) struct Entity {
    pub name: Option<BString>,
    pub email: Option<BString>,
    /// A time parsed from an environment variable, handling potential errors is delayed.
    pub time: Option<Result<gix_actor::Time, gix_date::parse::Error>>,
}

#[derive(Debug, Clone)]
pub(crate) struct Personas {
    user: Entity,
    committer: Entity,
    author: Entity,
}

impl Personas {
    pub fn from_config_and_env(config: &gix_config::File<'_>) -> Self {
        fn entity_in_section(
            config: &gix_config::File<'_>,
            name_key: &keys::Any,
            email_key: &keys::Any,
            fallback: Option<(&keys::Any, &keys::Any)>,
        ) -> (Option<BString>, Option<BString>) {
            let fallback = fallback.and_then(|(name_key, email_key)| {
                debug_assert_eq!(name_key.section.name(), email_key.section.name());
                config
                    .section("gitoxide", Some(name_key.section.name().into()))
                    .ok()
                    .map(|section| (section, name_key, email_key))
            });
            (
                config
                    .string(name_key.section.name(), None, name_key.name)
                    .or_else(|| fallback.as_ref().and_then(|(s, name_key, _)| s.value(name_key.name)))
                    .map(|v| v.into_owned()),
                config
                    .string(email_key.section.name(), None, email_key.name)
                    .or_else(|| fallback.as_ref().and_then(|(s, _, email_key)| s.value(email_key.name)))
                    .map(|v| v.into_owned()),
            )
        }
        let now = SystemTime::now();
        let parse_date = |key: &str, date: &keys::Time| -> Option<Result<gix_date::Time, gix_date::parse::Error>> {
            debug_assert_eq!(
                key,
                date.logical_name(),
                "BUG: drift of expected name and actual name of the key (we hardcode it to save an allocation)"
            );
            config
                .string_by_key(key)
                .map(|time| date.try_into_time(time, now.into()))
        };

        let fallback = (
            &gitoxide::Committer::NAME_FALLBACK,
            &gitoxide::Committer::EMAIL_FALLBACK,
        );
        let (committer_name, committer_email) =
            entity_in_section(config, &Committer::NAME, &Committer::EMAIL, Some(fallback));
        let fallback = (&gitoxide::Author::NAME_FALLBACK, &gitoxide::Author::EMAIL_FALLBACK);
        let (author_name, author_email) = entity_in_section(config, &Author::NAME, &Author::EMAIL, Some(fallback));
        let (user_name, mut user_email) = entity_in_section(config, &User::NAME, &User::EMAIL, None);

        let committer_date = parse_date("gitoxide.commit.committerDate", &gitoxide::Commit::COMMITTER_DATE);
        let author_date = parse_date("gitoxide.commit.authorDate", &gitoxide::Commit::AUTHOR_DATE);

        user_email = user_email.or_else(|| {
            config
                .string_by_key(gitoxide::User::EMAIL_FALLBACK.logical_name().as_str())
                .map(|v| v.into_owned())
        });
        Personas {
            user: Entity {
                name: user_name,
                email: user_email,
                time: None,
            },
            committer: Entity {
                name: committer_name,
                email: committer_email,
                time: committer_date,
            },
            author: Entity {
                name: author_name,
                email: author_email,
                time: author_date,
            },
        }
    }
}