Skip to content

Commit 685e2c5

Browse files
committed
Add a git hook script that will list all duplicate files within the directories specified
1 parent dd54f7b commit 685e2c5

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tools/git_hooks/find_duplicates.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from os import walk
2+
from os.path import join
3+
from argparse import ArgumentParser
4+
5+
if __name__ == "__main__":
6+
parser = ArgumentParser("Find duplicate file names within a directory structure")
7+
parser.add_argument("dirs", help="Directories to search for duplicate file names"
8+
, nargs="*")
9+
parser.add_argument("--silent", help="Supress printing of filenames, just return number of duplicates", action="store_true")
10+
args = parser.parse_args()
11+
12+
scanned_files = {}
13+
14+
for dir in args.dirs:
15+
for root, dirs, files in walk(dir):
16+
for file in files:
17+
scanned_files.setdefault(file, [])
18+
scanned_files[file].append(join(root, file))
19+
20+
count_dupe = 0
21+
for key, value in scanned_files.iteritems():
22+
if len(value) > 1:
23+
count_dupe += 1
24+
if not args.silent:
25+
print("Multiple files found with name {}".format(key))
26+
for file in value:
27+
print(" {}".format(file))
28+
29+
exit(count_dupe)
30+

0 commit comments

Comments
 (0)