Skip to content

Commit b54c3aa

Browse files
author
Dag Jomar Mersland
committed
feat: add force-push command to stack management
This commit introduces a new command 'fp' to the gitstack.sh script, allowing users to force-push all branches in a stack to remote. The usage documentation has been updated to reflect this addition. - Added 'fp' command to force-push stack branches - Updated usage function to include new command description
1 parent 8a7e710 commit b54c3aa

2 files changed

Lines changed: 147 additions & 0 deletions

File tree

bin/fp_test.sh

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#!/bin/bash
2+
#
3+
# fp_test.sh - Test the force-push functionality of gitstack
4+
#
5+
# This script tests the fp command by using a real remote repository
6+
7+
set -e # Exit immediately if a command exits with a nonzero status
8+
9+
# Get the absolute path of the script directory
10+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11+
12+
function fail() {
13+
echo "❌ Test failed: $1"
14+
exit 1
15+
}
16+
17+
function verify_branch_exists() {
18+
local branch="$1"
19+
local message="$2"
20+
21+
if git rev-parse --verify "$branch" >/dev/null 2>&1; then
22+
echo "$message"
23+
else
24+
echo "$message"
25+
echo " Branch '$branch' not found"
26+
exit 1
27+
fi
28+
}
29+
30+
# Create a temporary test directory
31+
TEST_DIR=$(mktemp -d)
32+
echo "Creating test repository in $TEST_DIR"
33+
34+
# Create the remote (bare) repository
35+
REMOTE_DIR="$TEST_DIR/remote"
36+
mkdir -p "$REMOTE_DIR"
37+
cd "$REMOTE_DIR" || exit 1
38+
git init --bare
39+
echo "Created bare repository in $REMOTE_DIR"
40+
41+
# Create the local repository
42+
LOCAL_DIR="$TEST_DIR/local"
43+
mkdir -p "$LOCAL_DIR"
44+
cd "$LOCAL_DIR" || exit 1
45+
git init
46+
git config --local user.email "test@example.com"
47+
git config --local user.name "Test User"
48+
49+
# Add remote
50+
git remote add origin "$REMOTE_DIR"
51+
52+
# Create initial commit
53+
touch README.md
54+
git add README.md
55+
git commit -m "Initial commit"
56+
57+
echo "Starting fp command tests..."
58+
59+
# Create test stack
60+
"$SCRIPT_DIR/gitstack.sh" create test-fp
61+
echo "test1" > test1.txt
62+
git add test1.txt
63+
git commit -m "test1"
64+
65+
"$SCRIPT_DIR/gitstack.sh" increment
66+
echo "test2" > test2.txt
67+
git add test2.txt
68+
git commit -m "test2"
69+
70+
"$SCRIPT_DIR/gitstack.sh" increment
71+
echo "test3" > test3.txt
72+
git add test3.txt
73+
git commit -m "test3"
74+
75+
echo "Testing fp with explicit stack name..."
76+
"$SCRIPT_DIR/gitstack.sh" fp test-fp
77+
78+
# Clone the remote to verify the pushes
79+
VERIFY_DIR="$TEST_DIR/verify"
80+
git clone "$REMOTE_DIR" "$VERIFY_DIR"
81+
cd "$VERIFY_DIR" || exit 1
82+
83+
# Verify all branches were pushed
84+
verify_branch_exists "origin/test-fp-0" "fp should push first branch"
85+
verify_branch_exists "origin/test-fp-1" "fp should push second branch"
86+
verify_branch_exists "origin/test-fp-2" "fp should push third branch"
87+
88+
# Return to local repo
89+
cd "$LOCAL_DIR" || exit 1
90+
91+
echo "Testing fp without stack name (using current branch)..."
92+
git checkout test-fp-1
93+
94+
# Make a change to test force-push
95+
echo "modified" > test2.txt
96+
git commit -a --amend -m "test2 modified"
97+
98+
"$SCRIPT_DIR/gitstack.sh" fp
99+
100+
# Verify in the verify repo
101+
cd "$VERIFY_DIR" || exit 1
102+
git fetch origin
103+
104+
# Get the commit message of test-fp-1
105+
commit_msg=$(git log -1 --format=%s origin/test-fp-1)
106+
if [ "$commit_msg" = "test2 modified" ]; then
107+
echo "✅ fp successfully force-pushed amended commit"
108+
else
109+
fail "fp did not force-push amended commit"
110+
fi
111+
112+
# Return to local repo
113+
cd "$LOCAL_DIR" || exit 1
114+
115+
echo "Testing error case - non-existent stack..."
116+
if "$SCRIPT_DIR/gitstack.sh" fp nonexistent-stack 2>/dev/null; then
117+
fail "fp should fail on non-existent stack"
118+
else
119+
echo "✅ fp correctly failed on non-existent stack"
120+
fi
121+
122+
echo "Testing error case - not on stack branch and no stack provided..."
123+
git checkout main
124+
if "$SCRIPT_DIR/gitstack.sh" fp 2>/dev/null; then
125+
fail "fp should fail when not on stack branch and no stack provided"
126+
else
127+
echo "✅ fp correctly failed when not on stack branch and no stack provided"
128+
fi
129+
130+
# Clean up
131+
git checkout main
132+
"$SCRIPT_DIR/gitstack.sh" delete -f test-fp
133+
rm -rf test1.txt test2.txt test3.txt
134+
135+
echo
136+
echo "🎉 All fp tests passed!"
137+
138+
# Clean up test repository
139+
cd - > /dev/null || exit 1
140+
rm -rf "$TEST_DIR"

bin/gitstack.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# gitstack.sh fix [stack] # <-- fix an unhealthy stack by rebasing divergent branches
1313
# gitstack.sh prev # <-- checkout previous branch in stack (e.g. feature-2 -> feature-1)
1414
# gitstack.sh next # <-- checkout next branch in stack (e.g. feature-2 -> feature-3)
15+
# gitstack.sh fp [stack] # <-- force-push all branches in a stack to remote
1516
#
1617
# Description:
1718
# create -> Creates a new branch named "<base_name>-0".
@@ -23,6 +24,7 @@
2324
# fix -> Fix an unhealthy stack by rebasing divergent branches.
2425
# prev -> Checkout previous branch in stack (e.g. feature-2 -> feature-1).
2526
# next -> Checkout next branch in stack (e.g. feature-2 -> feature-3).
27+
# fp -> Force-push all branches in a stack to remote.
2628
# -----------------------------------------------------------------------------
2729

2830
function usage() {
@@ -38,6 +40,7 @@ function usage() {
3840
echo " $0 fix [stack] (Fix an unhealthy stack by rebasing divergent branches)"
3941
echo " $0 prev (Checkout previous branch in stack)"
4042
echo " $0 next (Checkout next branch in stack)"
43+
echo " $0 fp [stack] (Force-push all branches in a stack to remote)"
4144
echo
4245
echo "Commands:"
4346
echo " create Creates a new branch named '<base_name>-0'."
@@ -49,6 +52,7 @@ function usage() {
4952
echo " fix Fix an unhealthy stack by rebasing divergent branches."
5053
echo " prev Checkout previous branch in stack (e.g. feature-2 -> feature-1)."
5154
echo " next Checkout next branch in stack (e.g. feature-2 -> feature-3)."
55+
echo " fp Force-push all branches in a stack to remote."
5256
exit 1
5357
}
5458

@@ -906,6 +910,9 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
906910
next)
907911
next_stack
908912
;;
913+
fp)
914+
fp_stack "$@"
915+
;;
909916
*)
910917
usage
911918
;;

0 commit comments

Comments
 (0)