Skip to content

Instantly share code, notes, and snippets.

@rtablada
Created August 9, 2024 17:04
Show Gist options
  • Save rtablada/80f43e43814e23010a634947b4848284 to your computer and use it in GitHub Desktop.
Save rtablada/80f43e43814e23010a634947b4848284 to your computer and use it in GitHub Desktop.
Rebase and automatically pnpm install to resolve conflicts. For everything else, interactively open vscode
#!/bin/zsh
is_rebasing() {
git rev-parse --git-dir &>/dev/null
if [[ $? -eq 0 && -d "$(git rev-parse --git-dir)/rebase-merge" ]] || [[ -d "$(git rev-parse --git-dir)/rebase-apply" ]]; then
return 0
else
return 1
fi
}
has_conflict_markers() {
file_path=$1
git diff --check "$file_path" | grep -q "^[<>|=]"
return $?
}
# Rebase to origin/develop
if ! is_rebasing; then
git fetch origin
git rebase origin/develop
fi
# Check for conflicts
root=$(git rev-parse --show-toplevel)
while is_rebasing; do
conflicts=$(git -C "$root" diff --name-only --diff-filter=U)
# If there are conflicts
if [[ -n $conflicts ]]; then
# Check if the only conflict is pnpm-lock
if [[ $conflicts == "pnpm-lock.yaml" ]]; then
echo -e "\e[32m✅✅✅ Found conflicts in pnpm-lock.yaml, autofixing those for you!\e[0m"
# Run "pnpm i" and add the file changes
pnpm i
git -C $root add pnpm-lock.yaml
git commit --no-edit --amend
git rebase --continue
else
echo -e "\e[31m🚨🚨🚨 Conflicts found in files other than pnpm-lock.\e[0m"
for file in $conflicts; do
if [[ $file != "pnpm-lock.yaml" ]]; then
while has_conflict_markers "$file"; do
echo -e "\e[33mConflicts found in \"$file\".\e[0m"
echo -e "\e[33mDo you want to open the conflicting files in VSCode? \"y\" to open, \"n\" to stop the script: \e[0m"
read open_vscode
if [[ $open_vscode =~ ^[Yy]$ ]]; then
echo "Opening \"$file\" press enter to return after you have resolved conflicts."
code "$root/$file"
else
echo -e "\e[31mExiting script, rerun \"rebase_pnpm\" when you are ready again.\e[0m"
return 1
fi
done
fi
done
fi
else
added_files=$(git diff --name-only --cached)
if [[ -n $added_files ]]; then
echo -e "\e[31m✅✅✅ All rebase conflicts are resolved, continuing rebase.\e[0m"
git commit --no-edit --amend
git rebase --continue
else
echo -e "\e[31m🚨🚨🚨 No conflicts found, you probably have an empty commit or strange merge conflict.\e[0m"
return 1
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment