mirror of
https://github.com/Llewellynvdm/starship.git
synced 2025-01-24 15:48:34 +00:00
perf(hg_branch): Replace direct calls to hg
(#803)
perf: don't use hg process to get branch / bookmark information, each call was taking 250ms and it was making on average 2 calls. the branch and bookmark information is available in files in the root of the repo, use that instead. now takes 300 micro seconds fix: do not use .hgignore to decide if it is an hg repo. was providing false positives. #721 fix: after changing branch the new branch was not show (the old branch was shown), the new branch is now correctly shown. #722 fix: if no branch override was set it would say (no branch) however if you commited in that state you would commit to the branch default so say that instead
This commit is contained in:
parent
8e5fa60fc8
commit
178f177dac
@ -1,4 +1,3 @@
|
||||
use std::process::Command;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
use super::{Context, Module, RootModuleConfig};
|
||||
@ -9,11 +8,7 @@ use crate::configs::hg_branch::HgBranchConfig;
|
||||
///
|
||||
/// Will display the bookmark or branch name if the current directory is an hg repo
|
||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
let is_hg_repo = context
|
||||
.try_begin_scan()?
|
||||
.set_files(&[".hgignore"])
|
||||
.set_folders(&[".hg"])
|
||||
.is_match();
|
||||
let is_hg_repo = context.try_begin_scan()?.set_folders(&[".hg"]).is_match();
|
||||
|
||||
if !is_hg_repo {
|
||||
return None;
|
||||
@ -25,7 +20,6 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
|
||||
module.get_prefix().set_value("on ");
|
||||
|
||||
let truncation_symbol = get_graphemes(config.truncation_symbol, 1);
|
||||
module.create_segment("symbol", &config.symbol);
|
||||
|
||||
// TODO: Once error handling is implemented, warn the user if their config
|
||||
@ -40,15 +34,13 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
config.truncation_length as usize
|
||||
};
|
||||
|
||||
let get_branch_name = |tmpl| get_hg_log_template(tmpl, context);
|
||||
|
||||
let branch_name = get_branch_name("{activebookmark}")
|
||||
.or_else(|| get_branch_name("{branch}"))
|
||||
.unwrap_or_else(|| "(no branch)".to_string());
|
||||
let branch_name =
|
||||
get_hg_current_bookmark(context).unwrap_or_else(|| get_hg_branch_name(context));
|
||||
|
||||
let truncated_graphemes = get_graphemes(&branch_name, len);
|
||||
// The truncation symbol should only be added if we truncated
|
||||
let truncated_and_symbol = if len < graphemes_len(&branch_name) {
|
||||
let truncation_symbol = get_graphemes(config.truncation_symbol, 1);
|
||||
truncated_graphemes + &truncation_symbol
|
||||
} else {
|
||||
truncated_graphemes
|
||||
@ -62,19 +54,16 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
Some(module)
|
||||
}
|
||||
|
||||
fn get_hg_log_template(hgtmpl: &str, ctx: &Context) -> Option<String> {
|
||||
let output = Command::new("hg")
|
||||
.args(&["log", "-r", ".", "--template", hgtmpl])
|
||||
.current_dir(&ctx.current_dir)
|
||||
.output()
|
||||
.ok()
|
||||
.and_then(|output| String::from_utf8(output.stdout).ok())?;
|
||||
fn get_hg_branch_name(ctx: &Context) -> String {
|
||||
std::fs::read_to_string(ctx.current_dir.join(".hg").join("branch"))
|
||||
.map(|s| s.trim().into())
|
||||
.unwrap_or_else(|_| "default".to_string())
|
||||
}
|
||||
|
||||
if output.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(output)
|
||||
}
|
||||
fn get_hg_current_bookmark(ctx: &Context) -> Option<String> {
|
||||
std::fs::read_to_string(ctx.current_dir.join(".hg").join("bookmarks.current"))
|
||||
.map(|s| s.trim().into())
|
||||
.ok()
|
||||
}
|
||||
|
||||
fn get_graphemes(text: &str, length: usize) -> String {
|
||||
|
@ -29,7 +29,7 @@ fn test_hg_get_branch_fails() -> io::Result<()> {
|
||||
expect_hg_branch_with_config(
|
||||
tempdir.path(),
|
||||
"",
|
||||
&[Expect::BranchName(&"(no branch)"), Expect::NoTruncation],
|
||||
&[Expect::BranchName(&"default"), Expect::NoTruncation],
|
||||
)
|
||||
}
|
||||
|
||||
@ -156,7 +156,7 @@ fn expect_hg_branch_with_config(
|
||||
|
||||
let actual = String::from_utf8(output.stdout).unwrap();
|
||||
|
||||
let mut expect_branch_name = "(no branch)";
|
||||
let mut expect_branch_name = "default";
|
||||
let mut expect_style = Color::Purple.bold();
|
||||
let mut expect_symbol = "\u{e0a0}";
|
||||
let mut expect_truncation_symbol = "…";
|
||||
|
Loading…
x
Reference in New Issue
Block a user