aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_tools/src/bin/pre-commit.rs
blob: 95bb55cae98a7f5a489ccbaf3c66374fb3cf1b27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use std::process::Command;

use ra_tools::{Result, run_rustfmt, run, project_root, Overwrite};

fn main() -> Result<()> {
    run_rustfmt(Overwrite)?;
    update_staged()
}

fn update_staged() -> Result<()> {
    let root = project_root();
    let output = Command::new("git")
        .arg("diff")
        .arg("--diff-filter=MAR")
        .arg("--name-only")
        .arg("--cached")
        .current_dir(&root)
        .output()?;
    if !output.status.success() {
        Err(format!(
            "`git diff --diff-filter=MAR --name-only --cached` exited with {}",
            output.status
        ))?;
    }
    for line in String::from_utf8(output.stdout)?.lines() {
        run(&format!("git update-index --add {}", root.join(line).to_string_lossy()), ".")?;
    }
    Ok(())
}