Really useful software project management and source code hosting.    tell me more...
BROWSE: projects / users / groups
Public clone: git://codaset.com/codaset/grit.git
star_disabled Dashboard Source Tickets Wiki Blog Network


Really Useful Social coding!

Codaset is an open system, so you can browse and search through all the open source projects, and check out what your friends are coding. Follow them, befriend them, and fork their code; quickly and easily.
Every single open source project you create is free, so come on and use Codaset at no cost. Your first private or semi-private project is also free. Read more about what it costs after that.

by Joel Moss
10 months ago
commit:e67401804013b28e245f5825316dbf8c56cfda8d
tree:2212d6eb332cbeaa5421da469f1b65b70f3cdc39
parent: 7ea89b0ebc0e26cadd938097dd4b2ea45880b354

README.md

Grit

Grit gives you object oriented read/write access to Git repositories via Ruby.
The main goals are stability and performance. To this end, some of the
interactions with Git repositories are done by shelling out to the system's
git command, and other interactions are done with pure Ruby reimplementations of core Git functionality. This choice, however, is
transparent to end users, and you need not know which method is being used.

This software was developed to power GitHub, and should be considered
production ready. An extensive test suite is provided to verify its
correctness.

Grit is maintained by Tom Preston-Werner, Scott Chacon, Chris Wanstrath, and
PJ Hyett.

This documentation is accurate as of Grit 2.0.

Requirements

Install

Easiest install is via RubyGems:

$ gem install grit -s http://gemcutter.org

Source

Grit's Git repo is available on GitHub, which can be browsed at:

http://github.com/mojombo/grit

and cloned with:

git clone git://github.com/mojombo/grit.git

Development

You will need these gems to get tests to pass:

  • jeweler
  • mocha

Contributing

If you'd like to contribute to Grit, we ask that you fork mojombo/grit on
GitHub, and push up a topic branch for each feature you add or bug you fix.
Then create an issue and link to the topic branch and explain what the code
does. This allows us to discuss and merge each change separately.

Usage

Grit gives you object model access to your Git repositories. Once you have
created a Repo object, you can traverse it to find parent commits,
trees, blobs, etc.

Initialize a Repo object

The first step is to create a Grit::Repo object to represent your repo. In
this documentation I include the Grit module to reduce typing.

require 'grit'
include Grit
repo = Repo.new("/Users/tom/dev/grit")

In the above example, the directory /Users/tom/dev/grit is my working
directory and contains the .git directory. You can also initialize Grit with
a bare repo.

repo = Repo.new("/var/git/grit.git")

Getting a list of commits

From the Repo object, you can get a list of commits as an array of Commit
objects.

repo.commits
# =<blockquote class="citation"> [#&lt;Grit::Commit &quot;e80bbd2ce67651aa18e57fb0b43618ad4baf7750&quot;&gt;,</blockquote>
      #&lt;Grit::Commit &quot;91169e1f5fa4de2eaea3f176461f5dc784796769&quot;<blockquote class="citation">,</blockquote>
      #&lt;Grit::Commit &quot;038af8c329ef7c1bae4568b98bd5c58510465493&quot;<blockquote class="citation">,</blockquote>
      #&lt;Grit::Commit &quot;40d3057d09a7a4d61059bca9dca5ae698de58cbe&quot;<blockquote class="citation">,</blockquote>
      #&lt;Grit::Commit &quot;4ea50f4754937bf19461af58ce3b3d24c77311d9&quot;<blockquote class="citation">]</blockquote>

Called without arguments, Repo#commits returns a list of up to ten commits
reachable by the master branch (starting at the latest commit). You can
ask for commits beginning at a different branch, commit, tag, etc.

repo.commits('mybranch')
repo.commits('40d3057d09a7a4d61059bca9dca5ae698de58cbe')
repo.commits('v0.1')

You can specify the maximum number of commits to return.

repo.commits('master', 100)

If you need paging, you can specify a number of commits to skip.

repo.commits('master', 10, 20)

The above will return commits 21-30 from the commit list.

The Commit object

Commit objects contain information about that commit.

head = repo.commits.first

head.id
# =<blockquote class="citation"> &quot;e80bbd2ce67651aa18e57fb0b43618ad4baf7750&quot;</blockquote>

head.parents
# =<blockquote class="citation"> [#&lt;Grit::Commit &quot;91169e1f5fa4de2eaea3f176461f5dc784796769&quot;&gt;]</blockquote>

head.tree
# =<blockquote class="citation"> #&lt;Grit::Tree &quot;3536eb9abac69c3e4db583ad38f3d30f8db4771f&quot;&gt;</blockquote>

