5 @working_directory = nil
9 # opens a bare Git Repository - no working directory options
10 def self.bare(git_dir)
11 self.new :repository => git_dir
14 # opens a new Git Project from a working directory
15 # you can specify non-standard git_dir and index file in the options
16 def self.open(working_dir, opts={})
17 default = {:working_directory => working_dir}
18 git_options = default.merge(opts)
23 # initializes a git repository
29 def self.init(working_dir, opts = {})
30 default = {:working_directory => working_dir,
31 :repository => File.join(working_dir, '.git')}
32 git_options = default.merge(opts)
34 if git_options[:working_directory]
35 # if !working_dir, make it
36 FileUtils.mkdir_p(git_options[:working_directory]) if !File.directory?(git_options[:working_directory])
40 Git::Lib.new(git_options).init
45 # clones a git repository locally
47 # repository - http://repo.or.cz/w/sinatra.git
58 def self.clone(repository, name, opts = {})
60 self.new(Git::Lib.new.clone(repository, name, opts))
63 def initialize(options = {})
64 if working_dir = options[:working_directory]
65 options[:repository] = File.join(working_dir, '.git') if !options[:repository]
66 options[:index] = File.join(working_dir, '.git', 'index') if !options[:index]
69 @working_directory = Git::WorkingDirectory.new(options[:working_directory]) if options[:working_directory]
70 @repository = Git::Repository.new(options[:repository]) if options[:repository]
71 @index = Git::Index.new(options[:index], false) if options[:index]
75 # returns a reference to the working directory
82 # returns reference to the git repository directory
88 # returns reference to the git index file
94 def set_working(work_dir, check = true)
96 @working_directory = Git::WorkingDirectory.new(work_dir.to_s, check)
99 def set_index(index_file, check = true)
101 @index = Git::Index.new(index_file.to_s, check)
104 # changes current working directory for a block
105 # to the git working directory
111 # @git.commit('message')
114 Dir.chdir(dir.path) do
119 # returns the repository size in bytes
122 Dir.chdir(repo.path) do
123 (size, dot) = `du -d0`.chomp.split
128 #g.config('user.name', 'Scott Chacon') # sets value
129 #g.config('user.email', 'email@email.com') # sets value
130 #g.config('user.name') # returns 'Scott Chacon'
131 #g.config # returns whole config hash
132 def config(name = nil, value = nil)
135 lib.config_set(name, value)
147 # returns a Git::Object of the appropriate type
148 # you can also call @git.gtree('tree'), but that's
149 # just for readability. If you call @git.gtree('HEAD') it will
150 # still return a Git::Object::Commit object.
152 # @git.object calls a factory method that will run a rev-parse
153 # on the objectish and determine the type of the object and return
154 # an appropriate object for that type
155 def object(objectish)
156 Git::Object.new(self, objectish)
160 Git::Object.new(self, objectish, 'tree')
163 def gcommit(objectish)
164 Git::Object.new(self, objectish, 'commit')
168 Git::Object.new(self, objectish, 'blob')
171 # returns a Git::Log object with count commits
173 Git::Log.new(self, count)
176 # returns a Git::Status object
178 Git::Status.new(self)
181 # returns a Git::Branches object of all the Git::Branch objects for this repo
183 Git::Branches.new(self)
186 # returns a Git::Branch object for branch_name
187 def branch(branch_name = 'master')
188 Git::Branch.new(self, branch_name)
191 # returns a Git::Remote object
192 def remote(remote_name = 'origin')
193 Git::Remote.new(self, remote_name)
196 # this is a convenience method for accessing the class that wraps all the
197 # actual 'git' forked system calls. At some point I hope to replace the Git::Lib
198 # class with one that uses native methods or libgit C bindings
200 @lib ||= Git::Lib.new(self)
203 # will run a grep for 'string' on the HEAD of the git repository
205 # to be more surgical in your grep, you can call grep() off a specific
206 # git object. for example:
208 # @git.object("v2.3").grep('TODO')
210 # in any case, it returns a hash of arrays of the type:
211 # hsh[tree-ish] = [[line_no, match], [line_no, match2]]
212 # hsh[tree-ish] = [[line_no, match], [line_no, match2]]
214 # so you might use it like this:
216 # @git.grep("TODO").each do |sha, arr|
217 # puts "in blob #{sha}:"
218 # arr.each do |match|
219 # puts "\t line #{match[0]}: '#{match[1]}'"
223 self.object('HEAD').grep(string)
226 # returns a Git::Diff object
227 def diff(objectish = 'HEAD', obj2 = nil)
228 Git::Diff.new(self, objectish, obj2)
231 # adds files from the working directory to the git repository
236 # removes file(s) from the git repository
237 def remove(path = '.', opts = {})
238 self.lib.remove(path, opts)
241 # resets the working directory to the provided commitish
242 def reset(commitish = nil, opts = {})
243 self.lib.reset(commitish, opts)
246 # resets the working directory to the commitish with '--hard'
247 def reset_hard(commitish = nil, opts = {})
248 opts = {:hard => true}.merge(opts)
249 self.lib.reset(commitish, opts)
252 # commits all pending changes in the index file to the git repository
253 def commit(message, opts = {})
254 self.lib.commit(message, opts)
257 # commits all pending changes in the index file to the git repository,
258 # but automatically adds all modified files without having to explicitly
259 # calling @git.add() on them.
260 def commit_all(message, opts = {})
261 opts = {:add_all => true}.merge(opts)
262 self.lib.commit(message, opts)
265 # checks out a branch as the new git working directory
266 def checkout(branch = 'master', opts = {})
267 self.lib.checkout(branch, opts)
270 # fetches changes from a remote branch - this does not modify the working directory,
271 # it just gets the changes from the remote if there are any
272 def fetch(remote = 'origin')
273 self.lib.fetch(remote)
276 # pushes changes to a remote repository - easiest if this is a cloned repository,
277 # otherwise you may have to run something like this first to setup the push parameters:
279 # @git.config('remote.remote-name.push', 'refs/heads/master:refs/heads/master')
281 def push(remote = 'origin', branch = 'master')
282 self.lib.push(remote, branch)
285 # merges one or more branches into the current working branch
287 # you can specify more than one branch to merge by passing an array of branches
288 def merge(branch, message = 'merge')
289 self.lib.merge(branch, message)
292 # fetches a branch from a remote and merges it into the current working branch
293 def pull(remote = 'origin', branch = 'master', message = 'origin pull')
295 merge(branch, message)
298 # returns an array of Git:Remote objects
300 self.lib.remotes.map { |r| Git::Remote.new(self, r) }
303 # adds a new remote to this repository
304 # url can be a git url or a Git::Base object if it's a local reference
306 # @git.add_remote('scotts_git', 'git://repo.or.cz/rubygit.git')
307 # @git.fetch('scotts_git')
308 # @git.merge('scotts_git/master')
310 def add_remote(name, url, opts = {})
311 if url.is_a?(Git::Base)
314 self.lib.remote_add(name, url, opts)
315 Git::Remote.new(self, name)
318 # returns an array of all Git::Tag objects for this repository
320 self.lib.tags.map { |r| tag(r) }
323 # returns a Git::Tag object
325 Git::Object.new(self, tag_name, 'tag', true)
328 # creates a new git tag (Git::Tag)
329 def add_tag(tag_name)
330 self.lib.tag(tag_name)
334 # creates an archive file of the given tree-ish
335 def archive(treeish, file = nil, opts = {})
336 self.object(treeish).archive(file, opts)
339 # repacks the repository
345 ## LOWER LEVEL INDEX OPERATIONS ##
347 def with_index(new_index)
349 set_index(new_index, false)
350 return_value = yield @index
355 def with_temp_index &blk
356 tempfile = Tempfile.new('temp-index')
357 temp_path = tempfile.path
359 with_index(temp_path, &blk)
362 def read_tree(treeish, opts = {})
363 self.lib.read_tree(treeish, opts)
370 def commit_tree(tree = nil, opts = {})
371 Git::Object::Commit.new(self, self.lib.commit_tree(tree, opts))
374 def write_and_commit_tree(opts = {})
376 commit_tree(tree, opts)
383 def with_working(work_dir)
385 old_working = @working_directory
386 set_working(work_dir)
387 Dir.chdir work_dir do
388 return_value = yield @working_directory
390 set_working(old_working)
394 def with_temp_working &blk
395 tempfile = Tempfile.new("temp-workdir")
396 temp_dir = tempfile.path
398 Dir.mkdir(temp_dir, 0700)
399 with_working(temp_dir, &blk)
403 # runs git rev-parse to convert the objectish to a full sha
405 # @git.revparse("HEAD^^")
406 # @git.revparse('v2.4^{tree}')
407 # @git.revparse('v2.4:/doc/index.html')
409 def revparse(objectish)
410 self.lib.revparse(objectish)
413 # returns the name of the branch the working directory is currently on
415 self.lib.branch_current