I don’t think there’s any easy way, no; or if you will, I’m not aware of any direct way to determine if a particular path is being tracked by Sublime as being in a git repository without having to determine that information for yourself.
Prior to the official integration between Text and Merge, I created a simple plugin to add the integration myself. For that I came up with the following code that may help you out here:
def _find_git_root(path):
"""
Find the containing git repository (if any) in a given path; returns the
repository root or None if one can't be found.
"""
if os.path.isdir(os.path.join(path, ".git")):
return path
nPath = os.path.split(path)[0]
if nPath != path:
return _find_git_root(nPath)
return None
def _git_root_for_view(view):
"""
Find the git repository that the file in the provided view exists in, if
any.
"""
if not hasattr(_git_root_for_view, "cache"):
_git_root_for_view.cache = {}
if not view or view.file_name() is None:
return None
path, file = os.path.split(view.file_name())
if path not in _git_root_for_view.cache:
_git_root_for_view.cache[path] = _find_git_root(path)
return _git_root_for_view.cache[path]
The first function performs a recursive search for a .git
folder within a given path and tells you where it found it (if it can). The second takes view
and uses the first to find the git repository that the file that it’s editing is stored in. It caches the results of path lookups so that once it finds a git repository, it always assumes that folder is a git repo until the plugin reloads (such as when you restart Sublime).
I’m not sure how elegant it is as a solution, but it worked for me at the time. Note also that I think that it’s possible for .git
to be a file in the case of work trees or something similar (but I’m not sure), in which case this would fail to find it unless you modify it to just test for the existence of .git
instead of verifying that it’s a folder.
I should also mention that I only really tested it out on Linux since there’s where I do the bulk of my development, though offhand I don’t see why it wouldn’t work for other OS’s as well. It may or may not also be interesting to do more introspection to determine if what you found is actually a git repository or not.