head.author
# =<blockquote class="citation"> #&lt;Grit::Actor &quot;Tom Preston-Werner &lt;tom@mojombo.com&gt;&quot;&gt;</blockquote>

head.authored_date
# =<blockquote class="citation"> Wed Oct 24 22:02:31 -0700 2007</blockquote>

head.committer
# =<blockquote class="citation"> #&lt;Grit::Actor &quot;Tom Preston-Werner &lt;tom@mojombo.com&gt;&quot;&gt;</blockquote>

head.committed_date
# =<blockquote class="citation"> Wed Oct 24 22:02:31 -0700 2007</blockquote>

head.message
# =<blockquote class="citation"> &quot;add Actor inspect&quot;</blockquote>

You can traverse a commit's ancestry by chaining calls to #parents.

repo.commits.first.parents[0].parents[0].parents[0]

The above corresponds to master^** or master~3** in Git parlance.

The Tree object

A tree records pointers to the contents of a directory. Let's say you want
the root tree of the latest commit on the master branch.

tree = repo.commits.first.tree
# =<blockquote class="citation"> #&lt;Grit::Tree &quot;3536eb9abac69c3e4db583ad38f3d30f8db4771f&quot;&gt;</blockquote>

tree.id
# =<blockquote class="citation"> &quot;3536eb9abac69c3e4db583ad38f3d30f8db4771f&quot;</blockquote>

Once you have a tree, you can get the contents.

contents = tree.contents
# =<blockquote class="citation"> [#&lt;Grit::Blob &quot;4ebc8aea50e0a67e000ba29a30809d0a7b9b2666&quot;&gt;,</blockquote>
      #&lt;Grit::Blob &quot;81d2c27608b352814cbe979a6acd678d30219678&quot;<blockquote class="citation">,</blockquote>
      #&lt;Grit::Tree &quot;c3d07b0083f01a6e1ac969a0f32b8d06f20c62e5&quot;<blockquote class="citation">,</blockquote>
      #&lt;Grit::Tree &quot;4d00fe177a8407dbbc64a24dbfc564762c0922d8&quot;<blockquote class="citation">]</blockquote>

This tree contains two Blob objects and two Tree objects. The trees are
subdirectories and the blobs are files. Trees below the root have additional
attributes.

contents.last.name
# =<blockquote class="citation"> &quot;lib&quot;</blockquote>

contents.last.mode
# =<blockquote class="citation"> &quot;040000&quot;</blockquote>

There is a convenience method that allows you to get a named sub-object
from a tree.

tree / &quot;lib&quot;
# =<blockquote class="citation"> #&lt;Grit::Tree &quot;e74893a3d8a25cbb1367cf241cc741bfd503c4b2&quot;&gt;</blockquote>

You can also get a tree directly from the repo if you know its name.

repo.tree
# =<blockquote class="citation"> #&lt;Grit::Tree &quot;master&quot;&gt;</blockquote>

repo.tree(&quot;91169e1f5fa4de2eaea3f176461f5dc784796769&quot;)
# =<blockquote class="citation"> #&lt;Grit::Tree &quot;91169e1f5fa4de2eaea3f176461f5dc784796769&quot;&gt;</blockquote>

The Blob object

A blob represents a file. Trees often contain blobs.

blob = tree.contents.first
# =<blockquote class="citation"> #&lt;Grit::Blob &quot;4ebc8aea50e0a67e000ba29a30809d0a7b9b2666&quot;&gt;</blockquote>

A blob has certain attributes.

blob.id
# =<blockquote class="citation"> &quot;4ebc8aea50e0a67e000ba29a30809d0a7b9b2666&quot;</blockquote>

blob.name
# =<blockquote class="citation"> &quot;README.txt&quot;</blockquote>

blob.mode
# =<blockquote class="citation"> &quot;100644&quot;</blockquote>

blob.size
# =<blockquote class="citation"> 7726</blockquote>

You can get the data of a blob as a string.

blob.data
# =<blockquote class="citation"> &quot;Grit is a library to ...&quot;</blockquote>

You can also get a blob directly from the repo if you know its name.

repo.blob(&quot;4ebc8aea50e0a67e000ba29a30809d0a7b9b2666&quot;)
# =<blockquote class="citation"> #&lt;Grit::Blob &quot;4ebc8aea50e0a67e000ba29a30809d0a7b9b2666&quot;&gt;</blockquote>

Other

There are many more API methods available that are not documented here. Please
reference the code for more functionality.

Copyright

Copyright (c) 2008 Tom Preston-Werner. See LICENSE for details.