EzDevInfo.com

grit

**Grit is no longer maintained. Check out libgit2/rugged.** Grit gives you object oriented read/write access to Git repositories via Ruby. grit

Grit: how to to tell between two commits which is newer?

I'd like to be able to tell between two Grit::Commit objects, which is newer. What I mean by newer is that if commit_A is a parent (or parent of a parent,etc) of commit_B, then commit_B is newer. This assumes that commit_A and commit_B are on the same branch.

I thought about using Grit::Commit#date(), but I think this'll be inaccurate.

Any ideas?


Source: (StackOverflow)

How to find blob in tree by its name

I have a tree of commit found by SHA-1,code is: tree = repo.tree(sha) now I need to find blob by file name and display its content


Source: (StackOverflow)

Advertisements

Finding what branch a git commit came from

Is there any way to find out what branch a commit comes from given its sha1?

Bonus points if you can tell me how to accomplish this using Ruby Grit.


Source: (StackOverflow)

How does Github allow for inline file editing? (Or how to add or edit files in a bare git repository)

I have a small application that manages several git repositories similar to Github/Gitorious. Github allows for inline file editing, and I'd like to know if anyone has any idea on how they manage this.

My initial thought was that it would do a complete clone of the repository, use your submission to replace the file, commit, and push, but this seems like an very expensive operation with large repositories like the linux kernel.

Any ideas on a more efficient way to add and edit files to a bare repository?


Source: (StackOverflow)

Is it possible to add/commit a file to the index of a local bare Git repo?

I'm messing around with the Ruby Grit gem... seeing how I can use it to manage/access a Gitosis server I'm running. Does anyone know if it is possible to add/commit files to a local bare repo, or will I need to set up a local 'normal' repo and use SSH to push it to the bare repo on the localhost?


Source: (StackOverflow)

get latest commit for blob with ruby/grit

i cloned a copy of the git-wiki for some educational programming. now, as a challange, i tried to figure out how to get the date for the blob.

the blob is fetched with

repository.tree/(page_name + extension)

as far as i saw it, you can only get a date for a commit. but how to get the latest commit containing the blob?


Source: (StackOverflow)

Grit remove file in commit

I using Grit/Git as a database to track files added to a blog.

I can't find any example of how I would delete a file from the index. I still want to be able to have the file in my Git history (being able to get it by going back in commit history), but I want to remove it from my current HEAD.

Any tips or tricks?


Source: (StackOverflow)

Using grit with Ruby on rails

When I use rails c, I could type repo = Grit::Repo.name("/path/path") and there is no error.

But when I type these in my rails controller, there is a error message showing: uninitialized constant AaaController::Grit , and if I add require 'grit' , it says no such file to load -- grit.

I am sure that I have added gem 'grit' to my Gemfile and did bundle install.

What's wrong with this?


Source: (StackOverflow)

A few questions about Grit

I have a few questions about Grit/Git that I hope you can help me with. Here's my code:

# create Repo
r = Repo.init_bare 'myrepo.git'
i = r.index

# first commit to master
i.add('myfile.txt', 'my file contents')
i.commit("This is my commit")

# second commit to master
i.read_tree("master")
i.add('myfile2.txt', 'my file 2 contents')
i.commit("This is my second commit", [r.commits.first])

# first commit to newbranch
i.read_tree("master")
i.add('myfile3.txt', 'my file 3 contents')
i.commit("This is my third commit", [r.commits.first], nil, nil, 'newbranch')

# second commit to newbranch
i.read_tree("newbranch")
i.add('myfile4.txt', 'my file 4 contents')
i.commit("This is my fourth commit", [r.commit("newbranch")], nil, nil, 'newbranch')

With this code I'm trying to create a repo, commit twice to master, create a new branch from master, and commit twice to this branch. But the problem is that when I do this:

r.commits("newbranch")  # => 3

It says that there's only 3 commits on "newbranch". It leaves out the second commit on master. Why is that? Is there something wrong with my code?

