File: README

package info (click to toggle)
libgit-ruby 1.0.7-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 3,652 kB
  • ctags: 771
  • sloc: ruby: 3,211; sh: 226; makefile: 19
file content (244 lines) | stat: -rw-r--r-- 6,691 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
== Git Library for Ruby

Library for using Git in Ruby.

= Homepage

The Ruby/Git homepage is currently at : 

http://jointheconversation.org/rubygit

Git public hosting of the project source code is at:

http://github/schacon/ruby-git

= Install

You can install Ruby/Git like this:

$ sudo gem install schacon-git --source=http://gems.github.com

= Major Objects

Git::Base - this is the object returned from a Git.open or Git.clone.  
Most major actions are called from this object.

Git::Object - this is the base object for your tree, blob and commit objects,
returned from @git.gtree or @git.object calls.  the Git::AbstractObject will
have most of the calls in common for all those objects.

Git::Diff - returns from a @git.diff command.  It is an Enumerable that returns
Git::Diff:DiffFile objects from which you can get per file patches and insertion/deletion
statistics.  You can also get total statistics from the Git::Diff object directly.

Git::Status - returns from a @git.status command.  It is an Enumerable that returns
Git:Status::StatusFile objects for each object in git, which includes files in the working
directory, in the index and in the repository.  Similar to running 'git status' on the command
line to determine untracked and changed files.

Git::Branches - Enumerable object that holds Git::Branch objects.  You can call .local or .remote
on it to filter to just your local or remote branches.

Git::Remote - A reference to a remote repository that is tracked by this repository.

Git::Log - An Enumerable object that references all the Git::Object::Commit objects that encompass
your log query, which can be constructed through methods on the Git::Log object, like:

 @git.log(20).object("some_file").since("2 weeks ago").between('v2.6', 'v2.7').each { |commit| [block] } 

= Examples

Here are a bunch of examples of how to use the Ruby/Git package. 

First you have to remember to require rubygems if it's not.  Then include the 'git' gem.

   require 'rubygems'
   require 'git'

Here are the operations that need read permission only.
      
   g = Git.open (working_dir, :log => Logger.new(STDOUT))
   
   g.index
   g.index.readable?
   g.index.writable?
   g.repo
   g.dir
   
   g.log   # returns array of Git::Commit objects
   g.log.since('2 weeks ago')
   g.log.between('v2.5', 'v2.6')
   g.log.each {|l| puts l.sha }
   g.gblob('v2.5:Makefile').log.since('2 weeks ago')
   
   g.object('HEAD^').to_s  # git show / git rev-parse
   g.object('HEAD^').contents
   g.object('v2.5:Makefile').size
   g.object('v2.5:Makefile').sha
   
   g.gtree(treeish)
   g.gblob(treeish)
   g.gcommit(treeish)
   
   
   commit = g.gcommit('1cc8667014381')
    commit.gtree
    commit.parent.sha
    commit.parents.size
    commit.author.name
    commit.author.email
    commit.author.date.strftime("%m-%d-%y")
    commit.committer.name
    commit.date.strftime("%m-%d-%y")
    commit.message
   
  tree = g.gtree("HEAD^{tree}")
    tree.blobs
    tree.subtrees
    tree.children # blobs and subtrees
   
   g.revparse('v2.5:Makefile')
   
   g.branches # returns Git::Branch objects
   g.branches.local
   g.branches.remote
   g.branches[:master].gcommit
   g.branches['origin/master'].gcommit
   
   g.grep('hello')  # implies HEAD
   g.blob('v2.5:Makefile').grep('hello')
   g.tag('v2.5').grep('hello', 'docs/')
   
   g.diff(commit1, commit2).size
   g.diff(commit1, commit2).stats
   g.gtree('v2.5').diff('v2.6').insertions
   g.diff('gitsearch1', 'v2.5').path('lib/')
   g.diff('gitsearch1', @git.gtree('v2.5'))
   g.diff('gitsearch1', 'v2.5').path('docs/').patch
   g.gtree('v2.5').diff('v2.6').patch
   
   g.gtree('v2.5').diff('v2.6').each do |file_diff|
     puts file_diff.path
     puts file_diff.patch
     puts file_diff.blob(:src).contents
   end
   
   g.config('user.name')  # returns 'Scott Chacon'
   g.config # returns whole config hash
   
   g.tag # returns array of Git::Tag objects
   
   
   
And here are the operations that will need to write to your git repository.
   
   
   g = Git.init
     Git.init('project')
     Git.init('/home/schacon/proj', 
	 	  { :git_dir => '/opt/git/proj.git', 
		     :index_file => '/tmp/index'} )
 	  
   g = Git.clone(URI, :name => 'name', :path => '/tmp/checkout')   
   g.config('user.name', 'Scott Chacon')
   g.config('user.email', 'email@email.com')      
   
   g.add('.')
   g.add([file1, file2])
   
   g.remove('file.txt')
   g.remove(['file.txt', 'file2.txt'])
		     
   g.commit('message')
   g.commit_all('message')
   
   g = Git.clone(repo, 'myrepo')
   g.chdir do
    new_file('test-file', 'blahblahblah')
    g.status.changed.each do |file|
     puts file.blob(:index).contents
    end
   end
   
   g.reset # defaults to HEAD
   g.reset_hard(Git::Commit)
   
   g.branch('new_branch') # creates new or fetches existing
   g.branch('new_branch').checkout
   g.branch('new_branch').delete
   g.branch('existing_branch').checkout
   
   g.checkout('new_branch')
   g.checkout(g.branch('new_branch'))
   
   g.branch(name).merge(branch2)
   g.branch(branch2).merge  # merges HEAD with branch2
   
   g.branch(name).in_branch(message) { # add files }  # auto-commits
   g.merge('new_branch')
   g.merge('origin/remote_branch')
   g.merge(b.branch('master'))
   g.merge([branch1, branch2])
   
   r = g.add_remote(name, uri)  # Git::Remote
   r = g.add_remote(name, Git::Base)  # Git::Remote
   
   g.remotes  # array of Git::Remotes
   g.remote(name).fetch
   g.remote(name).remove
   g.remote(name).merge
   g.remote(name).merge(branch)
   
   g.fetch
   g.fetch(g.remotes.first)
   
   g.pull
   g.pull(Git::Repo, Git::Branch) # fetch and a merge
   
   g.add_tag('tag_name') # returns Git::Tag
   
   g.repack

   g.push
   g.push(g.remote('name'))


Some examples of more low-level index and tree operations

   g.with_temp_index do 

     g.read_tree(tree3) # calls self.index.read_tree
     g.read_tree(tree1, :prefix => 'hi/')

     c = g.commit_tree('message')
     # or #
     t = g.write_tree
     c = g.commit_tree(t, :message => 'message', :parents => [sha1, sha2])

     g.branch('branch_name').update_ref(c)
     g.update_ref(branch, c)

     g.with_temp_working do # new blank working directory
       g.checkout
       g.checkout(another_index)
       g.commit # commits to temp_index
     end    
   end

   g.set_index('/path/to/index')
   
   
   g.with_index(path) do 
     # calls set_index, then switches back after
   end
   
   g.with_working(dir) do
   # calls set_working, then switches back after
   end
   
   g.with_temp_working(dir) do
     g.checkout_index(:prefix => dir, :path_limiter => path)
     # do file work
     g.commit # commits to index
   end