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