- Fix shellcheck SC2155 (separate variable assignment from declaration) - Replace ! bool && bool pattern with [ "$bool" = true ] && [ "$bool2" = true ] - Add retry logic for git clone with configurable attempts - Add retry logic for opencode session fork - Add NETWORK_RETRY_ATTEMPTS and NETWORK_RETRY_DELAY_SECONDS config vars - Add retry_with_backoff helper function in kugetsu-config.sh - Replace subshell cd with git -C for safer directory handling
192 lines
5.9 KiB
Bash
Executable File
192 lines
5.9 KiB
Bash
Executable File
#!/bin/bash
|
|
set -euo pipefail
|
|
|
|
issue_ref_to_worktree_name() {
|
|
local issue_ref="$1"
|
|
echo "$issue_ref" | sed 's/[\/:]/-/g' | sed 's/#/-/'
|
|
}
|
|
|
|
issue_ref_to_worktree_path() {
|
|
local issue_ref="$1"
|
|
local parent_dir="${2:-$WORKTREES_DIR}"
|
|
local worktree_name=$(issue_ref_to_worktree_name "$issue_ref")
|
|
echo "$parent_dir/$worktree_name"
|
|
}
|
|
|
|
issue_ref_to_branch_name() {
|
|
local issue_ref="$1"
|
|
local number_part=$(echo "$issue_ref" | grep -oE '#[0-9]+$' || echo "")
|
|
if [ -n "$number_part" ]; then
|
|
echo "fix/issue-${number_part#\#}"
|
|
else
|
|
local identifier=$(echo "$issue_ref" | grep -oE '#[^-]+$' || echo "")
|
|
if [ -n "$identifier" ]; then
|
|
local clean_id=$(echo "$identifier" | sed 's/^#//' | sed 's/-/_/g')
|
|
echo "fix/${clean_id}"
|
|
else
|
|
echo "fix/issue-temp"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
get_repo_url() {
|
|
local issue_ref="$1"
|
|
|
|
if [ -f "$REPOS_CONFIG" ]; then
|
|
local url=$(python3 -c "import json, sys; d=json.load(open('$REPOS_CONFIG')); print(d.get('$issue_ref', ''))" 2>/dev/null || echo "")
|
|
if [ -n "$url" ]; then
|
|
echo "$url"
|
|
return
|
|
fi
|
|
fi
|
|
|
|
local instance=$(echo "$issue_ref" | cut -d'/' -f1 | cut -d'#' -f1)
|
|
local rest=$(echo "$issue_ref" | sed 's/^[^\/]*\///' | sed 's/#.*//')
|
|
|
|
if [ -n "${GIT_SERVERS[$instance]:-}" ]; then
|
|
echo "${GIT_SERVERS[$instance]}/${rest}.git"
|
|
return
|
|
fi
|
|
|
|
if [ -n "${GIT_SERVERS[$DEFAULT_GIT_SERVER]:-}" ]; then
|
|
echo "${GIT_SERVERS[$DEFAULT_GIT_SERVER]}/${rest}.git"
|
|
return
|
|
fi
|
|
|
|
echo "https://${instance}/${rest}.git"
|
|
}
|
|
|
|
worktree_exists() {
|
|
local issue_ref="$1"
|
|
local parent_dir="${2:-$PWD}"
|
|
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$parent_dir")
|
|
[ -d "$worktree_path" ]
|
|
}
|
|
|
|
create_worktree() {
|
|
local issue_ref="$1"
|
|
local parent_dir="${2:-$PWD}"
|
|
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$parent_dir")
|
|
local branch_name=$(issue_ref_to_branch_name "$issue_ref")
|
|
local repo_url=$(get_repo_url "$issue_ref")
|
|
|
|
if [ -z "$repo_url" ]; then
|
|
echo "Error: Cannot determine repo URL for '$issue_ref'" >&2
|
|
echo "Please add to $REPOS_CONFIG or ensure worktree exists" >&2
|
|
exit 1
|
|
fi
|
|
|
|
local worktree_parent_dir
|
|
worktree_parent_dir=$(dirname "$worktree_path")
|
|
mkdir -p "$worktree_parent_dir"
|
|
|
|
if worktree_exists "$issue_ref" "$parent_dir"; then
|
|
echo "Removing existing worktree at '$worktree_path'..."
|
|
git worktree remove "$worktree_path" 2>/dev/null || rm -rf "$worktree_path"
|
|
fi
|
|
|
|
echo "Creating worktree at '$worktree_path'..."
|
|
|
|
local clone_success=false
|
|
local attempt=0
|
|
local max_attempts="${NETWORK_RETRY_ATTEMPTS:-3}"
|
|
|
|
while [ $attempt -lt $max_attempts ] && [ "$clone_success" = false ]; do
|
|
attempt=$((attempt + 1))
|
|
if [ $attempt -gt 1 ]; then
|
|
echo "Clone attempt $attempt of $max_attempts..."
|
|
sleep "$((attempt * 2))"
|
|
fi
|
|
|
|
if git clone "$repo_url" "$worktree_path" 2>/dev/null; then
|
|
clone_success=true
|
|
fi
|
|
done
|
|
|
|
if [ "$clone_success" = false ]; then
|
|
echo "Error: Failed to clone repository after $max_attempts attempts" >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "Creating branch '$branch_name'..."
|
|
if git -C "$worktree_path" checkout -b "$branch_name" origin/main 2>/dev/null; then
|
|
:
|
|
elif git -C "$worktree_path" checkout -b "$branch_name" main 2>/dev/null; then
|
|
:
|
|
else
|
|
echo "Warning: Could not checkout branch (may need to run from within worktree after session)" >&2
|
|
fi
|
|
|
|
echo "Worktree created at: $worktree_path"
|
|
}
|
|
|
|
remove_worktree_for_issue() {
|
|
local issue_ref="$1"
|
|
local parent_dir="${2:-$PWD}"
|
|
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$parent_dir")
|
|
|
|
if worktree_exists "$issue_ref" "$parent_dir"; then
|
|
echo "Removing worktree at '$worktree_path'..."
|
|
git worktree remove "$worktree_path" 2>/dev/null || rm -rf "$worktree_path"
|
|
fi
|
|
}
|
|
|
|
get_worktree_path_for_session() {
|
|
local session_file="$1"
|
|
if [ -f "$session_file" ]; then
|
|
python3 -c "import json; print(json.load(open('$session_file')).get('worktree_path', ''))" 2>/dev/null || echo ""
|
|
else
|
|
echo ""
|
|
fi
|
|
}
|
|
|
|
check_pr_status() {
|
|
local pr_url="$1"
|
|
|
|
if [ -z "$pr_url" ]; then
|
|
echo "no_pr_url"
|
|
return 1
|
|
fi
|
|
|
|
local hostname=$(echo "$pr_url" | sed -E 's|https://([^/]+)/.*|\1|')
|
|
|
|
local server_base="${GIT_SERVERS[$hostname]:-}"
|
|
if [ -z "$server_base" ]; then
|
|
echo "unknown_server"
|
|
return 1
|
|
fi
|
|
|
|
local api_base="${server_base}/api/v1"
|
|
|
|
local api_url=$(echo "$pr_url" | sed -E 's|https://[^/]+/([^/]+)/([^/]+)/(pulls|merge_requests)/([0-9]+)|'"${api_base}"'/repos/\1/\2/\3/\4|')
|
|
|
|
local token=""
|
|
if [[ "$hostname" == "github.com" ]]; then
|
|
token="${GITHUB_TOKEN:-}"
|
|
else
|
|
token="${GITEA_TOKEN:-}"
|
|
fi
|
|
|
|
local response_file="$KUGETSU_DIR/.pr_status_response_$$.json"
|
|
if [ -n "$token" ]; then
|
|
curl -s -H "Authorization: token $token" "$api_url" > "$response_file" 2>/dev/null || printf '{}' > "$response_file"
|
|
else
|
|
curl -s "$api_url" > "$response_file" 2>/dev/null || printf '{}' > "$response_file"
|
|
fi
|
|
|
|
local state=$(python3 -c "import json; print(json.load(open('$response_file')).get('state', 'unknown'))" 2>/dev/null || echo "unknown")
|
|
local merged=$(python3 -c "import json; print('true' if json.load(open('$response_file')).get('merged', False) else 'false')" 2>/dev/null || echo "false")
|
|
|
|
rm -f "$response_file"
|
|
|
|
if [ "$merged" = "true" ]; then
|
|
echo "merged"
|
|
elif [ "$state" = "closed" ]; then
|
|
echo "closed"
|
|
elif [ "$state" = "open" ]; then
|
|
echo "open"
|
|
else
|
|
echo "unknown"
|
|
fi
|
|
}
|