Skip to content

Commit 8a7e710

Browse files
author
Dag Jomar Mersland
committed
Add feature to convert existing branches to stack branches
This commit adds the ability to run 'git stack create' without arguments to convert the current branch into a stack branch by appending '-0' to its name. When run without arguments on a non-stack branch, the command will prompt for confirmation before converting. - Modified create_branch function to handle no-argument case - Added test_convert_to_stack function to verify the new functionality - Updated run_all_tests to include the new test - Maintains backward compatibility with existing usage
1 parent b3330d1 commit 8a7e710

2 files changed

Lines changed: 115 additions & 2 deletions

File tree

‎bin/gitstack.sh‎

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,46 @@ function usage() {
5555
# Create a new branch: "<base_name>-0"
5656
function create_branch() {
5757
local base_name="$1"
58+
local current_branch
59+
current_branch=$(git rev-parse --abbrev-ref HEAD)
5860

61+
# If no base name provided, try to convert current branch
5962
if [ -z "$base_name" ]; then
60-
echo "Error: Missing <base_name> for 'create'."
61-
usage
63+
# Check if already on a stack branch
64+
if get_stack_info; then
65+
echo "Already on a stack branch '$current_branch'."
66+
exit 0
67+
fi
68+
69+
# Prompt for confirmation
70+
read -r -p "Already on a branch named $current_branch. Create a stack from this branch? [Y/n] " response
71+
response=${response:-y} # Default to yes
72+
73+
if [[ ! "$response" =~ ^[Yy]$ ]]; then
74+
echo "Conversion cancelled."
75+
exit 0
76+
fi
77+
78+
# Create new stack branch
79+
local new_branch="${current_branch}-0"
80+
81+
# Check if the new branch already exists
82+
if git rev-parse --verify "$new_branch" &>/dev/null; then
83+
echo "Error: Branch '$new_branch' already exists. Aborting."
84+
exit 1
85+
fi
86+
87+
echo "Creating stack branch: $new_branch"
88+
if ! git checkout -b "$new_branch"; then
89+
echo "Error: Failed to create branch '$new_branch'."
90+
exit 1
91+
fi
92+
93+
echo "Branch '$new_branch' successfully created and checked out."
94+
return 0
6295
fi
6396

97+
# Original functionality for when base_name is provided
6498
local new_branch="${base_name}-0"
6599

66100
# Check if branch already exists
@@ -783,6 +817,60 @@ function next_stack() {
783817
echo "Successfully checked out next branch '$next_branch'."
784818
}
785819
820+
# Force-push all branches in a stack to remote
821+
# Usage: fp_stack [base_name]
822+
# If no base_name is provided, uses the current branch's stack
823+
function fp_stack() {
824+
local base_name="$1"
825+
local current_branch
826+
current_branch=$(git rev-parse --abbrev-ref HEAD)
827+
828+
# If no base name provided, try to get it from current branch
829+
if [ -z "$base_name" ]; then
830+
if ! get_stack_info; then
831+
echo "Error: Not currently on a stack branch and no stack name provided."
832+
echo "Usage: $0 fp [stack-name]"
833+
exit 1
834+
fi
835+
base_name="$STACK_BASE"
836+
fi
837+
838+
# Get all branches in the stack
839+
local branches
840+
branches=$(git branch --list "${base_name}-[0-9]*" --format="%(refname:short)" | sort -V)
841+
842+
if [ -z "$branches" ]; then
843+
echo "Error: No branches found in stack '$base_name'"
844+
exit 1
845+
fi
846+
847+
# Save current branch to return to it later
848+
local original_branch="$current_branch"
849+
local success=true
850+
851+
echo "Force-pushing branches in stack '$base_name' to remote..."
852+
while read -r branch; do
853+
echo "Pushing $branch..."
854+
if ! git push -f origin "$branch"; then
855+
echo "Error: Failed to push branch '$branch'"
856+
success=false
857+
break
858+
fi
859+
done <<< "$branches"
860+
861+
# Return to original branch if different
862+
if [ "$original_branch" != "$current_branch" ]; then
863+
git checkout "$original_branch"
864+
fi
865+
866+
if [ "$success" = true ]; then
867+
echo "✅ Successfully force-pushed all branches in stack '$base_name'"
868+
else
869+
echo "⚠️ Some branches may not have been pushed successfully"
870+
exit 1
871+
fi
872+
}
873+
786874
# Only process arguments if script is run directly (not sourced)
787875
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
788876
subcommand="$1"

‎bin/gitstack_test.sh‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,30 @@ function test_stack_navigation() {
413413
rm -f test1.txt test2.txt test3.txt
414414
}
415415

416+
# Test convert to stack functionality
417+
function test_convert_to_stack() {
418+
echo "Testing convert to stack..."
419+
420+
# Create a regular branch
421+
git checkout main 2>/dev/null || git checkout master 2>/dev/null
422+
git checkout -b dj/my-feature
423+
424+
# Test converting to stack branch
425+
# Simulate user input 'y' for the prompt
426+
echo "y" | "$SCRIPT_DIR/gitstack.sh" create
427+
428+
local current
429+
current=$(current_branch)
430+
if [ "$current" = "dj/my-feature-0" ]; then
431+
echo "✅ Successfully converted 'dj/my-feature' to 'dj/my-feature-0'"
432+
else
433+
fail "Failed to convert branch to stack, got '$current'"
434+
fi
435+
436+
git checkout main 2>/dev/null || git checkout master 2>/dev/null
437+
git branch -D dj/my-feature-0
438+
}
439+
416440
# Run all tests
417441
function run_all_tests() {
418442
source_gitstack
@@ -423,6 +447,7 @@ function run_all_tests() {
423447
test_status_command
424448
test_fix_command
425449
test_stack_navigation
450+
test_convert_to_stack
426451
}
427452

428453
# Get the absolute path of the script directory

0 commit comments

Comments
 (0)