Anshul Goyal
Anshul Goyal

Reputation: 76877

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())

Upvotes: 0

Views: 1566

Answers (2)

charlax
charlax

Reputation: 26049

pygit2 does have a mechanism to do this, from libgit2:

from pygit2 import Repository, discover_repository


def get_repo(path: str) -> Repository:
    repo_path = discover_repository(path)
    if not repo_path:
        raise ValueError(f"No repository found at '{path}' and its parents")
    return Repository(repo_path)

See https://www.pygit2.org/repository.html?highlight=discover#pygit2.discover_repository

Upvotes: 1

larsks
larsks

Reputation: 311516

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

Upvotes: 2

Related Questions