Ruby-Git g.status always fails - ruby

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: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.

Related

Obtain all commits in all branches Using Ruby Rugged

Using the Rugged gem, I want to obtain all commits in all branches, similar to this git command line:
$ git log --all
Walker requires a branch, so the following only works for the specified branch:
repo = Rugged::Repository.new(working_dir)
walker = Rugged::Walker.new(repo)
walker.sorting(Rugged::SORT_DATE)
walker.push(repo.branches['master'].target_id)
walker.each do |commit|
# do stuff
end
walker.push seems to be a requirement for walker to return a value, but I do not want to filter out any branches.

Unable to discard changes in working directory

I have this file in working directory called Gemfile.lock. Basically, this file gets refreshed every time another file Gemfile gets modified. But I was able to
git stash
Gemfile but Gemfile.lock did not get put in stash. So then I tried
git checkout Gemfile.lock
and
git checkout -- Gemfile.lock
But everytime I run 'git status', it remains highlighted in red in the working directory. I do not want to add it to the staging area to be committed to my local repository and ultimately the remote repository. But I also want to
git checkout
to another branch. But this file being in the working directory is preventing me from switching branches. What can I do?
Keep in mind, I do not want to add this file to .gitignore and invoke 'git rm --cached <file-name>', because I do want this file to be tracked in git. I just do not want my current revision to be tracked.
I had a case where spring caused troubles. Never figured out what happened. Should you use rails with spring, then try spring stop first.

Jekyll page generator causes git warning - fatal: ambiguous argument: unknown revision or path not in the working tree

I'm using Jekyll to build my static web-site. The source code is on GitHub. Further, I have a one big markdown (.md) file from which I'm generating build-time multiple smaller pages. This is done by a simple Jekyll plugin (Jekyll generator).
Everything works as a charm, except the fact I'm getting multiple (for every single page generated build-time) warnings:
fatal: ambiguous argument '<path-to-generated-file-here.md>': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
The ruby version s 2.3.0, but I think this is related to git especially, not Jekyll itself.
I'm not doing any 'git diff' or something else. I even tried to add the folder in which the .md files are generated to .gitignore, but to no avail.
Any help on this would be greatly appreciated, because I'm really stuck! :)

rake gen_deploy rejected in Octopress

I installed Octopress in GitHub Pages.
And I clone the repository.
$ git clone git#github.com:my-name/my-name.github.io.git
$ git checkout source
And
$ rake setup_github_pages
I input my repository name.
And
$ rake gen_deploy
I got error
! [rejected] master -> master (non-fast-forward)
my solution
I resolve this problem, in GitHub delete my-name.github.io.git, and make same name repository and
$ rake gen_deploy
But I don't want to delete repository
What is the best solution?
Without deleting the repository
Please keep in mind this is not considered best practice, but it may work for you.
The solution is to force a push on the master branch.
Edit the Rakefile and look for this line:
system "git push origin #{deploy_branch}"
Alter the line by adding a plus (+) before the #{deploy_branch} tag:
system "git push origin +#{deploy_branch}"
Run the command
rake deploy
It should succeed.
Undo the edit you made to the Rakefile!
Idea for this solution came from reading this: https://stackoverflow.com/a/9629458/1369730
I have the same problem when hosting my Octopress blog on github pages. I Googled a lot and finally solved this problem.
Just change the directory.
cd octopress/_deploy
git pull origin master
cd ..
rake deploy
Then it's fixed.

Missing File in Gem after Build

TL;DR:
Don't run bundle within an existing git repository. Weird things will happen without any error messages.
Original Question:
I've built a gem by adapting the steps in this tutorial:
http://net.tutsplus.com/tutorials/ruby/gem-creation-with-bundler/
As a final step, I've run gem build .gemspec
This succeeds, but when I install the gem I find the critical file, the one which contains my code, isn't in the gem. Another file in the same (lib) directory, "version.rb", does exist in the gem.
I don't know how to start debugging this...how does bundler/gem build decide which files to include in the gem?
Edit:
My workflow is:
gem build <project_name>.gemspec
gem unpack <project_name>
=> confirm file does not exist in <unpacked>/lib/
gem install <project name>
=> confirm file structure in ~/home/stefan/.rvm/... contains gem, but does not contain desired file
Edit 2 / Resolution:
I was finally able to get this working by committing all my code to a remote repository, creating a clean clone, and building the gem. The new gem included all the required files.
A bit of history...I originally created the code and committed it before thinking about making a gem (this is my first gem). I then used bundle inside the original repository, which didn't complain, but was probably the reason for the weirdness.
One of the things bundler did for you is start a local git repo to version-manage your gem code. Check that you have added the file in git
git add lib/gem_name/missing_file.rb
Bundler generated gems use git internally to track "membership" of source files for the gem. You can see this in the .gemspec where it uses backticks to call out to git and generate a file list:
gem.files = `git ls-files`.split($/)
Note this also means you should pay attention to what you list in .gitignore

Resources