Last active
July 26, 2022 14:07
-
-
Save valtyr/763f758426bea038554a42e4c5b4f397 to your computer and use it in GitHub Desktop.
This small script will help you find case mismatches in your git tree.
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
import os | |
import subprocess | |
import sys | |
from typing import Union, Mapping | |
rootdir = os.environ.get("PWD") | |
def recursive_listing_generator(folder: str): | |
path = os.path.join(rootdir, folder) | |
for currentpath, folder, files in os.walk(path): | |
for file in files: | |
yield os.path.join(currentpath, file) | |
def get_recursive_listing(folder: str): | |
return list(recursive_listing_generator(folder)) | |
def get_git_listing(folder: str): | |
results = subprocess.run(["git", "ls-files", folder], capture_output=True) | |
return [ | |
os.path.join(rootdir, line) | |
for line in results.stdout.decode("utf-8").splitlines() | |
] | |
def generate_case_insensitive_hash(lines: list[str]): | |
return {line.lower(): line for line in lines} | |
def get_argument(number: int, default: Union[None, str] = None): | |
try: | |
return sys.argv[number] | |
except IndexError: | |
return default | |
def diff(lspaths: Mapping[str, str], gitpaths: Mapping[str, str]): | |
for gitkey, gitval in gitpaths.items(): | |
lsval = lspaths.get(gitkey) | |
if lsval is None or lsval == gitval: | |
continue | |
gitpath = os.path.relpath(gitval, rootdir) | |
lspath = os.path.relpath(lsval, rootdir) | |
yield (gitpath, lspath) | |
if __name__ == "__main__": | |
folder = get_argument(1) | |
if folder is None: | |
sys.stderr.write("Missing directory argument") | |
exit(1) | |
ls = get_recursive_listing(folder) | |
git_ls = get_git_listing(folder) | |
ls_hash_map = generate_case_insensitive_hash(ls) | |
git_hash_map = generate_case_insensitive_hash(git_ls) | |
diff_result = list(diff(ls_hash_map, git_hash_map)) | |
if len(diff_result) == 0: | |
print(f"🧼 All good!") | |
exit(0) | |
print(f"😞 Found {len(diff_result)} issues:\n") | |
for (git, local) in diff_result: | |
print(f"💽 {local}") | |
print(f"🐙 {git}") | |
print("") | |
exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment