Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Vlad::Git

  set :source, Vlad::Git.new
  set :git_cmd, "git"

  ##
  # Returns the command that will check out +revision+ from the repository
  # into directory +destination+.  +revision+ can be any SHA1 or equivalent
  # (e.g. branch, tag, etc...)

  def checkout(revision, destination)
    destination = 'repo' if destination == '.'
    revision = 'HEAD' if revision =~ /head/i

    # Clone from scratch or update the existing copy of the repository.
    #
    # NOTE Requires source.checkout to be run separately from other commands
    # since 'if' can't be joined with && to other shell commands.
    [
      "if [ -d #{destination}/.git ]",
      "then cd #{destination} && #{git_cmd} checkout -f -b deployed-#{revision} #{revision}",
      "else #{git_cmd} clone #{repository} #{destination}",
      "fi"
    ].join(" ; ")
  end

  ##
  # Returns the command that will export +revision+ from the repository into
  # the directory +destination+.
  #
  # TODO Assumes that checkout will handle the appropriate version.

  def export(revision_or_source, destination)
    source_dir = 'repo' if revision_or_source == '.'
    revision_name = revision_or_source
    revision_name = 'HEAD' if revision_or_source == "."

    [
      "cd #{source_dir}",
      "mkdir -p #{destination}",
      "#{git_cmd} archive --format=tar #{revision_name} | (cd #{destination} && tar xf -)"
    ].join(" && ")
  end

  ##
  # Returns a command that maps human-friendly revision identifier +revision+
  # into a git SHA1.

  def revision(revision)
    revision = 'HEAD' if revision =~ /head/i

    "`#{git_cmd} rev-parse #{revision}`"
  end

end