From 727768a49c41165b03ddcdbc71ca88b66c330f32 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 3 Aug 2022 14:16:07 +0800 Subject: [PATCH] feat: `Head::prior_checked_out_branches()` (#427) --- git-repository/src/head.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/git-repository/src/head.rs b/git-repository/src/head.rs index 2a7c1cc991..9c12af0f76 100644 --- a/git-repository/src/head.rs +++ b/git-repository/src/head.rs @@ -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; @@ -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>> { + 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() + })) + } } }