willbe/entity/
git.rs

1#[ allow( clippy::std_instead_of_alloc, clippy::std_instead_of_core ) ]
2mod private
3{
4
5  use crate::*;
6
7  use std::fmt;
8  use process_tools::process;
9  use error::
10  {
11    untyped::{ format_err, Context },
12  };
13
14  #[ derive( Debug, Default, Clone ) ]
15  /// Represents an extended Git report with optional process reports.
16  pub struct ExtendedGitReport
17  {
18    /// Optional report for the `git add` process.
19    pub add : Option< process::Report >,
20    /// Optional report for the `git commit` process.
21    pub commit : Option< process::Report >,
22    /// Optional report for the `git push` process.
23    pub push : Option< process::Report >,
24  }
25
26  impl fmt::Display for ExtendedGitReport
27  {
28    fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
29    {
30      let Self { add, commit, push } = &self;
31
32      if let Some( add ) = add { writeln!( f, "{add}" )? }
33      if let Some( commit ) = commit { writeln!( f, "{commit}" )? }
34      if let Some( push ) = push { writeln!( f, "{push}" )? }
35
36      std::fmt::Result::Ok( () )
37    }
38  }
39
40  // aaa : for Bohdan : should not be here // aaa : done
41  // aaa : for Bohdan : documentation // aaa : done
42  /// The `GitOptions` struct represents a set of options used to perform a Git commit operation.
43  #[ derive( Debug, Clone ) ]
44  pub struct GitOptions
45  {
46    /// An absolute path to the root directory of the Git repository.
47    pub git_root : AbsolutePath,
48    /// A vector of absolute paths to the files or directories that should be committed.
49    pub items : Vec< AbsolutePath >,
50    /// A string containing the commit message.
51    pub message : String,
52    /// A boolean flag indicating whether the commit should be performed in dry run mode
53    /// (i.e., no changes are actually made to the repository)
54    pub dry : bool,
55  }
56
57  /// Performs a Git commit operation using the provided options
58  /// # Errors
59  /// qqq: doc
60  #[ allow( clippy::needless_pass_by_value ) ]
61  pub fn perform_git_commit( o : GitOptions ) -> error::untyped::Result< ExtendedGitReport >
62  // qqq : use typed error
63  {
64    use tool::git;
65    let mut report = ExtendedGitReport::default();
66    if o.items.is_empty() { return error::untyped::Result::Ok( report ); }
67    let items : error::untyped::Result< Vec< _ > > = o
68    .items
69    .iter()
70    .map
71    (
72      | item | item.as_ref().strip_prefix( o.git_root.as_ref() ).map( std::path::Path::to_string_lossy )
73      .with_context( || format!("git_root: {}, item: {}", o.git_root.as_ref().display(), item.as_ref().display() ) )
74    )
75    .collect();
76
77    let res = git::add( &o.git_root, &items?, o.dry ).map_err( | e | format_err!( "{report}\n{e}" ) )?;
78    report.add = Some( res );
79    let res = git::commit( &o.git_root, &o.message, o.dry ).map_err( | e | format_err!( "{report}\n{e}" ) )?;
80    report.commit = Some( res );
81
82    error::untyped::Result::Ok( report )
83  }
84}
85
86//
87
88crate::mod_interface!
89{
90  own use ExtendedGitReport;
91  own use GitOptions;
92  own use perform_git_commit;
93}