Class: Provider::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/depengine/provider/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#repository_local_dirObject

Returns the value of attribute repository_local_dir.



4
5
6
# File 'lib/depengine/provider/git.rb', line 4

def repository_local_dir
  @repository_local_dir
end

#repository_urlObject

Returns the value of attribute repository_url.



3
4
5
# File 'lib/depengine/provider/git.rb', line 3

def repository_url
  @repository_url
end

Instance Method Details

#checkout(branch_name, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/depengine/provider/git.rb', line 38

def checkout(branch_name, options={})
  Helper.validates_presence_of repository_url, "repository_url not set"
  Helper.validates_presence_of repository_local_dir, "repository_local_dir not set"

  Dir.chdir(repository_local_dir) do
    # Pull
    shell_cmd = "git pull origin master"
    output = system shell_cmd
    if not output
      $log.writer.error "Unable to pull branch #{branch_name}"
      exit 1
    end

    # Checkout
    shell_cmd = "git checkout -f #{branch_name}"
    output = system shell_cmd
    if not output
      $log.writer.error "Unable to checkout branch #{branch_name}"
      exit 1
    end
  end
end

#fetch(options = {}) ⇒ Object



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
# File 'lib/depengine/provider/git.rb', line 6

def fetch(options={})
  Helper.validates_presence_of repository_url, "repository_url not set"
  Helper.validates_presence_of repository_local_dir, "repository_local_dir not set"

  # Make sure directory exists
  if not File.exists? repository_local_dir
    FileUtils.mkdir_p(repository_local_dir)
  end

  # Fetch (or clone)
  Dir.chdir(repository_local_dir) do
    if not File.exists? ".git"
      $log.writer.info "Clone git repository"
      shell_cmd = "git clone #{repository_url} ."
      output = system shell_cmd
      if not output
        current_directory = Dir.getwd
        $log.writer.error "Unable to clone repository #{repository_url} into #{current_directory}"
        exit 1
      end
    else
      $log.writer.info "Fetch git repository"
      shell_cmd = "git fetch origin"
      output = system shell_cmd
      if not output
        $log.writer.error "Unable to fetch from origin"
        exit 1
      end
    end
  end      
end

#submodule(submodule_option, options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/depengine/provider/git.rb', line 61

def submodule(submodule_option, options={})
  Helper.validates_presence_of repository_url, "repository_url not set"
  Helper.validates_presence_of repository_local_dir, "repository_local_dir not set"

  Dir.chdir(repository_local_dir) do
    # Submodule init
    shell_cmd = "git submodule init"
    output = system shell_cmd

    # git submodule update --recursive
    shell_cmd = "git submodule update #{submodule_option}"
    output = system shell_cmd
    if not output
      $log.writer.error "Unable to pull submodule"
      exit 1
    end
  end
end

#tag(tag_name, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/depengine/provider/git.rb', line 81

def tag(tag_name, options={})
  Helper.validates_presence_of repository_url, "repository_url not set"
  Helper.validates_presence_of repository_local_dir, "repository_local_dir not set"

  Dir.chdir(repository_local_dir) do
    # Create tag
    shell_cmd = "git tag #{tag_name}"
    if not options[:hash].nil?
      shell_cmd << " #{options[:hash]}"
    end

    output = system shell_cmd
    if not output
      $log.writer.error "Unable to create tag #{tag_name}"
      exit 1
    end

    # Push tag
    shell_cmd = "git push -f --tags origin"
    output = system shell_cmd
    if not output
      $log.writer.error "Unable to push to origin"
      exit 1
    end
  end
end