Skip to content

Commit 8b5754b

Browse files
committed
Better detection of changes in remote origin in EasyKey.git
1 parent a9930e8 commit 8b5754b

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

EasyKey.git/ezk-git-functions.sh

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,34 @@ function addAllUntracked () {
6161
}
6262

6363
function mergeChanges () {
64-
echo ""
65-
importantLog "Checking for updates from origin/$actual"
66-
if git diff $actual origin/$actual | grep -q ".*"; then
67-
echo "... found diff between $actual and origin/$actual ..."
68-
if git status | grep "Your branch is ahead"; then
69-
echo "... your local branch is ahead of origin/$actual ... nothing to merge"
70-
else
71-
echo "... your local branch is behind of origin/$actual ... recommend merge !"
72-
diffDrillDownAdvanced "git diff --name-status $actual origin/$actual" " .*" "$actual" "origin/$actual"
73-
executeCommand "git diff --name-status $actual origin/$actual"
74-
read -p "Merge (y/n)? " -n 1 -r
75-
echo # (optional) move to a new line
76-
if [[ $REPLY =~ ^[Yy]$ ]]; then
77-
executeCommand "git merge origin/$actual"
78-
fi
64+
65+
# Get the name of the current branch
66+
current_branch=$(git rev-parse --abbrev-ref HEAD)
67+
68+
# Check if the current branch has an upstream (remote)
69+
if [ -z "$(git rev-parse --abbrev-ref --symbolic-full-name ${current_branch}@{upstream} 2>/dev/null)" ]; then
70+
echo "The current branch '$current_branch' doesn't have an upstream branch."
71+
exit 1
72+
fi
73+
74+
# Get the number of commits ahead of the remote branch
75+
ahead_count=$(git rev-list --count ${current_branch}@{upstream}..${current_branch})
76+
behind_count=$(git rev-list --count ${current_branch}..${current_branch}@{upstream})
77+
78+
# Check if the current branch is ahead of the remote branch
79+
if [ $ahead_count -gt 0 ]; then
80+
echo "Your current branch '$current_branch' is ahead of its remote counterpart by $ahead_count commit(s)."
81+
echo "... nothing to merge ..."
82+
elif [ $behind_count -gt 0 ]; then
83+
echo "Your current branch '$current_branch' is behind of its remote counterpart."
84+
coloredLog " MERGE RECOMMENDED " "$clrPurple" "$clrWhite"
85+
diffDrillDownAdvanced "git diff --name-status origin/$actual $actual" "awk '{print \$2}'" "origin/$actual" "$actual"
86+
executeCommand "git diff --name-status origin/$actual $actual"
87+
read -p "Merge (y/n)? " -n 1 -r
88+
echo # (optional) move to a new line
89+
if [[ $REPLY =~ ^[Yy]$ ]]; then
90+
executeCommand "git merge origin/$actual"
7991
fi
80-
else
81-
echo "... nothing to merge ... up to date"
8292
fi
8393
}
8494

0 commit comments

Comments
 (0)