#[ allow( clippy ::std_instead_of_alloc, clippy ::std_instead_of_core ) ]
mod private
{
use crate :: *;
use std ::fmt;
use process_tools ::process;
use error ::
{
untyped :: { format_err, Context },
};
#[ derive( Debug, Default, Clone ) ]
pub struct ExtendedGitReport
{
pub add: Option< process ::Report >,
pub commit: Option< process ::Report >,
pub push: Option< process ::Report >,
}
impl fmt ::Display for ExtendedGitReport
{
fn fmt( &self, f: &mut fmt ::Formatter< '_ > ) -> fmt ::Result
{
let Self { add, commit, push } = &self;
if let Some( add ) = add
{ writeln!( f, "{add}" )? }
if let Some( commit ) = commit
{ writeln!( f, "{commit}" )? }
if let Some( push ) = push
{ writeln!( f, "{push}" )? }
std ::fmt ::Result ::Ok( () )
}
}
#[ derive( Debug, Clone ) ]
pub struct GitOptions
{
pub git_root: AbsolutePath,
pub items: Vec< AbsolutePath >,
pub message: String,
pub dry: bool,
}
#[ allow( clippy ::needless_pass_by_value ) ]
pub fn perform_git_commit( o: GitOptions ) -> error ::untyped ::Result< ExtendedGitReport >
{
use tool ::git;
let mut report = ExtendedGitReport ::default();
if o.items.is_empty() { return error ::untyped ::Result ::Ok( report ); }
let items: error ::untyped ::Result< Vec< _ > > = o
.items
.iter()
.map
(
| item | item.as_ref().strip_prefix( o.git_root.as_ref() ).map( std ::path ::Path ::to_string_lossy )
.with_context( || format!("git_root: {}, item: {}", o.git_root.as_ref().display(), item.as_ref().display() ) )
)
.collect();
let res = git ::add( &o.git_root, &items?, o.dry ).map_err( | e | format_err!( "{report}\n{e}" ) )?;
report.add = Some( res );
let res = git ::commit( &o.git_root, &o.message, o.dry ).map_err( | e | format_err!( "{report}\n{e}" ) )?;
report.commit = Some( res );
error ::untyped ::Result ::Ok( report )
}
}
crate ::mod_interface!
{
own use ExtendedGitReport;
own use GitOptions;
own use perform_git_commit;
}