-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
196 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ mod testutils; | |
pub mod cli; | ||
pub mod gitutils; | ||
pub mod service; | ||
pub mod version_source; | ||
pub mod versioning; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use crate::gitutils; | ||
use git2::Repository; | ||
|
||
pub trait VersionSource { | ||
fn get_all_versions(&self, repo: &Repository) -> Vec<String>; | ||
fn checkout(&self, repo: &Repository, version: &str) -> Result<(), git2::Error>; | ||
fn create_new(&self, repo: &Repository, version: &str) -> Result<(), git2::Error>; | ||
} | ||
|
||
pub struct TagVersionSource; | ||
pub struct BranchVersionSource; | ||
|
||
impl VersionSource for TagVersionSource { | ||
fn get_all_versions(&self, repo: &Repository) -> Vec<String> { | ||
repo.tag_names(Some("*")) | ||
.expect("Failed to fetch tags") | ||
.iter() | ||
.filter_map(|s| s.map(|s| s.to_string())) | ||
.collect() | ||
} | ||
|
||
fn checkout(&self, repo: &Repository, version: &str) -> Result<(), git2::Error> { | ||
gitutils::checkout_tag(repo, version, None) | ||
} | ||
|
||
fn create_new(&self, repo: &Repository, version: &str) -> Result<(), git2::Error> { | ||
let head = repo.head()?; | ||
let head_id = head.target().unwrap(); | ||
gitutils::tag_oid(repo, head_id, version)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl VersionSource for BranchVersionSource { | ||
fn get_all_versions(&self, repo: &Repository) -> Vec<String> { | ||
repo.branches(Some(git2::BranchType::Local)) | ||
.expect("Failed to fetch branches") | ||
.filter_map(|b| b.ok()) | ||
.filter_map(|(branch, _)| branch.name().ok().flatten().map(|s| s.to_string())) | ||
.collect() | ||
} | ||
|
||
fn checkout(&self, repo: &Repository, version: &str) -> Result<(), git2::Error> { | ||
gitutils::checkout_branch(repo, version, false, None) | ||
} | ||
|
||
fn create_new(&self, repo: &Repository, version: &str) -> Result<(), git2::Error> { | ||
gitutils::checkout_branch(repo, version, true, None) | ||
} | ||
} |