@@ -10,6 +10,7 @@ use gix_ref::{
1010} ;
1111use smallvec:: SmallVec ;
1212
13+ use crate :: repository:: { new_commit, new_commit_as} ;
1314use 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,6 +407,49 @@ impl crate::Repository {
406407 self . commit_as ( committer, author, reference, message, tree, parents)
407408 }
408409
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+ ///
413+ /// The commit is created without message encoding field, which can be assumed to be UTF-8.
414+ /// `author` and `committer` fields are pre-set from the configuration, which can be altered
415+ /// [temporarily](crate::Repository::config_snapshot_mut()) before the call if required.
416+ pub fn new_commit (
417+ & self ,
418+ message : impl AsRef < str > ,
419+ tree : impl Into < ObjectId > ,
420+ parents : impl IntoIterator < Item = impl Into < ObjectId > > ,
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) ?)
425+ }
426+
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.
429+ ///
430+ /// This forces setting the commit time and author time by hand. Note that typically, committer and author are the same.
431+ /// The commit is created without message encoding field, which can be assumed to be UTF-8.
432+ pub fn new_commit_as < ' a , ' c > (
433+ & self ,
434+ committer : impl Into < gix_actor:: SignatureRef < ' c > > ,
435+ author : impl Into < gix_actor:: SignatureRef < ' a > > ,
436+ message : impl AsRef < str > ,
437+ tree : impl Into < ObjectId > ,
438+ parents : impl IntoIterator < Item = impl Into < ObjectId > > ,
439+ ) -> Result < Commit < ' _ > , new_commit_as:: Error > {
440+ let commit = gix_object:: Commit {
441+ message : message. as_ref ( ) . into ( ) ,
442+ tree : tree. into ( ) ,
443+ author : author. into ( ) . into ( ) ,
444+ committer : committer. into ( ) . into ( ) ,
445+ encoding : None ,
446+ parents : parents. into_iter ( ) . map ( Into :: into) . collect ( ) ,
447+ extra_headers : Default :: default ( ) ,
448+ } ;
449+ let id = self . write_object ( commit) ?;
450+ Ok ( id. object ( ) ?. into_commit ( ) )
451+ }
452+
409453 /// Return an empty tree object, suitable for [getting changes](Tree::changes()).
410454 ///
411455 /// Note that the returned object is special and doesn't necessarily physically exist in the object database.
0 commit comments