Skip to content

Commit

Permalink
[repository #164] another test and fix for commit()
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 12, 2021
1 parent c4984af commit 8d676d7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions git-repository/src/easy/ext/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub trait ObjectAccessExt: easy::Access + Sized {
///
/// `reference` will be created if it doesn't exist, and can be `"HEAD"` to automatically write-through to the symbolic reference
/// that `HEAD` points to if it is not detached. For this reason, detached head states cannot be created unless the `HEAD` is detached
/// already.
/// already. The reflog will be written as canonical git would do, like `<operation> (<detail>): <summary>`.
///
/// The first parent id in `parents` is expected to be the current target of `reference` and the operation will fail if it is not.
/// If there is no parent, the `reference` is expected to not exist yet.
Expand Down Expand Up @@ -137,7 +137,13 @@ pub trait ObjectAccessExt: easy::Access + Sized {
message: crate::reference::log::message("commit", &commit),
},
expected: match commit.parents.get(0).map(|p| Target::Peeled(*p)) {
Some(previous) => PreviousValue::ExistingMustMatch(previous),
Some(previous) => {
if reference.as_bstr() == "HEAD" {
PreviousValue::MustExistAndMatch(previous)
} else {
PreviousValue::ExistingMustMatch(previous)
}
}
None => PreviousValue::MustNotExist,
},
new: Target::Peeled(commit_id.inner),
Expand Down
19 changes: 19 additions & 0 deletions git-repository/tests/easy/ext/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ mod commit {
use git_repository as git;
use git_repository::prelude::{ObjectAccessExt, ReferenceAccessExt};
use git_testtools::hex_to_id;
#[test]
fn parent_in_initial_commit_causes_failure() {
let tmp = tempfile::tempdir().unwrap();
let repo = git::init(&tmp).unwrap().into_easy();
let empty_tree_id = repo.write_object(&git::objs::Tree::empty()).unwrap().detach();
let author = git::actor::Signature::empty();
let err = repo
.commit(
"HEAD",
&author.to_ref(),
&author.to_ref(),
"initial",
empty_tree_id,
[empty_tree_id],
)
.unwrap_err();
assert_eq!(err.to_string(), "Reference 'refs/heads/main' was supposed to exist with value 4b825dc642cb6eb9a060e54bf8d69288fbee4904, but didn't.", "cannot provide parent id in initial commit");
}

#[test]
fn single_line_initial_commit_empty_tree_ref_nonexisting() -> crate::Result {
Expand All @@ -41,6 +59,7 @@ mod commit {
);

let head = repo.head()?.into_referent();
assert_eq!(head.name().as_bstr(), "refs/heads/main", "'main' is the default name");
assert_eq!(
head.log()?
.iter_rev()?
Expand Down

0 comments on commit 8d676d7

Please sign in to comment.