Class: Heidi::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/heidi/project.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Project

Returns a new instance of Project.



10
11
12
13
14
15
# File 'lib/heidi/project.rb', line 10

def initialize(root)
  @root        = root
  @lock_file   = File.join(root, ".lock")
  @cached_root = File.join(root, "cached")
  @git         = Heidi::Git.new(@cached_root)
end

Instance Attribute Details

#cached_rootObject (readonly)

Returns the value of attribute cached_root.



8
9
10
# File 'lib/heidi/project.rb', line 8

def cached_root
  @cached_root
end

#lock_fileObject (readonly)

Returns the value of attribute lock_file.



8
9
10
# File 'lib/heidi/project.rb', line 8

def lock_file
  @lock_file
end

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'lib/heidi/project.rb', line 8

def root
  @root
end

Class Method Details

.find(commit) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/heidi/project.rb', line 28

def @builds.find(commit)
  return nil unless commit.is_a?(String)
  return nil unless commit.length >= 5

  self.select do |build|
    build.commit == commit || build.commit =~ Regexp.new("^#{commit}")
  end.first
end

Instance Method Details

#author(commit = self.commit) ⇒ Object



57
58
59
# File 'lib/heidi/project.rb', line 57

def author(commit=self.commit)
  @git.log(1, "%cN <%cE>", commit)
end

#basenameObject



49
50
51
# File 'lib/heidi/project.rb', line 49

def basename
  File.basename(@root)
end

#build_statusObject



86
87
88
# File 'lib/heidi/project.rb', line 86

def build_status
  @git["build.status"]
end

#build_status=(status) ⇒ Object



89
90
91
# File 'lib/heidi/project.rb', line 89

def build_status=(status)
  @git["build.status"] = status
end

#buildsObject



17
18
19
# File 'lib/heidi/project.rb', line 17

def builds
  @builds || load_builds
end

#commitObject



53
54
55
# File 'lib/heidi/project.rb', line 53

def commit
  @git.commit[0..8]
end

#current_buildObject



79
80
81
# File 'lib/heidi/project.rb', line 79

def current_build
  @git["build.current"]
end

#date(commit = self.commit) ⇒ Object



61
62
63
# File 'lib/heidi/project.rb', line 61

def date(commit=self.commit)
  @git.log(1, "%ci", commit)
end

#diff(commit) ⇒ Object



191
192
193
# File 'lib/heidi/project.rb', line 191

def diff(commit)
  @git.diff(commit)
end

#fetchObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/heidi/project.rb', line 127

def fetch
  if integration_branch && @git.branch != integration_branch
    if @git.branches.include? integration_branch
      @git.switch(integration_branch)
      @git.merge "origin/#{integration_branch}"

    else
      @git.checkout(integration_branch, "origin/#{integration_branch}")

    end
  end

  @git.pull

  # when the head has changed, update some stuff
  if last_commit != self.commit
    record_last_commit
  end
end

#integrate(forced = false) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/heidi/project.rb', line 106

def integrate(forced=false)
  return true if !forced && self.current_build == self.commit
  return "locked" if locked?

  status = "unknown"

  self.lock do
    record_current_build
    res = Heidi::Integrator.new(self).integrate
    if res == true
      status = nil
    elsif res.is_a? String
      status = res
    else
      status = "failed"
    end
  end

  return status
end

#integration_branchObject



93
94
95
96
# File 'lib/heidi/project.rb', line 93

def integration_branch
  name = @git["build.branch"]
  name == "" ? nil : name
end

#integration_branch=(name) ⇒ Object



97
98
99
100
# File 'lib/heidi/project.rb', line 97

def integration_branch=(name)
  name = name.split('/').last if name =~ /\//
  @git["build.branch"] = name
end

#integration_branchesObject



102
103
104
# File 'lib/heidi/project.rb', line 102

def integration_branches
  @git.remote_branches
end

#last_commitObject



65
66
67
# File 'lib/heidi/project.rb', line 65

def last_commit
  @git["commit"]
end

#latest_buildObject



72
73
74
# File 'lib/heidi/project.rb', line 72

def latest_build
  @git["build.latest"]
end

#load_buildsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/heidi/project.rb', line 21

def load_builds
  @builds = []
  Dir[File.join(root, "logs", "*")].each do |build|
    next unless File.directory? build
    @builds << Heidi::Build.new(self, File.basename(build))
  end

  def @builds.find(commit)
    return nil unless commit.is_a?(String)
    return nil unless commit.length >= 5

    self.select do |build|
      build.commit == commit || build.commit =~ Regexp.new("^#{commit}")
    end.first
  end

  return @builds
end

#lock(&block) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'lib/heidi/project.rb', line 147

def lock(&block)
  File.open(lock_file, File::TRUNC|File::CREAT|File::WRONLY) do |f|
    f.puts Time.now.strftime("%c")
  end

  if block_given?
    yield
    self.unlock
  end
end

#locked?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/heidi/project.rb', line 199

def locked?
  File.exists? lock_file
end

#logObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/heidi/project.rb', line 158

def log
  log = @git.graph(120)

  lines = []
  log.out.lines.each do |line|
    commit  = nil
    message = nil
    graph   = nil

    if line =~ /\*/
      color_less = line.gsub(/\e\[[^m]*m?/, '')
      commit = color_less.scan(/^[^a-z0-9]+([a-z0-9]+)/).flatten.first
      (graph, message) = line.chomp.split(commit)
    else
      graph = line.chomp
    end

    lines << {
      :line    => line,
      :commit  => commit,
      :build   => builds.find(commit),
      :graph   => graph,
      :message => message,
    }
  end

  return lines
end

#nameObject



45
46
47
# File 'lib/heidi/project.rb', line 45

def name
  @name ||= @git[:name]
end

#name=(name) ⇒ Object



40
41
42
43
# File 'lib/heidi/project.rb', line 40

def name=(name)
  @name = name
  @git["name"] = name
end

#record_current_buildObject



82
83
84
# File 'lib/heidi/project.rb', line 82

def record_current_build
  @git["build.current"] = self.commit
end

#record_last_commitObject



68
69
70
# File 'lib/heidi/project.rb', line 68

def record_last_commit
  @git["commit"] = self.commit
end

#record_latest_buildObject



75
76
77
# File 'lib/heidi/project.rb', line 75

def record_latest_build
  @git["build.latest"] = self.commit
end

#stat(commit) ⇒ Object



187
188
189
# File 'lib/heidi/project.rb', line 187

def stat(commit)
  @git.stat(commit)
end

#unlockObject



195
196
197
# File 'lib/heidi/project.rb', line 195

def unlock
  File.unlink lock_file
end