git-format-staged 2.1.3 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -16
- package/git-format-staged +6 -6
- package/package.json +2 -5
package/README.md
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
[](https://travis-ci.org/hallettj/git-format-staged)
|
|
4
4
|
|
|
5
5
|
Consider a project where you want all code formatted consistently. So you use
|
|
6
|
-
a formatting command. (For example I use [prettier
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
a formatting command. (For example I use [prettier][] in my Javascript and
|
|
7
|
+
Typescript projects.) You want to make sure that everyone working on the project
|
|
8
|
+
runs the formatter, so you use a tool like [husky][] to install a git pre-commit
|
|
9
|
+
hook. The naive way to write that hook would be to:
|
|
10
10
|
|
|
11
11
|
- get a list of staged files
|
|
12
12
|
- run the formatter on those files
|
|
@@ -30,10 +30,9 @@ version of the file that is committed will be formatted properly - the warning
|
|
|
30
30
|
just means that working tree copy of the file has been left unformatted. The
|
|
31
31
|
patch step can be disabled with the `--no-update-working-tree` option.
|
|
32
32
|
|
|
33
|
-
[prettier
|
|
33
|
+
[prettier]: https://prettier.io/
|
|
34
34
|
[husky]: https://www.npmjs.com/package/husky
|
|
35
35
|
|
|
36
|
-
|
|
37
36
|
## How to install
|
|
38
37
|
|
|
39
38
|
Requires Python version 3 or 2.7.
|
|
@@ -51,7 +50,6 @@ If you do not use npm you can copy the
|
|
|
51
50
|
place it in your executable path. The script is MIT-licensed - so you can check
|
|
52
51
|
the script into version control in your own open source project if you wish.
|
|
53
52
|
|
|
54
|
-
|
|
55
53
|
## How to use
|
|
56
54
|
|
|
57
55
|
For detailed information run:
|
|
@@ -73,6 +71,13 @@ file names.
|
|
|
73
71
|
The formatter command must read file content from `stdin`, and output formatted
|
|
74
72
|
content to `stdout`.
|
|
75
73
|
|
|
74
|
+
Note that the syntax of the `fnmatch` glob match is a is a bit different from
|
|
75
|
+
normal shell globbing. So if you need to match multiple patterns, you should
|
|
76
|
+
pass multiple arguments with different patterns, and they will be grouped.
|
|
77
|
+
So instead of e.g. `'src/**/*.{js,jsx,ts}'`, you would use:
|
|
78
|
+
|
|
79
|
+
$ git-format-staged --formatter 'prettier --stdin-filepath "{}"' 'src/*.js' 'src/*.jsx' 'src/*.ts'
|
|
80
|
+
|
|
76
81
|
Files can be excluded by prefixing a pattern with `!`. For example:
|
|
77
82
|
|
|
78
83
|
$ git-format-staged --formatter 'prettier --stdin-filepath "{}"' '*.js' '!flow-typed/*'
|
|
@@ -116,23 +121,26 @@ notation) so that you can see them.
|
|
|
116
121
|
Follow these steps to automatically format all Javascript files on commit in
|
|
117
122
|
a project that uses npm.
|
|
118
123
|
|
|
119
|
-
Install git-format-staged, husky, and a formatter (I use prettier
|
|
124
|
+
Install git-format-staged, husky, and a formatter (I use `prettier`):
|
|
120
125
|
|
|
121
|
-
$ npm install --save-dev git-format-staged husky prettier
|
|
126
|
+
$ npm install --save-dev git-format-staged husky prettier
|
|
122
127
|
|
|
123
|
-
Add a `
|
|
128
|
+
Add a `prepare` script to install husky when running `npm install`:
|
|
124
129
|
|
|
125
|
-
"
|
|
126
|
-
|
|
127
|
-
}
|
|
130
|
+
$ npm set-script prepare "husky install"
|
|
131
|
+
$ npm run prepare
|
|
128
132
|
|
|
129
|
-
|
|
130
|
-
|
|
133
|
+
Add the pre-commit hook:
|
|
134
|
+
|
|
135
|
+
$ npx husky add .husky/pre-commit "git-format-staged --formatter 'prettier --stdin-filepath \"{}\"' '*.js' '*.ts'"
|
|
136
|
+
$ git add .husky/pre-commit
|
|
137
|
+
|
|
138
|
+
Once again note that the formatter command and the `'*.js'` and `'*.ts'`
|
|
139
|
+
patterns are quoted!
|
|
131
140
|
|
|
132
141
|
That's it! Whenever a file is changed as a result of formatting on commit you
|
|
133
142
|
will see a message in the output from `git commit`.
|
|
134
143
|
|
|
135
|
-
|
|
136
144
|
## Comparisons to similar utilities
|
|
137
145
|
|
|
138
146
|
There are other tools that will format or lint staged files. What distinguishes
|
package/git-format-staged
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#!/usr/bin/env
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
2
|
#
|
|
3
3
|
# Git command to transform staged files according to a command that accepts file
|
|
4
4
|
# content on stdin and produces output on stdout. This command is useful in
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
# Usage: git-format-staged [OPTION]... [FILE]...
|
|
10
10
|
# Example: git-format-staged --formatter 'prettier --stdin-filepath "{}"' '*.js'
|
|
11
11
|
#
|
|
12
|
-
# Tested with Python 3.
|
|
12
|
+
# Tested with Python 3.10 and Python 2.7.
|
|
13
13
|
#
|
|
14
14
|
# Original author: Jesse Hallett <jesse@sitr.us>
|
|
15
15
|
|
|
@@ -22,12 +22,12 @@ import re
|
|
|
22
22
|
import subprocess
|
|
23
23
|
import sys
|
|
24
24
|
|
|
25
|
-
# The string
|
|
26
|
-
VERSION = '
|
|
25
|
+
# The string 3.0.0 is replaced during the publish process.
|
|
26
|
+
VERSION = '3.0.0'
|
|
27
27
|
PROG = sys.argv[0]
|
|
28
28
|
|
|
29
29
|
def info(msg):
|
|
30
|
-
print(msg, file=sys.
|
|
30
|
+
print(msg, file=sys.stdout)
|
|
31
31
|
|
|
32
32
|
def warn(msg):
|
|
33
33
|
print('{}: warning: {}'.format(PROG, msg), file=sys.stderr)
|
|
@@ -145,7 +145,7 @@ def replace_file_in_index(diff_entry, new_object_hash):
|
|
|
145
145
|
|
|
146
146
|
def patch_working_file(path, orig_object_hash, new_object_hash):
|
|
147
147
|
patch = subprocess.check_output(
|
|
148
|
-
['git', 'diff', orig_object_hash, new_object_hash]
|
|
148
|
+
['git', 'diff', '--color=never', orig_object_hash, new_object_hash]
|
|
149
149
|
)
|
|
150
150
|
|
|
151
151
|
# Substitute object hashes in patch header with path to working tree file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "git-format-staged",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Git command to transform staged files according to a command that accepts file content on stdin and produces output on stdout.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "ava",
|
|
@@ -28,9 +28,6 @@
|
|
|
28
28
|
"files": [
|
|
29
29
|
"git-format-staged"
|
|
30
30
|
],
|
|
31
|
-
"release": {
|
|
32
|
-
"branches": "master"
|
|
33
|
-
},
|
|
34
31
|
"devDependencies": {
|
|
35
32
|
"@commitlint/cli": "^8.3.5",
|
|
36
33
|
"@commitlint/config-conventional": "^8.3.4",
|
|
@@ -42,7 +39,7 @@
|
|
|
42
39
|
"husky": "^4.2.5",
|
|
43
40
|
"micromatch": "^4.0.2",
|
|
44
41
|
"prettier-standard": "^9.1.1",
|
|
45
|
-
"semantic-release": "^
|
|
42
|
+
"semantic-release": "^19.0.2",
|
|
46
43
|
"strip-indent": "^3.0.0",
|
|
47
44
|
"tmp": "0.2.0",
|
|
48
45
|
"ts-node": "^8.9.1",
|