1 == Git Library for Ruby
3 Library for using Git in Ruby.
7 The Ruby/Git homepage is currently at :
9 http://jointheconversation.org/rubygit
11 Git public hosting of the project source code is at:
13 http://repo.or.cz/w/rubygit.git
17 Right now I'm forking calls to the 'git' binary,
18 but eventually I'll replace that with either C bindings
19 to libgit or libgit-thin, or I'll write pure ruby
20 handlers for at least some of the Git stuff.
25 Here are a bunch of examples of how to use the Ruby/Git package.
27 First you have to remember to require rubygems if it's not. Then include the 'git' gem.
32 Here are the operations that need read permission only.
34 g = Git.open (working_dir = '.')
43 g.log # returns array of Git::Commit objects
44 g.log.since('2 weeks ago')
45 g.log.between('v2.5', 'v2.6')
46 g.log.each {|l| puts l.sha }
47 g.gblob('v2.5:Makefile').log.since('2 weeks ago')
49 g.object('HEAD^').to_s # git show / git rev-parse
50 g.object('HEAD^').contents
51 g.object('v2.5:Makefile').size
52 g.object('v2.5:Makefile').sha
59 commit = g.gcommit('1cc8667014381')
65 commit.author.date.strftime("%m-%d-%y")
67 commit.date.strftime("%m-%d-%y")
70 tree = g.gtree("HEAD^{tree}")
73 tree.children # blobs and subtrees
75 g.revparse('v2.5:Makefile')
77 g.branches # returns Git::Branch objects
80 g.branches[:master].gcommit
81 g.branches['origin/master'].gcommit
83 g.grep('hello') # implies HEAD
84 g.blob('v2.5:Makefile').grep('hello')
85 g.tag('v2.5').grep('hello', 'docs/')
87 g.diff(commit1, commit2).size
88 g.diff(commit1, commit2).stats
89 g.gtree('v2.5').diff('v2.6').insertions
90 g.diff('gitsearch1', 'v2.5').path('lib/')
91 g.diff('gitsearch1', @git.gtree('v2.5'))
92 g.diff('gitsearch1', 'v2.5').path('docs/').patch
93 g.gtree('v2.5').diff('v2.6').patch
95 g.gtree('v2.5').diff('v2.6').each do |file_diff|
98 puts file_diff.blob(:src).contents
101 g.config('user.name') # returns 'Scott Chacon'
102 g.config # returns whole config hash
104 g.tag # returns array of Git::Tag objects
108 And here are the operations that will need to write to your git repository.
113 Git.init('/home/schacon/proj',
114 { :git_dir => '/opt/git/proj.git',
115 :index_file => '/tmp/index'} )
117 g = Git.clone(URI, :name => 'name', :path => '/tmp/checkout')
118 g.config('user.name', 'Scott Chacon')
119 g.config('user.email', 'email@email.com')
122 g.add([file1, file2])
125 g.remove(['file.txt', 'file2.txt'])
128 g.commit_all('message')
130 g = Git.clone(repo, 'myrepo')
132 new_file('test-file', 'blahblahblah')
133 g.status.changed.each do |file|
134 puts file.blob(:index).contents
138 g.reset # defaults to HEAD
139 g.reset_hard(Git::Commit)
141 g.branch('new_branch') # creates new or fetches existing
142 g.branch('new_branch').checkout
143 g.branch('new_branch').delete
144 g.branch('existing_branch').checkout
146 g.checkout('new_branch')
147 g.checkout(g.branch('new_branch'))
149 g.branch(name).merge(branch2)
150 g.branch(branch2).merge # merges HEAD with branch2
152 g.branch(name).in_branch(message) { # add files } # auto-commits
153 g.merge('new_branch')
154 g.merge('origin/remote_branch')
155 g.merge(b.branch('master'))
156 g.merge([branch1, branch2])
158 r = g.add_remote(name, uri) # Git::Remote
159 r = g.add_remote(name, Git::Base) # Git::Remote
161 g.remotes # array of Git::Remotes
163 g.remote(name).remove
165 g.remote(name).merge(branch)
168 g.fetch(g.remotes.first)
171 g.pull(Git::Repo, Git::Branch) # fetch and a merge
173 g.add_tag('tag_name') # returns Git::Tag
178 g.push(g.remote('name'))