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.



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

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

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

#shellObject (readonly)

Returns the value of attribute shell.



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

def shell
  @shell
end

Instance Method Details

#checkoutObject



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

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



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

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



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

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

#fetchObject



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

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

#gc_repository_cacheObject



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

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

#same?(previous_revision, active_revision, paths = nil) ⇒ Boolean

git diff --exit-code returns 0 when nothing has changed 1 when there are changes

Thes method returns true when nothing has changed, fales otherwise

Returns:

  • (Boolean)


73
74
75
# File 'lib/engineyard-serverside/strategies/git.rb', line 73

def same?(previous_revision, active_revision, paths = nil)
  run("#{git} diff '#{previous_revision}'..'#{active_revision}' --exit-code --name-only -- #{Array(paths).join(' ')} >/dev/null 2>&1")
end

#short_log_message(rev) ⇒ Object



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

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

#to_checkoutObject



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

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



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

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

#usable_repository?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/engineyard-serverside/strategies/git.rb', line 20

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