Skip to content

Commit

Permalink
feat: add support for get branch by name
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 13, 2021
1 parent 0a89d69 commit bf969b9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/domain/git/branch.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
14 changes: 14 additions & 0 deletions src/infrastructure/git/git_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,18 @@ impl GitBranch {

coco_branches
}

pub fn get(name: &str, repo: Repository) -> Option<Branch> {
let filter: Vec<Branch> = GitBranch::list(repo)
.iter()
.filter(|br| br.name == name)
.cloned()
.collect();

return if filter.len() > 0 {
Some(filter[0].clone())
} else {
None
};
}
}
9 changes: 9 additions & 0 deletions src/infrastructure/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit bf969b9

Please sign in to comment.