Class: RJGit::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/repo.rb

Constant Summary collapse

PACK_LIST =
'objects/info/packs'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Repo.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/repo.rb', line 43

def initialize(path, options = {})
  epath = File.expand_path(path)
  gitpath = File.join(epath, '.git')
  
  # Default value for bare
  bare = false
  # If the repo path is new
  unless File.exists?(epath) 
    # take user setting if defined
    bare = !! options[:is_bare] unless options[:is_bare].nil?
  # If the repo path exists
  else
    # scan the directory for a .git directory
    bare = File.exists?(gitpath) ? false : true
    # but allow overriding user setting
    bare = !! options[:is_bare] unless options[:is_bare].nil? 
  end
  
  @path = bare ? epath : gitpath
  @config = RJGit::Configuration.new(File.join(@path, 'config'))
  repo_path = java.io.File.new(@path)
  @jrepo = bare ? RepositoryBuilder.new().set_bare.set_git_dir(repo_path).build() : RepositoryBuilder.new().set_git_dir(repo_path).build()
  @jrepo.create(bare) if options[:create]
  @git = RubyGit.new(@jrepo)
end

Instance Attribute Details

#gitObject

Returns the value of attribute git.



35
36
37
# File 'lib/repo.rb', line 35

def git
  @git
end

#jrepoObject

Returns the value of attribute jrepo.



36
37
38
# File 'lib/repo.rb', line 36

def jrepo
  @jrepo
end

#pathObject

Returns the value of attribute path.



37
38
39
# File 'lib/repo.rb', line 37

def path
  @path
end

Class Method Details

.create(path, options = {:is_bare => false}) ⇒ Object



74
75
76
77
# File 'lib/repo.rb', line 74

def self.create(path, options = {:is_bare => false})
  options[:create] = true
  Repo.new(path, options)
end

.new_from_jgit_repo(jrepo) ⇒ Object



83
84
85
86
# File 'lib/repo.rb', line 83

def self.new_from_jgit_repo(jrepo)
  path = File.dirname(jrepo.get_directory.get_path)
  Repo.new(path, {:is_bare => jrepo.is_bare})
end

Instance Method Details

#add(file_pattern) ⇒ Object



150
151
152
# File 'lib/repo.rb', line 150

def add(file_pattern)
  @git.add(file_pattern)
end

#bare?Boolean Also known as: bare

Returns:

  • (Boolean)


69
70
71
# File 'lib/repo.rb', line 69

def bare?
  @jrepo.is_bare
end

#blob(file_path, revstring = Constants::HEAD) ⇒ Object

Convenience method to retrieve a Blob by name



203
204
205
# File 'lib/repo.rb', line 203

def blob(file_path, revstring=Constants::HEAD)
  Blob.find_blob(@jrepo, file_path, revstring)
end

#branchObject



105
106
107
# File 'lib/repo.rb', line 105

def branch
  @jrepo.get_full_branch
end

#branchesObject



109
110
111
# File 'lib/repo.rb', line 109

def branches
  return @git.branch_list
end

#checkout(branch_name, options = {}) ⇒ Object Also known as: switch



125
126
127
# File 'lib/repo.rb', line 125

def checkout(branch_name, options = {})
  @git.checkout(branch_name, options)
end

#clean(options = {}) ⇒ Object



162
163
164
# File 'lib/repo.rb', line 162

def clean(options = {})
  @git.clean(options)
end

#commit(message, options = {}) ⇒ Object



158
159
160
# File 'lib/repo.rb', line 158

def commit(message, options = {})
  @git.commit(message, options)
end

#commits(ref = "master", limit = 100) ⇒ Object



88
89
90
91
# File 'lib/repo.rb', line 88

def commits(ref="master", limit=100)
  options = { :limit => limit }
  Commit.find_all(@jrepo, ref, options)
end

#configObject



101
102
103
# File 'lib/repo.rb', line 101

def config
  @config.load
end

#create!Object



79
80
81
# File 'lib/repo.rb', line 79

def create!
  @jrepo.create(self.bare?)
end

#create_branch(name) ⇒ Object



113
114
115
# File 'lib/repo.rb', line 113

def create_branch(name)
  @git.create_branch(name)
