Skip to content

Commit

Permalink
Apply review
Browse files Browse the repository at this point in the history
  • Loading branch information
TannazVhdBMWExt committed Aug 5, 2024
1 parent 25f88d4 commit 7a1ec07
Showing 1 changed file with 57 additions and 27 deletions.
84 changes: 57 additions & 27 deletions lobster/tools/core/online_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,66 @@ def is_git_main_module(path):
return os.path.isdir(os.path.join(path, ".git"))


def is_dir_in_git_submodule(directory):
"""
Checks if a given directory is nested inside a Git submodule.
Args:
directory (str): The path to the directory to check.
Returns:
bool: True if the directory is inside a Git submodule,
False otherwise.
str: The path to the superproject of submodule.
"""
try:
# Check if the directory is part of a Git submodule
result = subprocess.run(['git', 'rev-parse', '--show-superproject-working-tree'], cwd=directory,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
if result.returncode == 0 and result.stdout.strip():
return True, result.stdout.strip()
else:
return False, ''
except (subprocess.CalledProcessError, OSError):
return False, ''


def is_dir_in_git_main_module(directory):
"""
Checks if a given directory is nested inside a Git main module.
Args:
directory (str): The path to the directory to check.
Returns:
bool: True if the directory is inside a Git mainmodule,
False otherwise.
str: The path to the mainmodule.
"""
try:
# Check if the directory is part of a Git main module
result = subprocess.run(['git', 'rev-parse', '--show-toplevel'], cwd=directory,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, universal_newlines=True)
if result.returncode == 0 and result.stdout.strip():
return True, result.stdout.strip()
else:
return False, ''
except (subprocess.CalledProcessError, OSError):
return False, ''


def find_repo_main_root(file_path):
file_path = os.path.abspath(file_path)

try:
# Get top-level directory
repo_root = subprocess.run(
["git", "rev-parse", "--show-toplevel"],
check=True,
capture_output=True,
encoding="UTF-8",
cwd=os.path.dirname(file_path)
).stdout.strip()

if is_git_main_module(repo_root):
return repo_root

# Get the root of superproject if the current
# repository is a Submodule
repo_root = subprocess.run(
["git", "rev-parse", "--show-superproject-working-tree"],
check=True,
capture_output=True,
encoding="UTF-8",
cwd=os.path.dirname(file_path)
).stdout.strip()

return repo_root if repo_root else os.getcwd()

except subprocess.CalledProcessError:
return os.getcwd()
is_submodule, submodule_superproject_path = is_dir_in_git_submodule(os.path.dirname(file_path))

if is_submodule:
return submodule_superproject_path

is_mainmodule, mainmodule_path = is_dir_in_git_main_module(os.path.dirname(file_path))

return mainmodule_path if is_mainmodule else os.getcwd()


def path_starts_with_subpath(path, subpath):
Expand Down

0 comments on commit 7a1ec07

Please sign in to comment.