git-format-staged 3.0.0 → 3.1.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 +15 -1
- package/git-format-staged +19 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -35,6 +35,16 @@ patch step can be disabled with the `--no-update-working-tree` option.
|
|
|
35
35
|
|
|
36
36
|
## How to install
|
|
37
37
|
|
|
38
|
+
### Install with Nix
|
|
39
|
+
|
|
40
|
+
Install via the CLI:
|
|
41
|
+
|
|
42
|
+
$ nix profile add github:hallettj/git-format-staged
|
|
43
|
+
|
|
44
|
+
Or add to your flake imports, and use the `default` package output.
|
|
45
|
+
|
|
46
|
+
### Install with NPM
|
|
47
|
+
|
|
38
48
|
Requires Python version 3 or 2.7.
|
|
39
49
|
|
|
40
50
|
Install as a development dependency in a project that uses npm packages:
|
|
@@ -45,7 +55,11 @@ Or install globally:
|
|
|
45
55
|
|
|
46
56
|
$ npm install --global git-format-staged
|
|
47
57
|
|
|
48
|
-
|
|
58
|
+
### Or just copy the script
|
|
59
|
+
|
|
60
|
+
Requires Python version 3 or 2.7.
|
|
61
|
+
|
|
62
|
+
If you do not use the above methods you can copy the
|
|
49
63
|
[`git-format-staged`](./git-format-staged) script from this repository and
|
|
50
64
|
place it in your executable path. The script is MIT-licensed - so you can check
|
|
51
65
|
the script into version control in your own open source project if you wish.
|
package/git-format-staged
CHANGED
|
@@ -22,8 +22,8 @@ import re
|
|
|
22
22
|
import subprocess
|
|
23
23
|
import sys
|
|
24
24
|
|
|
25
|
-
# The string 3.
|
|
26
|
-
VERSION = '3.
|
|
25
|
+
# The string 3.1.0 is replaced during the publish process.
|
|
26
|
+
VERSION = '3.1.0'
|
|
27
27
|
PROG = sys.argv[0]
|
|
28
28
|
|
|
29
29
|
def info(msg):
|
|
@@ -36,7 +36,7 @@ def fatal(msg):
|
|
|
36
36
|
print('{}: error: {}'.format(PROG, msg), file=sys.stderr)
|
|
37
37
|
exit(1)
|
|
38
38
|
|
|
39
|
-
def format_staged_files(file_patterns, formatter, git_root, update_working_tree=True, write=True):
|
|
39
|
+
def format_staged_files(file_patterns, formatter, git_root, update_working_tree=True, write=True, verbose=False):
|
|
40
40
|
try:
|
|
41
41
|
output = subprocess.check_output([
|
|
42
42
|
'git', 'diff-index',
|
|
@@ -53,7 +53,7 @@ def format_staged_files(file_patterns, formatter, git_root, update_working_tree=
|
|
|
53
53
|
continue
|
|
54
54
|
if not (matches_some_path(file_patterns, entry_path)):
|
|
55
55
|
continue
|
|
56
|
-
if format_file_in_index(formatter, entry, update_working_tree=update_working_tree, write=write):
|
|
56
|
+
if format_file_in_index(formatter, entry, update_working_tree=update_working_tree, write=write, verbose=verbose):
|
|
57
57
|
info('Reformatted {} with {}'.format(entry['src_path'], formatter))
|
|
58
58
|
except Exception as err:
|
|
59
59
|
fatal(str(err))
|
|
@@ -61,9 +61,9 @@ def format_staged_files(file_patterns, formatter, git_root, update_working_tree=
|
|
|
61
61
|
# Run formatter on file in the git index. Creates a new git object with the
|
|
62
62
|
# result, and replaces the content of the file in the index with that object.
|
|
63
63
|
# Returns hash of the new object if formatting produced any changes.
|
|
64
|
-
def format_file_in_index(formatter, diff_entry, update_working_tree=True, write=True):
|
|
64
|
+
def format_file_in_index(formatter, diff_entry, update_working_tree=True, write=True, verbose=False):
|
|
65
65
|
orig_hash = diff_entry['dst_hash']
|
|
66
|
-
new_hash = format_object(formatter, orig_hash, diff_entry['src_path'])
|
|
66
|
+
new_hash = format_object(formatter, orig_hash, diff_entry['src_path'], verbose=verbose)
|
|
67
67
|
|
|
68
68
|
# If the new hash is the same then the formatter did not make any changes.
|
|
69
69
|
if not write or new_hash == orig_hash:
|
|
@@ -90,13 +90,16 @@ file_path_placeholder = re.compile('\{\}')
|
|
|
90
90
|
|
|
91
91
|
# Run formatter on a git blob identified by its hash. Writes output to a new git
|
|
92
92
|
# blob, and returns the hash of the new blob.
|
|
93
|
-
def format_object(formatter, object_hash, file_path):
|
|
93
|
+
def format_object(formatter, object_hash, file_path, verbose=False):
|
|
94
94
|
get_content = subprocess.Popen(
|
|
95
95
|
['git', 'cat-file', '-p', object_hash],
|
|
96
96
|
stdout=subprocess.PIPE
|
|
97
97
|
)
|
|
98
|
+
command = re.sub(file_path_placeholder, file_path, formatter)
|
|
99
|
+
if verbose:
|
|
100
|
+
info(command)
|
|
98
101
|
format_content = subprocess.Popen(
|
|
99
|
-
|
|
102
|
+
command,
|
|
100
103
|
shell=True,
|
|
101
104
|
stdin=get_content.stdout,
|
|
102
105
|
stdout=subprocess.PIPE
|
|
@@ -145,7 +148,7 @@ def replace_file_in_index(diff_entry, new_object_hash):
|
|
|
145
148
|
|
|
146
149
|
def patch_working_file(path, orig_object_hash, new_object_hash):
|
|
147
150
|
patch = subprocess.check_output(
|
|
148
|
-
['git', 'diff', '--color=never', orig_object_hash, new_object_hash]
|
|
151
|
+
['git', 'diff', '--no-ext-diff', '--color=never', orig_object_hash, new_object_hash]
|
|
149
152
|
)
|
|
150
153
|
|
|
151
154
|
# Substitute object hashes in patch header with path to working tree file
|
|
@@ -254,6 +257,11 @@ if __name__ == '__main__':
|
|
|
254
257
|
version='%(prog)s version {}'.format(VERSION),
|
|
255
258
|
help='Display version of %(prog)s'
|
|
256
259
|
)
|
|
260
|
+
parser.add_argument(
|
|
261
|
+
'--verbose',
|
|
262
|
+
help='Show the formatting commands that are running',
|
|
263
|
+
action='store_true'
|
|
264
|
+
)
|
|
257
265
|
parser.add_argument(
|
|
258
266
|
'files',
|
|
259
267
|
nargs='+',
|
|
@@ -266,5 +274,6 @@ if __name__ == '__main__':
|
|
|
266
274
|
formatter=vars(args)['formatter'],
|
|
267
275
|
git_root=get_git_root(),
|
|
268
276
|
update_working_tree=not vars(args)['no_update_working_tree'],
|
|
269
|
-
write=not vars(args)['no_write']
|
|
277
|
+
write=not vars(args)['no_write'],
|
|
278
|
+
verbose=vars(args)['verbose']
|
|
270
279
|
)
|
package/package.json
CHANGED