Class: Pione::Location::GitRepositoryLocation

Inherits:
BasicLocation show all
Defined in:
lib/pione/location/git-repository-location.rb

Overview

GitRepositoryLocation represents locations of git repository.

Instance Attribute Summary collapse

Attributes inherited from BasicLocation

#address

Instance Method Summary collapse

Methods inherited from BasicLocation

#==, #hash, #inspect, location_type

Constructor Details

#initialize(address) ⇒ GitRepositoryLocation

Returns a new instance of GitRepositoryLocation.



9
10
11
12
13
14
15
# File 'lib/pione/location/git-repository-location.rb', line 9

def initialize(address)
  @address = address[:git].to_s
  @tag = address[:tag].to_s
  @branch = address[:branch].to_s
  @hash_id = address[:hash_id].to_s
  @address_digest = Digest::SHA1.hexdigest(@address)
end

Instance Attribute Details

#tagObject (readonly)

Returns the value of attribute tag.



7
8
9
# File 'lib/pione/location/git-repository-location.rb', line 7

def tag
  @tag
end

Instance Method Details

#+(option) ⇒ Object

Return a new location with the option.



18
19
20
# File 'lib/pione/location/git-repository-location.rb', line 18

def +(option)
  self.class.new({git: @address, tag: @tag, branch: @branch, hash_id: @hash_id}.merge(option))
end

#compact_hash_idObject

Return compact version hash id string.



68
69
70
71
72
73
74
# File 'lib/pione/location/git-repository-location.rb', line 68

def compact_hash_id
  id = @hash_id if @hash_id
  id = ref(tag: @tag) if @tag
  id = ref(branch: @branch) if @branch
  id = ref(branch: "HEAD") unless id
  return short_hash_id(id)
end

#export(location) ⇒ Object

Export git repository by hash id.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pione/location/git-repository-location.rb', line 77

def export(location)
  hash_id = compact_hash_id

  # git archive
  path = Temppath.mkdir + "archive.zip"
  ChildProcess.build("git", "archive", "-o", path.to_s, hash_id).tap do |process|
    process.cwd = local.path
    process.start
    process.wait
    if process.crashed?
      raise GitError.new(@location, message: "'git archive' failed")
    end
  end

  # unzip
  Util::Zip.uncompress(Location[path], location)
end

#has_local?Boolean

Return true if the local location exists.

Returns:

  • (Boolean)


28
29
30
# File 'lib/pione/location/git-repository-location.rb', line 28

def has_local?
  local.exist?
end

#localObject

Return the local location of repository.



23
24
25
# File 'lib/pione/location/git-repository-location.rb', line 23

def local
  return Global.git_repository_directory + @address_digest
end

#ref(query) ⇒ Object

Return a hash id string of the referrence name.



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
# File 'lib/pione/location/git-repository-location.rb', line 33

def ref(query)
  clone_to_local unless has_local?

  # parse query
  type = query.keys.first
  name = query[type]

  # execute "git show-ref"
  out = Temppath.create.open("w+")
  process = ChildProcess.build("git", "show-ref")
  process.cwd = local.path.to_s
  process.io.stdout = out
  process.start
  process.wait

  if process.crashed?
    raise GitError.new("The command 'git clone' failed.", @address)
  end

  # find hash id
  out.rewind
  out.readlines.each do |line|
    hash_id, refname = line.split(" ")

    cond_tag = (type == :tag and "refs/tags/%s" % name == refname)
    cond_branch = (type == :branch and "refs/remotes/origin/%s" % name == refname)

    return hash_id if cond_tag or cond_branch
  end

  # the name not found
  return nil
end