Install

Rugged is a self-contained gem. You can install it by running:

$ gem install rugged

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, you can get CMake with:
bash
$ brew install cmake

If you want to build Rugged with HTTPS and SSH support, check out the list of optional libgit2 dependencies.

If you're using bundler and want to bundle libgit2 with Rugged, you can use the :submodules option:

oid=repo.write("This is a blob.",:blob)builder=Rugged::Tree::Builder.newbuilder<<{:type=>:blob,:name=>"README.md",:oid=>oid,:filemode=>0100644}options={}options[:tree]=builder.write(repo)options[:author]={:email=>"testuser@github.com",:name=>'Test Author',:time=>Time.now}options[:committer]={:email=>"testuser@github.com",:name=>'Test Author',:time=>Time.now}options[:message]||="Making a commit via Rugged!"options[:parents]=repo.empty??[]:[repo.head.target].compactoptions[:update_ref]='HEAD'Rugged::Commit.create(repo,options)

Blob Objects

Blob objects represent the data in the files of a Tree Object.

blob=repo.lookup('e1253910439ea902cf49be8a9f02f3c08d89ac73')blob.content# => Gives you the content of the blob.

Streaming Blob Objects

There is currently no way to stream data from a blob, because libgit2 itself does not (yet) support
streaming blobs out of the git object database. While there are hooks and interfaces for supporting it,
the default file system backend always loads the entire blob contents into memory.

If you need to access a Blob object through an IO-like API, you can wrap it with the StringIO class.
Note that the only advantage here is a stream-compatible interface, the complete blob object will still
be loaded into memory. Below is an example for streaming a Blob using the Sinatra framework:

Commit Walker

Rugged::Walker is a class designed to help you traverse a set of commits over
a repository.

You first push head SHAs onto the walker, and then call next to get a list of
the reachable commit objects one at a time. You can also hide() commits if you
are not interested in anything beneath them (useful in situations like when
you're running something like git log master ^origin/master).

Index ("staging") area

We can inspect and manipulate the Git Index as well. To work with the index
inside an existing repository, instantiate it by using the Repository.index
method instead of manually opening the Index by its path.

index=Rugged::Index.new(path)# Re-read the index file from disk.
index.reload# Count up index entries.
count=index.count# The collection of index entries.
index.entries# Iterating over index entries.
index.each{|i|putsi.inspect}# Get a particular entry in the index.
index[path]# Unstage.
index.remove(path)# Stage. Also updates existing entry if there is one.
index.add(ientry)# Stage. Create ientry from file in path, updates the index.
index.add(path)

Refs

You can access references through the Rugged::ReferenceCollection object returned by Repository#references.

Diffs

There are various ways to get hands on diffs:

# Diff between two subsequent commits
diff_commits=commit_object.parents[0].diff(commit_object)# Diff between two tree objects
diff_trees=tree_object_a.diff(tree_object_b)# Diff between index/staging and current working directory
diff_index=repository.index.diff# Diff between index/staging and another diffable (commit/tree/index)
diff_index_diffable=repository.index.diff(some_diffable)