From d6f497cdf03b253fd3231d4e95857f7d59cc2a99 Mon Sep 17 00:00:00 2001 From: Tega McKinney Date: Wed, 24 Sep 2025 22:06:56 -0400 Subject: [PATCH] feat(wtadd): add branch-name and commit-ish flags to allow more control --- wtadd.sh | 110 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 71 insertions(+), 39 deletions(-) diff --git a/wtadd.sh b/wtadd.sh index e4cd9c8..a3c8b7b 100755 --- a/wtadd.sh +++ b/wtadd.sh @@ -8,21 +8,30 @@ GREEN="\033[0;32m" YELLOW="\033[0;33m" CLEAR="\033[0m" VERBOSE= - -echo $1 +BRANCH_NAME= +COMMIT_ISH= function usage { cat < /dev/null 2>&1; then - if ! git worktree add "$parent_dir/$dirname" "$branchname"; then - die "failed to create git worktree $branchname" + # Validate commit-ish if provided + if [ -n "$COMMIT_ISH" ]; then + if ! git rev-parse --verify "$COMMIT_ISH^{commit}" >/dev/null 2>&1; then + die "Invalid commit-ish: $COMMIT_ISH" fi - # if the branch exists on a remote: - elif git for-each-ref --format='%(refname:lstrip=3)' refs/remotes/origin | grep -E "^$branchname$" > /dev/null 2>&1; then - if ! git worktree add "$parent_dir/$dirname" "$branchname"; then - die "failed to create git worktree $branchname" + fi + + # Handle worktree creation based on whether commit-ish is provided + if [ -n "$COMMIT_ISH" ]; then + # When commit-ish is provided, always create a new branch from that commit-ish + if ! git worktree add -b "$branchname" "$parent_dir/$dirname" "$COMMIT_ISH"; then + die "failed to create git worktree $branchname from $COMMIT_ISH" fi else - # otherwise, create a new branch - if ! git worktree add -b "$branchname" "$parent_dir/$dirname"; then - die "failed to create git worktree $branchname" + # Original logic: check if branch exists locally/remotely, or create new + # if the branch exists locally: + if git for-each-ref --format='%(refname:lstrip=2)' refs/heads | grep -E "^$branchname$" > /dev/null 2>&1; then + if ! git worktree add "$parent_dir/$dirname" "$branchname"; then + die "failed to create git worktree $branchname" + fi + # if the branch exists on a remote: + elif git for-each-ref --format='%(refname:lstrip=3)' refs/remotes/origin | grep -E "^$branchname$" > /dev/null 2>&1; then + if ! git worktree add "$parent_dir/$dirname" "$branchname"; then + die "failed to create git worktree $branchname" + fi + else + # otherwise, create a new branch + if ! git worktree add -b "$branchname" "$parent_dir/$dirname"; then + die "failed to create git worktree $branchname" + fi fi fi @@ -140,22 +164,22 @@ function _worktree { # other way around # # shellcheck disable=SC2207 - platform=`uname` + platform=$(uname) if $is_worktree; then copy_source="." else copy_source=./$(git rev-parse --abbrev-ref HEAD) fi if [ "$platform" = "Darwin" ]; then - files_to_copy=( $(find -E $copy_source -not -path '*node_modules*' -and \ + files_to_copy=( $(find -E "$copy_source" -not -path '*node_modules*' -and \ -iregex '.*\/\.(envrc|env|env.local|tool-versions|mise.toml)' ) ) else - files_to_copy=( $(find $copy_source -not -path '*node_modules*' -and \ + files_to_copy=( $(find "$copy_source" -not -path '*node_modules*' -and \ -regextype posix-extended -iregex '.*\/\.(envrc|env|env.local|tool-versions|mise.toml)' ) ) fi for f in "${files_to_copy[@]}"; do - target_path="${f#$copy_source/}" + target_path="${f#"$copy_source"/}" cp_cow "$f" "$parent_dir/$dirname/$target_path" done @@ -186,6 +210,14 @@ while true; do VERBOSE=true shift ;; + -b | --branch) + BRANCH_NAME="$2" + shift 2 + ;; + -c | --commit) + COMMIT_ISH="$2" + shift 2 + ;; *) break ;;