class Pione::Location::GitRepositoryLocation

GitRepositoryLocation represents locations of git repository.

Attributes

tag[R]

Public Class Methods

new(address) click to toggle source
# 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

Public Instance Methods

+(option) click to toggle source

Return a new location with the option.

# 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_id() click to toggle source

Return compact version hash id string.

# 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) click to toggle source

Export git repository by hash id.

# 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?() click to toggle source

Return true if the local location exists.

# File lib/pione/location/git-repository-location.rb, line 28
def has_local?
  local.exist?
end
local() click to toggle source

Return the local location of repository.

# File lib/pione/location/git-repository-location.rb, line 23
def local
  return Global.git_repository_directory + @address_digest
end
ref(query) click to toggle source

Return a hash id string of the referrence name.

# 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

Private Instance Methods

clone_to_local() click to toggle source

Call “git clone” from the repository into local location.

@param path [Pathname]

the path of cloned repository
# File lib/pione/location/git-repository-location.rb, line 101
def clone_to_local
  out = Temppath.create.open("w+")

  # call git clone
  process = ChildProcess.build("git", "clone", @address, local.path.to_s)
  process.io.stdout = out
  process.start
  process.wait

  # check the process result
  if process.crashed?
    raise GitError.new(self, message: "'git clone' failed")
  end

  # show debug message
  out.rewind
  Log::Debug.system("git clone: %s" % out.read)
end
short_hash_id(hash_id) click to toggle source

Return short hash id.

# File lib/pione/location/git-repository-location.rb, line 121
def short_hash_id(hash_id)
  out = Temppath.create.open("w+")

  # git rev-parse
  process = ChildProcess.build("git", "rev-parse", "--short", hash_id)
  process.cwd = local.path.to_s
  process.io.stdout = out
  process.start
  process.wait

  # check the process result
  if process.crashed?
    raise GitError.new(self, message: "Hash ID '%s' is unknown or too short" % hash_id)
  end

  # show debug message
  out.rewind
  return out.read.chomp
end