How to Get the Git Directory

Burdette Lamar - Oct 21 '18 - - Dev Community

Here's a code snippet that shows how to get the git directory for a git project:



def git_clone_dir_path
  git_dir = `git rev-parse --show-toplevel`.chomp
  $?.success? ? git_dir : nil
end

p git_clone_dir_path


Enter fullscreen mode Exit fullscreen mode

[Simplified per reply below. Thanks, Bez!]

This works in any directory in the project.

I use this for a test that involves a project-specific path. On my machine, a test might generate an actual-value path such as C:/Users/Burdette/Documents/GitHub/markdown_helperwhatever.

If I put use that path as the expected value, it

On your machine's clone, that path would be different.

So in my expected values, I put git_dir/whatever. The test can then substitute in the local git directory.

That lets the test work on any machine.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player