Skip to content

Commit

Permalink
feat: Head::prior_checked_out_branches() (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 3, 2022
1 parent 1e47bc1 commit 727768a
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions git-repository/src/head.rs
Expand Up @@ -83,6 +83,8 @@ impl<'repo> Head<'repo> {
}
///
pub mod log {
use crate::bstr::{BString, ByteSlice};
use git_hash::ObjectId;
use std::convert::TryInto;

use crate::Head;
Expand All @@ -96,6 +98,22 @@ pub mod log {
buf: Vec::new(),
}
}

/// Return a list of all branch names that were previously checked out with the first-ever checked out branch
/// being the first entry of the list, and the most recent is the last, along with the commit they were pointing to
/// at the time.
pub fn prior_checked_out_branches(&self) -> std::io::Result<Option<Vec<(BString, ObjectId)>>> {
Ok(self.log_iter().all()?.map(|log| {
log.filter_map(Result::ok)
.filter_map(|line| {
line.message
.strip_prefix(b"checkout: moving from ")
.and_then(|from_to| from_to.find(" to ").map(|pos| &from_to[..pos]))
.map(|from_branch| (from_branch.as_bstr().to_owned(), line.previous_oid()))
})
.collect()
}))
}
}
}

Expand Down

0 comments on commit 727768a

Please sign in to comment.