Class: MultiGit::GitBackend::Repository

Inherits:
Repository
  • Object
show all
Defined in:
lib/multi_git/git_backend/repository.rb

Constant Summary collapse

Utils =
MultiGit::Utils
OBJECT_CLASSES =
{
  :blob => Blob,
  :tree => Tree,
  :commit => Commit
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Repository

#branch, #head, #tag

Methods included from Utils::AbstractMethods

#abstract

Constructor Details

#initialize(path, options = {}) ⇒ Repository

Returns a new instance of Repository.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/multi_git/git_backend/repository.rb', line 36

def initialize(path, options = {})
  @git_binary = `which git`.chomp
  options = initialize_options(path, options)
  git_dir = options[:repository]
  @git = Cmd.new({'GIT_CONFIG_NOSYSTEM'=>'1'}, git_binary, :git_dir => git_dir )
  if !::File.exists?(git_dir) || MultiGit::Utils.empty_dir?(git_dir)
    if options[:init]
      if options[:bare]
        @git['init', :bare, git_dir]
      else
        @git['init', git_dir]
      end
    else
      raise MultiGit::Error::NotARepository, options[:repository]
    end
  end
  @git_dir = git_dir
  @git_work_tree = options[:working_directory]
  @index = options[:index]
  verify_bareness(path, options)
end

Instance Attribute Details

#git_binaryObject (readonly)

Returns the value of attribute git_binary.



34
35
36
# File 'lib/multi_git/git_backend/repository.rb', line 34

def git_binary
  @git_binary
end

#git_dirObject (readonly)

Returns the value of attribute git_dir.



32
33
34
# File 'lib/multi_git/git_backend/repository.rb', line 32

def git_dir
  @git_dir
end

#git_work_treeObject (readonly)

Returns the value of attribute git_work_tree.



33
34
35
# File 'lib/multi_git/git_backend/repository.rb', line 33

def git_work_tree
  @git_work_tree
end

Instance Method Details

#__backend__Object

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.



16
17
18
# File 'lib/multi_git/git_backend/repository.rb', line 16

def __backend__
  @git
end

#bare?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/multi_git/git_backend/repository.rb', line 28

def bare?
  git_work_tree.nil?
end

#configObject



135
136
137
# File 'lib/multi_git/git_backend/repository.rb', line 135

def config
  @config ||= Config.new(@git)
end

#each_branch(filter = :all) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/multi_git/git_backend/repository.rb', line 152

def each_branch(filter = :all)
  return to_enum(:each_branch, filter) unless block_given?
  which = case filter
          when :all, Regexp then [:a]
          when :local       then []
          when :remote      then [:r]
          end
  post_filter = TRUE_LAMBDA
  if filter.kind_of? Regexp
    post_filter = filter
  end
  @git['branch', *which].each_line do |line|
    name = line[2..-2]
    next unless post_filter === name
    yield branch(name)
  end
  return self
end

#each_tagObject



171
172
173
174
175
176
177
# File 'lib/multi_git/git_backend/repository.rb', line 171

def each_tag
  return to_enum(:each_tag) unless block_given?
  @git['tag'].each_line do |line|
    yield tag(line.chomp)
  end
  return self
end

#include?(oid) ⇒ Boolean

Parameters:

  • oid (String)

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
# File 'lib/multi_git/git_backend/repository.rb', line 117

def include?(oid)
  begin
    @git['cat-file', :e, oid.to_s]
    return true
  rescue Cmd::Error::ExitCode1
    return false
  end
end

#parse(oidish) ⇒ String

Parameters:

  • expression (String)

Returns:

  • (String)

    oid

Raises:



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/multi_git/git_backend/repository.rb', line 101

def parse(oidish)
  begin
    result = @git['rev-parse', :revs_only, :validate, oidish.to_s].chomp
    if result == ""
      raise MultiGit::Error::InvalidReference, oidish
    end
    return result
  rescue Cmd::Error::ExitCode128
    raise MultiGit::Error::InvalidReference, oidish
  end
end

#read(oidish) ⇒ MultiGit::Object

Parameters:

  • expression (String)

Returns:

Raises:



91
92
93
94
95
# File 'lib/multi_git/git_backend/repository.rb', line 91

def read(oidish)
  oid = parse(oidish)
  type = @git['cat-file',:t, oid].chomp
  return OBJECT_CLASSES[type.to_sym].new(self, oid)
end

#ref(name) ⇒ MultiGit::Ref

Parameters:

  • name (String)

Returns:



130
131
132
133
# File 'lib/multi_git/git_backend/repository.rb', line 130

def ref(name)
  validate_ref_name(name)
  MultiGit::GitBackend::Ref.new(self, name)
end

#remote(name_or_url) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/multi_git/git_backend/repository.rb', line 139

def remote( name_or_url )
  if looks_like_remote_url? name_or_url
    remote = Remote.new(self, name_or_url)
  else
    remote = Remote::Persistent.new(self, name_or_url)
  end
  return remote
end

#write(content, type = :blob) ⇒ MultiGit::Object

Parameters:

Returns:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/multi_git/git_backend/repository.rb', line 62

def write(content, type = :blob)
  if content.kind_of? MultiGit::Builder
    return content >> self
  end
  validate_type(type)
  oid = nil
  if content.kind_of? MultiGit::Object
    if include?(content.oid)
      return read(content.oid)
    end
    content = content.to_io
  end
  if content.respond_to? :path
    oid = @git["hash-object",:t, type.to_s,:w,'--', content.path].chomp
  else
    content = content.read if content.respond_to? :read
    @git.call('hash-object',:t,type.to_s, :w, :stdin) do |stdin, stdout|
      stdin.write(content)
      stdin.close
      oid = stdout.read.chomp
    end
  end
  return OBJECT_CLASSES[type].new(self,oid)
end