Class: EY::Serverside::Strategies::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/engineyard-serverside/strategies/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shell, opts) ⇒ Git

Returns a new instance of Git.



7
8
9
10
# File 'lib/engineyard-serverside/strategies/git.rb', line 7

def initialize(shell, opts)
  @shell = shell
  @opts = opts
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



5
6
7
# File 'lib/engineyard-serverside/strategies/git.rb', line 5

def opts
  @opts
end

#shellObject (readonly)

Returns the value of attribute shell.



5
6
7
# File 'lib/engineyard-serverside/strategies/git.rb', line 5

def shell
  @shell
end

Instance Method Details

#checkoutObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/engineyard-serverside/strategies/git.rb', line 31

def checkout
  shell.status "Deploying revision #{short_log_message(to_checkout)}"
  q = opts[:verbose] ? '' : '-q'
  in_repository_cache do
    (run("git checkout -f #{q} '#{to_checkout}'") ||
      run("git reset --hard #{q} '#{to_checkout}'")) &&
      run("git submodule sync") &&
      run("git submodule update --init") &&
      run("git clean -dfq")
  end
end

#clean_local_branch(ref) ⇒ Object



50
51
52
# File 'lib/engineyard-serverside/strategies/git.rb', line 50

def clean_local_branch(ref)
  system("#{git} show-branch #{ref} > /dev/null 2>&1 && #{git} branch -D #{ref} > /dev/null 2>&1")
end

#create_revision_file_command(dir) ⇒ Object



59
60
61
# File 'lib/engineyard-serverside/strategies/git.rb', line 59

def create_revision_file_command(dir)
  %Q{#{git} show --pretty=format:"%H" | head -1 > "#{dir}/REVISION"}
end

#fetchObject



22
23
24
25
26
27
28
29
# File 'lib/engineyard-serverside/strategies/git.rb', line 22

def fetch
  if usable_repository?
    run("#{git} fetch -q origin 2>&1")
  else
    FileUtils.rm_rf(repository_cache)
    run("git clone -q #{remote_uri} #{repository_cache} 2>&1")
  end
end

#gc_repository_cacheObject



54
55
56
57
# File 'lib/engineyard-serverside/strategies/git.rb', line 54

def gc_repository_cache
  shell.status "Garbage collecting cached git repository to reduce disk usage."
  run("#{git} gc")
end

#short_log_message(rev) ⇒ Object



63
64
65
# File 'lib/engineyard-serverside/strategies/git.rb', line 63

def short_log_message(rev)
  `#{git} log --pretty=oneline --abbrev-commit -n 1 '#{rev}'`.strip
end

#to_checkoutObject



43
44
45
46
47
48
# File 'lib/engineyard-serverside/strategies/git.rb', line 43

def to_checkout
  return @to_checkout if @opts_ref == opts[:ref]
  @opts_ref = opts[:ref]
  clean_local_branch(@opts_ref)
  @to_checkout = remote_branch?(@opts_ref) ? "origin/#{@opts_ref}" : @opts_ref
end

#update_repository_cacheObject



12
13
14
15
16
# File 'lib/engineyard-serverside/strategies/git.rb', line 12

def update_repository_cache
  unless fetch && checkout
    abort "*** [Error] Git could not checkout (#{to_checkout}) ***"
  end
end

#usable_repository?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/engineyard-serverside/strategies/git.rb', line 18

def usable_repository?
  File.directory?(repository_cache) && `#{git} remote -v | grep origin`[remote_uri]
end