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
    unless 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
    unless 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
  unless File.exist? repository_local_dir
    FileUtils.mkdir_p(repository_local_dir)
  end

  # Fetch (or clone)
  Dir.chdir(repository_local_dir) do
    if !File.exist? '.git'
      $log.writer.info 'Clone git repository'
      shell_cmd = "git clone #{repository_url} ."
      output = system shell_cmd
      unless 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
      unless output
        $log.writer.error 'Unable to fetch from origin'
        exit 1
      end
    end
  end
end

#history(r1 = '', r2 = 'HEAD') ⇒ Object

Gets the git commit history of a local git repository.

Parameters:

  • :r2 - Include commits that are reachable from r2

  • :r1 - but exclude those that are reachable from r1.

Please see ‘GITREVISIONS(7)` for more info on how to specify a revision range.



111
112
113
114
115
116
117
118
119
# File 'lib/depengine/provider/git.rb', line 111

def history(r1 = '', r2 = 'HEAD')
  rev_range = r1.empty? ? '' : "#{r1}..#{r2}"

  Dir.chdir(@repository_local_dir) do
    log_result = Processor.local_execute(["git log --oneline #{rev_range}"])
    result = "#{log_result[1].each_line.first(10).join}"
    result
  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'
    system(shell_cmd)

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

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



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

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}"
    shell_cmd << " #{options[:hash]}" if options[:hash]

    output = system shell_cmd
    unless output
      $log.writer.error "Unable to create tag #{tag_name}"
      fail "Unable to create tag #{tag_name}"
    end

    # Push tag
    shell_cmd = 'git push -f --tags origin'
    output = system shell_cmd
    unless output
      $log.writer.error 'Unable to push to origin'
      fail 'Unable to push to origin'
    end
  end
end