Skip to main content

garden/cmds/
git.rs

1use anyhow::Result;
2use clap::{Parser, ValueHint};
3
4use crate::cmds::exec;
5use crate::model;
6
7/// Evaluate garden expressions
8#[derive(Parser, Clone, Debug)]
9#[command(author, about, long_about)]
10pub struct GitOptions {
11    /// Filter trees by name post-query using a glob pattern
12    #[arg(long, short, default_value = "*")]
13    trees: String,
14    /// Perform a trial run without executing any commands
15    #[arg(long, short = 'N', short_alias = 'n')]
16    dry_run: bool,
17    /// Run commands in parallel using the specified number of jobs.
18    #[arg(long = "jobs", short = 'j', value_name = "JOBS")]
19    num_jobs: Option<usize>,
20    /// Be quiet
21    #[arg(short, long)]
22    quiet: bool,
23    /// Increase verbosity level (default: 0)
24    #[arg(short, long, action = clap::ArgAction::Count)]
25    verbose: u8,
26    /// Tree query for the gardens, groups or trees to run the command
27    #[arg(default_value = "@*", value_hint=ValueHint::Other)]
28    query: String,
29    /// Git command to run in the resolved environments
30    #[arg(
31        default_value = "status",
32        allow_hyphen_values = true,
33        trailing_var_arg = true
34    )]
35    command: Vec<String>,
36}
37
38/// Convert GitOptions into ExecOptions
39impl From<GitOptions> for exec::ExecOptions {
40    fn from(git_options: GitOptions) -> Self {
41        exec::ExecOptions {
42            dry_run: git_options.dry_run,
43            num_jobs: git_options.num_jobs,
44            quiet: git_options.quiet,
45            verbose: git_options.verbose,
46            query: git_options.query,
47            command: {
48                let mut cmd = vec!["git".to_string()];
49                cmd.extend(git_options.command);
50                cmd
51            },
52            trees: git_options.trees,
53        }
54    }
55}
56
57/// Main entry point for the "garden git" command
58pub fn main(app_context: &model::ApplicationContext, git_options: &mut GitOptions) -> Result<()> {
59    let mut exec_options: exec::ExecOptions = git_options.clone().into();
60
61    exec::main(app_context, &mut exec_options)
62}