Skip to content

Commit

Permalink
Partial sketch of check_mode "translation" to Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
EliahKagan committed Nov 29, 2024
1 parent ee33221 commit 967b9df
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tests/it/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ pub enum Subcommands {
#[clap(value_parser = AsPathSpec)]
patterns: Vec<gix::pathspec::Pattern>,
},
/// Check for executable bits that disagree with shebangs.
///
/// This checks and staged files, but not any unstaged files or changes, to find shell scripts
/// that either begin with a `#!` but not `+x` permissions, or do not begin with `#!` but do
/// have `+x` permissions. Such mismatches are reported but not automatically corrected. Some
/// plaforms (at least Windows) do not support such permissions, but Git still represents them.
///
/// This currently only checks files name with an `.sh` suffix, and only operates on the
/// current repository. Its main use is checking that fixture scripts are have correct modes.
#[clap(visible_alias = "cm")]
CheckMode {},
}

#[derive(Clone)]
Expand Down
50 changes: 50 additions & 0 deletions tests/it/src/commands/check_mode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
pub(super) mod function {
use anyhow::{bail, Context};
use gix::bstr::ByteSlice;
use std::ffi::OsString;
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};

pub fn check_mode() -> anyhow::Result<()> {
let root = find_root()?;
let mut mismatch = false;

let cmd = Command::new("git")
.arg("-C")
.arg(root)
.args(["ls-files", "-sz", "--", "*.sh"])
.stdout(Stdio::piped())
.spawn()
.context("Can't run `git` to list index")?;

let stdout = cmd.stdout.expect("should have captured stdout");
let reader = BufReader::new(stdout);
for record in reader.split(b'\0') {
// FIXME: Use the record, displaying messages and updating `mismatch`.
}

// FIXME: If `cmd` did not report successful completion, bail.
// FIXME: If `mismatch` (any mismatches), bail.
bail!("not yet implemented");
}

fn find_root() -> anyhow::Result<OsString> {
let output = Command::new("git")
.args(["rev-parse", "--show-toplevel"])
.output()
.context("Can't run `git` to find worktree root")?;

if !output.status.success() {
bail!("`git` failed to find worktree root");
}

let root = output
.stdout
.strip_suffix(b"\n")
.context("Can't parse worktree root")?
.to_os_str()?
.to_owned();

Ok(root)
}
}
3 changes: 3 additions & 0 deletions tests/it/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ pub use copy_royal::function::copy_royal;

pub mod git_to_sh;
pub use git_to_sh::function::git_to_sh;

pub mod check_mode;
pub use check_mode::function::check_mode;
1 change: 1 addition & 0 deletions tests/it/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn main() -> anyhow::Result<()> {
destination_dir,
patterns,
} => commands::copy_royal(dry_run, &worktree_root, destination_dir, patterns),
Subcommands::CheckMode {} => commands::check_mode(),
}
}

Expand Down

0 comments on commit 967b9df

Please sign in to comment.