Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only check staged content in pre-hooks #916

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions xtask/src/install_git_hooks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{path::PathBuf, process::Command};

use crate::util::project_dir;
use clap::Args;
use cross::shell::MessageInfo;
use cross::{shell::MessageInfo, CommandExt};

#[derive(Args, Debug)]
pub struct InstallGitHooks {
Expand All @@ -17,7 +19,11 @@ pub struct InstallGitHooks {

pub fn install_git_hooks(msg_info: &mut MessageInfo) -> cross::Result<()> {
let root = project_dir(msg_info)?;
let git_hooks = root.join(".git").join("hooks");
let git_hooks = Command::new("git")
.args(&["rev-parse", "--git-common-dir"])
.run_and_get_stdout(msg_info)
.map(|s| PathBuf::from(&s.trim()))?
.join("hooks");
let cross_dev = root.join("xtask").join("src");

std::fs::copy(
Expand Down
14 changes: 14 additions & 0 deletions xtask/src/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
#!/usr/bin/env bash
set -ex

tf=$(mktemp -t stash.XXX.$$)

cleanup() {
git apply --allow-empty --whitespace=nowarn < "$tf" && git stash drop -q
rm "$tf"
}

git diff --full-index --binary > "$tf"
git stash -q --keep-index

trap cleanup EXIT
Copy link
Contributor

@Alexhuszagh Alexhuszagh Jul 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work on SIGKILL or SIGSTOP, which would lead to data lost. It also doesn't work for new files, which would mean they would be tests but not staged. There probably needs to be some means of recover (consistent file naming, a script to undo the last action in the Git history, something).

Copy link
Contributor

@Alexhuszagh Alexhuszagh Jul 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simple example is:

$ echo "Sample contents" > file
$ git diff --full-index --binary # no output
$ git stash -q --keep-index      # doesn't stash anything


cargo xtask check --verbose
14 changes: 14 additions & 0 deletions xtask/src/pre-push.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
#!/usr/bin/env bash
set -ex

tf=$(mktemp -t stash.XXX.$$)

cleanup() {
git apply --allow-empty --whitespace=nowarn < "$tf" && git stash drop -q
rm "$tf"
}

git diff --full-index --binary > "$tf"
git stash -q --keep-index

trap cleanup EXIT

cargo xtask test --verbose