git_fetcher.rb 4.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#
# Copyright:: Copyright (c) 2012 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

18
19
20
21
22
module Omnibus

  # Fetcher implementation for projects in git.
  class GitFetcher < Fetcher

Daniel DeLeo's avatar
Daniel DeLeo committed
23
24
    name :git

25
26
27
28
29
    attr_reader :source
    attr_reader :project_dir
    attr_reader :version

    def initialize(software)
Daniel DeLeo's avatar
Daniel DeLeo committed
30
      @name         = software.name
31
32
      @source       = software.source
      @version      = software.version
33
      @project_dir  = software.project_dir
34
35
36
37
38
39
40
41
    end

    def description
      s=<<-E
repo URI:       #{@source[:git]}
local location: #{@project_dir}
E
    end
Marc Paradise's avatar
Marc Paradise committed
42
43
44
45
46
47
48
49
50
51
52
53
54
    
    def clean
      if existing_git_clone?
        log "cleaning existing build"
        clean_cmd = "git clean -fdx"
        shell = Mixlib::ShellOut.new(clean_cmd, :live_stream => STDOUT, :cwd => project_dir)
        shell.run_command
        shell.error!
      end
    rescue Exception => e
      ErrorReporter.new(e, self).explain("Failed to clean git repository '#{@source[:git]}'")
      raise
    end
Marc Paradise's avatar
Marc Paradise committed
55
56
57
58
    
    def fetch_required?
      !existing_git_clone? || !current_rev_matches_target_rev?
    end
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

    def fetch
      if existing_git_clone?
        fetch_updates unless current_rev_matches_target_rev?
      else
        clone
        checkout
      end
    rescue Exception => e
      ErrorReporter.new(e, self).explain("Failed to fetch git repository '#{@source[:git]}'")
      raise
    end

    private

    def clone
      puts "cloning the source from git"
      clone_cmd = "git clone #{@source[:git]} #{project_dir}"
      shell = Mixlib::ShellOut.new(clone_cmd, :live_stream => STDOUT)
      shell.run_command
      shell.error!
    end

    def checkout
      sha_ref = target_revision

      checkout_cmd = "git checkout #{sha_ref}"
      shell = Mixlib::ShellOut.new(checkout_cmd, :live_stream => STDOUT, :cwd => project_dir)
      shell.run_command
      shell.error!
    end

    def fetch_updates
      puts "fetching updates and resetting to revision #{target_revision}"
      fetch_cmd = "git fetch origin && git fetch origin --tags && git reset --hard #{target_revision}"
      shell = Mixlib::ShellOut.new(fetch_cmd, :live_stream => STDOUT, :cwd => project_dir)
      shell.run_command
      shell.error!
    end

    def existing_git_clone?
      File.exist?("#{project_dir}/.git")
    end

    def current_rev_matches_target_rev?
      current_revision && current_revision.strip.to_i(16) == target_revision.strip.to_i(16)
    end

    def current_revision
      @current_rev ||= begin
                         rev_cmd = "git rev-parse HEAD"
                         shell = Mixlib::ShellOut.new(rev_cmd, :live_stream => STDOUT, :cwd => project_dir)
                         shell.run_command
                         shell.error!
                         output = shell.stdout

                         sha_hash?(output) ? output : nil
                       end
    end

    def target_revision
      @target_rev ||= begin
                        if sha_hash?(version)
                          version
                        else
                          revision_from_remote_reference(version)
                        end
                      end
    end

    def sha_hash?(rev)
      rev =~ /^[0-9a-f]{40}$/
    end

    def revision_from_remote_reference(ref)
      # execute `git ls-remote`
      cmd = "git ls-remote origin #{ref}"
      shell = Mixlib::ShellOut.new(cmd, :live_stream => STDOUT, :cwd => project_dir)
      shell.run_command
      shell.error!
      stdout = shell.stdout

      # parse the output for the git SHA
      unless stdout =~ /^([0-9a-f]{40})\s+(\S+)/
        raise "Could not parse SHA reference"
      end
      return $1
    end
  end
end