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
# File 'lib/engineyard-serverside/strategies/git.rb', line 31

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

#create_revision_file_command(dir) ⇒ Object



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

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



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

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

#short_log_message(rev) ⇒ Object



57
58
59
# File 'lib/engineyard-serverside/strategies/git.rb', line 57

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

#to_checkoutObject



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

def to_checkout
  return @to_checkout if @opts_ref == opts[:ref]
  @opts_ref = opts[:ref]
  @to_checkout = 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