Class: Solano::Hg

Inherits:
SCM
  • Object
show all
Extended by:
SolanoConstant
Includes:
SolanoConstant
Defined in:
lib/solano/scm/hg.rb

Constant Summary

Constants inherited from SCM

SCM::SCMS

Instance Attribute Summary

Attributes inherited from SCM

#default_origin_url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SCM

configure, #get_snap_id, parse_scp_url, scp_url?, #support_data, #upload_file, valid_repo_url?, without_scheme

Constructor Details

#initializeHg

Returns a new instance of Hg.



10
11
12
# File 'lib/solano/scm/hg.rb', line 10

def initialize
  super
end

Class Method Details

.hg_changes?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/solano/scm/hg.rb', line 217

def hg_changes?(options={})
  options[:exclude] ||= []
  options[:exclude] = [options[:exclude]] unless options[:exclude].is_a?(Array)
  cmd = "hg status -mardu"
  p = IO.popen(cmd)
  changes = false
  while line = p.gets do
    line = line.strip
    status, name = line.split(/\s+/)
    next if options[:exclude].include?(name)
    if status !~ /^\?/ then
      changes = true
      break
    end
  end
  unless $?.success? then
    warn(Text::Warning::SCM_UNABLE_TO_DETECT)
    return false
  end
  return changes
end

.hg_push(this_branch, additional_refs = []) ⇒ Object



239
240
241
# File 'lib/solano/scm/hg.rb', line 239

def hg_push(this_branch, additional_refs=[])
  raise "not implemented"
end

.version_okObject



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/solano/scm/hg.rb', line 243

def version_ok
  version = nil
  begin
    version_string = `hg -q --version`
    m =  version_string.match(Dependency::VERSION_REGEXP)
    version = m[0] unless m.nil?
  rescue Errno
  rescue Exception
  end
  if version.nil? || version.empty? then
    return false
  end
  version_parts = version.split(".")
  if version_parts[0].to_i < 2 then
    warn(Text::Warning::HG_VERSION % version)
  end
  true
end

Instance Method Details

#changes?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/solano/scm/hg.rb', line 71

def changes?(options={})
  return Solano::Hg.hg_changes?(:exclude=>".hgignore")
end

#commitsObject



102
103
104
105
# File 'lib/solano/scm/hg.rb', line 102

def commits
  commits = HgCommitLogParser.new(self.latest_commit).commits
  return commits
end

#create_patch(session_id, options = {}) ⇒ Object



150
151
152
153
154
155
156
157
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
186
187
188
189
190
191
# File 'lib/solano/scm/hg.rb', line 150

def create_patch(session_id, options={})
  api = options[:api]
  snapshot_commit = options[:commit]
  if "#{snapshot_commit}" == `hg --debug id -i`.to_s.strip then
    say Text::Warning::SAME_SNAPSHOT_COMMIT
    return
  end
  #check if commit is known locally
  if (`hg log --rev "ancestors(.) and #{snapshot_commit}" 2>&1 >/dev/null | grep -o 'error:' | wc -l`).to_i > 0 then
    raise Text::Error::PATCH_CREATION_ERROR % snapshot_commit
  end

  file_name = "solano-#{SecureRandom.hex(10)}.patch"
  tmp_dir = Dir.mktmpdir("patches")
  file_path = File.join(tmp_dir, file_name)
  Dir
  out = `hg export -o #{tmp_dir}/patch-%n -r #{snapshot_commit}:tip`
  say out
  build_patch(tmp_dir, file_path)

  file_size = File.size(file_path)
  file_sha1 = Digest::SHA1.file(file_path).hexdigest.upcase

  #upload patch
  say Text::Process::REQUST_PATCH_URL
  res = api.request_patch_url({:session_id => session_id})
  if (auth_url = res['auth_url']) then
    say Text::Process::UPLOAD_PATCH % auth_url
    upload_file(auth_url, file_path)
  else
    raise Text::Error::NO_PATCH_URL
  end

  args = {  :session_id => session_id,
            :sha1 => file_sha1,
            :size => file_size,}
  api.upload_session_patch(args)

ensure
  FileUtils.rm_rf(file_path) if file_path && File.exists?(file_path)
  FileUtils.rm_rf(tmp_dir) if tmp_dir && File.exists?(tmp_dir)
end

#create_snapshot(session_id, options = {}) ⇒ Object



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
# File 'lib/solano/scm/hg.rb', line 112

