Created
November 7, 2015 20:32
-
-
Save rschroll/33c6dc52d38d42f1117b to your computer and use it in GitHub Desktop.
Check git history for additional copyright lines
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 subprocess | |
import os | |
def argsort(seq): | |
# http://stackoverflow.com/questions/3071415/efficient-method-to-calculate-the-rank-vector-of-a-list-in-python | |
return sorted(range(len(seq)), key = seq.__getitem__) | |
def check_file(f, start_commit, omit_commits): | |
if not f.endswith('.vala'): | |
return | |
fn = os.path.join(dir, f) | |
commits = subprocess.check_output(['git', 'log', '%s..HEAD' % start_commit, '--no-merges', | |
'--format=format:%an//%ai//%s//%h', | |
'--', fn]).split('\n') | |
authors = {} | |
for l in commits: | |
try: | |
author, date, subject, hash_ = l.split('//') | |
except ValueError: | |
continue | |
if hash_ in omit_commits or "code style" in subject.lower() or "code formatting" in subject.lower(): | |
continue | |
if author not in authors: | |
authors[author] = set() | |
year = date.split('-')[0] | |
authors[author].add(year) | |
if authors == {}: | |
return | |
auths = authors.keys() | |
auth_sort = argsort([min(authors[a]) for a in auths]) | |
head = [] | |
with open(fn, 'r') as source: | |
line = source.readline() | |
while len(line) > 1: | |
head.append(line) | |
line = source.readline() | |
res = "" | |
for i in xrange(len(auth_sort)): | |
a = auths[auth_sort[i]] | |
copyright_line = ([l for l in head if a in l] or [""])[0] | |
new_years = [y for y in sorted(authors[a]) if y not in copyright_line] | |
if new_years: | |
res += " * Copyright %s %s\n" % (", ".join(new_years), a) | |
if res: | |
print fn | |
print res | |
if __name__ == '__main__': | |
import sys | |
if len(sys.argv) < 3: | |
print "Usage: %s <directory> <starting commit>" % sys.argv[0] | |
exit(1) | |
for dir, dirs, files in os.walk(sys.argv[1]): | |
for f in files: | |
check_file(f, sys.argv[2], sys.argv[3:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment