From bf969b9bb230c8b4732ef2e5ad7531f71cd83210 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Wed, 13 Jan 2021 20:12:33 +0800 Subject: [PATCH] feat: add support for get branch by name --- src/domain/git/branch.rs | 2 +- src/infrastructure/git/git_branch.rs | 14 ++++++++++++++ src/infrastructure/git/mod.rs | 9 +++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/domain/git/branch.rs b/src/domain/git/branch.rs index f627e13f..2c96a858 100644 --- a/src/domain/git/branch.rs +++ b/src/domain/git/branch.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct Branch { pub name: String, pub fist_commit: String, diff --git a/src/infrastructure/git/git_branch.rs b/src/infrastructure/git/git_branch.rs index cd2b1224..79fbceed 100644 --- a/src/infrastructure/git/git_branch.rs +++ b/src/infrastructure/git/git_branch.rs @@ -15,4 +15,18 @@ impl GitBranch { coco_branches } + + pub fn get(name: &str, repo: Repository) -> Option { + let filter: Vec = GitBranch::list(repo) + .iter() + .filter(|br| br.name == name) + .cloned() + .collect(); + + return if filter.len() > 0 { + Some(filter[0].clone()) + } else { + None + }; + } } diff --git a/src/infrastructure/git/mod.rs b/src/infrastructure/git/mod.rs index d9113d70..92031c42 100644 --- a/src/infrastructure/git/mod.rs +++ b/src/infrastructure/git/mod.rs @@ -25,4 +25,13 @@ mod test { let branches = GitBranch::list(repo); assert_eq!(5, branches.len()); } + + #[test] + fn should_get_master() { + initialize(); + + let repo = GitRepository::clone("https://github.com/phodal/coco.fixtures"); + let branch = GitBranch::get("master", repo); + assert_eq!("master", branch.unwrap().name); + } }