RJGit
A JRuby wrapper around the JGit library for manipulating Git repositories, the Ruby way.
Authors
RJGit is being developed by Team Repotag:
With special thanks to:
Installation
Install the rjgit gem with the command:
$ gem install rjgit
Dependencies for using RJGit:
- JRuby >= 1.7.0
- mime-types >= 1.15
Further dependencies for developing RJGIT:
- rake >= 0.9.2.2
- rspec >= 2.0
- simplecov
Version scheme
RJGit's version number is the version of jgit used, plus our own patch level counter. For instance, RJGit 4.0.1.0 uses jgit 4.0.1, patch level 0.
Usage
RJGit wraps most (if not all) of JGit's core functionality; it has classes for all important Git objects, i.e., Repo, Blob, Tree, Commit, and Tag. It allows parsing and manipulation of these objects in an intuitive manner, either simulating ordinary git usage (with a working directory on disk) or on a lower level, through creating new git objects manually (also works with 'bare' repositories, without a working directory).
See below for some examples of what you can do with RJGit. Make sure you have JRuby installed.
Require the gem and include the RJGit module
require "rjgit"
include RJGit
Initializing an existing repository on the filesystem
repo = Repo.new("repo.git")
Creating a new repository
repo = Repo.new("repo.git", :create => true)
repo = Repo.new("repo.git", :create => true, :is_bare => true) # Create a 'bare' git repo.
repo. # Is this a bare repository?
Getting commits
repo.commits('master')
repo.commits('959329025f67539fb82e76b02782322fad032821')
repo.head
commit = repo.commits('master').first # a Commit object; try commit.actor, commit.id, etc.
# Similarly for getting tags, branches, trees (directories), and blobs (files).
Finding a single object by SHA
repo.find('959329025f67539fb82e76b02782322fad032821')
repo.find('959329025f67539fb82e76b02782322fad032821', :commit) # Find a specific :commit, :blob, :tree, or :tag
Getting diffs
sha1 = repo.head.id
sha2 = repo.commits.last.id
= {:old_rev => sha2, :new_rev => sha1, :file_path => "some/path.txt", :patch => true}
Porcelain.diff(repo, )
Getting tags
tag = repo.['example_tag']
tag.id # tag's object id
tag..name # Etcetera
some_object = Porcelain.object_for_tag(repo, tag) # Returns the tagged object; e.g. a Commit
Blobs and Trees
blob = repo.blob("example/file.txt") # Retrieve a file by filepath...
blob = repo.find("959329025f67539fb82e76b02782322fad032822", :blob) # ...or by SHA
blob.data # Cat the file; also blob.id, blob.mode, etc.
tree = repo.tree("example") # Retrieve a tree by filepath...
tree = repo.find("959329025f67539fb82e76b02782322fad032000", :tree) #...or by SHA
tree.data # List the tree's contents (blobs and trees). Also tree.id, tree.mode, etc.
tree.each {|entry| puts entry.inspect} # Loop over the Tree's children (Blobs and Trees)
tree.trees # An array of the Tree's child Trees
tree.blobs # An array of the Tree's child Blobs
Porcelain::ls_tree(repo, repo.tree("example"), :print => true, :recursive => true, :ref => 'mybranch') # Outputs the Tree's contents to $stdout. Faster for recursive listing than Tree#each. Passing nil as the second argument lists the entire repository. ref defaults to HEAD.
Creating blobs and trees from scratch
blob = Blob.new_from_string(repo, "Contents of the new blob.") # Inserts the blob into the repository, returns an RJGit::Blob
tree = Tree.new_from_hashmap(repo, {"newblob" => "contents", "newtree" => { "otherblob" => "this blob is contained in the tree 'newtree'" } } ) # Constructs the tree and its children based on the hashmap and inserts it into the repository, returning an RJGit::Tree. Tree.new_from_hashmap takes an RJGit::Tree as an optional third argument, in which case the new tree will consist of the children of that Tree *plus* the contents of the hashmap.
Committing and adding branches to repositories, 'porcelain' style (only works with non-bare repos)
repo.create_branch('new_branch') # Similarly for deleting, renaming
repo.checkout('new_branch')
repo.add('new_file.txt') # Similarly for removing
repo.commit('My message')
repo.update_ref(commit) # Fast forward HEAD (or another ref) to the commit just created
Committing and adding branches to repositories, 'plumbing' style (also works with bare repos)
repo = repo.new("repo.git")
tree = Tree.new_from_hashmap(repo, {"newblob" => "contents"}, repo.head.tree ) # As above, use the current head commit's tree as a starting point and add "newblob"
actor = RJGit::Actor.new("test", "[email protected]")
commit = Commit.new_with_tree(repo, tree, "My commit message", actor) # Create a new commit with tree as base tree
And more...
pack = RJGitReceivePack.new(repo) # Implement the smart-http protocol with RJGitReceivePack and RJGitUploadPack
pack.receive(client_msg) # Respond to a client's GET request
repo.config['remote origin']['url'] # Retrieve config values
Porcelain::diff(repo, )
Porcelain::blame(repo, )
Issues
Please let us know by creating a github issue.
Contributing
- Fork the project
- Create a new branch
- Modify the sources
- Add specs with full coverage for your new code
- Make sure the whole test suite passes by running rake
- Push the branch up to GitHub
- Send us a pull request
Running RSpec tests (the recommended way)
- Start the nailgun server
jruby --ng-server &
- Run rake against the nailgun instance
jruby --ng -S rake
License
Copyright (c) 2011 - 2015, Team Repotag
(Modified BSD License)
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL