|
| 1 | +### Example |
| 2 | + |
| 3 | +Let's say we have the following, clearly badly formatted C code: |
| 4 | +```c |
| 5 | +#include <stdio.h> |
| 6 | + |
| 7 | +int |
| 8 | +foo() { |
| 9 | + int x; |
| 10 | + x = printf("Hello World!\n"); |
| 11 | + |
| 12 | + if( x == 1) { |
| 13 | + return -1; |
| 14 | + } |
| 15 | + |
| 16 | + return 5; |
| 17 | +} |
| 18 | + |
| 19 | +/* TODO: implement bar() */ |
| 20 | +int bar() { return 8; |
| 21 | +} |
| 22 | + |
| 23 | +int main() |
| 24 | +{ int r; |
| 25 | + int f = foo(); |
| 26 | + |
| 27 | +int b = bar(); |
| 28 | + |
| 29 | + r = f + b; |
| 30 | + |
| 31 | + return(0 ); } |
| 32 | +``` |
| 33 | +
|
| 34 | +After 30 years you are at least to resolve the TODO; |
| 35 | +but contrary to your predecessors you want to adhere to some sane coding style. |
| 36 | +
|
| 37 | +So you edit the file as follows: |
| 38 | +```c |
| 39 | +#include <stdio.h> |
| 40 | +#include <stdlib.h> |
| 41 | +
|
| 42 | +int |
| 43 | +foo() { |
| 44 | + int x; |
| 45 | + x = printf("Hello World!\n"); |
| 46 | +
|
| 47 | + if( x == 1) { |
| 48 | + return -1; |
| 49 | + } |
| 50 | +
|
| 51 | + return 5; |
| 52 | +} |
| 53 | +
|
| 54 | +int bar(int n) |
| 55 | +{ |
| 56 | + char* x = malloc(n * (sizeof *x)); |
| 57 | + int y =sprintf(x, "Bar is now open!"); |
| 58 | + free(x); |
| 59 | + return y; } |
| 60 | +
|
| 61 | +int main() |
| 62 | +{ int r; |
| 63 | + int f = foo(); |
| 64 | +
|
| 65 | +int b = bar(8); |
| 66 | +
|
| 67 | + r = f + b; |
| 68 | +
|
| 69 | + return(0 ); } |
| 70 | +``` |
| 71 | + |
| 72 | +Let's see how a `$ git diff` looks like: |
| 73 | +```diff |
| 74 | +diff --git a/hello.c b/hello.c |
| 75 | +index c83a8c1..74337f8 100644 |
| 76 | +--- a/hello.c |
| 77 | ++++ b/hello.c |
| 78 | +@@ -1,4 +1,5 @@ |
| 79 | + #include <stdio.h> |
| 80 | ++#include <stdlib.h> |
| 81 | + |
| 82 | + int |
| 83 | + foo() { |
| 84 | +@@ -12,15 +13,18 @@ foo() { |
| 85 | + return 5; |
| 86 | + } |
| 87 | + |
| 88 | +-/* TODO: implement bar() */ |
| 89 | +-int bar() { return 8; |
| 90 | +-} |
| 91 | ++int bar(int n) |
| 92 | ++{ |
| 93 | ++ char* x = malloc(n * (sizeof *x)); |
| 94 | ++ int y =sprintf(x, "Bar is now open!"); |
| 95 | ++ free(x); |
| 96 | ++ return y; } |
| 97 | + |
| 98 | + int main() |
| 99 | + { int r; |
| 100 | + int f = foo(); |
| 101 | + |
| 102 | +-int b = bar(); |
| 103 | ++int b = bar(8); |
| 104 | + |
| 105 | + r = f + b; |
| 106 | + |
| 107 | +``` |
| 108 | + |
| 109 | +Nice, it doesn't look half as bad as other functions. |
| 110 | +But perhaps made some stylistic mistakes found their way into your code anyway |
| 111 | +and you'd want to check it. |
| 112 | +That's where `$ git fmt-diff` enters the stage: |
| 113 | +```diff |
| 114 | +diff --git a/hello.c b/hello.c |
| 115 | +index 74337f8..7a6d3fd 100644 |
| 116 | +--- a/hello.c |
| 117 | ++++ b/hello.c |
| 118 | +@@ -15,16 +15,17 @@ foo() { |
| 119 | + |
| 120 | + int bar(int n) |
| 121 | + { |
| 122 | +- char* x = malloc(n * (sizeof *x)); |
| 123 | +- int y =sprintf(x, "Bar is now open!"); |
| 124 | ++ char *x = malloc(n * (sizeof *x)); |
| 125 | ++ int y = sprintf(x, "Bar is now open!"); |
| 126 | + free(x); |
| 127 | +- return y; } |
| 128 | ++ return y; |
| 129 | ++} |
| 130 | + |
| 131 | + int main() |
| 132 | + { int r; |
| 133 | + int f = foo(); |
| 134 | + |
| 135 | +-int b = bar(8); |
| 136 | ++ int b = bar(8); |
| 137 | + |
| 138 | + r = f + b; |
| 139 | + |
| 140 | +``` |
| 141 | + |
| 142 | +As you can see, only added or modified lines got suggestions for reformatting. |
| 143 | +On top of that, even added lines which were already conforming to style didn't |
| 144 | +show up here either. Now you can decide whether to apply suggestions or commit |
| 145 | +the file in its current state. |
| 146 | + |
| 147 | +If you reviewed suggestions and think all of them are an improvement, |
| 148 | +you can just rerun the command piping to `git apply`: |
| 149 | +```sh |
| 150 | +$ git fmt-diff | git apply |
| 151 | +``` |
| 152 | + |
| 153 | +Your changes will be formatted and ready to merge: |
| 154 | +```c |
| 155 | +#include <stdio.h> |
| 156 | +#include <stdlib.h> |
| 157 | + |
| 158 | +int |
| 159 | +foo() { |
| 160 | + int x; |
| 161 | + x = printf("Hello World!\n"); |
| 162 | + |
| 163 | + if( x == 1) { |
| 164 | + return -1; |
| 165 | + } |
| 166 | + |
| 167 | + return 5; |
| 168 | +} |
| 169 | + |
| 170 | +int bar(int n) |
| 171 | +{ |
| 172 | + char* x = malloc(n * (sizeof *x)); |
| 173 | + int y = sprintf(x, "Bar is now open!"); |
| 174 | + free(x); |
| 175 | + return y; |
| 176 | +} |
| 177 | + |
| 178 | +int main() |
| 179 | +{ int r; |
| 180 | + int f = foo(); |
| 181 | + |
| 182 | + int b = bar(8); |
| 183 | + |
| 184 | + r = f + b; |
| 185 | + |
| 186 | + return(0 ); } |
| 187 | +``` |
0 commit comments