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
93 # changes current working directory for a block
94 # to the git working directory
100 # @git.commit('message')
103 Dir.chdir(dir.path) do
108 # returns the repository size in bytes
111 Dir.chdir(repo.path) do
112 (size, dot) = `du -d0`.chomp.split
117 #g.config('user.name', 'Scott Chacon') # sets value
118 #g.config('user.email', 'email@email.com') # sets value
119 #g.config('user.name') # returns 'Scott Chacon'
120 #g.config # returns whole config hash
121 def config(name = nil, value = nil)
124 lib.config_set(name, value)
136 # returns a Git::Object of the appropriate type
137 # you can also call @git.gtree('tree'), but that's
138 # just for readability. If you call @git.gtree('HEAD') it will
139 # still return a Git::Object::Commit object.
141 # @git.object calls a factory method that will run a rev-parse
142 # on the objectish and determine the type of the object and return
143 # an appropriate object for that type
144 def object(objectish)
145 Git::Object.new(self, objectish)
149 Git::Object.new(self, objectish, 'tree')
152 def gcommit(objectish)
153 Git::Object.new(self, objectish, 'commit')
157 Git::Object.new(self, objectish, 'blob')
160 # returns a Git::Log object with count commits
162 Git::Log.new(self, count)
165 # returns a Git::Status object
167 Git::Status.new(self)
170 # returns a Git::Branches object of all the Git::Branch objects for this repo
172 Git::Branches.new(self)
175 # returns a Git::Branch object for branch_name
176 def branch(branch_name = 'master')
177 Git::Branch.new(self, branch_name)
180 # returns a Git::Remote object
181 def remote(remote_name = 'origin')
182 Git::Remote.new(self, remote_name)
185 # this is a convenience method for accessing the class that wraps all the
186 # actual 'git' forked system calls. At some point I hope to replace the Git::Lib
187 # class with one that uses native methods or libgit C bindings
192 # will run a grep for 'string' on the HEAD of the git repository
194 # to be more surgical in your grep, you can call grep() off a specific
195 # git object. for example:
197 # @git.object("v2.3").grep('TODO')
199 # in any case, it returns a hash of arrays of the type:
200 # hsh[tree-ish] = [[line_no, match], [line_no, match2]]
201 # hsh[tree-ish] = [[line_no, match], [line_no, match2]]
203 # so you might use it like this:
205 # @git.grep("TODO").each do |sha, arr|
206 # puts "in blob #{sha}:"
207 # arr.each do |match|
208 # puts "\t line #{match[0]}: '#{match[1]}'"
212 self.object('HEAD').grep(string)
215 # returns a Git::Diff object
216 def diff(objectish = 'HEAD', obj2 = nil)
217 Git::Diff.new(self, objectish, obj2)
220 # adds files from the working directory to the git repository
225 # removes file(s) from the git repository
226 def remove(path = '.', opts = {})
227 self.lib.remove(path, opts)
230 # resets the working directory to the provided commitish
231 def reset(commitish = nil, opts = {})
232 self.lib.reset(commitish, opts)
235 # resets the working directory to the commitish with '--hard'
236 def reset_hard(commitish = nil, opts = {})
237 opts = {:hard => true}.merge(opts)
238 self.lib.reset(commitish, opts)
241 # commits all pending changes in the index file to the git repository
242 def commit(message, opts = {})
243 self.lib.commit(message, opts)
246 # commits all pending changes in the index file to the git repository,
247 # but automatically adds all modified files without having to explicitly
248 # calling @git.add() on them.
249 def commit_all(message, opts = {})
250 opts = {:add_all => true}.merge(opts)
251 self.lib.commit(message, opts)
254 # checks out a branch as the new git working directory
255 def checkout(branch = 'master', opts = {})
256 self.lib.checkout(branch, opts)
259 # fetches changes from a remote branch - this does not modify the working directory,
260 # it just gets the changes from the remote if there are any
261 def fetch(remote = 'origin')
262 self.lib.fetch(remote)
265 # pushes changes to a remote repository - easiest if this is a cloned repository,
266 # otherwise you may have to run something like this first to setup the push parameters:
268 # @git.config('remote.remote-name.push', 'refs/heads/master:refs/heads/master')
270 def push(remote = 'origin', branch = 'master')
271 self.lib.push(remote, branch)
274 # merges one or more branches into the current working branch
276 # you can specify more than one branch to merge by passing an array of branches
277 def merge(branch, message = 'merge')
278 self.lib.merge(branch, message)
281 # fetches a branch from a remote and merges it into the current working branch
282 def pull(remote = 'origin', branch = 'master', message = 'origin pull')
284 merge(branch, message)
287 # returns an array of Git:Remote objects
289 self.lib.remotes.map { |r| Git::Remote.new(self, r) }
292 # adds a new remote to this repository
293 # url can be a git url or a Git::Base object if it's a local reference
295 # @git.add_remote('scotts_git', 'git://repo.or.cz/rubygit.git')
296 # @git.fetch('scotts_git')
297 # @git.merge('scotts_git/master')
299 def add_remote(name, url, opts = {})
300 if url.is_a?(Git::Base)
303 self.lib.remote_add(name, url, opts)
304 Git::Remote.new(self, name)
307 # returns an array of all Git::Tag objects for this repository
309 self.lib.tags.map { |r| tag(r) }
312 # returns a Git::Tag object
314 Git::Object.new(self, tag_name, 'tag', true)
317 # creates a new git tag (Git::Tag)
318 def add_tag(tag_name)
319 self.lib.tag(tag_name)
323 # creates an archive file of the given tree-ish
324 def archive(treeish, file = nil, opts = {})
325 self.object(treeish).archive(file, opts)
328 # repacks the repository
333 # runs git rev-parse to convert the objectish to a full sha
335 # @git.revparse("HEAD^^")
336 # @git.revparse('v2.4^{tree}')
337 # @git.revparse('v2.4:/doc/index.html')
339 def revparse(objectish)
340 self.lib.revparse(objectish)
343 # returns the name of the branch the working directory is currently on
345 self.lib.branch_current