Created
November 8, 2009 19:50
-
-
Save seanh/229455 to your computer and use it in GitHub Desktop.
A Python script to check for matching filenames (regardless of path) in the current working directory. I use this as part of a pre-commit hook with git.
This file contains hidden or 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
| #!/usr/bin/env python | |
| """ | |
| Recursively checks that no filenames from the current working directory | |
| down are the same, even if they are in different subdirectories. Exits | |
| with status 0 if no matching filenames are found, or 1 if a match is | |
| found. | |
| """ | |
| import sys,os | |
| print "Checking for filename clashes...", | |
| filenames = [] | |
| clashes = [] | |
| for root, dirs, files in os.walk('.'): | |
| if root.startswith('./.git'): | |
| # Ignore the .git directory. | |
| continue | |
| for filename in files: | |
| if filename.endswith('~'): | |
| # Ignore automatically created backup files. | |
| continue | |
| if os.path.islink(os.path.join(root,filename)): | |
| # Don't count symlinks as filename clashes. | |
| continue | |
| if filename in filenames: | |
| clashes.append(os.path.join(root,filename)) | |
| filenames.append(filename) | |
| else: | |
| filenames.append(filename) | |
| print len(filenames), 'files found...', | |
| if clashes == []: | |
| print 'OK' | |
| sys.exit(0) | |
| else: | |
| print "clash(es) found:" | |
| for clash in clashes: | |
| print clash | |
| sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment