From 6b024f2949642d487c6ae71c7c726f98a627d138 Mon Sep 17 00:00:00 2001 From: Nathan Chancellor Date: Mon, 30 Dec 2024 20:37:46 -0700 Subject: [PATCH] env: Fix a few places that add and remove git worktrees with systemd-nspawn Signed-off-by: Nathan Chancellor --- fish/functions/cbl_upd_stbl_wrktrs.fish | 8 +++++++- fish/functions/fix_wrktrs_for_nspawn.fish | 11 +++++++++++ python/lib/utils.py | 9 +++++++++ python/scripts/cbl_report.py | 8 ++++++-- 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 fish/functions/fix_wrktrs_for_nspawn.fish diff --git a/fish/functions/cbl_upd_stbl_wrktrs.fish b/fish/functions/cbl_upd_stbl_wrktrs.fish index 8ba99772..8c946dc1 100644 --- a/fish/functions/cbl_upd_stbl_wrktrs.fish +++ b/fish/functions/cbl_upd_stbl_wrktrs.fish @@ -24,7 +24,9 @@ function cbl_upd_stbl_wrktrs -d "Update the worktrees for linux-stable" set stable_version (string split -f 3 '-' $basename) if not contains $stable_version $stable_versions header "Removing $worktree" - git -C $folder worktree remove --force $worktree + # Non-temporal git worktrees need to use the host path, as the + # have been converted to the host path below. + git -C $folder worktree remove --force (nspawn_path -H $worktree) git -C $folder bd linux-$stable_version.y if test $dirname = $CBL_SRC_P @@ -48,5 +50,9 @@ function cbl_upd_stbl_wrktrs -d "Update the worktrees for linux-stable" git -C $folder worktree add --track -b $branch $worktree origin/$branch end end + + # We need to ensure the git worktree links are valid for both + # host and systemd-nspawn + fix_wrktrs_for_nspawn $folder end end diff --git a/fish/functions/fix_wrktrs_for_nspawn.fish b/fish/functions/fix_wrktrs_for_nspawn.fish new file mode 100644 index 00000000..6e2016f1 --- /dev/null +++ b/fish/functions/fix_wrktrs_for_nspawn.fish @@ -0,0 +1,11 @@ +#!/usr/bin/env fish +# SPDX-License-Identifier: MIT +# Copyright (C) 2024 Nathan Chancellor + +function fix_wrktrs_for_nspawn -d 'Fix worktree paths in gitdir files created within systemd-nspawn' + PYTHONPATH=$PYTHON_FOLDER python3 -c 'import lib.utils, sys +from pathlib import Path + +for arg in sys.argv[1:]: + lib.utils.'(status function)'(Path(arg))' $argv +end diff --git a/python/lib/utils.py b/python/lib/utils.py index e9fa88ae..d303e36d 100755 --- a/python/lib/utils.py +++ b/python/lib/utils.py @@ -54,6 +54,15 @@ def detect_virt(*args): return chronic(['systemd-detect-virt', *args], check=False).stdout.strip() +def fix_wrktrs_for_nspawn(git_repo): + if not git_repo.joinpath('.git').is_dir(): + raise RuntimeError(f"{git_repo} does not appear to be a git repository?") + for gitdir in git_repo.glob('.git/worktrees/*/gitdir'): + # Transform '/run/host/home/...' into '/home/...' + if (gitdir_txt := gitdir.read_text(encoding='utf-8')).startswith('/run/host/'): + gitdir.write_text(gitdir_txt[len('/run/host'):], encoding='utf-8') + + def fzf(header, fzf_input, fzf_args=None): fzf_cmd = ['fzf', '--header', header, '--multi'] if fzf_args: diff --git a/python/scripts/cbl_report.py b/python/scripts/cbl_report.py index 37acc8d8..6dce6378 100755 --- a/python/scripts/cbl_report.py +++ b/python/scripts/cbl_report.py @@ -99,7 +99,7 @@ def get_report_repo(): def get_report_worktree(): - return Path(os.environ['CBL'], 'current-report') + return Path(os.environ['CBL'].removeprefix('/run/host'), 'current-report') def parse_parameters(): @@ -724,7 +724,10 @@ def finalize_report(args): # Remove worktree ('--force' due to submodules) if args.remove_worktree or args.all: - lib.utils.call_git(repo, ['worktree', 'remove', '--force', worktree], show_cmd=True) + lib.utils.call_git(repo, + ['worktree', 'remove', '--force', + worktree.removeprefix('/run/host')], + show_cmd=True) # Delete branch locally and remotely if necessary if args.delete_branch or args.all: @@ -770,6 +773,7 @@ def new_report(args): # Create worktree lib.utils.call_git(repo, worktree_add, show_cmd=True) + lib.utils.fix_wrktrs_for_nspawn(repo) # Push new branch if needed if (args.push or args.all) and push_to_remote: