git_stage_formatter 2.2.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.releaserc.yaml +3 -0
- data/.travis.yml +3 -2
- data/CHANGELOG.md +7 -0
- data/README.md +47 -22
- data/Rakefile +4 -0
- data/git-format-staged +10 -7
- data/git_stage_formatter.gemspec +29 -0
- data/lib/git_stage_formatter/version.rb +1 -1
- data/package-lock.json +16194 -6185
- data/package.json +1 -4
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eef46e1e61e9298617a81667ae1d75dda336b9abae96da8d3f31f504e7a369e7
|
4
|
+
data.tar.gz: 315e59902076c09d46b87c57ddd580819860b4e5919085c0d232dd59ca7cf0ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 38b0689f0aa55e86523f8081ff222b48933613df2df159c6ac9a6bd7c27d865d438e7bc2d6e7e899299b0dcf15492c46fd8b5925164717978a7a4323d9f3ac95
|
7
|
+
data.tar.gz: cc11ea3ed725beaa56c164f882d19ac589dc0f4e6f3dfc3ba401a32af6d3600ef627be690611900bee82e1ba032bda49607e6686893a70039225ead123dfe139
|
data/.gitignore
CHANGED
data/.releaserc.yaml
ADDED
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -3,10 +3,10 @@
|
|
3
3
|
[![Build Status](https://travis-ci.org/hallettj/git-format-staged.svg?branch=master)](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,14 +30,15 @@ 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.
|
40
39
|
|
40
|
+
### npm
|
41
|
+
|
41
42
|
Install as a development dependency in a project that uses npm packages:
|
42
43
|
|
43
44
|
$ npm install --save-dev git-format-staged
|
@@ -46,11 +47,25 @@ Or install globally:
|
|
46
47
|
|
47
48
|
$ npm install --global git-format-staged
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
50
|
+
### bundler
|
51
|
+
|
52
|
+
Add this gem to your Gemfile:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
gem 'git_stage_formatter'
|
56
|
+
```
|
57
|
+
|
58
|
+
### gem
|
59
|
+
|
60
|
+
Install the binary via gem:
|
61
|
+
|
62
|
+
```sh
|
63
|
+
gem install git_stage_formatter
|
64
|
+
```
|
53
65
|
|
66
|
+
### Manually
|
67
|
+
|
68
|
+
If you don't use `npm`, `bundler` or `gem`, you can copy the [`git-format-staged`](./git-format-staged) script from this repository and place it in your executable path, or somewhere convenient. The script is MIT-licensed - so you can check the script into version control in your own open source project if you wish.
|
54
69
|
|
55
70
|
## How to use
|
56
71
|
|
@@ -61,7 +76,7 @@ For detailed information run:
|
|
61
76
|
The command expects a shell command to run a formatter, and one or more file
|
62
77
|
patterns to identify which files should be formatted. For example:
|
63
78
|
|
64
|
-
$ git-format-staged --formatter 'prettier --stdin
|
79
|
+
$ git-format-staged --formatter 'prettier --stdin-filepath "{}"' 'src/*.js'
|
65
80
|
|
66
81
|
That will format all files under `src/` and its subdirectories using
|
67
82
|
`prettier`. The file pattern is tested against staged files using Python's
|
@@ -73,9 +88,16 @@ file names.
|
|
73
88
|
The formatter command must read file content from `stdin`, and output formatted
|
74
89
|
content to `stdout`.
|
75
90
|
|
91
|
+
Note that the syntax of the `fnmatch` glob match is a is a bit different from
|
92
|
+
normal shell globbing. So if you need to match multiple patterns, you should
|
93
|
+
pass multiple arguments with different patterns, and they will be grouped.
|
94
|
+
So instead of e.g. `'src/**/*.{js,jsx,ts}'`, you would use:
|
95
|
+
|
96
|
+
$ git-format-staged --formatter 'prettier --stdin-filepath "{}"' 'src/*.js' 'src/*.jsx' 'src/*.ts'
|
97
|
+
|
76
98
|
Files can be excluded by prefixing a pattern with `!`. For example:
|
77
99
|
|
78
|
-
$ git-format-staged --formatter 'prettier --stdin' '*.js' '!flow-typed/*'
|
100
|
+
$ git-format-staged --formatter 'prettier --stdin-filepath "{}"' '*.js' '!flow-typed/*'
|
79
101
|
|
80
102
|
Patterns are evaluated from left-to-right: if a file matches multiple patterns
|
81
103
|
the right-most pattern determines whether the file is included or excluded.
|
@@ -89,7 +111,7 @@ with the path of the file that is being formatted. This is useful if your
|
|
89
111
|
formatter needs to know the file extension to determine how to format or to
|
90
112
|
lint each file. For example:
|
91
113
|
|
92
|
-
$ git-format-staged -f 'prettier --stdin
|
114
|
+
$ git-format-staged -f 'prettier --stdin-filepath "{}"' '*.js' '*.css'
|
93
115
|
|
94
116
|
Do not attempt to read or write to `{}` in your formatter command! The
|
95
117
|
placeholder exists only for referencing the file name and path.
|
@@ -116,23 +138,26 @@ notation) so that you can see them.
|
|
116
138
|
Follow these steps to automatically format all Javascript files on commit in
|
117
139
|
a project that uses npm.
|
118
140
|
|
119
|
-
Install git-format-staged, husky, and a formatter (I use prettier
|
141
|
+
Install git-format-staged, husky, and a formatter (I use `prettier`):
|
120
142
|
|
121
|
-
$ npm install --save-dev git-format-staged husky prettier
|
143
|
+
$ npm install --save-dev git-format-staged husky prettier
|
122
144
|
|
123
|
-
Add a `
|
145
|
+
Add a `prepare` script to install husky when running `npm install`:
|
124
146
|
|
125
|
-
"
|
126
|
-
|
127
|
-
}
|
147
|
+
$ npm set-script prepare "husky install"
|
148
|
+
$ npm run prepare
|
128
149
|
|
129
|
-
|
130
|
-
|
150
|
+
Add the pre-commit hook:
|
151
|
+
|
152
|
+
$ npx husky add .husky/pre-commit "git-format-staged --formatter 'prettier --stdin-filepath \"{}\"' '*.js' '*.ts'"
|
153
|
+
$ git add .husky/pre-commit
|
154
|
+
|
155
|
+
Once again note that the formatter command and the `'*.js'` and `'*.ts'`
|
156
|
+
patterns are quoted!
|
131
157
|
|
132
158
|
That's it! Whenever a file is changed as a result of formatting on commit you
|
133
159
|
will see a message in the output from `git commit`.
|
134
160
|
|
135
|
-
|
136
161
|
## Comparisons to similar utilities
|
137
162
|
|
138
163
|
There are other tools that will format or lint staged files. What distinguishes
|
data/Rakefile
ADDED
data/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
|
|
@@ -27,7 +27,7 @@ VERSION = '$VERSION'
|
|
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',
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/git_stage_formatter/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "git_stage_formatter"
|
7
|
+
spec.version = GitStageFormatter::VERSION
|
8
|
+
spec.authors = ["Roger Oba"]
|
9
|
+
spec.email = ["rogerluan.oba@gmail.com"]
|
10
|
+
spec.summary = "Script to transform staged files using a formatting command"
|
11
|
+
spec.homepage = "https://github.com/rogerluan/git_stage_formatter"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
spec.metadata["source_code_uri"] = "https://github.com/rogerluan/git_stage_formatter"
|
16
|
+
spec.metadata["changelog_uri"] = "https://github.com/rogerluan/git_stage_formatter/blob/master/CHANGELOG.md"
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "bin"
|
24
|
+
spec.executables = ['git_stage_formatter']
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_development_dependency 'bundler', '>= 2.0.0', '< 3.0.0'
|
28
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
29
|
+
end
|