Skip to content

Instantly share code, notes, and snippets.

@BasixKOR
Created September 6, 2024 04:48
Show Gist options
  • Save BasixKOR/0a9ffd97b2c1b87f4e1e47d5f9b4f13e to your computer and use it in GitHub Desktop.
Save BasixKOR/0a9ffd97b2c1b87f4e1e47d5f9b4f13e to your computer and use it in GitHub Desktop.
yarn.lock resolve conflicts
#!/bin/zsh
set -euo pipefail
# yarn.lock 문제의 해소는 크게 3단계로 나뉩니다.
#
# 1. 먼저 conflict 난 파일 목록에 yarn.lock 외의 다른 파일이 없는지 봅니다.
# 2. 없다면, yarn.lock을 고칩니다.
# 3. 고쳐졌다면, 현재 작업이 merge/rebase인지 확인하고 알맞게 동작합니다.
# Copyright (c) 2024 Software Freedom Conversancy
# LICENSE: https://github.com/git/git/blob/master/COPYING
# Some codes are copied from: https://github.com/git/git/blob/2e7b89e038c0c888acf61f1b4ee5a43d4dd5e94c/contrib/completion/git-prompt.sh
eval "$(printf '
__git_SOH="\001" __git_STX="\002" __git_ESC="\033"
__git_LF="\n" __git_CRLF="\r\n"
')"
__git_eread () {
test -r "$1" && IFS=$__git_CRLF read -r "$2" <"$1"
}
local repo_info
repo_info="$(git rev-parse --show-toplevel --git-dir)"
if [ -z "$repo_info" ]; then
exit -1;
fi
local LF="$__git_LF"
local toplevel="${repo_info%$LF*}"
local g="${repo_info##*$LF}"
local _git_status _git_head
_update_status () {
if [ -d "$g/rebase-merge" ]; then
_git_status="rebase"
__git_eread "$g/rebase-merge/onto" _git_head
elif [ -f "$g/MERGE_HEAD" ]; then
_git_status="merge"
__git_eread "$g/MERGE_HEAD" _git_head
else
_git_status="none"
_git_head=""
fi
}
_update_status
while [[ $_git_status != 'none' ]]; do
# https://stackoverflow.com/a/10874862
other_conflicts=($(git diff --name-only --diff-filter=U))
for conflict in $other_conflicts; do
if [[ $conflict != 'yarn.lock' ]]; then
echo "Non yarn.lock conflicts found; please fix and run again";
exit -1;
fi
done
# Resolve the yarn.lock conflict
git restore -s "$_git_head" $toplevel/yarn.lock >/dev/null
gum spin --spinner dot --title "yarn install" -- yarn install
git add $toplevel/yarn.lock &>/dev/null
case $_git_status in
rebase)
GIT_EDITOR=true git rebase --continue &>/dev/null || true
;;
merge)
git commit --no-edit >/dev/null
;;
*)
esac
git show --oneline -s
_update_status
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment