navid/veins / 83508482911f348af86cb892949a35bdce0d6b2f949b4a0faab7cba9a499fb82 / veinsrs/src/identity.rs

identity.rs

← veinsrs/src · 1236 bytes · raw · download

use std::env;
use std::path::Path;

use crate::core;
use crate::object::commit::Sig;
use crate::time;

fn first_value(root: &Path, key: &str, name: bool, first: &str, second: &str) -> String {
    if let Some(value) = core::repo_config_value(root, key) {
        return value;
    }
    let user = if name {
        core::user_config_author_name()
    } else {
        core::user_config_author_email()
    };
    if let Some(value) = user {
        return value;
    }
    for variable in [first, second] {
        if let Ok(value) = env::var(variable)
            && !value.is_empty()
        {
            return value;
        }
    }
    "unknown".to_string()
}

pub fn signature(root: &Path) -> Sig {
    Sig {
        name: first_value(root, "user.name", true, "GIT_AUTHOR_NAME", "USER").into_bytes(),
        email: first_value(root, "user.email", false, "GIT_AUTHOR_EMAIL", "EMAIL").into_bytes(),
        seconds: time::now_seconds(),
        tz_min: 0,
    }
}

pub fn random_change_id() -> [u8; 16] {
    let first = core::random_u64().to_le_bytes();
    let second = core::random_u64().to_le_bytes();
    let mut value = [0u8; 16];
    value[..8].copy_from_slice(&first);
    value[8..].copy_from_slice(&second);
    value
}