File: git.rb

package info (click to toggle)
librarian-puppet 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 512 kB
  • sloc: ruby: 1,428; sh: 37; makefile: 9
file content (74 lines) | stat: -rw-r--r-- 1,904 bytes parent folder | download | duplicates (6)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'librarian/source/git'
require 'librarian/puppet/source/local'

module Librarian
  module Source
    class Git
      class Repository
        def hash_from(remote, reference)
          branch_names = remote_branch_names[remote]
          if branch_names.include?(reference)
            reference = "#{remote}/#{reference}"
          end

          command = %W(rev-parse #{reference}^{commit} --quiet)
          run!(command, :chdir => true).strip
        end
      end
    end
  end

  module Puppet
    module Source
      class Git < Librarian::Source::Git
        include Local
        include Librarian::Puppet::Util

        def cache!
          return vendor_checkout! if vendor_cached?

          if environment.local?
            raise Error, "Could not find a local copy of #{uri}#{" at #{sha}" unless sha.nil?}."
          end

          begin
            super
          rescue Librarian::Posix::CommandFailure => e
            raise Error, "Could not checkout #{uri}#{" at #{sha}" unless sha.nil?}: #{e}"
          end

          cache_in_vendor(repository.path) if environment.vendor?
        end

        private

        def vendor_tar
          environment.vendor_source.join("#{sha}.tar")
        end

        def vendor_tgz
          environment.vendor_source.join("#{sha}.tar.gz")
        end

        def vendor_cached?
          vendor_tgz.exist?
        end

        def vendor_checkout!
          repository.path.rmtree if repository.path.exist?
          repository.path.mkpath

          Librarian::Posix.run!(%W{tar xzf #{vendor_tgz}}, :chdir => repository.path.to_s)

          repository_cached!
        end

        def cache_in_vendor(tmp_path)
          Librarian::Posix.run!(%W{git archive -o #{vendor_tar} #{sha}}, :chdir => tmp_path.to_s)
          Librarian::Posix.run!(%W{gzip #{vendor_tar}}, :chdir => tmp_path.to_s)
        end

      end
    end
  end
end