Skip to content
Merged
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
9 changes: 8 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,15 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
report_type: test_results
files: junit.xml
- name: SonarCloud Scan
# Dependabot PRs get no secrets, so they skip the scan.
- name: Check for Sonar token
id: sonar
if: matrix.os == 'ubuntu-26.04' && matrix.python-version == '3.14'
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: echo "enabled=${SONAR_TOKEN:+true}" >> "$GITHUB_OUTPUT"
- name: SonarCloud Scan
if: steps.sonar.outputs.enabled == 'true'
uses: SonarSource/sonarqube-scan-action@v8
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
5 changes: 4 additions & 1 deletion src/git_nested/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,15 @@ def _flags_from_args(args: argparse.Namespace) -> Flags:
for opt, flag_attr in SUPPORTED_OPT_ATTRS:
if getattr(args, opt, None) is not None:
setattr(flags, flag_attr, getattr(args, opt))
_check_message_flags(flags)
return flags


def _check_message_flags(flags: Flags) -> None:
if flags.message_file and not Path(flags.message_file).is_file():
output.error(f"no commit message file at {flags.message_file}")
if flags.message_file and flags.message:
output.error("-m and --file can't be used together")
return flags


# Flags that name one thing, so they make no sense for every nested repository at once.
Expand Down
20 changes: 11 additions & 9 deletions src/git_nested/cli/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ def _check_existing_worktree(git: GitRunner, command: str, subdir: Path, gitnest
output.error(messages.worktree_exists(subdir, worktree_path, prunable=gitnested.exists()))


def _load_config(git: GitRunner, command: str, flags: Flags, gitnested: Path, upstream: str | None) -> NestedConfig:
# clone and init have no .gitnested yet. clean does not need it.
if command not in ['clone', 'init', 'clean']:
return gitfile.read_config(gitnested, flags)
# Check nested.format now, not after the content is in place.
if command != 'clean':
gitfile.new_file_is_toml(git)
return NestedConfig(remote=flags.remote or upstream or '', branch=flags.branch or '', pin=flags.pin or '')


def setup_command(
git: GitRunner, command: str, flags: Flags, subdir: str | Path | None, upstream: str | None
) -> tuple[Path, Path, str, NestedConfig]:
Expand All @@ -67,15 +77,7 @@ def setup_command(
if not flags.force:
_check_existing_worktree(git, command, subdir, gitnested, refs.nested_branch(git, subref))

# clone and init have no .gitnested yet. clean does not need it.
if command in ['clone', 'init', 'clean']:
# Check nested.format now, not after the content is in place.
if command != 'clean':
gitfile.new_file_is_toml(git)
config = NestedConfig(remote=flags.remote or upstream or '', branch=flags.branch or '', pin=flags.pin or '')
else:
config = gitfile.read_config(gitnested, flags)

config = _load_config(git, command, flags, gitnested, upstream)
# `patches` reads the patches folder as it is, committed or not
skip = config.patches_inside(subdir) if command == 'patches' else None
checks.warn_uncommitted_subdir_changes(git, command, flags, subdir, skip)
Expand Down
7 changes: 1 addition & 6 deletions src/git_nested/commands/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from __future__ import annotations

from pathlib import Path

from .. import filters, output
from ..cli import setup
from ..models import CommandContext
Expand All @@ -20,10 +18,7 @@ def cmd_diff(ctx: CommandContext) -> None:
return

upstream = fetch.do_fetch(git, config, subref)
if config.path:
upstream = f'{upstream}:{config.path}'
elif config.filter or config.exclude:
upstream = filters.build_filtered_commit(git, Path.cwd(), config.filter or [], config.exclude or [], upstream)
upstream = f'{upstream}:{config.path}' if config.path else filters.filtered_view(git, config, upstream)
local_tree = git.check_output(['rev-parse', f'HEAD:{subdir.as_posix()}'])
folder = config.patches_inside(subdir)
skip = [':(exclude,glob)**/.gitnested*', *([f':(exclude,literal){folder}'] if folder else [])]
Expand Down
11 changes: 8 additions & 3 deletions src/git_nested/commands/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

from pathlib import Path
from typing import NoReturn

from .. import discovery, output, refs
from ..cli import setup
Expand All @@ -12,6 +13,12 @@
from ..models import CommandContext, Flags, NestedConfig


def _raise_fetch_error(git: GitRunner, config: NestedConfig, cmd: list[str], stderr: str | None) -> NoReturn:
if config.branch and discovery.has_branch(git, config.remote, config.branch) is False:
raise GitNestedError(f"{config.remote} has no branch {config.branch} yet; 'git nested push' makes it")
raise GitNestedError(f"command failed: git {' '.join(cmd)}\n{stderr or ''}")


def do_fetch(git: GitRunner, config: NestedConfig, subref: str) -> str:
"""Fetch upstream content.

Expand All @@ -28,9 +35,7 @@ def do_fetch(git: GitRunner, config: NestedConfig, subref: str) -> str:

result = git.network(cmd, f"fetching {config.remote} {branch_info}", may_fail=True)
if result.returncode != 0:
if config.branch and discovery.has_branch(git, config.remote, config.branch) is False:
raise GitNestedError(f"{config.remote} has no branch {config.branch} yet; 'git nested push' makes it")
raise GitNestedError(f"command failed: git {' '.join(cmd)}\n{result.stderr or ''}")
_raise_fetch_error(git, config, cmd, result.stderr)

output.verbose("reading the upstream HEAD commit")
upstream_head_commit = git.check_output(['rev-parse', FETCH_HEAD_REV])
Expand Down
6 changes: 1 addition & 5 deletions src/git_nested/commands/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,7 @@ def _against_upstream(
behind = (
git.check_output(['rev-list', '--count', f'{config.commit}..{fetched}']) if git.rev_exists(fetched) else '-'
)
upstream = config.commit
if config.filter or config.exclude:
upstream = filters.build_filtered_commit(
git, Path.cwd(), config.filter or [], config.exclude or [], config.commit
)
upstream = filters.filtered_view(git, config, config.commit)
folder = config.patches_inside(subdir)
skipped = (*inner, *([folder] if folder else []))
return behind, 'same' if content.matches_upstream(git, subdir, upstream, skipped) else 'changed'
Expand Down
4 changes: 1 addition & 3 deletions src/git_nested/commands/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ def upstream_base(git: GitRunner, config: NestedConfig, subref: str, subdir: Pat
if config.path:
tree = git.check_output(['rev-parse', f'{commit}:{config.path}'])
return git.check_output(['commit-tree', '-p', commit, '-m', f'{config.path} of {commit}', tree])
if config.filter or config.exclude:
return filters.build_filtered_commit(git, Path.cwd(), config.filter or [], config.exclude or [], commit)
return commit
return filters.filtered_view(git, config, commit)


def _fetch_commit(git: GitRunner, config: NestedConfig, subref: str, subdir: Path) -> None:
Expand Down
15 changes: 11 additions & 4 deletions src/git_nested/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ def create_nested_branch(
return subdir_worktree


def _gitnested_files(gitnested: Path) -> list[Path]:
"""The .gitnested files to update.

A level file has the regular .gitnested next to it. Both are kept in step,
so that operating on the nested repo directly still sees current info.
"""
regular = gitnested.parent / GITNESTED_FILENAME
return [gitnested] + ([regular] if regular != gitnested and regular.exists() else [])


def commit_nested_branch(
git: GitRunner,
flags: Flags,
Expand All @@ -82,10 +92,7 @@ def commit_nested_branch(
# After the level files: an inner nested repo of our own needs none
keep_inner_nested(git, flags, config, subdir, head_commit)

# A level file has the regular .gitnested next to it, which is kept in
# step so that operating on the nested repo directly still sees current info.
regular = gitnested.parent / GITNESTED_FILENAME
for path in [gitnested] + ([regular] if regular != gitnested and regular.exists() else []):
for path in _gitnested_files(gitnested):
output.verbose(f"writing {path.as_posix()}")
gitfile.update_gitrepo_file(
git=git,
Expand Down
9 changes: 8 additions & 1 deletion src/git_nested/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from . import output
from .errors import GitNestedError
from .git import GitRunner
from .models import Flags
from .models import Flags, NestedConfig


def _classify(
Expand Down Expand Up @@ -97,6 +97,13 @@ def build_filtered_commit(git: GitRunner, cwd: Path, entries: list[str], exclude
)


def filtered_view(git: GitRunner, config: NestedConfig, commit: str) -> str:
"""The commit as git-nested sees it: filtered when the config has a filter or exclude."""
if config.filter or config.exclude:
return build_filtered_commit(git, Path.cwd(), config.filter or [], config.exclude or [], commit)
return commit


def _matches(patterns: list[re.Pattern], path: str) -> bool:
"""Whether `path` matches one of the regex filter patterns in full."""
return any(pattern.fullmatch(path) for pattern in patterns)
Expand Down
13 changes: 8 additions & 5 deletions src/git_nested/gitfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ def read_config(gitnested: Path, flags: Flags) -> NestedConfig:
return config


def _write_given_fields(nested: dict, flags: Flags) -> None:
"""Write these fields only when given."""
for field in ('filter', 'exclude', 'path', 'pin', 'submodules'):
if getattr(flags, field):
nested[field] = getattr(flags, field)


def update_gitrepo_file(
git: GitRunner,
flags: Flags,
Expand All @@ -110,11 +117,7 @@ def update_gitrepo_file(
nested['commit'] = upstream_head_commit
nested['method'] = flags.method or config.method or 'merge'
nested['cmdver'] = VERSION
# These are written only when given
for field in ('filter', 'exclude', 'path', 'pin', 'submodules'):
if getattr(flags, field):
nested[field] = getattr(flags, field)

_write_given_fields(nested, flags)
_update_lineage(nested, initial, flags, config, command)
# The parent only moves once the nested commit has caught up with upstream.
if head_commit and nested_commit_ref and git.check_output(['rev-parse', nested_commit_ref]) == upstream_head_commit:
Expand Down
Loading
Loading