Skip to content

Instantly share code, notes, and snippets.

@bjorn
Created August 23, 2012 20:47
Show Gist options
  • Save bjorn/3441515 to your computer and use it in GitHub Desktop.
Save bjorn/3441515 to your computer and use it in GitHub Desktop.
Generate copyright header author lists
#!/usr/bin/env ruby
# This script can be used to generate a list of authors for each source file,
# for inclusion in the copyright header.
require 'rugged'
require 'set'
repo = Rugged::Repository.new(".")
tree = repo.lookup(repo.head.target).tree
tree.each_blob { |entry|
filename = entry[:name]
if not filename.end_with? ".h", ".cpp" then
next
end
authors = Hash.new # author name => set of years
walker = Rugged::Walker.new(repo)
walker.sorting(Rugged::SORT_TOPO | Rugged::SORT_REVERSE)
walker.push(repo.head.target)
walker.each { |commit|
parents = commit.parents.map { |parent| parent.oid }
if parents.length > 0 then
sha1 = commit.oid
sha2 = parents[0]
if `git diff --name-only #{sha1} #{sha2}`.include? filename then
author = "#{commit.author[:name]} <#{commit.author[:email]}>"
year = commit.author[:time].to_s[0..3].to_i
if authors.include? author then
authors[author] << year
else
authors[author] = [year].to_set
end
end
end
}
puts "\n * #{filename}"
authors.each { |author, years|
puts " * Copyright (c) #{years.to_a.sort.join(', ')} #{author}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment