Skip to content

Instantly share code, notes, and snippets.

@dzaporozhets
Forked from bjorn/authors.rb
Created March 20, 2013 14:25
Show Gist options
  • Save dzaporozhets/5205067 to your computer and use it in GitHub Desktop.
Save dzaporozhets/5205067 to your computer and use it in GitHub Desktop.
#!/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