git-format-staged 2.1.1 → 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 +27 -19
- package/git-format-staged +12 -9
- 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:
|
|
@@ -61,7 +59,7 @@ For detailed information run:
|
|
|
61
59
|
The command expects a shell command to run a formatter, and one or more file
|
|
62
60
|
patterns to identify which files should be formatted. For example:
|
|
63
61
|
|
|
64
|
-
$ git-format-staged --formatter 'prettier --stdin
|
|
62
|
+
$ git-format-staged --formatter 'prettier --stdin-filepath "{}"' 'src/*.js'
|
|
65
63
|
|
|
66
64
|
That will format all files under `src/` and its subdirectories using
|
|
67
65
|
`prettier`. The file pattern is tested against staged files using Python's
|
|
@@ -73,9 +71,16 @@ 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
|
-
$ git-format-staged --formatter 'prettier --stdin' '*.js' '!flow-typed/*'
|
|
83
|
+
$ git-format-staged --formatter 'prettier --stdin-filepath "{}"' '*.js' '!flow-typed/*'
|
|
79
84
|
|
|
80
85
|
Patterns are evaluated from left-to-right: if a file matches multiple patterns
|
|
81
86
|
the right-most pattern determines whether the file is included or excluded.
|
|
@@ -89,7 +94,7 @@ with the path of the file that is being formatted. This is useful if your
|
|
|
89
94
|
formatter needs to know the file extension to determine how to format or to
|
|
90
95
|
lint each file. For example:
|
|
91
96
|
|
|
92
|
-
$ git-format-staged -f 'prettier --stdin
|
|
97
|
+
$ git-format-staged -f 'prettier --stdin-filepath "{}"' '*.js' '*.css'
|
|
93
98
|
|
|
94
99
|
Do not attempt to read or write to `{}` in your formatter command! The
|
|
95
100
|
placeholder exists only for referencing the file name and path.
|
|
@@ -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
|
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
# ignoring unstaged changes.
|
|
8
8
|
#
|
|
9
9
|
# Usage: git-format-staged [OPTION]... [FILE]...
|
|
10
|
-
# Example: git-format-staged --formatter 'prettier --stdin' '*.js'
|
|
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)
|
|
@@ -48,6 +48,9 @@ def format_staged_files(file_patterns, formatter, git_root, update_working_tree=
|
|
|
48
48
|
for line in output.splitlines():
|
|
49
49
|
entry = parse_diff(line.decode('utf-8'))
|
|
50
50
|
entry_path = normalize_path(entry['src_path'], relative_to=git_root)
|
|
51
|
+
if entry['dst_mode'] == '120000':
|
|
52
|
+
# Do not process symlinks
|
|
53
|
+
continue
|
|
51
54
|
if not (matches_some_path(file_patterns, entry_path)):
|
|
52
55
|
continue
|
|
53
56
|
if format_file_in_index(formatter, entry, update_working_tree=update_working_tree, write=write):
|
|
@@ -142,7 +145,7 @@ def replace_file_in_index(diff_entry, new_object_hash):
|
|
|
142
145
|
|
|
143
146
|
def patch_working_file(path, orig_object_hash, new_object_hash):
|
|
144
147
|
patch = subprocess.check_output(
|
|
145
|
-
['git', 'diff', orig_object_hash, new_object_hash]
|
|
148
|
+
['git', 'diff', '--color=never', orig_object_hash, new_object_hash]
|
|
146
149
|
)
|
|
147
150
|
|
|
148
151
|
# Substitute object hashes in patch header with path to working tree file
|
|
@@ -228,12 +231,12 @@ class CustomArgumentParser(argparse.ArgumentParser):
|
|
|
228
231
|
if __name__ == '__main__':
|
|
229
232
|
parser = CustomArgumentParser(
|
|
230
233
|
description='Transform staged files using a formatting command that accepts content via stdin and produces a result via stdout.',
|
|
231
|
-
epilog='Example: %(prog)s --formatter "prettier --stdin" "src/*.js" "test/*.js"'
|
|
234
|
+
epilog='Example: %(prog)s --formatter "prettier --stdin-filepath \'{}\'" "src/*.js" "test/*.js"'
|
|
232
235
|
)
|
|
233
236
|
parser.add_argument(
|
|
234
237
|
'--formatter', '-f',
|
|
235
238
|
required=True,
|
|
236
|
-
help='Shell command to format files, will run once per file. Occurrences of the placeholder `{}` will be replaced with a path to the file being formatted. (Example: "prettier --stdin
|
|
239
|
+
help='Shell command to format files, will run once per file. Occurrences of the placeholder `{}` will be replaced with a path to the file being formatted. (Example: "prettier --stdin-filepath \'{}\'")'
|
|
237
240
|
)
|
|
238
241
|
parser.add_argument(
|
|
239
242
|
'--no-update-working-tree',
|
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",
|