annotate extra/soundsoftware/create-repo-authormaps.rb @ 1538:87bea4981d6d live

Script to create authormaps for all repos
author Chris Cannam
date Thu, 21 May 2015 16:59:41 +0100
parents
children 22d57b0e0a77
rev   line source
Chris@1538 1 #!/usr/bin/env ruby
Chris@1538 2
Chris@1538 3 # Create authormap files for hg repos based on the changeset & project
Chris@1538 4 # member info available to Redmine.
Chris@1538 5 #
Chris@1538 6 # We have a set of hg repos in a given directory:
Chris@1538 7 #
Chris@1538 8 # /var/hg/repo_1
Chris@1538 9 # /var/hg/repo_2
Chris@1538 10 # /var/hg/repo_3
Chris@1538 11 #
Chris@1538 12 # and we want to produce authormap files in another directory:
Chris@1538 13 #
Chris@1538 14 # /var/repo-export/authormap/authormap_repo_1
Chris@1538 15 # /var/repo-export/authormap/authormap_repo_2
Chris@1538 16 # /var/repo-export/authormap/authormap_repo_3
Chris@1538 17 #
Chris@1538 18 # This script does that, if given the two directory names as arguments
Chris@1538 19 # to the -s and -o options. In the above example:
Chris@1538 20 #
Chris@1538 21 # create-repo-authormaps.rb -s /var/hg -o /var/repo-export/authormap
Chris@1538 22 #
Chris@1538 23 # Note that this script will overwrite any existing authormap
Chris@1538 24 # files. (That's why the output files are given an authormap_ prefix,
Chris@1538 25 # so we're less likely to clobber something else if the user gets the
Chris@1538 26 # arguments wrong.)
Chris@1538 27
Chris@1538 28 require 'getoptlong'
Chris@1538 29
Chris@1538 30 opts = GetoptLong.new(
Chris@1538 31 ['--scm-dir', '-s', GetoptLong::REQUIRED_ARGUMENT],
Chris@1538 32 ['--out-dir', '-o', GetoptLong::REQUIRED_ARGUMENT]
Chris@1538 33 )
Chris@1538 34
Chris@1538 35 $repos_base = ''
Chris@1538 36 $out_base = ''
Chris@1538 37
Chris@1538 38 def usage
Chris@1538 39 puts "See source code for supported options"
Chris@1538 40 exit
Chris@1538 41 end
Chris@1538 42
Chris@1538 43 begin
Chris@1538 44 opts.each do |opt, arg|
Chris@1538 45 case opt
Chris@1538 46 when '--scm-dir'; $repos_base = arg.dup
Chris@1538 47 when '--out-dir'; $out_base = arg.dup
Chris@1538 48 end
Chris@1538 49 end
Chris@1538 50 rescue
Chris@1538 51 exit 1
Chris@1538 52 end
Chris@1538 53
Chris@1538 54 if ($repos_base.empty? or $out_base.empty?)
Chris@1538 55 usage
Chris@1538 56 end
Chris@1538 57
Chris@1538 58 unless File.directory?($repos_base)
Chris@1538 59 log("input directory '#{$repos_base}' doesn't exist", :exit => true)
Chris@1538 60 end
Chris@1538 61
Chris@1538 62 unless File.directory?($out_base)
Chris@1538 63 log("output directory '#{$out_base}' doesn't exist", :exit => true)
Chris@1538 64 end
Chris@1538 65
Chris@1538 66 projects = Project.find(:all)
Chris@1538 67
Chris@1538 68 if projects.nil?
Chris@1538 69 log('No projects found', :exit => true)
Chris@1538 70 end
Chris@1538 71
Chris@1538 72 projects.each do |proj|
Chris@1538 73 next unless proj.respond_to?(:repository)
Chris@1538 74
Chris@1538 75 repo = proj.repository
Chris@1538 76
Chris@1538 77 repo_url = repo.url
Chris@1538 78 repo_url = repo_url.gsub(/^file:\/*/, "/");
Chris@1538 79 if repo_url != File.join($repos_base, proj.identifier)
Chris@1538 80 log('Project #{proj.identifier} has repo in unsupported location #{repo_url}, skipping')
Chris@1538 81 next
Chris@1538 82 end
Chris@1538 83
Chris@1538 84 csets = repo.changesets
Chris@1538 85 committers = csets.map do |c| c.committer end.sort.uniq
Chris@1538 86
Chris@1538 87 authormap = ""
Chris@1538 88 committers.each do |c|
Chris@1538 89 if not c =~ /[^<]+<.*@.*>/ then
Chris@1538 90 user = repo.find_committer_user c
Chris@1538 91 authormap << "#{c}=#{u.name} <#{u.mail}>\n" unless u.nil?
Chris@1538 92 end
Chris@1538 93 end
Chris@1538 94
Chris@1538 95 File.open (File.join($out_base, "authormap_#{proj.identifier}"), "w") do |f|
Chris@1538 96 f.puts(authormap)
Chris@1538 97 end
Chris@1538 98
Chris@1538 99 end
Chris@1538 100