The thing I'm most confused with is how to specify the parent commit when branching out, and when making the second commit on the "newbranch".

Hope you can help. Thanks


Source: (StackOverflow)

Example of a git push using either rugged or grit

I'm looking for some code examples, for either rugged or grit, showing how to do a git push.

Background

I have rake tasks deploy:staging and deploy:production that I use to deploy my app.

I'm deploying to heroku, so these tasks essentially do the following:

  1. Get the most recent tag (eg. git describe --abbrev=0)
  2. Push the version represented by that tag to the specified remote (eg. git push staging v1.00)
  3. Store the version in a heroku config var (eg. heroku config:add APP_VERSION=v1.00)

(There's also some checks in there to make sure I haven't forgotten to create a new tag before pushing etc.)

Initially I was using system calls from my Rakefile for these CLI commands; then I moved to using the git and heroku-api gems.

The git gem appears to be abandoned however (no commits in the past year); it seems that Grit and rugged are now the standard gems for working with Git.

Unfortunately, given the lack of documentation, I can't figure out how to do a git push with either of these libraries.

(In the following examples, assume that the remote/branch I'm pushing to is origin/master, and is already setup as a remote in the local repo)

Starting with rugged:

$ irb
2.0.0-p0 :001 > require 'rugged'
 => true 
2.0.0-p0 :002 > repo = Rugged::Repository.new('/path/to/repo')
 => #<Rugged::Repository:0x007fe8b48821c0 @encoding=#<Encoding:UTF-8>> 
2.0.0-p0 :003 > remote = Rugged::Remote.lookup(repo, 'origin')
 NoMethodError: undefined method `lookup' for Rugged::Remote:Class

Now for grit:

$ irb
2.0.0-p0 :001 > require 'grit'
 => true 
2.0.0-p0 :002 > repo = Grit::Repo.new('/path/to/repo')
 => #<Grit::Repo "/path/to/repo/.git"> 
2.0.0-p0 :004 > remote = repo.remotes.last
 => #<Grit::Remote "origin/master"> 
2.0.0-p0 :005 > repo.git.push(remote)
NoMethodError: undefined method `delete' for #<Grit::Remote "origin/master">

Any help would be greatly appreciated.


Source: (StackOverflow)

Can I get the progress of a git clone command, issued in grit, output to stdout?

I'm fairly confident this is either not possible or I'm missing an obvious option, but after consulting grit's Git class, the gist linked in this SO post, and the other grit tagged questions on SO, I'm coming up blank.

I'm using grit for a series of rake tasks that install my application. One of these tasks clones a few repositories.

Using the code in the linked gist as an example, this is the output of git cloning in grit (should work out of the box after a gem install grit in irb, ruby 1.9.2):

> require 'grit'
> gritty = Grit::Git.new('/tmp/filling-in')
=> #<Grit::Git:0x007f93ae105df8 @git_dir="/tmp/filling-in", @work_tree="/tmp/filling-in", @bytes_read=0>
> gritty.clone({:quiet => false, :verbose => true, :progress => true, :branch => '37s', :timeout => false}, "git://github.com/cookbooks/aws.git", "/tmp/aws") 
=> "Cloning into /tmp/aws...\n" 

My question is this: Can I recover the rest of the clone command's stdout, preferably while the clone is actually happening? The "Cloning into /tmp/aws...\n" is the first line of output, and is only returned once the clone completes.

A secondary question would be: If it's not possible to recover the clone's progress while it's happening with grit, is there another way to show progress of the command while it happens? My concern is a few of our repositories are quite large, and I'd like to give my rakefile's users something so they don't conclude the script is simply hanging while trying to communicate with the remote.

For reference, the "normal" out put of the git clone command would be:

$ git clone git://github.com/cookbooks/aws.git /tmp/test-aws
Cloning into /tmp/test-aws...
remote: Counting objects: 12364, done.
remote: Compressing objects: 100% (3724/3724), done.
remote: Total 12364 (delta 7220), reused 12330 (delta 7203)
Receiving objects: 100% (12364/12364), 5.92 MiB | 70 KiB/s, done.
Resolving deltas: 100% (7220/7220), done.

Quick note: Some of the functionality described here, namely :process_info requires an up-to-date version of the gem, which hasn't been updated since 23 Jan 2011, as of today, 26 Sept 2011 (many thanks to Daniel Brockman for pointing that out, below). You'll have to clone it from github and build it manually.

SOLUTION

Clone passes the correct information to stderr if you give it :process_info and :progress (my original example failed because I had an outdated gem). The following is working code. You need to build the gem manually for reasons described above, at least at this time.

> require 'grit'
> repo = Grit::Git.new('/tmp/throw-away')
> process = repo.clone({:process_info => true, :progress => true, :timeout => false}, 'git://github.com/cookbooks/aws.git', '/tmp/testing-aws-again')  # output supressed
> print process[2]   # i.e. the stderr string of the output
remote: Counting objects: 12364, done.
remote: Compressing objects: 100% (3724/3724), done.
remote: Total 12364 (delta 7220), reused 12330 (delta 7203)
Receiving objects: 100% (12364/12364), 5.92 MiB | 801 KiB/s, done.
Resolving deltas: 100% (7220/7220), done.

Source: (StackOverflow)

Grit warnings on ruby 1.9

I'm learning grit (version 2.4.1). Here is my basic code:

#!/usr/bin/env ruby -wKU

require "grit"
repo = Grit::Repo.new("./myproject")

p repo.commits

Running this code gave me a lot of warnings. This GitHub diff, as pointed out by user @Dogbert, helped me removing some of them and now I get the only the following:

UPDATE

changing hunk = hunk.map { |block| yield block } into hunk = hunk.map { |blk| yield block } both at lines 266 and 303 of diff-lcs (v1.1.2) removed this two warnings:

/Users/mircospino/.rvm/gems/ruby-1.9.2-p180/gems/diff-lcs-1.1.2/lib/diff/lcs.rb:266: warning: shadowing outer local variable - block
/Users/mircospino/.rvm/gems/ruby-1.9.2-p180/gems/diff-lcs-1.1.2/lib/diff/lcs.rb:303: warning: shadowing outer local variable - block

UPDATE 2

As user @injekt says here process.rb will be removed in the next release. This will get rid of:

/Users/mircospino/.rvm/gems/ruby-1.9.2-p180/gems/grit-2.4.1/lib/grit/process.rb:289: warning: method redefined; discarding old spawn
/Users/mircospino/.rvm/gems/ruby-1.9.2-p180/gems/grit-2.4.1/lib/grit/process.rb:221: warning: previous definition of spawn was here

UPDATE 3

THX to user @DogBert...

/Users/mircospino/.rvm/gems/ruby-1.9.2-p180/gems/diff-lcs-1.1.2/lib/diff/lcs/hunk.rb:69: warning: method redefined; discarding old flag_context=

...Disappeared by changing line 68 of hunk.rb, from :attr_accessor to :attr_reader inside the diff-lcs gem

Now I have a "stackoverflow meta" question: what I have to do with this question?


Source: (StackOverflow)

Download and modify file from Git Repository using Ruby Grit over SSH

How can I download a file (or clone the repo) from a secure Git Repo (e.g. SSH on GitHub), then commit changes back to the repo using Ruby Grit? Thanks!


Source: (StackOverflow)

How to display commit content in ruby

How do you display the commit content specified with SHA-1 in a Ruby on Rails application?


Source: (StackOverflow)

How to get the last commit date of a file with ruby/grit?

I have a jekyll site, and I want to find the last commit date of a certain post using ruby/grit.

I know that I can do the following using git:

git log -1 --format="%cd" -- <file>

How can I do something equivalent using ruby/grit please?


Source: (StackOverflow)