new git branching strategy and linking
This commit is contained in:
parent
951dd38e9d
commit
b363f169ce
4 changed files with 55 additions and 24 deletions
|
|
@ -1,16 +1,7 @@
|
|||
branch() {
|
||||
local branch_name=${1:-}
|
||||
if [ -z "$branch_name" ]; then
|
||||
echo "Usage: branch <name>" >&2
|
||||
return 2
|
||||
fi
|
||||
|
||||
# branch <name> — create or open a worktree for <name>
|
||||
# This function will change directory into the selected worktree so the
|
||||
# caller's shell is moved into it.
|
||||
|
||||
local xdg=${XDG_DATA_HOME:-$HOME/.local/share}
|
||||
|
||||
# Determine repo root early so we can run branches inside it
|
||||
local common_dir
|
||||
if ! common_dir=$(git rev-parse --git-common-dir 2>/dev/null); then
|
||||
echo "Not inside a git repository." >&2
|
||||
|
|
@ -25,6 +16,36 @@ branch() {
|
|||
return 1
|
||||
fi
|
||||
|
||||
# If no branch was provided, present an interactive selector combining local and remote branches
|
||||
if [ -z "$branch_name" ]; then
|
||||
if ! command -v fzf >/dev/null 2>&1; then
|
||||
echo "Usage: branch <name>" >&2
|
||||
return 2
|
||||
fi
|
||||
|
||||
local branches_list_raw branches_list selection
|
||||
if declare -f local_branches >/dev/null 2>&1; then
|
||||
branches_list_raw=$(local_branches 2>/dev/null || true; remote_branches 2>/dev/null || true)
|
||||
else
|
||||
branches_list_raw=$(git -C "$repo_dir" branch --format='%(refname:short)' 2>/dev/null || true; git -C "$repo_dir" branch -r --format='%(refname:short)' 2>/dev/null | sed 's#^.*/##' || true)
|
||||
fi
|
||||
|
||||
branches_list=$(printf "%s
|
||||
" "$branches_list_raw" | awk '!seen[$0]++')
|
||||
if [ -z "$branches_list" ]; then
|
||||
echo "No branches found." >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
selection=$(printf "%s\n" "$branches_list" | fzf --height=40% --prompt="Select branch: ")
|
||||
if [ -z "$selection" ]; then
|
||||
echo "No branch selected." >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
branch_name="$selection"
|
||||
fi
|
||||
|
||||
local repo_base repo_hash default_branch
|
||||
repo_base=$(basename "$repo_dir")
|
||||
repo_hash=$(printf "%s" "$repo_dir" | sha1sum | awk '{print $1}')
|
||||
|
|
|
|||
|
|
@ -38,24 +38,26 @@ EOF
|
|||
return 2
|
||||
fi
|
||||
|
||||
local -a candidates
|
||||
IFS=$'\0' read -r -d '' -a candidates < <(git -C "$repo_root" ls-files --others --ignored --exclude-standard -z || true)
|
||||
local -a candidates=()
|
||||
while IFS= read -r -d '' file; do
|
||||
candidates+=("$file")
|
||||
done < <(git -C "$repo_root" ls-files --others --ignored --exclude-standard -z || true)
|
||||
|
||||
if [ ${#candidates[@]} -eq 0 ]; then
|
||||
echo "No untracked/ignored files found in $repo_root"
|
||||
return 0
|
||||
fi
|
||||
|
||||
declare -A _seen
|
||||
local -a tops=()
|
||||
for c in "${candidates[@]}"; do
|
||||
c="${c%/}"
|
||||
local top="${c%%/*}"
|
||||
[ -z "$top" ] && continue
|
||||
if [ -z "${_seen[$top]:-}" ]; then
|
||||
_seen[$top]=1
|
||||
tops+=("$top")
|
||||
fi
|
||||
local found=0
|
||||
for existing in "${tops[@]}"; do
|
||||
[ "$existing" = "$top" ] && found=1 && break
|
||||
done
|
||||
[ "$found" -eq 0 ] && tops+=("$top")
|
||||
done
|
||||
|
||||
if [ ${#tops[@]} -eq 0 ]; then
|
||||
|
|
@ -89,7 +91,12 @@ EOF
|
|||
if [ -z "$selected" ]; then
|
||||
echo "No files selected." && return 0
|
||||
fi
|
||||
IFS=$'\n' read -r -d '' -a chosen < <(printf "%s\n" "$selected" && printf '\0')
|
||||
chosen=()
|
||||
while IFS= read -r line; do
|
||||
chosen+=("$line")
|
||||
done <<EOF
|
||||
$selected
|
||||
EOF
|
||||
else
|
||||
chosen=("${filtered[@]}")
|
||||
fi
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue