-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstak
More file actions
executable file
·1237 lines (1031 loc) · 38.5 KB
/
stak
File metadata and controls
executable file
·1237 lines (1031 loc) · 38.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
set -e
STACKS_DIR=".git/stacks"
CURRENT_STACK_FILE=".git/stack-current"
die() { echo "error: $1" >&2; exit 1; }
# Get current stack name (default if not set)
current_stack_name() {
if [ -f "$CURRENT_STACK_FILE" ]; then
cat "$CURRENT_STACK_FILE"
else
echo "default"
fi
}
# Get path to current stack file
stack_file() {
echo "$STACKS_DIR/$(current_stack_name)"
}
# Migrate old .git/stack to new format
migrate_old_stack() {
if [ -f ".git/stack" ] && [ ! -d "$STACKS_DIR" ]; then
mkdir -p "$STACKS_DIR"
mv ".git/stack" "$STACKS_DIR/default"
echo "default" > "$CURRENT_STACK_FILE"
fi
}
# Check if fzf is available and interactive mode is enabled
has_fzf() {
[ -z "$STAK_NO_INTERACTIVE" ] && command -v fzf >/dev/null 2>&1
}
# Common fzf options for nice display
FZF_OPTS="--reverse --height=~50% --border=rounded --margin=1 --padding=1"
# Build stack display list for fzf
# Format: "N ● branch (commits)" or "N → branch (commits)"
build_stack_display() {
local stack="$1"
local current=$(current_branch)
local display=""
local i=0
while IFS= read -r branch; do
i=$((i + 1))
local marker="○"
local commits=""
[ "$branch" = "$current" ] && marker="●"
if [ $i -gt 1 ]; then
local prev=$(echo "$stack" | sed -n "$((i - 1))p")
local count=$(git rev-list --count "$prev".."$branch" 2>/dev/null || echo "0")
commits="($count)"
fi
display+="$i $marker $branch $commits"$'\n'
done <<< "$stack"
echo "$display" | sed '/^$/d'
}
# Extract branch name from fzf selection (field 3: "N marker branch")
extract_branch() {
echo "$1" | awk '{print $3}'
}
current_branch() {
local branch=$(git rev-parse --abbrev-ref HEAD)
[ "$branch" = "HEAD" ] && die "cannot operate in detached HEAD state"
echo "$branch"
}
ensure_git() { git rev-parse --git-dir > /dev/null 2>&1 || die "not a git repository"; }
ensure_clean() {
git diff --quiet && git diff --cached --quiet || die "working tree is dirty - commit or stash changes first"
}
validate_stack_name() {
local name="$1"
[ -z "$name" ] && return 0
[[ "$name" == *"/"* ]] && die "invalid stack name: cannot contain '/'"
[[ "$name" == *".."* ]] && die "invalid stack name: cannot contain '..'"
[[ "$name" == "."* ]] && die "invalid stack name: cannot start with '.'"
if [[ "$name" =~ [^a-zA-Z0-9_-] ]]; then
die "invalid stack name: use only letters, numbers, underscore, hyphen"
fi
return 0
}
validate_branch_name() {
local name="$1"
[ -z "$name" ] && return 0
[[ "$name" == *".."* ]] && die "invalid branch name: cannot contain '..'"
[[ "$name" == *"~"* ]] && die "invalid branch name: cannot contain '~'"
[[ "$name" == *"^"* ]] && die "invalid branch name: cannot contain '^'"
[[ "$name" == *":"* ]] && die "invalid branch name: cannot contain ':'"
[[ "$name" == *"\\"* ]] && die "invalid branch name: cannot contain '\\'"
[[ "$name" == *" "* ]] && die "invalid branch name: cannot contain spaces"
return 0
}
is_rebasing() {
[ -d ".git/rebase-merge" ] || [ -d ".git/rebase-apply" ]
}
warn_if_rebasing() {
if is_rebasing; then
echo "⚠️ Rebase in progress. Run 'stak continue' or 'stak abort' first." >&2
fi
}
load_stack() {
local sf=$(stack_file)
[ -f "$sf" ] && grep -v '^$' "$sf" 2>/dev/null || echo ""
}
save_stack() {
local sf=$(stack_file)
mkdir -p "$STACKS_DIR"
echo "$1" > "$sf"
}
find_in_stack() {
local branch="$1"
local stack="$2"
echo "$stack" | grep -nFx "$branch" | cut -d: -f1
}
stack_top() {
local stack="$1"
echo "$stack" | tail -n 1
}
cmd_new() {
[ -z "$1" ] && die "usage: stak new <branch-name>"
local name="$1"
validate_branch_name "$name"
local current=$(current_branch)
local stack=$(load_stack)
# Validate: can only create from top of stack or from outside stack
local pos=""
if [ -n "$stack" ]; then
pos=$(find_in_stack "$current" "$stack")
local top=$(stack_top "$stack")
if [ -n "$pos" ] && [ "$current" != "$top" ]; then
die "can only create branches from the top of the stack ('$top')"
fi
fi
# Create branch from current position
git checkout -b "$name"
# Add to stack
if [ -z "$stack" ]; then
save_stack "$current"$'\n'"$name"
elif [ -n "$pos" ]; then
# Current is top of stack, append
save_stack "$stack"$'\n'"$name"
else
# Current not in stack, start new stack
save_stack "$current"$'\n'"$name"
fi
echo "Created '$name' on top of '$current'"
}
cmd_insert() {
ensure_clean
local head=$(git rev-parse --abbrev-ref HEAD)
[ "$head" = "HEAD" ] && die "cannot insert in detached HEAD state - checkout a branch first"
local name="$1"
local stack=$(load_stack)
[ -z "$stack" ] && die "no stack exists"
local total=$(echo "$stack" | wc -l | tr -d ' ')
[ "$total" -lt 1 ] && die "stack is empty"
# Get insert position
local after_branch=""
local after_pos=""
if [ -z "$name" ] && has_fzf; then
# Interactive: first get name
echo -n "New branch name: "
read name
[ -z "$name" ] && die "branch name required"
fi
[ -z "$name" ] && die "usage: stak insert <branch-name> [--after <branch>]"
validate_branch_name "$name"
# Check branch doesn't exist
git rev-parse --verify "$name" >/dev/null 2>&1 && die "branch '$name' already exists"
# Get position argument or use fzf
shift || true
if [ "$1" = "--after" ] && [ -n "$2" ]; then
after_branch="$2"
after_pos=$(find_in_stack "$after_branch" "$stack")
[ -z "$after_pos" ] && die "'$after_branch' is not in the stack"
elif has_fzf; then
# Interactive: select where to insert
local display=$(build_stack_display "$stack")
local selected=$(echo "$display" | fzf --prompt="insert after❯ " --header="Insert '$name' after which branch?" $FZF_OPTS)
[ -z "$selected" ] && exit 0
after_branch=$(extract_branch "$selected")
after_pos=$(find_in_stack "$after_branch" "$stack")
else
# Default: insert after first branch (base)
after_branch=$(echo "$stack" | head -n1)
after_pos=1
fi
# Can't insert after the last branch (that's just 'stak new')
if [ "$after_pos" -eq "$total" ]; then
die "use 'stak new $name' to add at the top"
fi
echo "Inserting '$name' after '$after_branch'..."
# Create new branch from after_branch
git checkout "$after_branch" --quiet
git checkout -b "$name" --quiet
# Update stack file: insert at position after_pos + 1
local new_stack=""
local i=0
while IFS= read -r branch; do
i=$((i + 1))
new_stack+="$branch"$'\n'
if [ "$i" -eq "$after_pos" ]; then
new_stack+="$name"$'\n'
fi
done <<< "$stack"
save_stack "$(echo "$new_stack" | sed '/^$/d')"
# Rebase all branches above onto the new branch
local rebase_stack=$(load_stack)
local insert_pos=$((after_pos + 1))
echo "Rebasing branches above..."
for ((j=insert_pos+1; j<=total+1; j++)); do
local branch_to_rebase=$(echo "$rebase_stack" | sed -n "${j}p")
[ -z "$branch_to_rebase" ] && continue
local prev_branch=$(echo "$rebase_stack" | sed -n "$((j-1))p")
echo " Rebasing '$branch_to_rebase' onto '$prev_branch'..."
git checkout "$branch_to_rebase" --quiet
git rebase "$prev_branch" --quiet || {
echo ""
echo "Conflict! Resolve, then run 'stak continue'"
exit 1
}
done
git checkout "$name" --quiet
echo ""
echo "Inserted '$name' after '$after_branch'"
cmd_status
}
cmd_split() {
ensure_clean
local head=$(git rev-parse --abbrev-ref HEAD)
[ "$head" = "HEAD" ] && die "cannot split in detached HEAD state - checkout a branch first"
local current=$(current_branch)
local stack=$(load_stack)
[ -z "$stack" ] && die "no stack exists"
local pos=$(find_in_stack "$current" "$stack")
[ -z "$pos" ] && die "'$current' is not in the stack"
[ "$pos" -eq 1 ] && die "cannot split the base branch"
local parent=$(echo "$stack" | sed -n "$((pos - 1))p")
# Get commits between parent and current
local commits=$(git log --oneline "$parent..$current" --reverse)
[ -z "$commits" ] && die "no commits to split"
local commit_count=$(echo "$commits" | wc -l | tr -d ' ')
[ "$commit_count" -lt 2 ] && die "need at least 2 commits to split"
local new_name="$1"
if [ -z "$new_name" ] && has_fzf; then
echo -n "New branch name (for bottom part): "
read new_name
[ -z "$new_name" ] && die "branch name required"
fi
[ -z "$new_name" ] && die "usage: stak split <new-branch-name>"
validate_branch_name "$new_name"
# Check branch doesn't exist
git rev-parse --verify "$new_name" >/dev/null 2>&1 && die "branch '$new_name' already exists"
if has_fzf; then
echo ""
echo "Select commits to move to '$new_name' (bottom branch):"
echo "Remaining commits will stay in '$current'"
echo ""
# Multi-select commits with fzf
local selected=$(echo "$commits" | fzf --multi --prompt="split❯ " \
--header="Tab to select commits for '$new_name', Enter when done" $FZF_OPTS)
[ -z "$selected" ] && exit 0
local selected_count=$(echo "$selected" | wc -l | tr -d ' ')
[ "$selected_count" -eq "$commit_count" ] && die "cannot move all commits - keep at least one in '$current'"
# Get the last selected commit (to find split point)
# We need contiguous commits from the bottom
local first_commit=$(echo "$commits" | head -n1 | awk '{print $1}')
local selected_hashes=$(echo "$selected" | awk '{print $1}')
# Verify commits are contiguous from bottom
local split_after=""
local expected_pos=1
while IFS= read -r commit_line; do
local hash=$(echo "$commit_line" | awk '{print $1}')
local current_pos=$(echo "$commits" | grep -n "^$hash" | cut -d: -f1)
if echo "$selected_hashes" | grep -q "^$hash$"; then
if [ "$current_pos" -ne "$expected_pos" ]; then
die "commits must be contiguous from the bottom"
fi
split_after="$hash"
expected_pos=$((expected_pos + 1))
fi
done <<< "$commits"
[ -z "$split_after" ] && die "no commits selected"
else
echo "split requires fzf for commit selection."
echo "Install with: stak setup-interactive"
exit 1
fi
echo ""
echo "Splitting '$current'..."
echo " '$new_name' will have $selected_count commit(s)"
echo " '$current' will have $((commit_count - selected_count)) commit(s)"
echo ""
# Create new branch from parent
git checkout "$parent" --quiet
git checkout -b "$new_name" --quiet
# Cherry-pick selected commits
echo "Cherry-picking commits to '$new_name'..."
echo "$selected_hashes" | while read hash; do
git cherry-pick "$hash" --quiet || {
echo ""
echo "Conflict during cherry-pick!"
echo "Resolve the conflict, then:"
echo " git add . && git cherry-pick --continue"
echo " git checkout $current && git rebase --onto $new_name $split_after"
exit 1
}
done
# Update stack: insert new_name before current
local new_stack=""
while IFS= read -r branch; do
if [ "$branch" = "$current" ]; then
new_stack+="$new_name"$'\n'
fi
new_stack+="$branch"$'\n'
done <<< "$stack"
save_stack "$(echo "$new_stack" | sed '/^$/d')"
# Rebase current onto new_name, excluding the moved commits
echo "Rebasing '$current' onto '$new_name'..."
git checkout "$current" --quiet
git rebase --onto "$new_name" "$split_after" "$current" --quiet || {
echo ""
echo "Conflict! Resolve, then run 'stak continue'"
exit 1
}
# Rebase any branches above current
local total=$(echo "$stack" | wc -l | tr -d ' ')
if [ "$pos" -lt "$total" ]; then
echo "Rebasing branches above..."
local updated_stack=$(load_stack)
local new_pos=$(find_in_stack "$current" "$updated_stack")
for ((j=new_pos+1; j<=total+1; j++)); do
local branch_to_rebase=$(echo "$updated_stack" | sed -n "${j}p")
[ -z "$branch_to_rebase" ] && continue
local prev_branch=$(echo "$updated_stack" | sed -n "$((j-1))p")
echo " Rebasing '$branch_to_rebase' onto '$prev_branch'..."
git checkout "$branch_to_rebase" --quiet
git rebase "$prev_branch" --quiet || {
echo ""
echo "Conflict! Resolve, then run 'stak continue'"
exit 1
}
done
fi
git checkout "$new_name" --quiet
echo ""
echo "Split complete! Now on '$new_name'"
cmd_status
}
cmd_up() {
local current=$(current_branch)
local stack=$(load_stack)
[ -z "$stack" ] && die "no stack exists"
local pos=$(find_in_stack "$current" "$stack")
[ -z "$pos" ] && die "'$current' is not in the stack"
local total=$(echo "$stack" | wc -l | tr -d ' ')
[ "$pos" -ge "$total" ] && die "already at top of stack"
local next=$(echo "$stack" | sed -n "$((pos + 1))p")
git checkout "$next"
echo "Moved up to '$next'"
}
cmd_down() {
local current=$(current_branch)
local stack=$(load_stack)
[ -z "$stack" ] && die "no stack exists"
local pos=$(find_in_stack "$current" "$stack")
[ -z "$pos" ] && die "'$current' is not in the stack"
[ "$pos" -le 1 ] && die "already at bottom of stack"
local prev=$(echo "$stack" | sed -n "$((pos - 1))p")
git checkout "$prev"
echo "Moved down to '$prev'"
}
cmd_status() {
warn_if_rebasing
local branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
local current="$branch"
[ "$branch" = "HEAD" ] && current="DETACHED"
local stack=$(load_stack)
local stack_name=$(current_stack_name)
if [ -z "$stack" ]; then
echo "No stack. Use 'stak new <name>' to start."
return
fi
local total=$(echo "$stack" | wc -l | tr -d ' ')
echo ""
echo "Stack: $stack_name"
echo ""
local i=0
while IFS= read -r branch; do
i=$((i + 1))
local commits=""
local line_char="│"
if [ $i -gt 1 ]; then
local prev=$(echo "$stack" | sed -n "$((i - 1))p")
local count=$(git rev-list --count "$prev".."$branch" 2>/dev/null || echo "0")
if [ "$count" -eq 0 ]; then
commits="(no commits)"
elif [ "$count" -eq 1 ]; then
commits="(1 commit)"
else
commits="($count commits)"
fi
fi
# Visual tree
if [ $i -eq 1 ]; then
# Base branch
if [ "$branch" = "$current" ]; then
echo " ● $branch (base) ◀ you are here"
else
echo " ○ $branch (base)"
fi
elif [ $i -eq "$total" ]; then
# Top branch
if [ "$branch" = "$current" ]; then
echo " ╰─▶ $branch $commits ◀ you are here"
else
echo " ╰── $branch $commits"
fi
else
# Middle branch
if [ "$branch" = "$current" ]; then
echo " ├─▶ $branch $commits ◀ you are here"
else
echo " ├── $branch $commits"
fi
fi
done <<< "$stack"
echo ""
}
cmd_sync() {
ensure_clean
local stack=$(load_stack)
[ -z "$stack" ] && die "no stack exists"
local original=$(current_branch)
# Validate all branches exist
while IFS= read -r branch; do
git rev-parse --verify "$branch" >/dev/null 2>&1 || \
die "branch '$branch' no longer exists. Remove it from stack with: git branch -D $branch (if needed) and edit .git/stacks/$(current_stack_name)"
done <<< "$stack"
# Collect branch tips BEFORE any rebasing
declare -a branches
declare -a old_tips
local i=0
while IFS= read -r branch; do
branches[$i]="$branch"
old_tips[$i]=$(git rev-parse "$branch")
i=$((i + 1))
done <<< "$stack"
echo "Syncing stack..."
for ((i=1; i<${#branches[@]}; i++)); do
local branch="${branches[$i]}"
local prev="${branches[$((i-1))]}"
local old_base="${old_tips[$((i-1))]}"
echo " [$i/${#branches[@]}] Rebasing '$branch' onto '$prev'..."
git checkout "$branch" --quiet
# Use --onto to handle case where prev was also rebased
# This replays only commits unique to $branch (after old $prev tip)
git rebase --onto "$prev" "$old_base" "$branch" --quiet || {
echo ""
echo "Rebase conflict in '$branch'. Resolve, then run:"
echo " git add <files>"
echo " stak continue"
echo ""
echo "Or to abort:"
echo " stak abort"
exit 1
}
done
git checkout "$original" --quiet
echo "Stack synced."
}
cmd_push() {
local stack=$(load_stack)
[ -z "$stack" ] && die "no stack exists"
local arg="$1"
# Get pushable branches (skip base)
local pushable=$(echo "$stack" | tail -n +2)
[ -z "$pushable" ] && die "no branches to push"
local to_push=""
if [ "$arg" = "--all" ] || [ "$arg" = "-a" ]; then
# Push all (skip interactive)
to_push="$pushable"
elif [ -z "$arg" ] && has_fzf; then
# Interactive multi-select with fzf
local display=$(build_stack_display "$stack" | tail -n +2)
local selected=$(echo "$display" | fzf --multi --prompt="push❯ " --header="Select branches (Tab to toggle, Enter to push)" $FZF_OPTS)
[ -z "$selected" ] && exit 0
# Extract branch names
to_push=$(echo "$selected" | while read -r line; do extract_branch "$line"; done)
else
# No fzf, push all
to_push="$pushable"
fi
echo "Pushing selected branches..."
while IFS= read -r branch; do
[ -z "$branch" ] && continue
echo " Pushing '$branch'..."
git push -f origin "$branch" 2>/dev/null || git push -u origin "$branch"
done <<< "$to_push"
echo "Done."
}
cmd_drop() {
ensure_clean
local current=$(current_branch)
local stack=$(load_stack)
[ -z "$stack" ] && die "no stack exists"
local pos=$(find_in_stack "$current" "$stack")
[ -z "$pos" ] && die "'$current' is not in the stack"
[ "$pos" -eq 1 ] && die "cannot drop the base of the stack"
local top=$(stack_top "$stack")
[ "$current" != "$top" ] && die "can only drop from the top of the stack (currently '$top')"
local prev=$(echo "$stack" | sed -n "$((pos - 1))p")
# Remove from stack
local new_stack=$(echo "$stack" | sed "${pos}d")
save_stack "$new_stack"
# Checkout previous and delete branch
git checkout "$prev"
git branch -D "$current"
echo "Dropped '$current', now on '$prev'"
}
show_branch_log() {
local branch="$1"
local prev="$2"
local current="$3"
local count=$(git rev-list --count "$prev".."$branch" 2>/dev/null || echo "0")
local marker="│"
[ "$branch" = "$current" ] && marker="▶"
echo ""
if [ "$count" -eq 0 ]; then
echo "$marker ─── $branch (no commits yet)"
else
echo "$marker ─── $branch ($count commits)"
git log --oneline --format="$marker %C(yellow)%h%C(reset) %s" "$prev".."$branch"
fi
}
cmd_log() {
local stack=$(load_stack)
[ -z "$stack" ] && die "no stack exists"
local current=$(current_branch)
local arg="$1"
if [ -n "$arg" ] && [ "$arg" != "--all" ] && [ "$arg" != "-a" ]; then
# Specific branch by name/number
if [[ "$arg" =~ ^[0-9]+$ ]]; then
local branch=$(echo "$stack" | sed -n "${arg}p")
else
local branch=$(echo "$stack" | grep -F "$arg" | head -n1)
fi
[ -z "$branch" ] && die "branch not found: $arg"
local pos=$(find_in_stack "$branch" "$stack")
[ "$pos" -eq 1 ] && die "no commits to show for base branch"
local prev=$(echo "$stack" | sed -n "$((pos - 1))p")
show_branch_log "$branch" "$prev" "$current"
return
elif [ -z "$arg" ] && has_fzf; then
# Interactive select
local display=$(build_stack_display "$stack" | tail -n +2)
[ -z "$display" ] && die "no branches to show"
local selected=$(echo "$display" | fzf --prompt="log❯ " --header="Select branch to view log" $FZF_OPTS)
[ -z "$selected" ] && exit 0
local branch=$(extract_branch "$selected")
local pos=$(find_in_stack "$branch" "$stack")
local prev=$(echo "$stack" | sed -n "$((pos - 1))p")
show_branch_log "$branch" "$prev" "$current"
return
fi
# Show all branches
echo ""
local prev=""
while IFS= read -r branch; do
if [ -n "$prev" ]; then
show_branch_log "$branch" "$prev" "$current"
else
if [ "$branch" = "$current" ]; then
echo "● $branch (base) ◀ you are here"
else
echo "○ $branch (base)"
fi
fi
prev="$branch"
done <<< "$stack"
echo ""
}
cmd_land() {
ensure_clean
local force=false
[ "$1" = "--force" ] || [ "$1" = "-f" ] && force=true
local stack=$(load_stack)
[ -z "$stack" ] && die "no stack exists"
local base=$(echo "$stack" | head -n 1)
local first=$(echo "$stack" | sed -n '2p')
[ -z "$first" ] && die "no branches to land"
# Check if first branch is merged into base
git checkout "$base" --quiet
git fetch origin "$base" --quiet 2>/dev/null || true
if ! git merge-base --is-ancestor "$first" "$base" 2>/dev/null; then
if [ "$force" = true ]; then
echo "Warning: '$first' is not an ancestor of '$base' (squash merge?)"
echo "Proceeding with --force..."
else
echo "error: '$first' is not yet merged into '$base'"
echo ""
echo "If the PR was squash-merged, use:"
echo " stak land --force"
exit 1
fi
else
echo "'$first' is merged into '$base'. Cleaning up stack..."
fi
# Remove the landed branch from stack
local new_stack=$(echo "$stack" | sed '2d')
save_stack "$new_stack"
# Delete the local branch
git branch -D "$first" 2>/dev/null || true
# Sync remaining stack onto updated base
local remaining=$(echo "$new_stack" | wc -l | tr -d ' ')
if [ "$remaining" -gt 1 ]; then
echo "Syncing remaining stack onto '$base'..."
cmd_sync
fi
echo "Landed '$first'. Stack updated."
cmd_status
}
do_fold() {
local branch_to_fold="$1"
local direction="$2"
local stack=$(load_stack)
local total=$(echo "$stack" | wc -l | tr -d ' ')
local pos=$(find_in_stack "$branch_to_fold" "$stack")
if [ "$direction" = "up" ]; then
local parent=$(echo "$stack" | sed -n "$((pos - 1))p")
echo "Folding '$branch_to_fold' up into '$parent'..."
git checkout "$parent" --quiet
git reset --hard "$branch_to_fold" --quiet
local new_stack=$(echo "$stack" | sed "${pos}d")
save_stack "$new_stack"
git branch -D "$branch_to_fold" 2>/dev/null
echo "Folded '$branch_to_fold' into '$parent'."
[ "$pos" -lt "$total" ] && echo "Branches above are still correctly based."
else
local child=$(echo "$stack" | sed -n "$((pos + 1))p")
echo "Folding '$branch_to_fold' down into '$child'..."
local new_stack=$(echo "$stack" | sed "${pos}d")
save_stack "$new_stack"
git checkout "$child" --quiet
git branch -D "$branch_to_fold" 2>/dev/null
echo "Folded '$branch_to_fold' into '$child'."
fi
cmd_status
}
cmd_fold() {
ensure_clean
local stack=$(load_stack)
[ -z "$stack" ] && die "no stack exists"
local total=$(echo "$stack" | wc -l | tr -d ' ')
local arg="$1"
# Interactive mode with fzf (no args)
if [ -z "$arg" ] && has_fzf; then
# Step 1: Select branch to fold
local display=$(build_stack_display "$stack")
# Filter: can't fold base (pos 1)
local foldable=$(echo "$display" | tail -n +2) # skip first line (base)
[ -z "$foldable" ] && die "no branches to fold"
local selected=$(echo "$foldable" | fzf --prompt="fold❯ " --header="Select branch to fold" $FZF_OPTS)
[ -z "$selected" ] && exit 0
local branch_to_fold=$(extract_branch "$selected")
local pos=$(find_in_stack "$branch_to_fold" "$stack")
# Step 2: Select direction
local directions=""
local parent=$(echo "$stack" | sed -n "$((pos - 1))p")
local child=$(echo "$stack" | sed -n "$((pos + 1))p")
# Can fold up if not into base (pos > 2)
[ "$pos" -gt 2 ] && directions+="↑ up into $parent"$'\n'
# Can fold down if not at top
[ "$pos" -lt "$total" ] && directions+="↓ down into $child"$'\n'
directions=$(echo "$directions" | sed '/^$/d')
if [ -z "$directions" ]; then
die "cannot fold '$branch_to_fold' - no valid direction"
elif [ $(echo "$directions" | wc -l) -eq 1 ]; then
# Only one option, use it
local dir_choice="$directions"
else
local dir_choice=$(echo "$directions" | fzf --prompt="❯ " --header="Fold '$branch_to_fold' into:" $FZF_OPTS)
[ -z "$dir_choice" ] && exit 0
fi
if [[ "$dir_choice" == ↑* ]]; then
do_fold "$branch_to_fold" "up"
else
do_fold "$branch_to_fold" "down"
fi
return
fi
# Non-interactive mode (current behavior)
local direction="up"
[ "$arg" = "--down" ] || [ "$arg" = "-d" ] && direction="down"
local current=$(current_branch)
local pos=$(find_in_stack "$current" "$stack")
[ -z "$pos" ] && die "'$current' is not in the stack"
[ "$pos" -eq 1 ] && die "cannot fold the base of the stack"
if [ "$direction" = "up" ]; then
[ "$pos" -eq 2 ] && die "cannot fold into the base - use 'land' after merging"
else
[ "$pos" -eq "$total" ] && die "cannot fold down - no branch above"
fi
do_fold "$current" "$direction"
}
cmd_goto() {
local current=$(current_branch)
local stack=$(load_stack)
[ -z "$stack" ] && die "no stack exists"
local target="$1"
local display=$(build_stack_display "$stack")
if [ -n "$target" ]; then
# Direct jump by number or name
if [[ "$target" =~ ^[0-9]+$ ]]; then
local branch=$(echo "$stack" | sed -n "${target}p")
[ -z "$branch" ] && die "invalid position: $target"
else
local branch=$(echo "$stack" | grep -F "$target" | head -n1)
[ -z "$branch" ] && die "branch not found: $target"
fi
elif has_fzf; then
local selected=$(echo "$display" | fzf --prompt="goto❯ " --header="Select branch (↑/↓ move, Enter select)" $FZF_OPTS)
[ -z "$selected" ] && exit 0
local branch=$(extract_branch "$selected")
else
echo "fzf not found. Options:"
echo ""
echo " stak goto <number> Jump by position"
echo " stak goto <name> Jump by branch name"
echo " stak setup-interactive Install fzf"
echo ""
echo "Current stack:"
echo "$display"
exit 1
fi
[ "$branch" = "$current" ] && echo "Already on '$branch'" && exit 0
git checkout "$branch"
echo "Switched to '$branch'"
}
cmd_continue() {
# Check if we're in a rebase
if [ ! -d ".git/rebase-merge" ] && [ ! -d ".git/rebase-apply" ]; then
die "no rebase in progress"
fi
echo "Continuing rebase..."
git rebase --continue || {
echo ""
echo "Still have conflicts. Fix them, then run:"
echo " git add <files>"
echo " stak continue"
exit 1
}
echo ""
echo "Rebase continued. Running sync to finish remaining branches..."
cmd_sync
}
cmd_abort() {
# Check if we're in a rebase
if [ ! -d ".git/rebase-merge" ] && [ ! -d ".git/rebase-apply" ]; then
die "no rebase in progress"
fi
git rebase --abort
echo "Rebase aborted. Stack unchanged."
}
cmd_setup_interactive() {
echo "Stack Interactive Setup"
echo "========================"
echo ""
# Check if fzf already installed
if command -v fzf >/dev/null 2>&1; then
echo "✓ fzf is already installed"
echo ""
echo "Interactive mode is ready. Try: stak goto"
exit 0
fi
echo "fzf is required for interactive branch selection."
echo ""
# Detect platform and package manager
local pkg_manager=""
local install_cmd=""
if [[ "$OSTYPE" == "darwin"* ]]; then
if command -v brew >/dev/null 2>&1; then
pkg_manager="Homebrew"
install_cmd="brew install fzf"
else
echo "macOS detected but Homebrew not found."
echo "Install Homebrew first: https://brew.sh"
echo "Then run: brew install fzf"
exit 1
fi
elif command -v apt >/dev/null 2>&1; then
pkg_manager="apt"
install_cmd="sudo apt install -y fzf"
elif command -v dnf >/dev/null 2>&1; then
pkg_manager="dnf"
install_cmd="sudo dnf install -y fzf"
elif command -v yum >/dev/null 2>&1; then
pkg_manager="yum"
install_cmd="sudo yum install -y fzf"
elif command -v pacman >/dev/null 2>&1; then
pkg_manager="pacman"
install_cmd="sudo pacman -S --noconfirm fzf"
elif command -v apk >/dev/null 2>&1; then
pkg_manager="apk"
install_cmd="sudo apk add fzf"
else
echo "Could not detect package manager."
echo ""
echo "Install fzf manually:"
echo " https://github.com/junegunn/fzf#installation"
exit 1
fi
echo "Detected: $pkg_manager"
echo "Command: $install_cmd"
echo ""
printf "Install fzf now? [y/N]: "
read -r confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
echo ""
echo "Running: $install_cmd"
echo ""
eval "$install_cmd" || {
echo ""
echo "Installation failed. Try running manually:"
echo " $install_cmd"
exit 1
}
echo ""
echo "✓ fzf installed successfully"
echo ""
echo "Interactive mode is ready. Try: stak goto"
else
echo ""
echo "Skipped. To install later, run:"
echo " $install_cmd"