Class: Corundum::Monotone

Inherits:
VersionControl show all
Defined in:
lib/corundum/version_control/monotone.rb

Instance Method Summary collapse

Methods inherited from VersionControl

#default_configuration

Instance Method Details

#base_revisionObject



73
74
75
# File 'lib/corundum/version_control/monotone.rb', line 73

def base_revision
  mtn_automate("get_base_revision_id").chomp
end

#defineObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/corundum/version_control/monotone.rb', line 99

def define
  super

  in_namespace do
    task :on_branch do
      branches = parse_certs(mtn_automate("certs", base_revision))["branch"] || []
      unless branches.include?(branch)
        fail "Not on branch #{branch}"
      end
    end

    task :not_tagged do
      items = parse_basic_io(mtn_automate("tags", branch))
      tags = items.find{|pair| pair[0] == "tag" && pair[1] == tag}
      unless tags.nil?
        fail "Tag #{tag} already exists in branch #{branch}"
      end
    end

    task :workspace_committed => :on_branch do
      items = parse_basic_io(mtn_automate("inventory"))
      changed = items.find{|pair| pair[0] == "changes"}
      unless changed.nil?
        fail "Uncommitted changes exist in workspace"
      end
    end

    task :gemspec_files_added => :on_branch do
      items = stanzas("path", parse_basic_io(mtn_automate("inventory")))
      items.delete_if{|item| item["status"] == "unknown"}
      known_paths = items.each_with_object({}) do |item, hash|
        hash[item["path"]] = true
      end

      files = gemspec_files.dup
      files.delete_if{|path| known_paths[path]}
      unless files.empty?
        fail "Gemspec files not in version control: #{files.join(" ")}"
      end
    end

    task :tag => :on_branch do
      mtn_automate("cert", base_revision, "tag", tag)
    end

    task :sync do
      mtn_automate("sync")
    end

    task :check_in => [:sync]
  end
end

#guess_branchObject



77
78
79
80
81
82
# File 'lib/corundum/version_control/monotone.rb', line 77

def guess_branch
  puts "Guessing branch - configure Monotone > branch"
  certs = parse_certs(mtn_automate("certs", base_revision))
  puts "  Guessed: #{certs["branch"].first}"
  certs["branch"].first
end

#mtn_automate(cmd, *args) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/corundum/version_control/monotone.rb', line 14

def mtn_automate(cmd, *args)
  result = Mattock::CommandLine.new("mtn", "automate", cmd) do |cmd|
    cmd.options += args
  end.run
  result.must_succeed!
  result.stdout
end

#parse_basic_io(string) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/corundum/version_control/monotone.rb', line 22

def parse_basic_io(string)
  items = []
  scanner = StringScanner.new(string)
  scanner.scan(/\s*/m)
  until scanner.eos? do

    symbol = scanner.scan(/[a-z_]*/)
    scanner.scan(/\s*/)
    value = ""
    case scanner.scan(/["\[]/)
    when '"'
      while (value += scanner.scan(/[^"]*/)) =~ /\\$/
      end
      scanner.scan(/"/)
    when '['
      value = scanner.scan(/[^\]]*/)
      scanner.scan(/]/)
    else
      raise "Improperly formatted basic_io: \
        \n  Got: #{items.inspect} + #{symbol}\
        \n  Rest:\
        \n    #{scanner.rest}"
    end
    items << [symbol, value]
    scanner.scan(/\s*/m)
  end
  return items
end

#parse_certs(string) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/corundum/version_control/monotone.rb', line 51

def parse_certs(string)
  items = parse_basic_io(string)
  pair = []
  hash = Hash.new do |h,k|
    h[k] = []
  end
  items.each do |name, value|
    case name
    when "name"
      pair[0] = value
    when "value"
      pair[1] = value
    when "trust"
      if value == "trusted"
        hash[pair[0]] << pair[1]
      end
      pair = []
    end
  end
  hash
end

#resolve_configurationObject



9
10
11
12
# File 'lib/corundum/version_control/monotone.rb', line 9

def resolve_configuration
  super
  @branch ||= guess_branch
end

#stanzas(first_item, items) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/corundum/version_control/monotone.rb', line 86

def stanzas(first_item, items)
  stanzas = []
  current_stanza = {}
  items.each do |name, value|
    if name == first_item
      current_stanza = {}
      stanzas << current_stanza
    end
    current_stanza[name] = value
  end
  return stanzas
end