def create_snapshot(session_id, options={})
  api = options[:api]
  res = api.request_snapshot_url({:session_id => session_id})
  auth_url = res['auth_url']

  say Text::Process::SNAPSHOT_URL % auth_url

  unique = SecureRandom.hex(10)
  snaphot_path = File.join(Dir.tmpdir,".solano-#{unique}-snapshot")
  file = File.join(Dir.tmpdir, "solano-#{unique}-snapshot.tar")

  # #git default branch
  # branch = options[:default_branch]
  # branch ||= /\-\>.*\/(.*)$/.match( (`git branch -r | grep origin/HEAD`).strip)[1]

  # if branch.nil? then
  #   raise Text::Error::DEFAULT_BRANCH
  # end

  out = `hg clone #{root} #{snaphot_path}`
  out = `tar -C #{snaphot_path} -czpf #{file} .`
  upload_file(auth_url, file)
  Dir.chdir(snaphot_path){
    @snap_id = (`hg --debug id -i`).strip
  }

  desc = {"url" => auth_url.gsub(/\?.*/,''),
    "size" => File.stat(file).size,
    "sha1"=> Digest::SHA1.file(file).hexdigest.upcase,
    "commit_id"=> @snap_id,
    "session_id" => session_id,
  }
  api.update_snapshot({:repo_snapshot => desc})
ensure
  FileUtils.rm_rf(snaphot_path) if snaphot_path && File.exists?(snaphot_path)
  FileUtils.rm_f(file) if file && File.exists?(file)
end

#current_branchObject



60
61
62
63
64
# File 'lib/solano/scm/hg.rb', line 60

def current_branch
  branch = `hg branch`
  branch.chomp!
  return branch
end

#current_commitObject



96
97
98
99
100
# File 'lib/solano/scm/hg.rb', line 96

def current_commit
  commit = `hg id -i`
  commit.chomp!
  return commit
end

#default_branchObject



66
67
68
69
# File 'lib/solano/scm/hg.rb', line 66

def default_branch
  # NOTE: not necessarily quite right in HG 2.1+ with a default bookmark
  return "default"
end

#hg_push(uri) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/solano/scm/hg.rb', line 75

def hg_push(uri)
  cmd = "hg push -f -b #{self.current_branch} "
  cmd += " #{uri}"

  # git outputs something to stderr when it runs git push.
  # hg doesn't always ... so show the command that's being run and its
  # output to indicate progress.
  puts cmd
  puts `#{cmd}`
  return [0,1].include?( $?.exitstatus )
end

#ignore_pathObject



55
56
57
58
# File 'lib/solano/scm/hg.rb', line 55

def ignore_path
  path = File.join(self.root, Config::HG_IGNORE)
  return path
end

#number_of_commits(id_from, id_to) ⇒ Object



107
108
109
110
# File 'lib/solano/scm/hg.rb', line 107

def number_of_commits(id_from, id_to)
  result = `hg log --template='{node}\\n' #{id_from}..#{id_to}`
  result.split("\n").length
end

#origin_urlObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/solano/scm/hg.rb', line 40

def origin_url
  return @default_origin_url if @default_origin_url

  result = `hg paths default`
  return nil unless $?.success?
  return nil if result.empty?
  result.strip!
  u = URI.parse(result) rescue nil
  if u && u.host.nil? then
    warn(Text::Warning::HG_PATHS_DEFAULT_NOT_URI)
    return nil
  end
  return result
end

#push_latest(session_data, suite_details, options = {}) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/solano/scm/hg.rb', line 87

def push_latest(session_data, suite_details, options={})
  uri = if options[:use_private_uri] then
          suite_details["git_repo_private_uri"] || suite_details["git_repo_uri"]
        else
          suite_details["git_repo_uri"]
        end
  self.hg_push(uri)
end

#repo?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
# File 'lib/solano/scm/hg.rb', line 18

def repo?
  if File.directory?('.hg') then
    return true
  end
  ignore = `hg status 2>&1`
  ok = $?.success?
  return ok
end

#repo_nameObject



36
37
38
# File 'lib/solano/scm/hg.rb', line 36

def repo_name
  return File.basename(self.root)
end

#rootObject



27
28
29
30
31
32
33
34
# File 'lib/solano/scm/hg.rb', line 27

def root
  root = `hg root`
  if $?.exitstatus == 0 then
    root.chomp! if root
    return root
  end
  return Dir.pwd
end

#scm_nameObject



14
15
16
# File 'lib/solano/scm/hg.rb', line 14

def scm_name
  return 'hg'
end