From c2c4e745a5db4f2e53aa70bf22b6c828fa1b4040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 28 Feb 2024 09:21:12 +0000 Subject: fix(publish): error if there are uncommitted changes (#22613) Closes https://github.com/denoland/deno/issues/22330 --- cli/tools/registry/mod.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'cli/tools') diff --git a/cli/tools/registry/mod.rs b/cli/tools/registry/mod.rs index e01c1563b..c14b10b8d 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; @@ -937,6 +940,12 @@ pub async fn publish( return Ok(()); } + if !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, @@ -1016,6 +1025,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; -- cgit v1.2.3