Class: R10K::Git::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/r10k/git/repository.rb

Overview

Define an abstract base class for git repositories.

Direct Known Subclasses

Cache, WorkingDir

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#basedirObject (readonly)

Returns the value of attribute basedir.



13
14
15
# File 'lib/r10k/git/repository.rb', line 13

def basedir
  @basedir
end

#dirnameObject (readonly)

Returns the value of attribute dirname.



17
18
19
# File 'lib/r10k/git/repository.rb', line 17

def dirname
  @dirname
end

#git_dirObject (readonly)

Returns the value of attribute git_dir.



24
25
26
# File 'lib/r10k/git/repository.rb', line 24

def git_dir
  @git_dir
end

#remoteObject (readonly)

Returns the value of attribute remote.



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

def remote
  @remote
end

Instance Method Details

#remotesHash<String, String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns A hash of remote names and fetch URLs.

Returns:

  • (Hash<String, String>)

    A hash of remote names and fetch URLs



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/r10k/git/repository.rb', line 82

def remotes
  output = git ['remote', '-v'], :git_dir => git_dir

  ret = {}
  output.stdout.each_line do |line|
    next if line.match(/\(push\)/)
    name, url, _ = line.split(/\s+/)
    ret[name] = url
  end

  ret
end

#resolve_commit(pattern) ⇒ Object

Define the same interface for resolving refs.



72
73
74
75
76
77
78
# File 'lib/r10k/git/repository.rb', line 72

def resolve_commit(pattern)
  output = git ['rev-parse', "#{pattern}^{commit}"], :git_dir => git_dir, :raise_on_fail => false

  if output.success?
    output.stdout.chomp
  end
end

#resolve_head(pattern) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/r10k/git/repository.rb', line 54

def resolve_head(pattern)
  output = git ['show-ref', '--heads', '-s', pattern], :git_dir => git_dir, :raise_on_fail => false

  if output.success?
    output.stdout.lines.first
  end
end

#resolve_ref(pattern) ⇒ String Also known as: rev_parse

Resolve a ref to a git commit. The given pattern can be a commit, tag, or a local or remote branch

Parameters:

  • pattern (String)

Returns:

  • (String)

    The dereferenced hash of ‘pattern`



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/r10k/git/repository.rb', line 32

def resolve_ref(pattern)
  commit   = resolve_tag(pattern)
  commit ||= resolve_remote_head(pattern)
  commit ||= resolve_head(pattern)
  commit ||= resolve_commit(pattern)

  if commit
    commit.chomp
  else
    raise R10K::Git::UnresolvableRefError.new(:ref => pattern, :git_dir => git_dir)
  end
end

#resolve_remote_head(pattern, remote = 'origin') ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/r10k/git/repository.rb', line 62

def resolve_remote_head(pattern, remote = 'origin')
  pattern = "refs/remotes/#{remote}/#{pattern}"
  output = git ['show-ref', '-s', pattern], :git_dir => git_dir, :raise_on_fail => false

  if output.success?
    output.stdout.lines.first
  end
end

#resolve_tag(pattern) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/r10k/git/repository.rb', line 46

def resolve_tag(pattern)
  output = git ['show-ref', '--tags', '-s', pattern], :git_dir => git_dir, :raise_on_fail => false

  if output.success?
    output.stdout.lines.first
  end
end

#tagsObject



95
96
97
98
99
100
# File 'lib/r10k/git/repository.rb', line 95

def tags
  entries = []
  output = git(['tag', '-l'], :git_dir => @git_dir).stdout
  output.each_line { |line| entries << line.chomp }
  entries
end