Refactor worktree code

This commit is contained in:
2025-07-28 18:31:20 -04:00
parent 1e88f1b912
commit 4b66de068b
2 changed files with 35 additions and 18 deletions

1
.gitignore vendored
View File

@@ -1,6 +1,7 @@
src/ src/
bin/ bin/
.vscode/ .vscode/
*.code-workspace
backups/ backups/
.dependencies_source .dependencies_source
.dependencies_ib .dependencies_ib

View File

@@ -489,23 +489,25 @@ from_source() {
[[ -d "$BUILD_DIR" ]] && execute rm -rf "$BUILD_DIR" [[ -d "$BUILD_DIR" ]] && execute rm -rf "$BUILD_DIR"
fi fi
# Update source code # Pull or clone source code
if [[ ! -d "$SRC_DIR" ]]; then if [[ -d "$SRC_DIR" ]]; then
execute git -C "$SRC_DIR" worktree prune --verbose
execute git -C "$SRC_DIR" pull
else
execute mkdir -p "$SRC_DIR" execute mkdir -p "$SRC_DIR"
execute git clone "$src_url" "$SRC_DIR" execute git clone "$src_url" "$SRC_DIR"
fi fi
execute git -C "$SRC_DIR" worktree prune --verbose # Determine commit-ish for git worktree
execute git -C "$SRC_DIR" pull
# Generate commitish for git worktree
case "$RELEASE" in case "$RELEASE" in
snapshot) wt_commit="origin/main" ;; snapshot)
wt_commit="main"
;;
[0-9][0-9].[0-9][0-9].*) [0-9][0-9].[0-9][0-9].*)
local branch="openwrt-${RELEASE%.*}" branch="openwrt-${RELEASE%.*}"
local tag="v$RELEASE" tag="v$RELEASE"
if ask_ok "Use $branch branch HEAD (y, recommended) or $tag tag (n)?"; then if ask_ok "Use $branch branch HEAD (y, recommended) or $tag tag (n)?"; then
wt_commit="origin/$branch" wt_commit="$branch"
else else
wt_commit="$tag" wt_commit="$tag"
fi fi
@@ -516,17 +518,30 @@ from_source() {
;; ;;
esac esac
# Pull existing or add new git worktree # Clean up orphaned worktree if needed
if [[ -d "$WORKTREE_DIR" && -d "$worktree_meta" ]]; then if [[ ! -d "$WORKTREE_DIR" || ! -d "$worktree_meta" ]]; then
execute git -C "$WORKTREE_DIR" checkout "$wt_commit"
execute git -C "$WORKTREE_DIR" pull
else
[[ -d "$WORKTREE_DIR" ]] && execute rm -rf "$WORKTREE_DIR" [[ -d "$WORKTREE_DIR" ]] && execute rm -rf "$WORKTREE_DIR"
[[ -d "$worktree_meta" ]] && execute rm -rf "$worktree_meta" [[ -d "$worktree_meta" ]] && execute rm -rf "$worktree_meta"
execute git -C "$SRC_DIR" worktree add --force --force --detach "$WORKTREE_DIR" "$wt_commit" execute git -C "$SRC_DIR" worktree prune --verbose
fi fi
# Print commit information # Add or update worktree
if [[ -d "$WORKTREE_DIR" && -d "$worktree_meta" ]]; then
execute git -C "$WORKTREE_DIR" checkout "$wt_commit"
# Only pull if wt_commit is a branch
if execute git -C "$SRC_DIR" show-ref --verify --quiet "refs/heads/$wt_commit"; then
execute git -C "$WORKTREE_DIR" pull
fi
else
# If wt_commit is a branch, do not use --detach
if execute git -C "$SRC_DIR" show-ref --verify --quiet "refs/heads/$wt_commit"; then
execute git -C "$SRC_DIR" worktree add "$WORKTREE_DIR" "$wt_commit"
else
execute git -C "$SRC_DIR" worktree add --detach "$WORKTREE_DIR" "$wt_commit"
fi
fi
# Print commit information from state
commit=$(git -C "$WORKTREE_DIR" rev-parse HEAD) commit=$(git -C "$WORKTREE_DIR" rev-parse HEAD)
description=$(git -C "$WORKTREE_DIR" describe) description=$(git -C "$WORKTREE_DIR" describe)
echo "Current commit hash: $commit" echo "Current commit hash: $commit"
@@ -537,8 +552,9 @@ from_source() {
# Enter worktree # Enter worktree
execute pushd "$WORKTREE_DIR" || return 1 execute pushd "$WORKTREE_DIR" || return 1
# Cleanup build environment
((DEBUG)) && make_opts+=("V=sc") ((DEBUG)) && make_opts+=("V=sc")
# Cleanup build environment
execute make "${make_opts[@]}" "-j1" distclean execute make "${make_opts[@]}" "-j1" distclean
# make clean # compiled output # make clean # compiled output
# make targetclean # compiled output, toolchain # make targetclean # compiled output, toolchain