rdavila-rugged 0.24.0b13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +619 -0
- data/ext/rugged/extconf.rb +105 -0
- data/ext/rugged/rugged.c +527 -0
- data/ext/rugged/rugged.h +185 -0
- data/ext/rugged/rugged_backend.c +34 -0
- data/ext/rugged/rugged_blame.c +292 -0
- data/ext/rugged/rugged_blob.c +638 -0
- data/ext/rugged/rugged_branch.c +195 -0
- data/ext/rugged/rugged_branch_collection.c +408 -0
- data/ext/rugged/rugged_commit.c +691 -0
- data/ext/rugged/rugged_config.c +404 -0
- data/ext/rugged/rugged_cred.c +148 -0
- data/ext/rugged/rugged_diff.c +686 -0
- data/ext/rugged/rugged_diff_delta.c +105 -0
- data/ext/rugged/rugged_diff_hunk.c +103 -0
- data/ext/rugged/rugged_diff_line.c +83 -0
- data/ext/rugged/rugged_index.c +1255 -0
- data/ext/rugged/rugged_note.c +376 -0
- data/ext/rugged/rugged_object.c +383 -0
- data/ext/rugged/rugged_patch.c +245 -0
- data/ext/rugged/rugged_reference.c +396 -0
- data/ext/rugged/rugged_reference_collection.c +446 -0
- data/ext/rugged/rugged_remote.c +691 -0
- data/ext/rugged/rugged_remote_collection.c +457 -0
- data/ext/rugged/rugged_repo.c +2669 -0
- data/ext/rugged/rugged_revwalk.c +495 -0
- data/ext/rugged/rugged_settings.c +155 -0
- data/ext/rugged/rugged_signature.c +106 -0
- data/ext/rugged/rugged_submodule.c +852 -0
- data/ext/rugged/rugged_submodule_collection.c +384 -0
- data/ext/rugged/rugged_tag.c +251 -0
- data/ext/rugged/rugged_tag_collection.c +347 -0
- data/ext/rugged/rugged_tree.c +919 -0
- data/lib/rugged.rb +23 -0
- data/lib/rugged/attributes.rb +41 -0
- data/lib/rugged/blob.rb +28 -0
- data/lib/rugged/branch.rb +19 -0
- data/lib/rugged/commit.rb +54 -0
- data/lib/rugged/console.rb +9 -0
- data/lib/rugged/credentials.rb +43 -0
- data/lib/rugged/diff.rb +20 -0
- data/lib/rugged/diff/delta.rb +53 -0
- data/lib/rugged/diff/hunk.rb +18 -0
- data/lib/rugged/diff/line.rb +47 -0
- data/lib/rugged/index.rb +13 -0
- data/lib/rugged/object.rb +7 -0
- data/lib/rugged/patch.rb +36 -0
- data/lib/rugged/reference.rb +7 -0
- data/lib/rugged/remote.rb +4 -0
- data/lib/rugged/repository.rb +227 -0
- data/lib/rugged/submodule_collection.rb +48 -0
- data/lib/rugged/tag.rb +50 -0
- data/lib/rugged/tree.rb +38 -0
- data/lib/rugged/version.rb +3 -0
- data/lib/rugged/walker.rb +5 -0
- metadata +146 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 13b0e41bb437817471d90635aa64acc108c5f061
|
4
|
+
data.tar.gz: b8d6955478d409db23e83b0711e9889db9e4b435
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c3fea886e3c30e4cb90e498d6095ae1ac042487d89594f5fcbd68423d61ea8e62b32f08d1a1b4f5e47169c5b68b3884a3e9ebd712252f2255d032ac3968d246a
|
7
|
+
data.tar.gz: fe37765778cbf8b6bcd00754cec7f62092f73ee975951556dcb1c80461cf926f572761c83cec1efe2e127b8d4d107e6d30320e650a15e6010e2a21b462363b24
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2015 GitHub, Inc
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,619 @@
|
|
1
|
+
# Rugged
|
2
|
+
**libgit2 bindings in Ruby**
|
3
|
+
|
4
|
+
Rugged is a library for accessing [libgit2](https://github.com/libgit2/libgit2) in Ruby. It gives you the speed and
|
5
|
+
portability of libgit2 with the beauty of the Ruby language.
|
6
|
+
|
7
|
+
### libgit2
|
8
|
+
|
9
|
+
libgit2 is a pure C implementation of the Git core methods. It's designed to be
|
10
|
+
fast and portable. For more information about libgit2,
|
11
|
+
[check out libgit2's website](http://libgit2.github.com) or browse the
|
12
|
+
[libgit2 organization](https://github.com/libgit2) on GitHub.
|
13
|
+
|
14
|
+
## Install
|
15
|
+
|
16
|
+
Rugged is a self-contained gem. You can install it by running:
|
17
|
+
|
18
|
+
$ gem install rugged
|
19
|
+
|
20
|
+
You need to have CMake and `pkg-config` installed on your system to be able to build the included version of `libgit2`. On OS X, after installing [Homebrew](http://brew.sh/), you can get CMake with:
|
21
|
+
```bash
|
22
|
+
$ brew install cmake
|
23
|
+
```
|
24
|
+
|
25
|
+
If you want to build Rugged with HTTPS and SSH support, check out the list of optional [libgit2 dependencies](https://github.com/libgit2/libgit2#optional-dependencies).
|
26
|
+
|
27
|
+
If you're using bundler and want to bundle `libgit2` with Rugged, you can use the `:submodules` option:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem 'rugged', git: 'git://github.com/libgit2/rugged.git', submodules: true
|
31
|
+
```
|
32
|
+
|
33
|
+
To load Rugged, you'll usually want to add something like this:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'rugged'
|
37
|
+
```
|
38
|
+
|
39
|
+
### Use the system provided libgit2
|
40
|
+
|
41
|
+
By default, Rugged builds and uses a bundled version of libgit2. If you
|
42
|
+
want to use the system library instead, you can install rugged as follows:
|
43
|
+
|
44
|
+
```
|
45
|
+
gem install rugged -- --use-system-libraries
|
46
|
+
```
|
47
|
+
|
48
|
+
Or if you are using bundler:
|
49
|
+
|
50
|
+
```
|
51
|
+
bundle config build.rugged --use-system-libraries
|
52
|
+
bundle install
|
53
|
+
```
|
54
|
+
|
55
|
+
However, note that Rugged does only support specific versions of libgit2.
|
56
|
+
|
57
|
+
## Usage
|
58
|
+
|
59
|
+
Rugged gives you access to the many parts of a Git repository. You can read and
|
60
|
+
write objects, walk a tree, access the staging area, and lots more. Let's look
|
61
|
+
at each area individually.
|
62
|
+
|
63
|
+
### Repositories
|
64
|
+
|
65
|
+
#### Instantiation
|
66
|
+
|
67
|
+
The repository is naturally central to Git. Rugged has a `Repository` class that
|
68
|
+
you can instantiate with a path to open an existing repository :
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
repo = Rugged::Repository.new('path/to/my/repository')
|
72
|
+
# => #<Rugged::Repository:2228536260 {path: "path/to/my/repository/.git/"}>
|
73
|
+
```
|
74
|
+
|
75
|
+
You can create a new repository with `init_at`. Add a second parameter `:bare` to make a bare repository:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
Rugged::Repository.init_at('.', :bare)
|
79
|
+
```
|
80
|
+
|
81
|
+
You can also let Rugged discover the path to the .git directory if you give it a
|
82
|
+
subdirectory.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
Rugged::Repository.discover("/Users/me/projects/repo/lib/subdir/")
|
86
|
+
# => "/Users/me/projects/repo/.git/"
|
87
|
+
```
|
88
|
+
|
89
|
+
Once your Repository instantiated (in the following examples, as `repo`), you
|
90
|
+
can access or modify it.
|
91
|
+
|
92
|
+
#### Accessing a Repository
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
# Does the given SHA1 exist in this repository?
|
96
|
+
repo.exists?('07b44cbda23b726e5d54e2ef383495922c024202')
|
97
|
+
# => true
|
98
|
+
|
99
|
+
# Boolean repository state values:
|
100
|
+
repo.bare?
|
101
|
+
# => false
|
102
|
+
repo.empty?
|
103
|
+
# => true
|
104
|
+
repo.head_unborn?
|
105
|
+
# => false
|
106
|
+
repo.head_detached?
|
107
|
+
# => false
|
108
|
+
|
109
|
+
# Path accessors
|
110
|
+
repo.path
|
111
|
+
# => "path/to/my/repository/.git/"
|
112
|
+
repo.workdir
|
113
|
+
# => "path/to/my/repository/"
|
114
|
+
|
115
|
+
# The HEAD of the repository.
|
116
|
+
ref = repo.head
|
117
|
+
# => #<Rugged::Reference:2228467240 {name: "refs/heads/master", target: #<Rugged::Commit:2228467250 {message: "helpful message", tree: #<Rugged::Tree:2228467260 {oid: 5d6f29220a0783b8085134df14ec4d960b6c3bf2}>}>
|
118
|
+
|
119
|
+
# From the returned ref, you can also access the `name`, `target`, and target SHA:
|
120
|
+
ref.name
|
121
|
+
# => "refs/heads/master"
|
122
|
+
ref.target
|
123
|
+
# => #<Rugged::Commit:2228467250 {message: "helpful message", tree: #<Rugged::Tree:2228467260 {oid: 5d6f29220a0783b8085134df14ec4d960b6c3bf2}>}>
|
124
|
+
ref.target_id
|
125
|
+
# => "2bc6a70483369f33f641ca44873497f13a15cde5"
|
126
|
+
|
127
|
+
# Reading an object
|
128
|
+
object = repo.read('a0ae5566e3c8a3bddffab21022056f0b5e03ef07')
|
129
|
+
# => #<Rugged::OdbObject:0x109a64780>
|
130
|
+
object.len
|
131
|
+
# => 237
|
132
|
+
object.data
|
133
|
+
# => "tree 76f23f186076fc291742816721ea8c3e95567241\nparent 8e3c5c52b8f29da0adc7e8be8a037cbeaea6de6b\nauthor Vicent Mart\303\255 <tanoku@gmail.com> 1333859005 +0200\ncommitter Vicent Mart\303\255 <tanoku@gmail.com> 1333859005 +0200\n\nAdd `Repository#blob_at`\n"
|
134
|
+
object.type
|
135
|
+
# => :commit
|
136
|
+
```
|
137
|
+
|
138
|
+
#### Writing to a Repository
|
139
|
+
|
140
|
+
There's a few ways to write to a repository. To write directly from your
|
141
|
+
instantiated repository object:
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
sha = repo.write(content, type)
|
145
|
+
```
|
146
|
+
|
147
|
+
You can also use the `Commit` object directly to craft a commit; this is a bit
|
148
|
+
more high-level, so it may be preferable:
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
oid = repo.write("This is a blob.", :blob)
|
152
|
+
index = repo.index
|
153
|
+
index.read_tree(repo.head.target.tree)
|
154
|
+
index.add(:path => "README.md", :oid => oid, :mode => 0100644)
|
155
|
+
|
156
|
+
options = {}
|
157
|
+
options[:tree] = index.write_tree(repo)
|
158
|
+
|
159
|
+
options[:author] = { :email => "testuser@github.com", :name => 'Test Author', :time => Time.now }
|
160
|
+
options[:committer] = { :email => "testuser@github.com", :name => 'Test Author', :time => Time.now }
|
161
|
+
options[:message] ||= "Making a commit via Rugged!"
|
162
|
+
options[:parents] = repo.empty? ? [] : [ repo.head.target ].compact
|
163
|
+
options[:update_ref] = 'HEAD'
|
164
|
+
|
165
|
+
Rugged::Commit.create(repo, options)
|
166
|
+
```
|
167
|
+
|
168
|
+
---
|
169
|
+
|
170
|
+
### Objects
|
171
|
+
|
172
|
+
`Object` is the main object class - it shouldn't be created directly, but all of
|
173
|
+
these methods should be useful in their derived classes.
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
obj = repo.lookup(sha)
|
177
|
+
obj.oid # object sha
|
178
|
+
obj.type # One of :commit, :tree, :blob or :tag
|
179
|
+
|
180
|
+
robj = obj.read_raw
|
181
|
+
str = robj.data
|
182
|
+
int = robj.len
|
183
|
+
```
|
184
|
+
|
185
|
+
There are four base object types in Git: **blobs**, **commits**, **tags**, and
|
186
|
+
**trees**. Each of these object types have a corresponding class within Rugged.
|
187
|
+
|
188
|
+
### Commit Objects
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
commit = repo.lookup('a0ae5566e3c8a3bddffab21022056f0b5e03ef07')
|
192
|
+
# => #<Rugged::Commit:2245304380>
|
193
|
+
|
194
|
+
commit.message
|
195
|
+
# => "Add `Repository#blob_at`\n"
|
196
|
+
|
197
|
+
commit.time
|
198
|
+
# => Sat Apr 07 21:23:25 -0700 2012
|
199
|
+
|
200
|
+
commit.author
|
201
|
+
# => {:email=>"tanoku@gmail.com", :name=>"Vicent Mart\303\255", :time=>Sun Apr 08 04:23:25 UTC 2012}
|
202
|
+
|
203
|
+
commit.tree
|
204
|
+
# => #<Rugged::Tree:2245269740>
|
205
|
+
|
206
|
+
commit.parents
|
207
|
+
# => [#<Rugged::Commit:2245264600 {message: "Merge pull request #47 from isaac/remotes\n\nAdd Rugged::Repository#remotes", tree: #<Rugged::Tree:2245264240 {oid: 6a2aee58a41fa007d07aa55565e2231f9b39b4a9}>]
|
208
|
+
```
|
209
|
+
|
210
|
+
You can also write new objects to the database this way:
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
author = {:email=>"tanoku@gmail.com", :time=>Time.now, :name=>"Vicent Mart\303\255"}
|
214
|
+
|
215
|
+
Rugged::Commit.create(r,
|
216
|
+
:author => author,
|
217
|
+
:message => "Hello world\n\n",
|
218
|
+
:committer => author,
|
219
|
+
:parents => ["2cb831a8aea28b2c1b9c63385585b864e4d3bad1"],
|
220
|
+
:tree => some_tree,
|
221
|
+
:update_ref => "HEAD") #=> "f148106ca58764adc93ad4e2d6b1d168422b9796"
|
222
|
+
```
|
223
|
+
|
224
|
+
### Tag Objects
|
225
|
+
|
226
|
+
```ruby
|
227
|
+
tag = repo.lookup(tag_sha)
|
228
|
+
|
229
|
+
object = tag.target
|
230
|
+
sha = tag.target.oid
|
231
|
+
str = tag.target_type # :commit, :tag, :blob
|
232
|
+
str = tag.name # "v1.0"
|
233
|
+
str = tag.message
|
234
|
+
person = tag.tagger
|
235
|
+
```
|
236
|
+
|
237
|
+
### Tree Objects
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
tree = repo.lookup('779fbb1e17e666832773a9825875300ea736c2da')
|
241
|
+
# => #<Rugged::Tree:2245194360>
|
242
|
+
|
243
|
+
# number of tree entries
|
244
|
+
tree.count
|
245
|
+
|
246
|
+
tree[0] # or...
|
247
|
+
tree.first # or...
|
248
|
+
tree.get_entry(0)
|
249
|
+
# => {:type=>:blob, :oid=>"99e7edb53db9355f10c6f2dfaa5a183f205d93bf", :filemode=>33188, :name=>".gitignore"}
|
250
|
+
```
|
251
|
+
|
252
|
+
The tree object is an Enumerable, so you can also do stuff like this:
|
253
|
+
|
254
|
+
```ruby
|
255
|
+
tree.each { |e| puts e[:oid] }
|
256
|
+
tree.sort { |a, b| a[:oid] <=> b[:oid] }.map { |e| e[:name] }.join(':')
|
257
|
+
```
|
258
|
+
|
259
|
+
And there are some Rugged-specific methods, too:
|
260
|
+
|
261
|
+
```ruby
|
262
|
+
tree.each_tree { |entry| puts entry[:name] } # list subdirs
|
263
|
+
tree.each_blob { |entry| puts entry[:name] } # list only files
|
264
|
+
```
|
265
|
+
|
266
|
+
You can also write trees with the `TreeBuilder`:
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
oid = repo.write("This is a blob.", :blob)
|
270
|
+
builder = Rugged::Tree::Builder.new(repo)
|
271
|
+
builder << { :type => :blob, :name => "README.md", :oid => oid, :filemode => 0100644 }
|
272
|
+
|
273
|
+
options = {}
|
274
|
+
options[:tree] = builder.write
|
275
|
+
|
276
|
+
options[:author] = { :email => "testuser@github.com", :name => 'Test Author', :time => Time.now }
|
277
|
+
options[:committer] = { :email => "testuser@github.com", :name => 'Test Author', :time => Time.now }
|
278
|
+
options[:message] ||= "Making a commit via Rugged!"
|
279
|
+
options[:parents] = repo.empty? ? [] : [ repo.head.target ].compact
|
280
|
+
options[:update_ref] = 'HEAD'
|
281
|
+
|
282
|
+
Rugged::Commit.create(repo, options)
|
283
|
+
```
|
284
|
+
|
285
|
+
### Blob Objects
|
286
|
+
|
287
|
+
Blob objects represent the data in the files of a Tree Object.
|
288
|
+
|
289
|
+
```ruby
|
290
|
+
blob = repo.lookup('e1253910439ea902cf49be8a9f02f3c08d89ac73')
|
291
|
+
blob.content # => Gives you the content of the blob.
|
292
|
+
```
|
293
|
+
|
294
|
+
#### Streaming Blob Objects
|
295
|
+
|
296
|
+
There is currently no way to stream data from a blob, because `libgit2` itself does not (yet) support
|
297
|
+
streaming blobs out of the git object database. While there are hooks and interfaces for supporting it,
|
298
|
+
the default file system backend always loads the entire blob contents into memory.
|
299
|
+
|
300
|
+
If you need to access a Blob object through an IO-like API, you can wrap it with the `StringIO` class.
|
301
|
+
Note that the only advantage here is a stream-compatible interface, the complete blob object will still
|
302
|
+
be loaded into memory. Below is an example for streaming a Blob using the Sinatra framework:
|
303
|
+
|
304
|
+
```ruby
|
305
|
+
# Sinatra endpoint
|
306
|
+
get "/blobs/:sha" do
|
307
|
+
repo = Rugged::Repository.new(my_repo_path)
|
308
|
+
blob = repo.lookup params[:sha]
|
309
|
+
|
310
|
+
headers({
|
311
|
+
"Vary" => "Accept",
|
312
|
+
"Connection" => "keep-alive",
|
313
|
+
"Transfer-Encoding" => "chunked",
|
314
|
+
"Content-Type" => "application/octet-stream",
|
315
|
+
})
|
316
|
+
|
317
|
+
stream do |out|
|
318
|
+
StringIO.new(blob.content).each(8000) do |chunk|
|
319
|
+
out << chunk
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
```
|
324
|
+
|
325
|
+
---
|
326
|
+
|
327
|
+
### Commit Walker
|
328
|
+
|
329
|
+
`Rugged::Walker` is a class designed to help you traverse a set of commits over
|
330
|
+
a repository.
|
331
|
+
|
332
|
+
You first push head SHAs onto the walker, and then call next to get a list of
|
333
|
+
the reachable commit objects one at a time. You can also `hide()` commits if you
|
334
|
+
are not interested in anything beneath them (useful in situations like when
|
335
|
+
you're running something like `git log master ^origin/master`).
|
336
|
+
|
337
|
+
```ruby
|
338
|
+
walker = Rugged::Walker.new(repo)
|
339
|
+
walker.sorting(Rugged::SORT_TOPO | Rugged::SORT_REVERSE) # optional
|
340
|
+
walker.push(hex_sha_interesting)
|
341
|
+
walker.hide(hex_sha_uninteresting)
|
342
|
+
walker.each { |c| puts c.inspect }
|
343
|
+
walker.reset
|
344
|
+
```
|
345
|
+
|
346
|
+
---
|
347
|
+
|
348
|
+
### Index ("staging") area
|
349
|
+
|
350
|
+
We can inspect and manipulate the Git Index as well. To work with the index
|
351
|
+
inside an existing repository, instantiate it by using the `Repository.index`
|
352
|
+
method instead of manually opening the Index by its path.
|
353
|
+
|
354
|
+
```ruby
|
355
|
+
index = Rugged::Index.new(path)
|
356
|
+
|
357
|
+
# Re-read the index file from disk.
|
358
|
+
index.reload
|
359
|
+
|
360
|
+
# Count up index entries.
|
361
|
+
count = index.count
|
362
|
+
|
363
|
+
# The collection of index entries.
|
364
|
+
index.entries
|
365
|
+
|
366
|
+
# Iterating over index entries.
|
367
|
+
index.each { |i| puts i.inspect }
|
368
|
+
|
369
|
+
# Get a particular entry in the index.
|
370
|
+
index[path]
|
371
|
+
|
372
|
+
# Unstage.
|
373
|
+
index.remove(path)
|
374
|
+
|
375
|
+
# Stage. Also updates existing entry if there is one.
|
376
|
+
index.add(ientry)
|
377
|
+
|
378
|
+
# Stage. Create ientry from file in path, updates the index.
|
379
|
+
index.add(path)
|
380
|
+
```
|
381
|
+
|
382
|
+
---
|
383
|
+
|
384
|
+
### Refs
|
385
|
+
|
386
|
+
You can access references through the `Rugged::ReferenceCollection` object returned by `Repository#references`.
|
387
|
+
|
388
|
+
```ruby
|
389
|
+
ref = repo.references["refs/heads/master"]
|
390
|
+
|
391
|
+
sha = ref.target_id
|
392
|
+
str = ref.type # :direct
|
393
|
+
str = ref.name # "refs/heads/master"
|
394
|
+
```
|
395
|
+
|
396
|
+
You can also easily iterate over all references:
|
397
|
+
|
398
|
+
```ruby
|
399
|
+
repo.references.each do |ref|
|
400
|
+
puts ref.name
|
401
|
+
end
|
402
|
+
```
|
403
|
+
|
404
|
+
Or only over references that match the given pattern (glob):
|
405
|
+
|
406
|
+
```ruby
|
407
|
+
repo.references.each("refs/tags/*") do |ref|
|
408
|
+
puts ref.name
|
409
|
+
end
|
410
|
+
```
|
411
|
+
|
412
|
+
It is also easy to create, update, rename or delete a reference:
|
413
|
+
|
414
|
+
```ruby
|
415
|
+
ref = repo.references.create("refs/heads/unit_test", some_commit_sha)
|
416
|
+
|
417
|
+
repo.references.update(ref, new_sha) # or...
|
418
|
+
repo.references.update("refs/heads/unit_test", new_sha)
|
419
|
+
|
420
|
+
repo.references.rename(ref, "refs/heads/blead") # or...
|
421
|
+
repo.references.rename("refs/heads/unit_test", "refs/heads/blead")
|
422
|
+
|
423
|
+
repo.references.delete(ref) # or...
|
424
|
+
repo.references.delete("refs/heads/unit_test") # or...
|
425
|
+
```
|
426
|
+
|
427
|
+
Finally, you can access the reflog for any branch:
|
428
|
+
|
429
|
+
```ruby
|
430
|
+
ref = repo.references["refs/heads/master"]
|
431
|
+
entry = ref.log.first
|
432
|
+
sha = entry[:id_old]
|
433
|
+
sha = entry[:id_new]
|
434
|
+
str = entry[:message]
|
435
|
+
prsn = entry[:committer]
|
436
|
+
```
|
437
|
+
|
438
|
+
---
|
439
|
+
|
440
|
+
### Branches
|
441
|
+
|
442
|
+
The `Rugged::BranchCollection` object returned by `Repository#branches` will help
|
443
|
+
you with all of your branch-related needs.
|
444
|
+
|
445
|
+
Iterate over all branches:
|
446
|
+
|
447
|
+
```ruby
|
448
|
+
repo.branches.each_name().sort
|
449
|
+
# => ["master", "origin/HEAD", "origin/master", "origin/packed"]
|
450
|
+
|
451
|
+
repo.branches.each_name(:local).sort
|
452
|
+
# => ["master"]
|
453
|
+
|
454
|
+
repo.branches.each_name(:remote).sort
|
455
|
+
# => ["origin/HEAD", "origin/master", "origin/packed"]
|
456
|
+
```
|
457
|
+
|
458
|
+
Look up branches and get attributes:
|
459
|
+
|
460
|
+
```ruby
|
461
|
+
branch = repo.branches["master"]
|
462
|
+
branch.name # => 'master'
|
463
|
+
branch.canonical_name # => 'refs/heads/master'
|
464
|
+
```
|
465
|
+
|
466
|
+
Look up the id for the target of a branch:
|
467
|
+
|
468
|
+
```ruby
|
469
|
+
repo.branches["master"].target_id
|
470
|
+
# => "36060c58702ed4c2a40832c51758d5344201d89a"
|
471
|
+
```
|
472
|
+
|
473
|
+
Creation and deletion:
|
474
|
+
|
475
|
+
```ruby
|
476
|
+
branch = repo.branches.create("test_branch", "HEAD")
|
477
|
+
|
478
|
+
repo.branches.rename("test_branch", "new_branch") # or...
|
479
|
+
repo.branches.rename("refs/heads/test_branch", "new_branch") # or...
|
480
|
+
repo.branches.rename(ref, "new_branch") # or...
|
481
|
+
|
482
|
+
repo.branches.delete("test_branch") # or...
|
483
|
+
repo.branches.delete("refs/heads/test_branch") # or...
|
484
|
+
repo.branches.delete(ref) # or...
|
485
|
+
```
|
486
|
+
|
487
|
+
---
|
488
|
+
|
489
|
+
### Diffs
|
490
|
+
|
491
|
+
There are various ways to get hands on diffs:
|
492
|
+
|
493
|
+
```ruby
|
494
|
+
# Diff between two subsequent commits
|
495
|
+
diff_commits = commit_object.parents[0].diff(commit_object)
|
496
|
+
|
497
|
+
# Diff between two tree objects
|
498
|
+
diff_trees = tree_object_a.diff(tree_object_b)
|
499
|
+
|
500
|
+
# Diff between index/staging and current working directory
|
501
|
+
diff_index = repository.index.diff
|
502
|
+
|
503
|
+
# Diff between index/staging and another diffable (commit/tree/index)
|
504
|
+
diff_index_diffable = repository.index.diff(some_diffable)
|
505
|
+
```
|
506
|
+
|
507
|
+
When you already have a diff object, you can examine it:
|
508
|
+
|
509
|
+
```ruby
|
510
|
+
# Get patch
|
511
|
+
diff.patch
|
512
|
+
=> "diff --git a/foo1 b/foo1\nnew file mode 100644\nindex 0000000..81b68f0\n--- /dev/null\n+++ b/foo1\n@@ -0,0 +1,2 @@\n+abc\n+add line1\ndiff --git a/txt1 b/txt1\ndeleted file mode 100644\nindex 81b68f0..0000000\n--- a/txt1\n+++ /dev/null\n@@ -1,2 +0,0 @@\n-abc\n-add line1\ndiff --git a/txt2 b/txt2\nindex a7bb42f..a357de7 100644\n--- a/txt2\n+++ b/txt2\n@@ -1,2 +1,3 @@\n abc2\n add line2-1\n+add line2-2\n"
|
513
|
+
|
514
|
+
# Get delta (faster, if you only need information on what files changed)
|
515
|
+
diff.each_delta{ |d| puts d.inspect }
|
516
|
+
#<Rugged::Diff::Delta:70144372137380 {old_file: {:oid=>"0000000000000000000000000000000000000000", :path=>"foo1", :size=>0, :flags=>6, :mode=>0}, new_file: {:oid=>"81b68f040b120c9627518213f7fc317d1ed18e1c", :path=>"foo1", :size=>14, :flags=>6, :mode=>33188}, similarity: 0, status: :added>
|
517
|
+
#<Rugged::Diff::Delta:70144372136540 {old_file: {:oid=>"81b68f040b120c9627518213f7fc317d1ed18e1c", :path=>"txt1", :size=>14, :flags=>6, :mode=>33188}, new_file: {:oid=>"0000000000000000000000000000000000000000", :path=>"txt1", :size=>0, :flags=>6, :mode=>0}, similarity: 0, status: :deleted>
|
518
|
+
#<Rugged::Diff::Delta:70144372135780 {old_file: {:oid=>"a7bb42f71183c162efea5e4c80597437d716c62b", :path=>"txt2", :size=>17, :flags=>6, :mode=>33188}, new_file: {:oid=>"a357de7d870823acc3953f1b2471f9c18d0d56ea", :path=>"txt2", :size=>29, :flags=>6, :mode=>33188}, similarity: 0, status: :modified>
|
519
|
+
|
520
|
+
# Detect renamed files
|
521
|
+
# Note that the status field changed from :added/:deleted to :renamed
|
522
|
+
diff.find_similar!
|
523
|
+
diff.each_delta{ |d| puts d.inspect }
|
524
|
+
#<Rugged::Diff::Delta:70144372230920 {old_file: {:oid=>"81b68f040b120c9627518213f7fc317d1ed18e1c", :path=>"txt1", :size=>14, :flags=>6, :mode=>33188}, new_file: {:oid=>"81b68f040b120c9627518213f7fc317d1ed18e1c", :path=>"foo1", :size=>14, :flags=>6, :mode=>33188}, similarity: 100, status: :renamed>
|
525
|
+
#<Rugged::Diff::Delta:70144372230140 {old_file: {:oid=>"a7bb42f71183c162efea5e4c80597437d716c62b", :path=>"txt2", :size=>17, :flags=>6, :mode=>33188}, new_file: {:oid=>"a357de7d870823acc3953f1b2471f9c18d0d56ea", :path=>"txt2", :size=>29, :flags=>6, :mode=>33188}, similarity: 0, status: :modified>
|
526
|
+
|
527
|
+
# Merge one diff into another (mutating the first one)
|
528
|
+
diff1.merge!(diff2)
|
529
|
+
|
530
|
+
# Write a patch into a file (or any other object responding to write)
|
531
|
+
# Note that the patch as in diff.patch will be written, it won't be applied
|
532
|
+
file = File.open('/some/file', 'w')
|
533
|
+
diff.write_patch(file)
|
534
|
+
file.close
|
535
|
+
```
|
536
|
+
|
537
|
+
---
|
538
|
+
|
539
|
+
### Config files
|
540
|
+
|
541
|
+
It's also easy to read and manipulate the Git config file data with Rugged.
|
542
|
+
|
543
|
+
```ruby
|
544
|
+
# Read values
|
545
|
+
repo.config['core.bare']
|
546
|
+
|
547
|
+
# Set values
|
548
|
+
repo.config['user.name'] = true
|
549
|
+
|
550
|
+
# Delete values
|
551
|
+
repo.config.delete('user.name')
|
552
|
+
```
|
553
|
+
|
554
|
+
---
|
555
|
+
|
556
|
+
### General methods
|
557
|
+
|
558
|
+
Rugged also includes a general library for handling basic Git operations. One of
|
559
|
+
these is converting a raw sha (20 bytes) into a readable hex sha (40
|
560
|
+
characters).
|
561
|
+
|
562
|
+
```ruby
|
563
|
+
Rugged.hex_to_raw('bfde59cdd0dfac1d892814f66a95641abd8a1faf')
|
564
|
+
# => "\277\336Y\315\320\337\254\035\211(\024\366j\225d\032\275\212\037\257"
|
565
|
+
|
566
|
+
Rugged.raw_to_hex("\277\336Y\315\320\337\254\035\211(\024\366j\225d\032\275\212\037\257")
|
567
|
+
=> "bfde59cdd0dfac1d892814f66a95641abd8a1faf"
|
568
|
+
```
|
569
|
+
|
570
|
+
---
|
571
|
+
|
572
|
+
### Alternative backends
|
573
|
+
|
574
|
+
You can store bare repositories in alternative backends instead of storing on disk. (see
|
575
|
+
`redbadger/rugged-redis` for an example of how a rugged backend works).
|
576
|
+
|
577
|
+
```ruby
|
578
|
+
a_backend = Rugged::InMemory::Backend.new(opt1: 'setting', opt2: 'setting')
|
579
|
+
|
580
|
+
repo = Rugged::Repository.init_at('repo_name', :bare, backend: a_backend)
|
581
|
+
|
582
|
+
# or
|
583
|
+
|
584
|
+
repo = Rugged::Repository.bare('repo_name', backend: a_backend)
|
585
|
+
```
|
586
|
+
---
|
587
|
+
|
588
|
+
## Contributing
|
589
|
+
|
590
|
+
Fork libgit2/rugged on GitHub, make it awesomer (preferably in a branch named
|
591
|
+
for the topic), send a pull request.
|
592
|
+
|
593
|
+
|
594
|
+
## Development
|
595
|
+
|
596
|
+
Simply clone and install:
|
597
|
+
|
598
|
+
$ git clone https://github.com/libgit2/rugged.git
|
599
|
+
$ cd rugged
|
600
|
+
$ bundle install
|
601
|
+
$ rake compile
|
602
|
+
$ rake test
|
603
|
+
|
604
|
+
## Support
|
605
|
+
|
606
|
+
We encourage you to use StackOverflow for any questions or concerns regarding Rugged. Please tag your questions with the [rugged](http://stackoverflow.com/questions/tagged/rugged) keyword.
|
607
|
+
|
608
|
+
For bug reports, please open a ticket on the GitHub [issue tracker](https://github.com/libgit2/rugged/issues).
|
609
|
+
|
610
|
+
## Authors
|
611
|
+
|
612
|
+
* Vicent Marti <tanoku@gmail.com>
|
613
|
+
* Scott Chacon <schacon@gmail.com>
|
614
|
+
* Arthur Schreiber <schreiber.arthur@gmail.com>
|
615
|
+
|
616
|
+
|
617
|
+
## License
|
618
|
+
|
619
|
+
MIT. See LICENSE file.
|