Skip to content

Commit 0000063

Browse files
committed
used associative array to prevent unnecessary rebasing which messes up everything
1 parent 0000062 commit 0000063

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

git-linearize

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,16 @@ function cmd_linearize() {
233233
pre_branch=$(git branch --show-current)
234234

235235
if $EL_REBASE_SPLITS; then
236-
split_commits=()
236+
declare -A split_commit_map
237237
for branch in $(git for-each-ref --format="%(refname:short)" refs/heads/); do
238238
if [ $branch == $pre_branch ]; then
239239
continue
240240
fi
241241
# merge_base=$(git merge-base "${commits[0]}" "$branch")
242242
merge_base=$(git merge-base $pre_branch $branch)
243243
echodebug "Found merge base $merge_base for branch $branch"
244-
split_commits+=("$merge_base")
244+
split_commit_map["$merge_base"]="$branch"
245245
done
246-
echodebug "Split commits: ${split_commits[*]}"
247246
fi
248247

249248
# Find start commit
@@ -292,20 +291,17 @@ function cmd_linearize() {
292291
echoinfo "[x] $sha1 is now $new_sha"
293292

294293
if $EL_REBASE_SPLITS; then
295-
if [[ " ${split_commits[*]} " =~ " ${sha1} " ]]; then
294+
# https://stackoverflow.com/a/46243464/14681457
295+
if [[ -n "${split_commit_map[$sha1]+unset}" ]]; then
296296
# List all branches that share this commit
297297
# Then loop through them and rebase them onto the new commit.
298-
echodebug "branches $EL_REBASE_SPLITS that contain this commit: $(git branch --contains $sha1)"
299-
for branch in $(git branch --contains $sha1); do
300-
if [ $branch != $pre_branch ]; then
301-
echodebug "rebasing $branch onto $new_sha"
302-
git switch $branch | debug
303-
git rebase --onto $new_sha $sha1 | debug
304-
git switch extremely-linear | debug
305-
fi
306-
done
298+
branch=${split_commit_map[$sha1]}
299+
git switch $branch | debug
300+
git rebase --onto $new_sha $sha1 | debug
301+
git switch extremely-linear | debug
307302
else
308-
echo "$sha1 not in $split_commits"
303+
# echo "$sha1 not in ${split_commit_map[*]}"
304+
printf "$sha1 not in %s\n" "${!split_commit_map[@]}" | debug
309305
fi
310306
fi
311307
fi # skip if hash is already nice

testytest.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ make_dummy_commit
1010
make_dummy_commit
1111
git switch main
1212
make_dummy_commit
13+
git checkout -b branch-2
14+
make_dummy_commit
15+
git switch main
1316
make_dummy_commit
1417
# a-b-e-f main
1518
# \
@@ -26,4 +29,4 @@ else
2629
echo "nay! $(git merge-base main new-branch)"
2730
fi
2831

29-
# git log --graph --all
32+
git log --graph --all

0 commit comments

Comments
 (0)