Giter Club home page Giter Club logo

rjgit's Introduction

RJGit

A JRuby wrapper around the JGit library for manipulating Git repositories, the Ruby way.

Ruby Build Coverage Status Gem Version Cutting Edge Dependency Status

Summary

RJGit provides a fully featured library for accessing and manipulating git repositories by wrapping the Java JGit library. It thus provides similiar functionality as e.g. rugged on JRuby.

Installation

Install the rjgit gem with:

$ gem install rjgit

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.bare? # 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.

Getting branches

repo.branches

Finding a single object by SHA

repo.find('959329025f67539fb82e76b02782322fad032821')
repo.find('959329025f67539fb82e76b02782322fad032821', :commit) # Find a specific :commit, :blob, :tree, or :tag

Blobs and Trees

blob = repo.blob("example/file.txt") # Retrieve a file by filepath, from the HEAD commit...
blob = repo.blob("example/file.txt", "refs/heads/mybranch') # Retrieve the same file from a different branch
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.find_blob {|entry| entry[:name] == 'foo.txt'} # Find a single blob that matches the block
tree.find_tree {|entry| entry[:name] == 'mytree' } # Find a single tree that matches the block
tree.find {|entry| ...} # Find a single entry that matches the block
tree.find_all {|entry entry[:name].include?('.') } # Find all entries matching the block
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.

Getting tags

tag = repo.tags['example_tag']
tag.id # tag's object id
tag.author.name # Etcetera
some_object = Porcelain.object_for_tag(repo, tag) # Returns the tagged object; e.g. a Commit

Getting notes

repo.notes
note = repo.head.note
note.message # the String message of the note
note.annotates # the SHA ID of the object this note is attached to
repo.head.note = "Happy note writing"
repo.head.note_remove

Getting diffs

sha1 = repo.head.id
sha2 = repo.commits.last.id
options = {:old_rev => sha2, :new_rev => sha1, :file_path => "some/path.txt", :patch => true}
Porcelain.diff(repo, options)

Logs

repo.git.log # Returns an Array of Commits constituting the log for the default branch
repo.git.log("follow-rename.txt", "HEAD", follow: true, list_renames: true) # Log for a specific path, tracking the pathname over renames. Returns an Array of TrackingCommits, which store the tracked filename: [#<RJGit::TrackingCommit:0x773014d3 @tracked_pathname="follow-rename.txt" ...>]

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, options)
Porcelain::blame(repo, options)

Issues

Please let us know by creating a github issue.

Contributing

  1. Fork the project
  2. Create a new branch
  3. Modify the sources
  4. Add specs with full coverage for your new code
  5. Make sure the whole test suite passes by running bundle exec rake
  6. Push the branch up to GitHub
  7. Send us a pull request

Authors

RJGit is being developed by:

License

Copyright (c) 2011 - 2023, 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 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

rjgit's People

Contributors

alyssais avatar andersonmills avatar arlettevanwissen avatar aslakknutsen avatar bartkamphorst avatar betesh avatar blablatros avatar dometto avatar halfnelson avatar maarten avatar rykov avatar sadfuzzy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

rjgit's Issues

Persistent resource consumption due to failure to close JGit objects

Investigating a heap dump from a suspiciously bloated backend service found a substantial number of integer arrays owned by live jgit objects.

I suspect this is a result of RJGit failing to close() AutoCloseable JGit objects - to test this hypothesis I've added:

  module RJGit
    class Repo
      def close
        @jrepo.close
      end
    end
  end

And stuffed a call to this in an ensure block. I added a similar change to my variant of RJGit's Commit#stats call to close the DiffFormatter it uses.

After 12 hours the process has grown by about 100MB (post full-GC), which is considerably less than might have been expected prior.

Illegal reflective access operation

