Distinguish "git clang-format" from "clang-format"
dblume

dblume commited on 2024-12-22 12:29:20
Showing 1 changed files, with 15 additions and 8 deletions.


Mention that "git clang-format" only formats your staged changes, but it's
often more useful to just clang-format the whole staged files.

This is especially so when there's a merge pipeline that'll run
clang-format on the whole file itself.
... ...
@@ -76,24 +76,31 @@ And move $HOME/.local/bin ahead of /usr/bin in your $PATH.
76 76
 Then, after making your edits, use the [vim integration](https://clang.llvm.org/docs/ClangFormat.html#vim-integration)
77 77
 or manually format in one of a couple of ways:
78 78
 
79
-### Use git-clang-format
79
+### Option 1: Use git clang-format to format your staged changes
80
+
81
+**NOTE** git-clang-format only formats your changes. Often it's better to
82
+always format the whole document.
80 83
 
81 84
 `git-clang-format` is installed with clang-format and will format staged files
82
-into the unstaged area.
85
+into the unstaged area. Use it like this:
83 86
 
84 87
 1. Stage the files you want to format.
85
-2. Run `git-clang-format --style=file`, and it'll leave unstaged formatting changes.
88
+2. Run `git clang-format`, and it'll leave unstaged formatting changes.
86 89
 3. Review with `git difftool` and accept the desired changes or unstage.
87 90
 
88
-### Manually clang-format
91
+### Option 2: Manually clang-format the staged files
92
+
93
+The following formats the staged files, not only your staged changes:
89 94
 
90 95
     git status --porcelain | \
91
-    awk 'match($1, "M|A") && match($2, "(c|h)(pp)?$"){print $2}' | \
92
-    xargs clang-format -style=file -i
96
+    awk '/^[MTARC].+\.([hc](pp)?)"?$/ {$1=""; $0=$0; $1=$1; print}' | \
97
+    xargs clang-format -i
93 98
 
94
-Or, shorter but less comprehensive:
99
+Or a similar approach but it'll also overwrite your unstaged changes too:
95 100
 
96
-    git ls-files -m | egrep ".(c|h)(pp)?$" | xargs clang-format -style=file -i
101
+    git ls-files -m | \
102
+    grep -E ".[ch](pp)?$" | tr "\n" "\0" | \
103
+    xargs -0 clang-format -i
97 104
 
98 105
 ## Current Features
99 106
 
100 107