Skip to content

Instantly share code, notes, and snippets.

@paboldin
Created October 13, 2016 07:10
Show Gist options
  • Save paboldin/4a59cfdb5fd9ff610ddb60ece6264f89 to your computer and use it in GitHub Desktop.
Save paboldin/4a59cfdb5fd9ff610ddb60ece6264f89 to your computer and use it in GitHub Desktop.
[DRAFT] Indent-checking `git commit` hook
#!/bin/sh
# git config hooks.indent "indent -linux -li0"
# NOTE: this code is horrible, you have been warned
indent_cmd="$(git config hooks.indent)"
if test -z "$indent_cmd"; then
exit 0
fi
set -e
exec 1>&2
tmpdir="$(mktemp -d)"
trap "rm -rf '$tmpdir'" 0
filenames="$@"
if test -z "$filenames"; then
filenames="$(git diff --cached --name-only --diff-filter M)"
fi
filenames="$(echo $filenames | grep '.[ch]$')"
if test -z "$filenames"; then
exit 0
fi
mkdir -p $tmpdir/orig $tmpdir/indent $tmpdir/newindent
cp --parents $filenames $tmpdir/newindent
cp --parents $filenames $tmpdir/orig
cp --parents $filenames $tmpdir/indent
# TODO write_original_files() {...
for filename in $filenames; do
git show HEAD:$filename > $tmpdir/orig/$filename
cp $tmpdir/orig/$filename $tmpdir/indent/$filename
done
${indent_cmd} $(echo $filenames | sed "s,^,$tmpdir/newindent/,g") \
$(echo $filenames | sed "s,^,$tmpdir/indent/,g")
failed=""
for filename in $filenames; do
diff -u0 $tmpdir/newindent/$filename $filename >$tmpdir/newindent/$filename.diff || :
diff -u0 $tmpdir/indent/$filename $tmpdir/orig/$filename >$tmpdir/indent/$filename.diff || :
sed -i '1,2d' $tmpdir/indent/$filename.diff $tmpdir/newindent/$filename.diff
diff -u0 $tmpdir/indent/$filename.diff $tmpdir/newindent/$filename.diff > $tmpdir/orig/$filename.diff || :
#echo "diff -u0 $tmpdir/indent/$filename.diff $tmpdir/newindent/$filename.diff"
awk '\
BEGIN { status = 0; }
/^+@@/ || /^-@@/ {
current = substr($0, 0, 1);
if (current == previous) {
split(prevline, arr);
print arr[2];
print code;
status = 1;
}
previous = current;
code = "";
prevline = $0;
}
/^@@/ {
previous = current = 0;
code = "";
}
!/^[+-]?@@/ && !/^(---|\+\+\+)/ {
prefix = substr($0, 1, 2);
if (prefix == "++" || prefix == "-0") {
if (code)
code = code"\n"substr($0, 3);
else
code = substr($0, 3);
}
}
END {
if (code) {
split(prevline, arr);
print arr[2];
print code;
status = 1;
}
exit status;
}' $tmpdir/orig/$filename.diff > $tmpdir/orig/$filename.log || {
failed="$failed $filename"
}
done
if test -n "$failed"; then
for fail in $failed; do
echo "FAILED indent check for $fail" >&2
cat $tmpdir/orig/$filename.log >&2
done
exit 1
fi
exit 0
@paboldin
Copy link
Author

Use "indent -linux -il0" instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment