diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/args/flags.rs | 9 | ||||
-rw-r--r-- | cli/tools/registry/mod.rs | 40 |
2 files changed, 49 insertions, 0 deletions
diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 05d9a3973..26e0b089a 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -307,6 +307,7 @@ pub struct PublishFlags { pub token: Option<String>, pub dry_run: bool, pub allow_slow_types: bool, + pub allow_dirty: bool, pub no_provenance: bool, } @@ -2436,6 +2437,11 @@ fn publish_subcommand() -> Command { .action(ArgAction::SetTrue), ) .arg( + Arg::new("allow-dirty") + .long("allow-dirty") + .help("Allow publishing if the repository has uncommited changed") + .action(ArgAction::SetTrue), + ).arg( Arg::new("no-provenance") .long("no-provenance") .help("Disable provenance attestation. Enabled by default on Github actions, publicly links the package to where it was built and published from.") @@ -3897,6 +3903,7 @@ fn publish_parse(flags: &mut Flags, matches: &mut ArgMatches) { token: matches.remove_one("token"), dry_run: matches.get_flag("dry-run"), allow_slow_types: matches.get_flag("allow-slow-types"), + allow_dirty: matches.get_flag("allow-dirty"), no_provenance: matches.get_flag("no-provenance"), }); } @@ -8620,6 +8627,7 @@ mod tests { "--no-provenance", "--dry-run", "--allow-slow-types", + "--allow-dirty", "--token=asdf", ]); assert_eq!( @@ -8629,6 +8637,7 @@ mod tests { token: Some("asdf".to_string()), dry_run: true, allow_slow_types: true, + allow_dirty: true, no_provenance: true, }), type_check_mode: TypeCheckMode::Local, diff --git a/cli/tools/registry/mod.rs b/cli/tools/registry/mod.rs index faea95a1a..f8c79d85c 100644 --- a/cli/tools/registry/mod.rs +++ b/cli/tools/registry/mod.rs @@ -2,6 +2,8 @@ use std::collections::HashMap; use std::io::IsTerminal; +use std::path::Path; +use std::process::Stdio; use std::rc::Rc; use std::sync::Arc; @@ -24,6 +26,7 @@ use lsp_types::Url; use serde::Deserialize; use serde::Serialize; use sha2::Digest; +use tokio::process::Command; use crate::args::jsr_api_url; use crate::args::jsr_url; @@ -943,6 +946,15 @@ pub async fn publish( return Ok(()); } + if std::env::var("DENO_TESTING_DISABLE_GIT_CHECK") + .ok() + .is_none() + && !publish_flags.allow_dirty + && check_if_git_repo_dirty(cli_options.initial_cwd()).await + { + bail!("Aborting due to uncomitted changes",); + } + perform_publish( cli_factory.http_client(), prepared_data.publish_order_graph, @@ -1022,6 +1034,34 @@ fn verify_version_manifest( Ok(()) } +async fn check_if_git_repo_dirty(cwd: &Path) -> bool { + let bin_name = if cfg!(windows) { "git.exe" } else { "git" }; + + // Check if git exists + let git_exists = Command::new(bin_name) + .arg("--version") + .stderr(Stdio::null()) + .stdout(Stdio::null()) + .status() + .await + .map_or(false, |status| status.success()); + + if !git_exists { + return false; // Git is not installed + } + + // Check if there are uncommitted changes + let output = Command::new(bin_name) + .current_dir(cwd) + .args(["status", "--porcelain"]) + .output() + .await + .expect("Failed to execute command"); + + let output_str = String::from_utf8_lossy(&output.stdout); + !output_str.trim().is_empty() +} + #[cfg(test)] mod tests { use super::tar::PublishableTarball; |