开发者

Ruby-Git g.status always fails

开发者 https://www.devze.com 2023-01-04 04:35 出处:网络
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, alt

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 开发者_StackOverflow社区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:incommand_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:inconstruct_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:innew'

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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号