From aa656d3a1940f80fc263fdc03016711b5627c76b Mon Sep 17 00:00:00 2001 From: Hubert Date: Sun, 18 Jul 2021 14:40:56 +0200 Subject: [PATCH] head --- src/gitcommit.rs | 14 ++++++-------- src/gitrepo.rs | 4 ++++ src/main.rs | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/gitcommit.rs b/src/gitcommit.rs index 8838bbb..1ef9b0e 100644 --- a/src/gitcommit.rs +++ b/src/gitcommit.rs @@ -4,6 +4,7 @@ use std::fmt::{Display, Formatter}; pub enum GitRef{ Commit(String), Branch(String), + Head, } impl GitRef { @@ -14,17 +15,14 @@ impl GitRef { pub fn new_branch(s : String) -> GitRef { GitRef::Branch(s) } - - fn value(&self) -> &String { - match self { - GitRef::Commit(s) => s, - GitRef::Branch(s) => s - } - } } impl Display for GitRef { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.value()) + write!(f, "{}", match &self { + GitRef::Commit(s) => {s.as_str()} + GitRef::Branch(s) => {s.as_str()} + GitRef::Head => {"HEAD"} + }) } } diff --git a/src/gitrepo.rs b/src/gitrepo.rs index 7b26989..4c934ff 100644 --- a/src/gitrepo.rs +++ b/src/gitrepo.rs @@ -30,6 +30,10 @@ impl GitRepo { let branch = self.git2.find_branch(branch.as_str(), BranchType::Local)?; branch.get().peel_to_commit()? } + GitRef::Head => { + let head = self.git2.head()?; + head.peel_to_commit()? + } }; let tree = commit.tree()?; let fullname = PathBuf::from("/"); diff --git a/src/main.rs b/src/main.rs index 472925b..d378a00 100644 --- a/src/main.rs +++ b/src/main.rs @@ -158,7 +158,7 @@ async fn git_main( }; let commit = match commitnameopt { None => {match branchopt { - None => {GitRef::Branch("master".to_string())} + None => {GitRef::Head} Some(s) => {GitRef::Branch(s)} }} Some(s) => {GitRef::Commit(s)}