Skip to content

Fix status() for case where there is no remote repo #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 51 additions & 38 deletions bin/gg
Original file line number Diff line number Diff line change
Expand Up @@ -526,39 +526,50 @@ fetch() {
}

status() {
printf "\e[2m\e[90m[\e[22m\e[0m$(success "$(current_branch)") \e[90m->\e[39m $(success "$(current_remote)/$(current_branch)")\e[2m\e[90m|\e[22m\e[0m$(notice $(latest_commit_hash))\e[2m\e[90m]\e[22m\e[0m \e[90m%s\e[39m\n" "$(latest_commit_message)"
if [ -z "$(git remote)" ]; then
echo "No remote repo."
echo

echo
has_remote=0
else
has_remote=1
fi

# http://stackoverflow.com/a/3278427
git fetch --quiet
if [ $has_remote -eq 1 ];then
printf "\e[2m\e[90m[\e[22m\e[0m$(success "$(current_branch)") \e[90m->\e[39m $(success "$(current_remote)/$(current_branch)")\e[2m\e[90m|\e[22m\e[0m$(notice $(latest_commit_hash))\e[2m\e[90m]\e[22m\e[0m \e[90m%s\e[39m\n" "$(latest_commit_message)"

echo

LOCAL=$(git rev-parse @{0})
REMOTE=$(git rev-parse @{u})
BASE=$(git merge-base @{0} @{u})
# http://stackoverflow.com/a/3278427
git fetch --quiet

push=0
pull=0
LOCAL=$(git rev-parse @{0})
REMOTE=$(git rev-parse @{u})
BASE=$(git merge-base @{0} @{u})

if [ "$LOCAL" != "$REMOTE" ]; then
position=($(git rev-list --left-right --count $(current_branch)...$(current_remote)/$(current_branch)))
push=0
pull=0

ahead=${position[0]}
behind=${position[1]}
fi
if [ "$LOCAL" != "$REMOTE" ]; then
position=($(git rev-list --left-right --count $(current_branch)...$(current_remote)/$(current_branch)))

if [ "$LOCAL" = "$REMOTE" ]; then
display "position" "up-to-date with remote"
elif [ "$LOCAL" = "$BASE" ]; then
display_active "position" "$(notice "$behind commits behind remote (pull)")"
pull=1
elif [ "$REMOTE" = "$BASE" ]; then
display_active "position" "$(notice "$ahead commits ahead of remote (push)")"
push=1
else
display_active "position" "$(whoops "diverged from remote") $(whoops_subtext "($ahead commits ahead, $behind commits behind)")"
pull=1
push=1
ahead=${position[0]}
behind=${position[1]}
fi

if [ "$LOCAL" = "$REMOTE" ]; then
display "position" "up-to-date with remote"
elif [ "$LOCAL" = "$BASE" ]; then
display_active "position" "$(notice "$behind commits behind remote (pull)")"
pull=1
elif [ "$REMOTE" = "$BASE" ]; then
display_active "position" "$(notice "$ahead commits ahead of remote (push)")"
push=1
else
display_active "position" "$(whoops "diverged from remote") $(whoops_subtext "($ahead commits ahead, $behind commits behind)")"
pull=1
push=1
fi
fi

git diff --exit-code --quiet
Expand All @@ -576,24 +587,26 @@ status() {
display_active "commits " "$(notice "not all changes committed (commit changes)")"
fi

if [ $push -eq 1 ] | [ $pull -eq 1 ]; then
echo
fi
if [ $has_remote -eq 1 ]; then
if [ $push -eq 1 ] | [ $pull -eq 1 ]; then
echo
fi

if [ $push -eq 1 ]; then
echo
if [ $push -eq 1 ]; then
echo

printf "\e[2mcommits ahead (to push):\e[22m\n"
printf "\e[2mcommits ahead (to push):\e[22m\n"

all_commits_to_push_pretty
fi
all_commits_to_push_pretty
fi

if [ $pull -eq 1 ]; then
echo
if [ $pull -eq 1 ]; then
echo

printf "\e[2mcommits behind (to pull):\e[22m\n"
printf "\e[2mcommits behind (to pull):\e[22m\n"

all_commits_to_pull_pretty
all_commits_to_pull_pretty
fi
fi

# Source: http://stackoverflow.com/questions/5096268/git-reports-get-changed-files
Expand Down