gnostr_asyncgit/cached/
branchname.rs

1use sync::Head;
2
3use crate::{
4    error::Result,
5    sync::{self, branch::get_branch_name, RepoPathRef},
6};
7
8///
9pub struct BranchName {
10    last_result: Option<(Head, String)>,
11    repo: RepoPathRef,
12}
13
14impl BranchName {
15    ///
16    pub const fn new(repo: RepoPathRef) -> Self {
17        Self {
18            repo,
19            last_result: None,
20        }
21    }
22
23    ///
24    pub fn lookup(&mut self) -> Result<String> {
25        let current_head = sync::get_head_tuple(&self.repo.borrow())?;
26
27        if let Some((last_head, branch_name)) = self.last_result.as_ref() {
28            if *last_head == current_head {
29                return Ok(branch_name.clone());
30            }
31        }
32
33        self.fetch(current_head)
34    }
35
36    ///
37    pub fn last(&self) -> Option<String> {
38        self.last_result.as_ref().map(|last| last.1.clone())
39    }
40
41    fn fetch(&mut self, head: Head) -> Result<String> {
42        let name = get_branch_name(&self.repo.borrow())?;
43        self.last_result = Some((head, name.clone()));
44        Ok(name)
45    }
46}