Class: R10K::Git::ShellGit::BaseRepository

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/r10k/git/shellgit/base_repository.rb

Direct Known Subclasses

BareRepository, WorkingRepository

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS

Instance Method Summary collapse

Methods included from Logging

debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level

Instance Method Details

#branchesArray<String>

Returns All local branches in this repository.

Returns:

  • (Array<String>)

    All local branches in this repository



29
30
31
# File 'lib/r10k/git/shellgit/base_repository.rb', line 29

def branches
  for_each_ref('refs/heads')
end

#git_dirPathname

This method is abstract.

Returns The path to the Git directory.

Returns:

  • (Pathname)

    The path to the Git directory

Raises:

  • (NotImplementedError)


9
10
11
# File 'lib/r10k/git/shellgit/base_repository.rb', line 9

def git_dir
  raise NotImplementedError
end

#is_branch?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/r10k/git/shellgit/base_repository.rb', line 33

def is_branch?(pattern)
  result = git ['rev-parse', '-q', '--verify', "refs/heads/#{pattern}"], :git_dir => git_dir.to_s, :raise_on_fail => false

  result.success?
end

#is_tag?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/r10k/git/shellgit/base_repository.rb', line 44

def is_tag?(pattern)
  result = git ['rev-parse', '-q', '--verify', "refs/tags/#{pattern}"], :git_dir => git_dir.to_s, :raise_on_fail => false

  result.success?
end

#ref_type(pattern) ⇒ Symbol

Returns The type of the given ref, one of :branch, :tag, :commit, or :unknown.

Returns:

  • (Symbol)

    The type of the given ref, one of :branch, :tag, :commit, or :unknown



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/r10k/git/shellgit/base_repository.rb', line 51

def ref_type(pattern)
  @_ref_type_cache ||= {}

  @_ref_type_cache[pattern] ||= begin
    # Try to match and resolve SHA refs as quickly as possible.
    if pattern =~ /^[0-9a-f]{5,40}$/i && resolve(pattern)
      :commit
    elsif is_tag? pattern
      :tag
    elsif is_branch? pattern
      :branch
    elsif resolve(pattern)
      :commit
    else
      :unknown
    end
  end
end

#remotesHash

Returns Collection of remotes for this repo, keys are the remote name and values are the remote URL.

Returns:

  • (Hash)

    Collection of remotes for this repo, keys are the remote name and values are the remote URL.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/r10k/git/shellgit/base_repository.rb', line 71

def remotes
  result = git ['config', '--local', '--get-regexp', '^remote\..*\.url$'], :git_dir => git_dir.to_s, :raise_on_fail => false

  if result.success?
    Hash[
      result.stdout.split("\n").collect do |remote|
        matches = /^remote\.(.*)\.url (.*)$/.match(remote)

        [matches[1], matches[2]]
      end
    ]
  else
    {}
  end
end

#resolve(pattern) ⇒ String? Also known as: rev_parse

Resolve the given Git ref to a commit

Parameters:

  • pattern (String)

    The git ref to resolve

Returns:

  • (String, nil)

    The commit SHA if the ref could be resolved, nil otherwise.



17
18
19
20
21
22
# File 'lib/r10k/git/shellgit/base_repository.rb', line 17

def resolve(pattern)
  result = git ['rev-parse', "#{pattern}^{commit}"], :git_dir => git_dir.to_s, :raise_on_fail => false
  if result.success?
    result.stdout
  end
end

#tagsArray<String>

Returns All tags in this repository.

Returns:

  • (Array<String>)

    All tags in this repository



40
41
42
# File 'lib/r10k/git/shellgit/base_repository.rb', line 40

def tags
  for_each_ref('refs/tags')
end