Skip to content

Commit

Permalink
Merge pull request ehamiter#71 from gka/submodule-support
Browse files Browse the repository at this point in the history
add submodule support
  • Loading branch information
ehamiter authored Dec 18, 2019
2 parents 40d4265 + 156c51c commit 05e422b
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions githubinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,22 @@ def run(self, edit, copyonly=False, permalink=False, mode="blob", branch=None, o
relative_git_path = relative_git_path.replace("\\", "/")
file_name = file_name.replace("\\", "/")

is_git_submodule = not os.path.isdir(os.path.join(git_path, ".git"))
# Read the config file in .git
git_config_path = os.path.join(git_path, ".git", "config")
if not is_git_submodule:
git_config_path = os.path.join(git_path, ".git", "config")
else:
# we're in a submodule!
with codecs.open(os.path.join(git_path, ".git"), "r", "utf-8") as git_submodule_file:
# we need to get the link to the git folder from the .git file
git_submodule = git_submodule_file.read()
result = re.search(r"^gitdir: (.*) *$", git_submodule)
if result:
matches = result.groups()
if matches[0]:
git_path = os.path.join(git_path, matches[0])
git_config_path = os.path.join(git_path, "config")

with codecs.open(git_config_path, "r", "utf-8") as git_config_file:
config = git_config_file.read()

Expand Down Expand Up @@ -161,7 +175,11 @@ def get_git_status(self, git_path):
return sha, branch

def get_ref(self, git_path):
with open(os.path.join(git_path, ".git", "HEAD"), "r") as f:
if ".git/modules" not in git_path:
head_path = os.path.join(git_path, ".git", "HEAD")
else:
head_path = os.path.join(git_path, "HEAD")
with open(head_path, "r") as f:
# Something like "ref: refs/heads/master"
return f.read().replace("ref: ", "")[:-1]

Expand All @@ -174,7 +192,11 @@ def get_sha_from_packed_refs(self, git_path, ref):
# pack-refs with: peeled fully-peeled sorted
0252a960f3cb3d93f1d080539f5be92efbc41200 refs/remotes/origin/master
"""
packed_ref_path = os.path.join(git_path, ".git", "packed-refs")
if ".git/modules" not in git_path:
packed_ref_path = os.path.join(git_path, ".git", "packed-refs")
else:
packed_ref_path = os.path.join(git_path, "packed-refs")

if not os.path.isfile(packed_ref_path):
return None
regex = r"\s{0}(\s.*)?$".format(ref)
Expand All @@ -187,15 +209,19 @@ def get_sha_from_packed_refs(self, git_path, ref):
pass

def get_sha_from_ref(self, git_path, ref):
with open(os.path.join(git_path, ".git", ref), "r") as f:
if ".git/modules" not in git_path:
ref_path = os.path.join(git_path, ".git", ref)
else:
ref_path = os.path.join(git_path, ref)
with open(ref_path, "r") as f:
return f.read().strip()

def recurse_dir(self, path, folder):
"""Traverse through parent directories until we find `folder`, starting
in `path`"""
items = os.listdir(path)
# Is `folder` a directory in the current path?
if folder in items and os.path.isdir(os.path.join(path, folder)):
if folder in items:
return path
# Check the parent directory
dirname = os.path.dirname(path)
Expand Down

0 comments on commit 05e422b

Please sign in to comment.