Skip to content

Commit

Permalink
cache branchname lookup (closes #159)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Dilly committed Jul 4, 2020
1 parent 715c179 commit a33db8d
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- hooks ignored when running `gitui` in subfolder of workdir ([#151](https://github.com/extrawurst/gitui/issues/151))
- better scrolling in file-trees [[@tisorlawan](https://github.com/tisorlawan)] ([#144](https://github.com/extrawurst/gitui/issues/144))
- show untracked files in stash commit details [[@MCord](https://github.com/MCord)] ([#130](https://github.com/extrawurst/gitui/issues/130))
- in some repos looking up the branch name was a bottleneck ([#159](https://github.com/extrawurst/gitui/issues/159))
- some optimizations in reflog

## [0.7.0] - 2020-06-15
Expand Down
41 changes: 41 additions & 0 deletions asyncgit/src/cached/branchname.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::{
error::Result,
sync::{self, CommitId},
};

///
pub struct BranchName {
last_result: Option<(CommitId, String)>,
repo_path: String,
}

impl BranchName {
///
pub fn new(path: &str) -> Self {
Self {
repo_path: path.to_string(),
last_result: None,
}
}

///
pub fn lookup(&mut self) -> Result<String> {
let current_head = sync::get_head(self.repo_path.as_str())?;

if let Some((last_head, branch_name)) =
self.last_result.as_ref()
{
if *last_head == current_head {
return Ok(branch_name.clone());
}
}

self.fetch(current_head)
}

fn fetch(&mut self, head: CommitId) -> Result<String> {
let name = sync::get_branch_name(self.repo_path.as_str())?;
self.last_result = Some((head, name.clone()));
Ok(name)
}
}
7 changes: 7 additions & 0 deletions asyncgit/src/cached/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! cached lookups:
//! parts of the sync api that might take longer
//! to compute but change seldom so doing them async might be overkill

mod branchname;

pub use branchname::BranchName;
1 change: 1 addition & 0 deletions asyncgit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![deny(clippy::result_unwrap_used)]
#![deny(clippy::panic)]

pub mod cached;
mod commit_files;
mod diff;
mod error;
Expand Down
3 changes: 2 additions & 1 deletion asyncgit/src/sync/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::{
use scopetime::scope_time;

/// returns the branch-name head is currently pointing to
pub fn get_branch_name(repo_path: &str) -> Result<String> {
/// this might be expensive, see `cached::BranchName`
pub(crate) fn get_branch_name(repo_path: &str) -> Result<String> {
scope_time!("get_branch_name");

let repo = utils::repo(repo_path)?;
Expand Down
2 changes: 1 addition & 1 deletion asyncgit/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub mod status;
mod tags;
pub mod utils;

pub use branch::get_branch_name;
pub(crate) use branch::get_branch_name;
pub use commit::{amend, commit};
pub use commit_details::{get_commit_details, CommitDetails};
pub use commit_files::get_commit_files;
Expand Down
6 changes: 4 additions & 2 deletions src/components/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
ui::style::SharedTheme,
};
use anyhow::Result;
use asyncgit::{sync, StatusItem, StatusItemType, CWD};
use asyncgit::{cached, sync, StatusItem, StatusItemType, CWD};
use crossterm::event::Event;
use std::path::Path;
use strings::commands;
Expand Down Expand Up @@ -39,6 +39,7 @@ pub struct ChangesComponent {
files: FileTreeComponent,
is_working_dir: bool,
queue: Queue,
branch_name: cached::BranchName,
}

impl ChangesComponent {
Expand All @@ -60,13 +61,14 @@ impl ChangesComponent {
),
is_working_dir,
queue,
branch_name: cached::BranchName::new(CWD),
}
}

///
pub fn update(&mut self, list: &[StatusItem]) -> Result<()> {
if self.is_working_dir {
if let Ok(branch_name) = sync::get_branch_name(CWD) {
if let Ok(branch_name) = self.branch_name.lookup() {
self.files.set_title(format!(
"{} - {{{}}}",
&self.title, branch_name,
Expand Down
5 changes: 4 additions & 1 deletion src/tabs/revlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
};
use anyhow::Result;
use asyncgit::{
cached,
sync::{self, CommitId},
AsyncLog, AsyncNotification, FetchStatus, CWD,
};
Expand All @@ -31,6 +32,7 @@ pub struct Revlog {
git_log: AsyncLog,
queue: Queue,
visible: bool,
branch_name: cached::BranchName,
}

impl Revlog {
Expand All @@ -50,6 +52,7 @@ impl Revlog {
list: CommitList::new(strings::LOG_TITLE, theme),
git_log: AsyncLog::new(sender),
visible: false,
branch_name: cached::BranchName::new(CWD),
}
}

Expand Down Expand Up @@ -80,7 +83,7 @@ impl Revlog {
}

self.list.set_branch(
sync::get_branch_name(CWD).map(Some).unwrap_or(None),
self.branch_name.lookup().map(Some).unwrap_or(None),
);

if self.commit_details.is_visible() {
Expand Down

0 comments on commit a33db8d

Please sign in to comment.