When running git-clang-format in a pre-commit hook it's very useful to be able to tell git-clang-format to only look at the --staged/--cached files and not the working directory.
Note this patch is a rebase/fork from D41147: git-clang-format: Add new --staged option. which is a fork of D15465: [git-clang-format]: New option to perform formatting against staged changes only
This is tested with:
mkdir /tmp/gcfs cd /tmp/gcfs git init cat <<EOF > foo.cc int main() { int x = 1; return 0; } EOF git add foo.cc git commit -m 'first commit' rm foo.cc cat <<EOF > foo.cc int main() { int x = 1; int y = 2; int z = 3; return 0; } EOF git add foo.cc git commit -m 'second commit' sed -i -e 's/1;/10;/' foo.cc git add foo.cc sed -i -e 's/10;/1;/' foo.cc sed -i -e 's/3;/30;/' foo.cc $ git-clang-format --diff diff --git a/foo.cc b/foo.cc index cb2dbc9..2e1b0e1 100644 --- a/foo.cc +++ b/foo.cc @@ -1,6 +1,6 @@ int main() { int x = 1; int y = 2; - int z = 30; + int z = 30; return 0; } $ git-clang-format --diff --staged diff --git a/foo.cc b/foo.cc index 3ea8c6c..7da0033 100644 --- a/foo.cc +++ b/foo.cc @@ -1,5 +1,5 @@ int main() { - int x = 10; + int x = 10; int y = 2; int z = 3; return 0; $ git-clang-format --diff HEAD~ HEAD diff --git a/foo.cc b/foo.cc index 7bfdb83..ce6f65e 100644 --- a/foo.cc +++ b/foo.cc @@ -1,6 +1,6 @@ int main() { int x = 1; - int y = 2; - int z = 3; + int y = 2; + int z = 3; return 0; }
Does there need to be an equivalent check that --staged requires --diff? Could you test to make sure that works as expected?