tags:

views:

40

answers:

2

I have been trying to use the ruby-git gem to make commits etc from with in a ruby script however the method to check the current status always throws an error. My understanding is that this code, although not doing too much, should be valid.

#gem install git
require 'rubygems'
require 'git'

g = Git.init
g.status

but it returns:

Git::GitExecuteError: git diff-index "HEAD" 2>&1:fatal: ambiguous argument 'HEAD': >unknown revision or path not in the working tree.
Use '--' to separate paths from revisions
from /Users/X/bin/ruby-ee-1.8.7/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git/lib.rb:700:in command'
from /Users/X/bin/ruby-ee-1.8.7/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git/lib.rb:672:in
command_lines'
from /Users/X/bin/ruby-ee-1.8.7/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git/lib.rb:287:in diff_index'
from /Users/X/bin/ruby-ee-1.8.7/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git/status.rb:99:in
construct_status'
from /Users/X/bin/ruby-ee-1.8.7/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git/status.rb:8:in initialize'
from /Users/X/bin/ruby-ee-1.8.7/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git/base.rb:175:in
new'
from /Users/X/bin/ruby-ee-1.8.7/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git/base.rb:175:in `status'
from (irb):5

Does any one have examples of how to get the current git status within ruby (using ruby-git)?

Pages I have looked at:
http://rubygems.org/gems/git
http://git.rubyforge.org/

Thanks

A: 

Path may be the important bit of that error message

unknown revision or path not in the working tree. I tried running:

$ irb
> require 'rubygems'
> require 'git'
> g = Git.init
> g.status

in a directory that has no git repo, and it blew up like yours did.

After cding into the root directory of a project with a git repo, it worked.

Check that you are in the root of a git repo. There should be a .git directory in it. If not, you could also pass the path to Git.init if cding is inconvenient for some reason.

BaroqueBobcat
The Git.init should make the current directory a git repo. So not sure why yours failed. But I am running in a folder that contains a .git/ .
Munkymorgy
Ah, I tried a pre existing .git/ repo and it worked. It must be that Git.init is not doing all I think it is.Thanks
Munkymorgy
Status fails horribly if there are no commits.
Munkymorgy
A: 

I can't answer the question of how to get the status if no commits have been made, but I can try and explain why it's blowing up.

The Git::Status class works by doing a git diff of HEAD against the repository. In Git "HEAD" refers to the current branch & working tree.

Since a branch is basically a pointer to a particular commit (the branch pointer gets moved along the history as new commits are made) no branches truly exist (not even master) until there is a commit to point it at. If there are no branches, HEAD doesn't really exist either. Therefore referring to it in a git diff throws an error.

This is a great visual guide to understanding HEAD/branches/commits: http://marklodato.github.com/visual-git-guide/

So in conclusion, it seems that trying to do anything in a git repo which has no commits is somewhat meaningless. Of course, to do a commit, you need a file. So you probably need to:

#gem install git
require 'rubygems'
require 'git'

#Create an empty file
FileUtils.touch 'README'

g = Git.init
g.add('README')
g.commit('First Commit')
g.status

If you want to check if a file has been added to the repo:

#gem install git
require 'rubygems'
require 'git'

#Create an empty file
FileUtils.touch 'README'

g = Git.init
g.ls_files.has_key?("README") #False

g.add("README")
g.commit("First Commit")

g.ls_files.has_key?("README") #True
latentflip
Thanks. I understand it now, I do not like it though. I think it should handle things like this a little more gracefully, for command line git it is ok as it is outputting information, but for a ruby command to just fail is annoying.I was trying to do a status to see if my file had been added, which I can not do until it has been added! It is for a script version controlling only 1 file.
Munkymorgy
ls_files that old chestnut. Thanks was not in Scott Chacons example I kept referring to.
Munkymorgy