1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-01 01:04:00 +00:00

perf(git_status): query git stash count via gitoxide (#5238)

This commit is contained in:
David Knaack 2023-07-11 11:26:50 +02:00 committed by GitHub
parent 9bbc7b469d
commit 410f107c72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -244,18 +244,28 @@ fn get_repo_status(context: &Context, config: &GitStatusConfig) -> Option<RepoSt
} }
fn get_stashed_count(context: &Context) -> Option<usize> { fn get_stashed_count(context: &Context) -> Option<usize> {
let stash_output = context.exec_cmd( let repo = context.get_repo().ok()?.open();
"git", let reference = match repo.try_find_reference("refs/stash") {
&[ Ok(Some(reference)) => reference,
OsStr::new("-C"), // No stash reference found
context.current_dir.as_os_str(), Ok(None) => return Some(0),
OsStr::new("--no-optional-locks"), Err(err) => {
OsStr::new("stash"), log::warn!("Error finding stash reference: {err}");
OsStr::new("list"), return None;
], }
)?; };
Some(stash_output.stdout.trim().lines().count()) match reference.log_iter().all() {
Ok(Some(log)) => Some(log.count()),
Ok(None) => {
log::debug!("No reflog found for stash");
Some(0)
}
Err(err) => {
log::warn!("Error getting stash log: {err}");
None
}
}
} }
#[derive(Default, Debug, Copy, Clone)] #[derive(Default, Debug, Copy, Clone)]
@ -744,9 +754,38 @@ mod tests {
.output()?; .output()?;
let actual = ModuleRenderer::new("git_status") let actual = ModuleRenderer::new("git_status")
.config(toml::toml! {
[git_status]
format = "$stashed"
})
.path(repo_dir.path()) .path(repo_dir.path())
.collect(); .collect();
let expected = format_output("$"); let expected = Some(String::from("$"));
assert_eq!(expected, actual);
repo_dir.close()
}
#[test]
fn shows_no_stashed_after_undo() -> io::Result<()> {
let repo_dir = fixture_repo(FixtureProvider::Git)?;
create_stash(repo_dir.path())?;
undo_stash(repo_dir.path())?;
create_command("git")?
.args(["reset", "--hard", "HEAD"])
.current_dir(repo_dir.path())
.output()?;
let actual = ModuleRenderer::new("git_status")
.config(toml::toml! {
[git_status]
format = "$stashed"
})
.path(repo_dir.path())
.collect();
let expected = None;
assert_eq!(expected, actual); assert_eq!(expected, actual);
repo_dir.close() repo_dir.close()
@ -756,6 +795,9 @@ mod tests {
fn shows_stashed_with_count() -> io::Result<()> { fn shows_stashed_with_count() -> io::Result<()> {
let repo_dir = fixture_repo(FixtureProvider::Git)?; let repo_dir = fixture_repo(FixtureProvider::Git)?;
create_stash(repo_dir.path())?;
undo_stash(repo_dir.path())?;
create_stash(repo_dir.path())?;
create_stash(repo_dir.path())?; create_stash(repo_dir.path())?;
create_command("git")? create_command("git")?
@ -770,7 +812,7 @@ mod tests {
}) })
.path(repo_dir.path()) .path(repo_dir.path())
.collect(); .collect();
let expected = format_output("$1"); let expected = format_output("$2");
assert_eq!(expected, actual); assert_eq!(expected, actual);
repo_dir.close() repo_dir.close()
@ -1177,7 +1219,8 @@ mod tests {
} }
fn create_stash(repo_dir: &Path) -> io::Result<()> { fn create_stash(repo_dir: &Path) -> io::Result<()> {
File::create(repo_dir.join("readme.md"))?.sync_all()?; let (file, _path) = tempfile::NamedTempFile::new_in(repo_dir)?.keep()?;
file.sync_all()?;
create_command("git")? create_command("git")?
.args(["stash", "--all"]) .args(["stash", "--all"])
@ -1187,6 +1230,15 @@ mod tests {
Ok(()) Ok(())
} }
fn undo_stash(repo_dir: &Path) -> io::Result<()> {
create_command("git")?
.args(["stash", "pop"])
.current_dir(repo_dir)
.output()?;
Ok(())
}
fn create_untracked(repo_dir: &Path) -> io::Result<()> { fn create_untracked(repo_dir: &Path) -> io::Result<()> {
File::create(repo_dir.join("license"))?.sync_all()?; File::create(repo_dir.join("license"))?.sync_all()?;