Skip to content

Commit

Permalink
fix: do not write formatting changes if formatter produced empty outp…
Browse files Browse the repository at this point in the history
…ut (#43)
  • Loading branch information
hallettj committed Jan 6, 2021
1 parent 2b385a9 commit 1ca5201
Show file tree
Hide file tree
Showing 4 changed files with 15,172 additions and 48 deletions.
18 changes: 18 additions & 0 deletions git-format-staged
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def format_file_in_index(formatter, diff_entry, update_working_tree=True, write=
if not write or new_hash == orig_hash:
return None

# If the content of the new object is empty then the formatter did not
# produce any output. We want to abort instead of replacing the file with an
# empty one.
if object_is_empty(new_hash):
return None

replace_file_in_index(diff_entry, new_hash)

if update_working_tree:
Expand Down Expand Up @@ -114,6 +120,18 @@ def format_object(formatter, object_hash, file_path):

return new_hash.decode('utf-8').rstrip()

def object_is_empty(object_hash):
get_content = subprocess.Popen(
['git', 'cat-file', '-p', object_hash],
stdout=subprocess.PIPE
)
content, err = get_content.communicate()

if get_content.returncode != 0:
raise Exception('unable to verify content of formatted object')

return not content

def replace_file_in_index(diff_entry, new_object_hash):
subprocess.check_call(['git', 'update-index',
'--cacheinfo', '{},{},{}'.format(
Expand Down

0 comments on commit 1ca5201

Please sign in to comment.