added fixed message length padding for OpenCorn::Log
[opencorn.git] / checkout_git_repository / checkout.rb
blob0b317326fb274d19e40746c94086c83d6f40097a
1 #!/usr/bin/env ruby
3 require 'rubygems'
4 require 'git'
5 require 'pp'
6 require 'fileutils'
7 require 'tempfile'
8 require 'opencorn/config'
10 DEBUG = false
12 # This script is supposed to be run regularly.
14 # It clones the 'official' git repository and figures
15 # out which is the latest tag that contains 2 or more
16 # signatures from board members.
18 # It then resets the checked out git repository to this state
19 # and clones it to its final place.
21 def valid_signature(tag)
22         system "git-verify-tag #{tag}"
23 end
25 def signer_id(tag)
26         result = `git-verify-tag #{tag} 2>&1`
27         result[/key ID ([0-9A-F]{8})/, 1]
28 end
30 if OpenCorn::Config['GNUPGHOME'] then
31         ENV['GNUPGHOME'] = OpenCorn::Config['GNUPGHOME']
32 end
34 # update accepted repo
35 Git.open(OpenCorn::Config['ACCEPTED_REPO']).pull
37 # update revocation repo
38 Git.open(OpenCorn::Config['REVOCATION_REPO']).pull
40 tmpdir = Dir.mktmpdir
41 # check out in tmpdir
42 g = Git.clone(OpenCorn::Config['ACCEPTED_REPO'], tmpdir)
44 object_signatures = {}
45 most_current_signed_object = nil
46 pp g.tags if DEBUG
47 # iterate over all tags, as they are not sorted by time
48 g.tags.each do |tag|
49         puts "Checking tag #{tag.name}" if DEBUG
50         if ! tag.name[/^[a-zA-Z0-9]+$/] then
51                 STDERR.puts 'Argh, non-alphanumeric tag name, WTF?'
52                 next
53         end
54         object_id = tag.contents_array[0][/object ([a-f0-9]+)/, 1]
55         puts "Refers to object id #{object_id}" if DEBUG
56         if valid_signature(tag.name) then
57                 puts "Valid signature on #{tag.name} by #{signer_id(tag.name)}" \
58                         if DEBUG
59                 object_signatures[object_id] ||= {}
60                 object_signatures[object_id][signer_id(tag.name)] = 1
61         end
62 end
63 pp object_signatures if DEBUG
65 # iterate over all commits to find the first that has more than one signature
66 g.log.each do |log|
67         if object_signatures[log.objectish] \
68         && object_signatures[log.objectish].keys.size >= 2 then
69                 most_current_signed_object = log.objectish
70                 break
71         end
72 end
74 if ! most_current_signed_object then
75         STDERR.puts "Sorry, no object with more than one signed tag found."
76         exit 1
77 end
79 g.reset_hard(most_current_signed_object)
81 begin
82 FileUtils.rm_r OpenCorn::Config['ACCEPTED_SIGNED_REPO'], :secure => true
83 rescue # ignore errors deleting the directory
84 end
85 Git.clone(tmpdir, OpenCorn::Config['ACCEPTED_SIGNED_REPO'], :depth => 1)