git-dir and work-tree #151

Closed
poysama opened this Issue Jan 4, 2013 · 2 comments

Comments

Projects
None yet
2 participants

poysama commented Jan 4, 2013

Not a bug or feature but a question for the code.

First of all, here's my code:

require 'grit'

repo = Grit::Repo.init('/tmp/repo')
repo.remote_add('origin','git@example.com')
repo.git.fetch({:all=>true})
repo.git.reset({:hard=>true},'origin/master')

# or i could say
repo.git.merge({},'origin/master')

In a non-grit way, this could have been (in native git)

mkdir /tmp/repo
cd /tmp/repo
git init
git remote add origin git@example.com
git fetch --all
git reset --hard origin/master

Reading grit code, I discovered that running a command, sends your to a method_missing and calls a method call native.

So what happens is that every native command that is going to be executed in grit, the --git-dir option is always prefixed to the command. so example, a:

repo.git.reset({:hard=>true}, 'origin/master')

would be translated to:

git --git-dir=/tmp/repo/.git reset --hard origin/master

After this, when typing git log on my repo, it shows my current HEAD commit but all my files under git status are marked as modified and deleted.

I tried again on the command line and set the --work-tree(--git-dir without the .git) option and it works as intended.

So I am confused on why grit only prepends the --git-dir option without --work-tree.

Also, if i manually specify a work_tree option under the first argument of native, it will treat work-tree as an option to the command and not an option to the git binary.

nand2 commented Apr 6, 2013

I'm using this workaround:

mypath = '/xx/xx'
repo = Grit::Repo.new(mypath)
[..]
repo.git.reset({:env => {'GIT_WORK_TREE' => mypath}, :hard => true}, commit_id)

poysama commented Apr 8, 2013

@nand2 Yeah this could work. Thanks!

poysama closed this Apr 8, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment