Stream: helpdesk (published)

Topic: Root dir of a git repo


view this post on Zulip Martin Roa (Jun 01 2022 at 14:20):

I'm looking for a function (or macro) that returns the root directory of a git repo when run from a file inside the repo (sort of how @__DIR__ does it, but then should return the git root dir). Has anybody implemented this?

view this post on Zulip Martin Roa (Jun 01 2022 at 14:43):

I came up with this approach:

function gitrootdir(currdir)
  while true
    currdir == homedir() && return nothing
    currdir == "/" && return nothing
    ".git" in readdir(currdir) && return currdir
    currdir = dirname(currdir)
  end
end
gitrootdir(@__DIR__)

If someone has a better approach or an improvement for this one, please let me know.

view this post on Zulip Sukera (Jun 01 2022 at 14:46):

There's probably someting in LibGit2 for this

view this post on Zulip Martin Roa (Jun 01 2022 at 14:54):

Thanks! There is indeed LibGit2.path(repo::GitRepo).


Last updated: Oct 02 2023 at 04:34 UTC