Skip to content

Commit e74d1ef

Browse files
authored
feat(CLI): add git dirty check for run commands (#1618)
* feat: add dirty git checker * feat: add dirty_git_check command into run, jssg/run and workflow/run command * refactor: using inquire insted of std::io * feat: add .git and git is installed check for dirty cheker * refactor: delete condition for check git status result in porcelain
1 parent 5353fce commit e74d1ef

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

crates/cli/src/commands/jssg/run.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use codemod_sandbox::sandbox::{
88
};
99
use std::{path::Path, sync::Arc};
1010

11+
use crate::dirty_git_check;
12+
1113
#[derive(Args, Debug)]
1214
pub struct Command {
1315
/// Path to the JavaScript file to execute
@@ -39,6 +41,10 @@ pub struct Command {
3941
/// File extensions to process (comma-separated)
4042
#[arg(long)]
4143
pub extensions: Option<String>,
44+
45+
/// Allow dirty git status
46+
#[arg(long)]
47+
pub allow_dirty: bool,
4248
}
4349

4450
pub async fn handler(args: &Command) -> Result<()> {
@@ -60,6 +66,8 @@ pub async fn handler(args: &Command) -> Result<()> {
6066
let mut config = ExecutionConfig::new(filesystem, resolver, loader, script_base_dir);
6167
let mut walk_options = WalkOptions::default();
6268

69+
dirty_git_check::dirty_check(args.allow_dirty)?;
70+
6371
// Apply command line options
6472
if args.no_gitignore {
6573
walk_options.respect_gitignore = false;

crates/cli/src/commands/run.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::path::{Path, PathBuf};
66
use std::process::Command as ProcessCommand;
77

88
use crate::auth_provider::CliAuthProvider;
9+
use crate::dirty_git_check;
910
use crate::workflow_runner::{run_workflow, WorkflowRunConfig};
1011
use butterflow_core::engine::Engine;
1112
use butterflow_core::registry::{RegistryClient, RegistryConfig, RegistryError};
@@ -35,12 +36,18 @@ pub struct Command {
3536
/// Additional arguments to pass to the codemod
3637
#[arg(last = true)]
3738
args: Vec<String>,
39+
40+
/// Allow dirty git status
41+
#[arg(long)]
42+
allow_dirty: bool,
3843
}
3944

4045
pub async fn handler(engine: &Engine, args: &Command) -> Result<()> {
4146
// Create auth provider
4247
let auth_provider = CliAuthProvider::new()?;
4348

49+
dirty_git_check::dirty_check(args.allow_dirty)?;
50+
4451
// Get cache directory and default registry from config
4552
let cache_dir = get_cache_dir()?;
4653
let config = auth_provider.storage.load_config()?;

crates/cli/src/commands/workflow/run.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use butterflow_core::engine::Engine;
33
use butterflow_core::utils;
44
use clap::Args;
55

6+
use crate::dirty_git_check;
67
use crate::workflow_runner::{resolve_workflow_source, run_workflow, WorkflowRunConfig};
78

89
#[derive(Args, Debug)]
@@ -14,6 +15,10 @@ pub struct Command {
1415
/// Workflow parameters (format: key=value)
1516
#[arg(long = "param", value_name = "KEY=VALUE")]
1617
params: Vec<String>,
18+
19+
/// Allow dirty git status
20+
#[arg(long)]
21+
allow_dirty: bool,
1722
}
1823

1924
/// Run a workflow
@@ -32,6 +37,8 @@ pub async fn handler(engine: &Engine, args: &Command) -> Result<()> {
3237
wait_for_completion: true,
3338
};
3439

40+
dirty_git_check::dirty_check(args.allow_dirty)?;
41+
3542
// Run workflow using the extracted workflow runner
3643
run_workflow(engine, config).await?;
3744

crates/cli/src/dirty_git_check.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use anyhow::Result;
2+
use inquire::Confirm;
3+
use std::path::Path;
4+
use std::process::Command;
5+
6+
pub fn dirty_check(allow_dirty: bool) -> Result<()> {
7+
if !allow_dirty
8+
&& Command::new("git").arg("--version").output().is_ok()
9+
&& Path::new(".git").exists()
10+
{
11+
let output = Command::new("git")
12+
.args(["status", "--porcelain"])
13+
.output()
14+
.expect("Failed to run git");
15+
16+
if !output.stdout.is_empty() {
17+
let answer =
18+
Confirm::new("⚠️ You have uncommitted changes. Do you want to continue anyway?")
19+
.with_default(false)
20+
.with_help_message("Press 'y' to continue or 'n' to abort")
21+
.prompt()?;
22+
23+
if !answer {
24+
return Err(anyhow::anyhow!("Aborting due to uncommitted changes"));
25+
}
26+
}
27+
}
28+
29+
Ok(())
30+
}

crates/cli/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod ascii_art;
66
mod auth;
77
mod auth_provider;
88
mod commands;
9+
mod dirty_git_check;
910
mod engine;
1011
mod workflow_runner;
1112
use ascii_art::print_ascii_art;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy