From 15bcaafa854b87346bf22bfae1e2461db5ad1e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Mon, 7 Sep 2026 14:13:56 +0000 Subject: [PATCH] feat: add exit-code to link check --- src/cli/link.rs | 4 ++++ src/main.rs | 3 ++- src/scripts/link.rs | 10 +++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/cli/link.rs b/src/cli/link.rs index d777a08..58d29de 100644 --- a/src/cli/link.rs +++ b/src/cli/link.rs @@ -24,6 +24,10 @@ pub enum Link { #[arg(long)] porcelain: bool, + /// Exit with 1 when anything is outside the store, for scripts + #[arg(long)] + exit_code: bool, + /// Terminate porcelain entries with NUL #[arg(short = 'z')] null: bool, diff --git a/src/main.rs b/src/main.rs index db7d96f..3a020eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,8 @@ fn main() -> Result<()> { paths, porcelain, null, - } => scripts::link::check(&paths, porcelain, null), + exit_code, + } => scripts::link::check(&paths, porcelain, null, exit_code), }, cli::Commands::Completions { shell } => scripts::completions::completions(shell), } diff --git a/src/scripts/link.rs b/src/scripts/link.rs index 92f2d79..83e2619 100644 --- a/src/scripts/link.rs +++ b/src/scripts/link.rs @@ -2,8 +2,10 @@ use std::cell::Cell; use std::env; use std::ffi::OsString; use std::fs; +use std::io::{self, Write}; use std::os::unix::fs::symlink; use std::path::{Path, PathBuf}; +use std::process; use std::process::Command; use anyhow::{Context, Result, anyhow}; @@ -62,7 +64,7 @@ fn warn(msg: impl std::fmt::Display) { } // list untracked paths not in the store, i.e. what a sandbox can still read -pub fn check(paths: &[PathBuf], porcelain: bool, null: bool) -> Result<()> { +pub fn check(paths: &[PathBuf], porcelain: bool, null: bool, exit_code: bool) -> Result<()> { let repo = Repo::discover()?; let pathspecs = relative_pathspecs(&repo, paths)?; @@ -89,6 +91,12 @@ pub fn check(paths: &[PathBuf], porcelain: bool, null: bool) -> Result<()> { print_listing(&repo, &exposed); } + // git's --exit-code convention: nothing to report is 0, anything is 1 + if exit_code && !exposed.is_empty() { + io::stdout().flush().context("writing the listing")?; + process::exit(1); + } + Ok(()) }