Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions oops/commands/submodules/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from pathlib import Path

import click
from git import Repo

from oops.utils.git import is_pull_request, read_gitmodules

from oops.core.config import config
from oops.core.messages import commit_messages
from oops.git.core import GitRepository
from oops.git.submodules import GitSubmodules
from oops.utils.helpers import str_to_list
from oops.utils.io import (
desired_path,
Expand Down Expand Up @@ -89,9 +90,7 @@ def main( # noqa: C901, PLR0915, PLR0913
no_commit = options["no_commit"]
dry_run = options["dry_run"]

# FIXME: use Repo from gitpython
repo = GitRepository()
submodules = GitSubmodules()
repo = Repo()

# Compute target path and name
try:
Expand All @@ -103,12 +102,14 @@ def main( # noqa: C901, PLR0915, PLR0913
sub_path_str = desired_path(
url, prefix=base_dir, pull_request=bool(pull_request), suffix=pull_request or None
)
sub_path = repo.path / sub_path_str
sub_path = Path(repo.working_dir) / sub_path_str
sub_name = name or f"{owner}/{repo_name}"
if bool(pull_request):
sub_name = f"PRs/{sub_name}/{pull_request}"

# Plan summary
rows = [
["Repo Root", repo.path],
["Repo Root", repo.working_dir],
["URL", url],
["Branch", branch],
["Submodule name", sub_name],
Expand All @@ -132,33 +133,35 @@ def main( # noqa: C901, PLR0915, PLR0913

# Add submodule
click.echo("[add] git submodule add")
# FIXME: checkout to the branch before commit
submodules.add(url, sub_name, sub_path_str, branch=branch)
repo.create_submodule(
name=sub_name,
path=sub_path_str,
url=url,
branch=branch,
)

# Pin branch in .gitmodules (redundant but explicit)
click.echo("[config] record branch in .gitmodules")

if branch:
submodules.set_branch(sub_name, branch)

# Sync and fetch content
submodules.sync()
submodules.update()
gitmodules = read_gitmodules(repo)
click.echo(f"Setting branch {branch!r} for submodule {sub_name!r}...")
gitmodules.set_value(f'submodule "{sub_name}"', "branch", branch)

created_links = []

def create_symlink(addon_dir: Path):
link_name = f"{addon_dir.name}"
link_path = repo.path / link_name
link_path = Path(repo.working_dir) / link_name
# Determine relative target from repo root to the addon_dir
target_rel = relpath(repo.path, addon_dir)
target_rel = relpath(Path(repo.working_dir), addon_dir)
if link_path.exists() or link_path.is_symlink():
click.echo(f" [skip] {link_name} already exists")
return
os.symlink(target_rel, link_path)
created_links.append(link_name)
# Stage symlink
repo.add([link_name])
repo.index.add([link_name])

if auto_symlinks or addons:
click.echo("[scan] detecting addon folders…")
Expand All @@ -185,12 +188,11 @@ def create_symlink(addon_dir: Path):
click.echo(f"Addons not found: {human_readable(diff)}")

# Stage .gitmodules and submodule path
repo.add([".gitmodules", sub_path_str])
repo.index.add([".gitmodules", sub_path_str])

if not no_commit:
repo.commit(
commit_messages.submodule_add.format(name=sub_name),
description=commit_messages.submodule_add_desc.format(
repo.index.commit(
commit_messages.submodule_add.format(name=sub_name,
url=url,
branch=branch,
path=sub_path_str,
Expand Down
4 changes: 2 additions & 2 deletions oops/core/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class CommitMessages:
submodules_replace: str = (
"chore(submodules): replace submodule(s) and update symlinks\n\n{description}"
)
submodule_add: str = "chore(submodules): add submodule {name}"
submodule_add_desc: str = """
submodule_add: str = """chore(submodules): add submodule {name}"

- url: {url}
- branch: {branch}
- path: {path}
Expand Down
2 changes: 1 addition & 1 deletion oops/git/submodules.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class GitSubmodules:
"""Class for handling git submodule operations."""

path: str
path: str = ""

def add(self, url: str, name: str, path: str, branch: Optional[str] = None) -> None:
"""Add a git submodule to the repository.
Expand Down