Skip to content

Commit d4c2542

Browse files
committed
refactor
1 parent 3c0b737 commit d4c2542

File tree

6 files changed

+104
-244
lines changed

6 files changed

+104
-244
lines changed

gix/examples/raw-commit-demo.rs

Lines changed: 0 additions & 114 deletions
This file was deleted.

gix/src/commit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::convert::Infallible;
66
/// An empty array of a type usable with the `gix::easy` API to help declaring no parents should be used
77
pub const NO_PARENT_IDS: [gix_hash::ObjectId; 0] = [];
88

9-
/// The error returned by [`commit(…)`][crate::Repository::commit()].
9+
/// The error returned by [`commit(…)`](crate::Repository::commit()).
1010
#[derive(Debug, thiserror::Error)]
1111
#[allow(missing_docs)]
1212
pub enum Error {

gix/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
doc = ::document_features::document_features!()
8787
)]
8888
#![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg, doc_auto_cfg))]
89-
#![deny(missing_docs, rust_2018_idioms, unsafe_code)]
89+
#![deny(missing_docs, unsafe_code)]
9090
#![allow(clippy::result_large_err)]
9191

9292
// Re-exports to make this a potential one-stop shop crate avoiding people from having to reference various crates themselves.

gix/src/repository/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,36 @@ mod submodule;
6363
mod thread_safe;
6464
mod worktree;
6565

66+
///
67+
mod new_commit {
68+
/// The error returned by [`new_commit(…)`](crate::Repository::new_commit()).
69+
#[derive(Debug, thiserror::Error)]
70+
#[allow(missing_docs)]
71+
pub enum Error {
72+
#[error(transparent)]
73+
ParseTime(#[from] crate::config::time::Error),
74+
#[error("Committer identity is not configured")]
75+
CommitterMissing,
76+
#[error("Author identity is not configured")]
77+
AuthorMissing,
78+
#[error(transparent)]
79+
NewCommitAs(#[from] crate::repository::new_commit_as::Error),
80+
}
81+
}
82+
83+
///
84+
mod new_commit_as {
85+
/// The error returned by [`new_commit_as(…)`](crate::Repository::new_commit_as()).
86+
#[derive(Debug, thiserror::Error)]
87+
#[allow(missing_docs)]
88+
pub enum Error {
89+
#[error(transparent)]
90+
WriteObject(#[from] crate::object::write::Error),
91+
#[error(transparent)]
92+
FindCommit(#[from] crate::object::find::existing::Error),
93+
}
94+
}
95+
6696
///
6797
#[cfg(feature = "blame")]
6898
pub mod blame_file {

gix/src/repository/object.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use gix_ref::{
1010
};
1111
use smallvec::SmallVec;
1212

13+
use crate::repository::{new_commit, new_commit_as};
1314
use crate::{commit, ext::ObjectIdExt, object, tag, Blob, Commit, Id, Object, Reference, Tag, Tree};
1415

1516
/// Tree editing
@@ -264,7 +265,7 @@ impl crate::Repository {
264265
/// Create a tag reference named `name` (without `refs/tags/` prefix) pointing to a newly created tag object
265266
/// which in turn points to `target` and return the newly created reference.
266267
///
267-
/// It will be created with `constraint` which is most commonly to [only create it][PreviousValue::MustNotExist]
268+
/// It will be created with `constraint` which is most commonly to [only create it](PreviousValue::MustNotExist)
268269
/// or to [force overwriting a possibly existing tag](PreviousValue::Any).
269270
pub fn tag(
270271
&self,
@@ -406,37 +407,36 @@ impl crate::Repository {
406407
self.commit_as(committer, author, reference, message, tree, parents)
407408
}
408409

409-
/// Create a raw commit object with `message` referring to `tree` with `parents`, without writing it to the object database
410-
/// or updating any references. The commit object can later be written using [`Self::write_object()`].
411-
///
410+
/// Create a new commit object with `message` referring to `tree` with `parents`, and write it to the object database.
411+
/// Do not, however, update any references.
412+
///
412413
/// The commit is created without message encoding field, which can be assumed to be UTF-8.
413414
/// `author` and `committer` fields are pre-set from the configuration, which can be altered
414415
/// [temporarily](crate::Repository::config_snapshot_mut()) before the call if required.
415-
pub fn commit_raw(
416+
pub fn new_commit(
416417
&self,
417418
message: impl AsRef<str>,
418419
tree: impl Into<ObjectId>,
419420
parents: impl IntoIterator<Item = impl Into<ObjectId>>,
420-
) -> Result<gix_object::Commit, commit::Error> {
421-
let author = self.author().ok_or(commit::Error::AuthorMissing)??;
422-
let committer = self.committer().ok_or(commit::Error::CommitterMissing)??;
423-
self.commit_as_raw(committer, author, message, tree, parents)
421+
) -> Result<Commit<'_>, new_commit::Error> {
422+
let author = self.author().ok_or(new_commit::Error::AuthorMissing)??;
423+
let committer = self.committer().ok_or(new_commit::Error::CommitterMissing)??;
424+
Ok(self.new_commit_as(committer, author, message, tree, parents)?)
424425
}
425426

426-
/// Create a raw commit object with `message` referring to `tree` with `parents`, using the specified
427-
/// `committer` and `author`, without writing it to the object database or updating any references.
428-
/// The commit object can later be written using [`Self::write_object()`].
427+
/// Create a nwe commit object with `message` referring to `tree` with `parents`, using the specified
428+
/// `committer` and `author`, and write it to the object database. Do not, however, update any references.
429429
///
430430
/// This forces setting the commit time and author time by hand. Note that typically, committer and author are the same.
431431
/// The commit is created without message encoding field, which can be assumed to be UTF-8.
432-
pub fn commit_as_raw<'a, 'c>(
432+
pub fn new_commit_as<'a, 'c>(
433433
&self,
434434
committer: impl Into<gix_actor::SignatureRef<'c>>,
435435
author: impl Into<gix_actor::SignatureRef<'a>>,
436436
message: impl AsRef<str>,
437437
tree: impl Into<ObjectId>,
438438
parents: impl IntoIterator<Item = impl Into<ObjectId>>,
439-
) -> Result<gix_object::Commit, commit::Error> {
439+
) -> Result<Commit<'_>, new_commit_as::Error> {
440440
let commit = gix_object::Commit {
441441
message: message.as_ref().into(),
442442
tree: tree.into(),
@@ -446,7 +446,8 @@ impl crate::Repository {
446446
parents: parents.into_iter().map(Into::into).collect(),
447447
extra_headers: Default::default(),
448448
};
449-
Ok(commit)
449+
let id = self.write_object(commit)?;
450+
Ok(id.object()?.into_commit())
450451
}
451452

452453
/// Return an empty tree object, suitable for [getting changes](Tree::changes()).

0 commit comments

Comments
 (0)