Skip to content

Commit 589c84b

Browse files
committedAug 23, 2023
README
1 parent 1af724a commit 589c84b

File tree

2 files changed

+228
-0
lines changed

2 files changed

+228
-0
lines changed
 

Diff for: ‎README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
`git fmt-diff`
2+
==============
3+
4+
Show how lines you changed would look formatted
5+
6+
## Dependencies
7+
8+
* POSIX compatible shell (e.g. Bash)
9+
* [Git](https://git-scm.com/)
10+
* [Vim](https://www.vim.org/) _(optional)_ - for config-less filetype detection
11+
* a formatter program of your choice
12+
13+
## Usage
14+
15+
See: [example usage scenario](scenario.md)
16+
17+
```
18+
usage: git fmt-diff [<options>] [<commit>] [--] [<path>...]
19+
or: git fmt-diff [<options>] --cached [<commit>] [--] [<path>...]
20+
or: git fmt-diff [<options>] <commit>...<commit> [--] [<path>...]
21+
or: git fmt-diff [<options>] <blob> <blob>
22+
23+
options:
24+
-h display this help and exit
25+
--cached view the changes you staged for the next commit relative to
26+
the named <commit> (which, if not provided, defaults to HEAD)
27+
--staged a synonym of --cached
28+
--color always show colors
29+
--no-color turn off colored diff
30+
--config <file> read config from specified file rather than $GIT_DIR/.git-fmt-diff
31+
```
32+
33+
## Installation
34+
35+
1. Download [git-fmt-diff](https://raw.githubusercontent.com/Jorengarenar/git-fmt-diff/master/git-fmt-diff) file
36+
2. Set it as executable: `$ chmod +x git-fmt-diff`
37+
3. Put it into any direcory listed in `$PATH` variable
38+
39+
## Configuration
40+
41+
TODO

Diff for: ‎scenario.md

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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

Comments
 (0)
Please sign in to comment.