-
-
Notifications
You must be signed in to change notification settings - Fork 690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New hook 'destroyed-symlinks' #511
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import argparse | ||
import shlex | ||
import subprocess | ||
from typing import List | ||
from typing import Optional | ||
from typing import Sequence | ||
|
||
from pre_commit_hooks.util import cmd_output | ||
from pre_commit_hooks.util import zsplit | ||
|
||
ORDINARY_CHANGED_ENTRIES_MARKER = '1' | ||
PERMS_LINK = '120000' | ||
PERMS_NONEXIST = '000000' | ||
|
||
|
||
def find_destroyed_symlinks(files: Sequence[str]) -> List[str]: | ||
destroyed_links: List[str] = [] | ||
if not files: | ||
return destroyed_links | ||
for line in zsplit( | ||
cmd_output('git', 'status', '--porcelain=v2', '-z', '--', *files), | ||
): | ||
splitted = line.split(' ') | ||
if splitted and splitted[0] == ORDINARY_CHANGED_ENTRIES_MARKER: | ||
# https://git-scm.com/docs/git-status#_changed_tracked_entries | ||
( | ||
_, _, _, | ||
mode_HEAD, | ||
mode_index, | ||
_, | ||
hash_HEAD, | ||
hash_index, | ||
*path_splitted, | ||
) = splitted | ||
path = ' '.join(path_splitted) | ||
if ( | ||
mode_HEAD == PERMS_LINK and | ||
mode_index != PERMS_LINK and | ||
mode_index != PERMS_NONEXIST | ||
): | ||
if hash_HEAD == hash_index: | ||
# if old and new hashes are equal, it's not needed to check | ||
# anything more, we've found a destroyed symlink for sure | ||
destroyed_links.append(path) | ||
else: | ||
# if old and new hashes are *not* equal, it doesn't mean | ||
# that everything is OK - new file may be altered | ||
# by something like trailing-whitespace and/or | ||
# mixed-line-ending hooks so we need to go deeper | ||
SIZE_CMD = ('git', 'cat-file', '-s') | ||
size_index = int(cmd_output(*SIZE_CMD, hash_index).strip()) | ||
size_HEAD = int(cmd_output(*SIZE_CMD, hash_HEAD).strip()) | ||
|
||
# in the worst case new file may have CRLF added | ||
# so check content only if new file is bigger | ||
# not more than 2 bytes compared to the old one | ||
if size_index <= size_HEAD + 2: | ||
head_content = subprocess.check_output( | ||
('git', 'cat-file', '-p', hash_HEAD), | ||
).rstrip() | ||
index_content = subprocess.check_output( | ||
('git', 'cat-file', '-p', hash_index), | ||
).rstrip() | ||
if head_content == index_content: | ||
destroyed_links.append(path) | ||
return destroyed_links | ||
|
||
|
||
def main(argv: Optional[Sequence[str]] = None) -> int: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('filenames', nargs='*', help='Filenames to check.') | ||
args = parser.parse_args(argv) | ||
destroyed_links = find_destroyed_symlinks(files=args.filenames) | ||
if destroyed_links: | ||
print('Destroyed symlinks:') | ||
for destroyed_link in destroyed_links: | ||
print(f'- {destroyed_link}') | ||
print('You should unstage affected files:') | ||
print( | ||
'\tgit reset HEAD -- {}'.format( | ||
' '.join(shlex.quote(link) for link in destroyed_links), | ||
), | ||
) | ||
print( | ||
'And retry commit. As a long term solution ' | ||
'you may try to explicitly tell git that your ' | ||
'environment does not support symlinks:', | ||
) | ||
print('\tgit config core.symlinks false') | ||
return 1 | ||
else: | ||
return 0 | ||
|
||
|
||
if __name__ == '__main__': | ||
exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import os | ||
import subprocess | ||
|
||
import pytest | ||
|
||
from pre_commit_hooks.destroyed_symlinks import find_destroyed_symlinks | ||
from pre_commit_hooks.destroyed_symlinks import main | ||
|
||
TEST_SYMLINK = 'test_symlink' | ||
TEST_SYMLINK_TARGET = '/doesnt/really/matters' | ||
TEST_FILE = 'test_file' | ||
TEST_FILE_RENAMED = f'{TEST_FILE}_renamed' | ||
|
||
|
||
@pytest.fixture | ||
def repo_with_destroyed_symlink(tmpdir): | ||
source_repo = tmpdir.join('src') | ||
os.makedirs(source_repo, exist_ok=True) | ||
test_repo = tmpdir.join('test') | ||
with source_repo.as_cwd(): | ||
subprocess.check_call(('git', 'init')) | ||
os.symlink(TEST_SYMLINK_TARGET, TEST_SYMLINK) | ||
with open(TEST_FILE, 'w') as f: | ||
print('some random content', file=f) | ||
subprocess.check_call(('git', 'add', '.')) | ||
subprocess.check_call( | ||
('git', 'commit', '--no-gpg-sign', '-m', 'initial'), | ||
) | ||
assert b'120000 ' in subprocess.check_output( | ||
('git', 'cat-file', '-p', 'HEAD^{tree}'), | ||
) | ||
subprocess.check_call( | ||
('git', '-c', 'core.symlinks=false', 'clone', source_repo, test_repo), | ||
) | ||
with test_repo.as_cwd(): | ||
subprocess.check_call( | ||
('git', 'config', '--local', 'core.symlinks', 'true'), | ||
) | ||
subprocess.check_call(('git', 'mv', TEST_FILE, TEST_FILE_RENAMED)) | ||
assert not os.path.islink(test_repo.join(TEST_SYMLINK)) | ||
yield test_repo | ||
|
||
|
||
def test_find_destroyed_symlinks(repo_with_destroyed_symlink): | ||
with repo_with_destroyed_symlink.as_cwd(): | ||
assert find_destroyed_symlinks([]) == [] | ||
assert main([]) == 0 | ||
|
||
subprocess.check_call(('git', 'add', TEST_SYMLINK)) | ||
assert find_destroyed_symlinks([TEST_SYMLINK]) == [TEST_SYMLINK] | ||
assert find_destroyed_symlinks([]) == [] | ||
assert main([]) == 0 | ||
assert find_destroyed_symlinks([TEST_FILE_RENAMED, TEST_FILE]) == [] | ||
ALL_STAGED = [TEST_SYMLINK, TEST_FILE_RENAMED] | ||
assert find_destroyed_symlinks(ALL_STAGED) == [TEST_SYMLINK] | ||
assert main(ALL_STAGED) != 0 | ||
|
||
with open(TEST_SYMLINK, 'a') as f: | ||
print(file=f) # add trailing newline | ||
subprocess.check_call(['git', 'add', TEST_SYMLINK]) | ||
assert find_destroyed_symlinks(ALL_STAGED) == [TEST_SYMLINK] | ||
assert main(ALL_STAGED) != 0 | ||
|
||
with open(TEST_SYMLINK, 'w') as f: | ||
print('0' * len(TEST_SYMLINK_TARGET), file=f) | ||
subprocess.check_call(('git', 'add', TEST_SYMLINK)) | ||
assert find_destroyed_symlinks(ALL_STAGED) == [] | ||
assert main(ALL_STAGED) == 0 | ||
|
||
with open(TEST_SYMLINK, 'w') as f: | ||
print('0' * (len(TEST_SYMLINK_TARGET) + 3), file=f) | ||
subprocess.check_call(('git', 'add', TEST_SYMLINK)) | ||
assert find_destroyed_symlinks(ALL_STAGED) == [] | ||
assert main(ALL_STAGED) == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note that you can use
git -C source_repo ...
instead ofas_cwd()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know but this way it's a bit more readable IMO.