[PATCH] D41147: git-clang-format: Add new --staged option.

Mark Lodato via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 12 17:41:09 PST 2017


lodato created this revision.
lodato added reviewers: djasper, klimek, Alexander-Shukaev.

This new mode, which requires --diff, operates very similarly to the two <commit> mode, except that the stage is formatted instead of the second commit.

The main intent of this feature is to use in pre-commit hooks so that you can ensure that all commits are properly formatted. Without this, the commit hook would have to format the working directory which is not necessarily what will be committed.

This mode requires --diff because otherwise there would be no place for the update to be written to: writing to the index would blow away changes, and writing to the working directory would be incorrect in case the working directory differs from the index.

Note: this patch was forked from https://reviews.llvm.org/D15465.

Testing of all three modes:

  # setup
  $ mkdir tmp
  $ cd tmp
  $ git init
  $ cat > foo.cc
  int main() {
    int x =  1;
    return 0;
  }
  EOF
  $ git add foo.cc
  $ git commit -m 'initial commit'
  $ rm foo.cc
  $ cat > foo.cc
  int main() {
    int x =  1;
    int y =  2;
    int z =  3;
    return 0;
  }
  EOF
  $ git add foo.cc
  $ git commit -m 'commit 2'
  $ 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;
   }




https://reviews.llvm.org/D41147

Files:
  google3/third_party/llvm/llvm/tools/clang/tools/clang-format/git-clang-format

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41147.126654.patch
Type: text/x-patch
Size: 3919 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171213/fb51852a/attachment.bin>


More information about the cfe-commits mailing list