git-format-staged 3.0.0 → 3.1.1

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 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
- If you do not use npm you can copy the
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.0.0 is replaced during the publish process.
26
- VERSION = '3.0.0'
25
+ # The string 3.1.1 is replaced during the publish process.
26
+ VERSION = '3.1.1'
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:
@@ -86,17 +86,20 @@ def format_file_in_index(formatter, diff_entry, update_working_tree=True, write=
86
86
 
87
87
  return new_hash
88
88
 
89
- file_path_placeholder = re.compile('\{\}')
89
+ file_path_placeholder = re.compile(r'\{\}')
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
- re.sub(file_path_placeholder, file_path, formatter),
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
@@ -164,7 +167,7 @@ def patch_working_file(path, orig_object_hash, new_object_hash):
164
167
  raise Exception('could not apply formatting changes to working tree file {}'.format(path))
165
168
 
166
169
  # Format: src_mode dst_mode src_hash dst_hash status/score? src_path dst_path?
167
- diff_pat = re.compile('^:(\d+) (\d+) ([a-f0-9]+) ([a-f0-9]+) ([A-Z])(\d+)?\t([^\t]+)(?:\t([^\t]+))?$')
170
+ diff_pat = re.compile(r'^:(\d+) (\d+) ([a-f0-9]+) ([a-f0-9]+) ([A-Z])(\d+)?\t([^\t]+)(?:\t([^\t]+))?$')
168
171
 
169
172
  # Parse output from `git diff-index`
170
173
  def parse_diff(diff):
@@ -182,7 +185,7 @@ def parse_diff(diff):
182
185
  'dst_path': m.group(8)
183
186
  }
184
187
 
185
- zeroed_pat = re.compile('^0+$')
188
+ zeroed_pat = re.compile(r'^0+$')
186
189
 
187
190
  # Returns the argument unless the argument is a string of zeroes, in which case
188
191
  # returns `None`
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "git-format-staged",
3
- "version": "3.0.0",
3
+ "version": "3.1.1",
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",