Skip to content
Snippets Groups Projects
Commit 879029f03324 authored by Mitchell Kember's avatar Mitchell Kember
Browse files

rust-config: add username parsing

This adds Config::username which returns HGUSER, ui.username, or EMAIL in that
order, similar to ui.username() in Python.

I considered following the pattern of EDITOR, VISUAL, PAGER, etc. and using
add_for_environment_variable, but it's not possible to get the same precendence
as in Python that way (in particular HGUSER coming after the repo .hg/hgrc), at
least not without significant changes.

This will be used for 'rhg annotate -r wdir() -u' to annotate the username on
lines that were changed in the working directory.
parent d81714a1c88d
No related branches found
No related tags found
2 merge requests!1306rust-annotate: support -Tjson,!1255rust-annotate: add support for wdir
......@@ -23,6 +23,7 @@
use self::layer::ConfigValue;
use crate::errors::HgError;
use crate::errors::{HgResultExt, IoResultExt};
use crate::exit_codes;
use crate::utils::files::get_bytes_from_os_str;
use format_bytes::{write_bytes, DisplayBytes};
use std::collections::HashSet;
......@@ -841,6 +842,32 @@
_ => None,
}
}
/// Returns the default username to be used in commits. Like ui.username()
/// in Python with acceptempty=False, but aborts rather than prompting for
/// input or falling back to the OS username and hostname.
pub fn username(&self) -> Result<Vec<u8>, HgError> {
if let Some(value) = env::var_os("HGUSER") {
if !value.is_empty() {
return Ok(value.into_encoded_bytes());
}
}
if let Some(value) = self.get_str(b"ui", b"username")? {
if !value.is_empty() {
return Ok(value.as_bytes().to_vec());
}
}
if let Some(value) = env::var_os("EMAIL") {
if !value.is_empty() {
return Ok(value.into_encoded_bytes());
}
}
Err(HgError::abort(
"no username supplied",
exit_codes::ABORT,
Some("use 'hg config --edit' to set your username".to_string()),
))
}
}
/// Corresponds to `usage.resources[.<dimension>]`.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment