-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(git): split repository in multiple modules
- Loading branch information
Showing
14 changed files
with
496 additions
and
474 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use std::fmt::{self, Debug, Display, Formatter}; | ||
|
||
use crate::OidOf; | ||
use crate::git::oid::OidOf; | ||
|
||
use colored::*; | ||
|
||
|
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,77 @@ | ||
use crate::error::CocogittoError; | ||
use crate::git::repository::Repository; | ||
use anyhow::{anyhow, Result}; | ||
use git2::Oid; | ||
|
||
impl Repository { | ||
pub(crate) fn commit(&self, message: &str) -> Result<Oid> { | ||
let sig = self.0.signature()?; | ||
let tree_id = self.0.index()?.write_tree()?; | ||
let tree = self.0.find_tree(tree_id)?; | ||
let is_empty = self.0.is_empty()?; | ||
let has_delta = self.get_diff(false).is_some(); | ||
|
||
if !is_empty && has_delta { | ||
let head = &self.0.head()?; | ||
let head_target = head.target().expect("Cannot get HEAD target"); | ||
let tip = &self.0.find_commit(head_target)?; | ||
|
||
self.0 | ||
.commit(Some("HEAD"), &sig, &sig, message, &tree, &[tip]) | ||
.map_err(|err| anyhow!(err)) | ||
} else if is_empty && has_delta { | ||
// First repo commit | ||
self.0 | ||
.commit(Some("HEAD"), &sig, &sig, message, &tree, &[]) | ||
.map_err(|err| anyhow!(err)) | ||
} else { | ||
let err = self | ||
.get_branch_shorthand() | ||
.map(|branch| CocogittoError::NothingToCommitWithBranch { branch }) | ||
.unwrap_or_else(|| CocogittoError::NothingToCommit); | ||
|
||
Err(anyhow!(err)) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::git::repository::Repository; | ||
use crate::test_helpers::run_test_with_context; | ||
use anyhow::Result; | ||
use speculoos::prelude::*; | ||
|
||
#[test] | ||
fn create_commit_ok() -> Result<()> { | ||
run_test_with_context(|context| { | ||
let repo = Repository::init(".")?; | ||
std::fs::write(context.test_dir.join("file"), "changes")?; | ||
repo.add_all()?; | ||
|
||
assert_that!(repo.commit("feat: a test commit")).is_ok(); | ||
Ok(()) | ||
}) | ||
} | ||
|
||
#[test] | ||
fn not_create_empty_commit() -> Result<()> { | ||
run_test_with_context(|_| { | ||
let repo = Repository::init(".")?; | ||
|
||
assert_that!(repo.commit("feat: a test commit")).is_err(); | ||
Ok(()) | ||
}) | ||
} | ||
|
||
#[test] | ||
fn not_create_empty_commit_with_unstaged_changed() -> Result<()> { | ||
run_test_with_context(|context| { | ||
let repo = Repository::init(".")?; | ||
std::fs::write(context.test_dir.join("file"), "changes")?; | ||
|
||
assert_that!(repo.commit("feat: a test commit")).is_err(); | ||
Ok(()) | ||
}) | ||
} | ||
} |
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,80 @@ | ||
use crate::git::repository::Repository; | ||
use git2::{Diff, DiffOptions}; | ||
|
||
impl Repository { | ||
pub(crate) fn get_diff(&self, include_untracked: bool) -> Option<Diff> { | ||
let mut options = DiffOptions::new(); | ||
options.include_untracked(include_untracked); | ||
|
||
let diff = match &self.get_head() { | ||
Some(head) => self | ||
.0 | ||
.diff_tree_to_index(head.as_tree(), None, Some(&mut options)), | ||
None => self | ||
.0 | ||
.diff_tree_to_workdir_with_index(None, Some(&mut options)), | ||
}; | ||
|
||
match diff { | ||
Ok(diff) => { | ||
if diff.deltas().len() > 0 { | ||
Some(diff) | ||
} else { | ||
None | ||
} | ||
} | ||
Err(..) => None, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::git::repository::Repository; | ||
use crate::test_helpers::run_test_with_context; | ||
use anyhow::Result; | ||
|
||
#[test] | ||
fn get_diff_some() -> Result<()> { | ||
run_test_with_context(|context| { | ||
let repo = Repository::init(".")?; | ||
std::fs::write(context.test_dir.join("file"), "changes")?; | ||
repo.add_all()?; | ||
|
||
assert!(repo.get_diff(false).is_some()); | ||
Ok(()) | ||
}) | ||
} | ||
|
||
#[test] | ||
fn get_diff_none() -> Result<()> { | ||
run_test_with_context(|context| { | ||
let repo = Repository::init(".")?; | ||
std::fs::write(context.test_dir.join("file"), "changes")?; | ||
|
||
assert!(repo.get_diff(false).is_none()); | ||
Ok(()) | ||
}) | ||
} | ||
|
||
#[test] | ||
fn get_diff_include_untracked_some() -> Result<()> { | ||
run_test_with_context(|context| { | ||
let repo = Repository::init(".")?; | ||
std::fs::write(context.test_dir.join("file"), "changes")?; | ||
|
||
assert!(repo.get_diff(true).is_some()); | ||
Ok(()) | ||
}) | ||
} | ||
|
||
#[test] | ||
fn get_diff_include_untracked_none() -> Result<()> { | ||
run_test_with_context(|_| { | ||
let repo = Repository::init(".")?; | ||
|
||
assert!(repo.get_diff(true).is_none()); | ||
Ok(()) | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
pub mod commit; | ||
pub mod diff; | ||
pub mod hook; | ||
pub mod oid; | ||
pub mod repository; | ||
pub mod revspec; | ||
pub mod stash; | ||
pub mod status; | ||
pub mod tag; |
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,24 @@ | ||
use std::fmt::{Display, Formatter}; | ||
|
||
use git2::Oid; | ||
|
||
use crate::git::tag::Tag; | ||
|
||
/// A wrapper for git2 oid including tags and HEAD ref | ||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum OidOf { | ||
Tag(Tag), | ||
Head(Oid), | ||
Other(Oid), | ||
} | ||
|
||
impl Display for OidOf { | ||
/// Print the oid according to it's type | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
OidOf::Tag(tag) => write!(f, "{}", tag), | ||
OidOf::Head(_) => write!(f, "HEAD"), | ||
OidOf::Other(oid) => write!(f, "{}", &oid.to_string()[0..6]), | ||
} | ||
} | ||
} |
Oops, something went wrong.