Seeing this on OpenJDK 14/15:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.bouncycastle.jcajce.provider.drbg.DRBG (file:/home/freaky/.rbenv/versions/jruby-9.2.14.0/lib/ruby/gems/shared/gems/rjgit-5.9.0.1/lib/java/jars/bcprov-jdk15on-161.jar) to constructor sun.security.provider.Sun()
WARNING: Please consider reporting this to the maintainers of org.bouncycastle.jcajce.provider.drbg.DRBG
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

This looks to be bcgit/bc-java#510

RJGit::Commit#parents NullPointerException

jruby-9.0.0.0, rjgit 0.3.12.

Commit#parents reliably throws NullPointerException, like so:

    [125] pry(main)> RJGit::Repo.new('/repos/hardenedBSD.git').head.parents
    Java::JavaLang::NullPointerException:
    from org.eclipse.jgit.util.RawParseUtils.author(org/eclipse/jgit/util/RawParseUtils.java:567)
    [126] pry(main)> wtf
    Exception: Java::JavaLang::NullPointerException:
    --
    0: org.eclipse.jgit.util.RawParseUtils.author(org/eclipse/jgit/util/RawParseUtils.java:567)
    1: org.eclipse.jgit.revwalk.RevCommit.getAuthorIdent(org/eclipse/jgit/revwalk/RevCommit.java:396)
    2: java.lang.reflect.Method.invoke(java/lang/reflect/Method.java:497)
    3: RUBY.initialize(/home/freaky/.rbenv/versions/jruby-9.0.0.0/lib/ruby/gems/shared/gems/rjgit-0.3.12/lib/commit.rb:27)
    4: RUBY.block in parents(/home/freaky/.rbenv/versions/jruby-9.0.0.0/lib/ruby/gems/shared/gems/rjgit-0.3.12/lib/commit.rb:41)

@jcommit.get_author_ident seems to be what's blowing up - possibly a JGit issue?

Git#log performs slowly when following renames

This, for a blob with a small history (3 commits, including 1 rename),

jruby-9.1.8.0 :006 > repo.git.log("renamed.md", "HEAD", :follow => true)

Performs very, very slowly. Intuitively, I'd say JGit must be able to do this faster, so perhaps we should look at our own own implementation of rename following.

I see this line has been commented out -- could using the pathFilter speed things up possibly?

Update JGit from 3.2.0 to 3.3.2

Swapping out the jars brings about two failures in the test suite. Both failures have to do with expecting a straightforward fast-forward result, while we are seeing a forced update with jgit 3.3.2. E.g.,:

Failure/Error: res.should == "FAST_FORWARD"
expected: "FAST_FORWARD"
got: "FORCED" (using ==)

Not sure why this behavior has changed. Maybe jgit is only now picking up on a way of updating that is not clean?

No ssh transport available for clone_command (and others)

I'm very interested in using the ssh transport for some git commands. I currently see an implementation of the UsernamePasswordCredentialsProvider, but no implementation of which would provide ssh transport.

Have y'all worked on this at all? It seems to be a deep well, as I dive in, involving implementing callbacks (org.eclipse.jgit.api.TransportConfigCallback), which I'm guessing might not be implementable in jruby, but need to be java directly.

I've been working on getting this implemented, and was just wondering where y'all stood with it.

Anderson

Provide API to access global user git settings

JGit now provides this functionality, e.g.:

 org.eclipse.jgit.util.SystemReader.getInstance().getUserConfig().getString(ConfigConstants::CONFIG_INIT_SECTION, nil, ConfigConstants::CONFIG_KEY_DEFAULT_BRANCH)

Unable to find org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider

Getting this error when running the specs on jruby-9.2.9.0:

  3) RJGit::RubyGit setting the transport with set_command_transport calls set_credentials_provider for options[:username] == 'something'
     Failure/Error: Unable to find org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider.<init>(org/eclipse/jgit/transport/UsernamePasswordCredentialsProvider.java to read failed line
     Java::JavaLang::NullPointerException:
     # org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider.<init>(org/eclipse/jgit/transport/UsernamePasswordCredentialsProvider.java:35)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.