Created
April 18, 2018 20:34
-
-
Save overthink/469f232f3ea05b229ad46df845e1b207 to your computer and use it in GitHub Desktop.
find duplicate files in a git repo
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/python | |
# Print filenames that are duplicated at least once (by content hash) in the | |
# repo. | |
import fileinput | |
from collections import defaultdict | |
import subprocess | |
digest_to_path = defaultdict(list) | |
p = subprocess.Popen( | |
["git", "ls-tree", "--full-tree", "-r", "HEAD"], | |
stdout=subprocess.PIPE) | |
for line in p.stdout.readlines(): | |
attrs, path = line.rstrip().split("\t") | |
_, _, digest = attrs.split() | |
digest_to_path[digest].append(path) | |
for digest, paths in digest_to_path.items(): | |
if len(paths) == 1: | |
del digest_to_path[digest] | |
for digest, paths in sorted(digest_to_path.items(), key=lambda (_, v): len(v), reverse=True): | |
if len(paths) > 2: | |
print "{} duplicates:".format(len(paths)) | |
for path in paths: | |
print " - " + path.rstrip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment