Search code examples
pythongitpython-3.xpygit2

Get path of the current repo in PyGit2 from anywhere within the repo


I'm using Pygit2 to run certain operations within the repo I'm working on.

If my code file is not at the root of the repo, how can I get the path of the repo from anywhere within the repo?

I can do the below in case the function is called from root, but how to do it if I run it from anywhere within the repository code?

$ cd /home/test/Desktop/code/Project
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

$ ipython3

In [1]: import os, pygit2
In [2]: repo = pygit2.Repository(os.getcwd())

Solution

  • One option is simply to traverse parent directories until you find a .git directory:

    import os
    import pathlib
    import pygit2
    
    def find_toplevel(path, last=None):
        path = pathlib.Path(path).absolute()
    
        if path == last:
            return None
        if (path / '.git').is_dir():
            return path
    
        return find_toplevel(path.parent, last=path)
    
    toplevel = find_toplevel('.')
    if toplevel is not None:
      repo = pygit2.Repository(str(toplevel))
    

    There are some caveats here, of course. You won't necessarily find a .git directory if someone has set the GIT_DIR environment variable. If you have a git worktree, then .git is a file, not a directory, and libgit2 doesn't seem to handle this (as of version 0.24).