end

#delete_branch(name) ⇒ Object



117
118
119
# File 'lib/repo.rb', line 117

def delete_branch(name)
  @git.delete_branch(name)
end

#find(sha, type = :any) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/repo.rb', line 166

def find(sha, type = :any)
    begin
      oi = ObjectId.from_string(sha)
      walk = RevWalk.new(@jrepo)
      rev_object = case type
      when :any
        walk.parse_any(oi)
      when :tree
        walk.parse_tree(oi)
      when :blob
        walk.parse_any(oi)
      when :tag
        walk.parse_tag(oi)
      when :commit
        walk.parse_commit(oi)
      else nil
      end
    rescue Java::OrgEclipseJgitErrors::MissingObjectException, Java::JavaLang::IllegalArgumentException, Java::JavaLang::NullPointerException
      return nil
    end
  return nil if rev_object.nil?
  object_type = (type == :any || type == :blob) ? RJGit.sym_for_type(rev_object.get_type) : type
  return nil if type == :blob && object_type != :blob # Blobs need to be found with parse_any, so make sure that the result of this is actually a blob.
    return case object_type
    when :tree
      Tree.new(@jrepo, nil, nil, rev_object)
    when :blob
      mode = RJGit.get_file_mode(@jrepo, rev_object)
      Blob.new(@jrepo, mode, nil, rev_object)
    when :tag
      Tag.new(rev_object)
    when :commit
      Commit.new(jrepo, rev_object)
    end
end

#headObject



93
94
95
# File 'lib/repo.rb', line 93

def head
  Commit.find_head(@jrepo)
end

#remove(file_pattern) ⇒ Object



154
155
156
# File 'lib/repo.rb', line 154

def remove(file_pattern)
  @git.remove(file_pattern)
end

#rename_branch(old_name, new_name) ⇒ Object



121
122
123
# File 'lib/repo.rb', line 121

def rename_branch(old_name, new_name)
  @git.rename_branch(old_name, new_name)
end

#tags(lightweight = false) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/repo.rb', line 131

def tags(lightweight = false)
  jtags = @jrepo.get_tags.to_hash
  if lightweight
    jtags.each_with_object( Hash.new ) do |(key, value), hash| 
      hash[key] = ObjectId.to_string(value.get_object_id)
    end
  else
    tags = Hash.new
    jtags.each do |key, value|
      jtag = @git.resolve_tag(value)
      if jtag
        tag = Tag.new(jtag)
        tags[key] = tag 
      end
    end
    tags
  end
end

#tree(file_path, revstring = Constants::HEAD) ⇒ Object

Convenience method to retrieve a Tree by name



208
209
210
# File 'lib/repo.rb', line 208

def tree(file_path, revstring=Constants::HEAD)
  Tree.find_tree(@jrepo, file_path, revstring)
end

#update_ref(commit, force = false, ref = "refs/heads/#{Constants::MASTER}") ⇒ Object



212
213
214
215
216
217
# File 'lib/repo.rb', line 212

def update_ref(commit, force = false, ref = "refs/heads/#{Constants::MASTER}")
  ref_updater = @jrepo.updateRef(ref)
  ref_updater.setForceUpdate(force)
  ref_updater.setNewObjectId(RJGit.commit_type(commit))
  ref_updater.update.to_string
end

#update_server_infoObject

Update the info files required for fetching files over the dumb-HTTP protocol



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/repo.rb', line 220

def update_server_info
  # First update the $GIT_DIR/refs/info file
  refs = @jrepo.get_all_refs # Note: JGit will include directories under $GIT_DIR/refs/heads that start with a '.' in its search for refs. Filter these out in LocalRefWriter?
  writer = LocalRefWriter.new(refs, @path)
  writer.write_info_refs
  
  # Now update the $GIT_OBJECT_DIRECTORY/info/packs file
  f = File.new(File.join(@path, PACK_LIST), "w")
  @jrepo.get_object_database.get_packs.each do |pack|
    f.write "P " + pack.get_pack_file.get_name + "\n"
  end
  f.write "\n"
  f.close
  
  return true
end

#valid?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/repo.rb', line 97

def valid?
  @jrepo.get_object_database.exists
end