Class: Depl::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/depl/main.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Main



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/depl/main.rb', line 3

def initialize(options)
  config_path = options[:config_file] || "./.deploy"
  @config = options[:config]

  if File.exist? config_path
    @config ||= YAML::load_file(config_path)
  else
    @config ||= {}
  end

  @options = options
end

Instance Method Details

#commit_countObject



73
74
75
# File 'lib/depl/main.rb', line 73

def commit_count
  diff.split("\n").size
end

#deploy_branchObject



24
25
26
# File 'lib/depl/main.rb', line 24

def deploy_branch
  "#{prefix}#{environment}"
end

#diffObject



60
61
62
# File 'lib/depl/main.rb', line 60

def diff
  execute "git log --pretty=format:'    %h %<(20)%an %ar\t   %s' -10 #{remote_sha}..#{local_sha}"
end

#environmentObject



16
17
18
# File 'lib/depl/main.rb', line 16

def environment
  @options[:environment]
end

#local_shaObject



54
55
56
57
58
# File 'lib/depl/main.rb', line 54

def local_sha
  rev = @options[:rev] || @config[:branch] || 'head'
  sha = execute("git rev-parse -q --verify #{rev}").chomp
  sha if sha != ""
end

#older_local_shaObject



68
69
70
71
# File 'lib/depl/main.rb', line 68

def older_local_sha
  return false unless remote_sha
  execute("git merge-base --is-ancestor #{local_sha} #{remote_sha}") && $?.exitstatus == 0
end

#prefixObject



20
21
22
# File 'lib/depl/main.rb', line 20

def prefix
  @config[:prefix] || "deploy-"
end

#remote_shaObject



44
45
46
47
48
# File 'lib/depl/main.rb', line 44

def remote_sha
  `git fetch origin`
  sha = execute("git rev-parse -q --verify origin/#{deploy_branch}").chomp
  sha if sha != ""
end

#reverse_diffObject



64
65
66
# File 'lib/depl/main.rb', line 64

def reverse_diff
  execute "git log --pretty=format:'    %h %<(20)%an %ar\t   %s' -10 #{local_sha}..#{remote_sha}"
end

#run!Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/depl/main.rb', line 32

def run!
  if @config['before_hook']
    `#{@config['before_hook']}`
  end

  save_sha

  if @config['after_hook']
    `#{@config['after_hook']}`
  end
end

#save_shaObject



28
29
30
# File 'lib/depl/main.rb', line 28

def save_sha
  execute("git push --force origin #{local_sha}:refs/heads/#{deploy_branch}")
end

#up_to_dateObject



50
51
52
# File 'lib/depl/main.rb', line 50

def up_to_date
  local_sha == remote_sha
end