summaryrefslogtreecommitdiffstats
path: root/sonar-base.sh
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-base.sh')
-rwxr-xr-xsonar-base.sh134
1 files changed, 79 insertions, 55 deletions
diff --git a/sonar-base.sh b/sonar-base.sh
index 5ea2a0f..95d943e 100755
--- a/sonar-base.sh
+++ b/sonar-base.sh
@@ -7,11 +7,11 @@ remote=""
rcfile_path="$HOME"
timethis() {
- cmd="$@"
+ cmd=("$@")
start=$(gdate +%s.%N)
- eval $cmd
+ eval "${cmd[@]}"
dur=$(echo "$(gdate +%s.%N) - $start" | bc)
- echo "$1 - $dur" >> $HOME/duration.dat
+ echo "$1 - $dur" >> "$HOME/duration.dat"
}
get_fetch_time() {
@@ -24,7 +24,7 @@ get_fetch_time() {
fi
FETCH_TIME="${GIT_RADAR_FETCH_TIME:-"$((5 * 60))"}"
- echo $FETCH_TIME
+ echo "$FETCH_TIME"
}
@@ -75,28 +75,29 @@ prepare_zsh_colors() {
PRINT_F_OPTION="%s"
- COLOR_REMOTE_AHEAD="%{${GIT_RADAR_COLOR_REMOTE_AHEAD:-$fg_bold[green]}%}"
- COLOR_REMOTE_BEHIND="%{${GIT_RADAR_COLOR_REMOTE_BEHIND:-$fg_bold[red]}%}"
- COLOR_REMOTE_DIVERGED="%{${GIT_RADAR_COLOR_REMOTE_DIVERGED:-$fg_bold[yellow]}%}"
- COLOR_REMOTE_NOT_UPSTREAM="%{${GIT_RADAR_COLOR_REMOTE_NOT_UPSTREAM:-$fg_bold[red]}%}"
+ COLOR_REMOTE_AHEAD="%{${GIT_RADAR_COLOR_REMOTE_AHEAD:-${fg_bold[green]}}%}"
+ COLOR_REMOTE_BEHIND="%{${GIT_RADAR_COLOR_REMOTE_BEHIND:-${fg_bold[red]}}%}"
+ COLOR_REMOTE_DIVERGED="%{${GIT_RADAR_COLOR_REMOTE_DIVERGED:-${fg_bold[yellow]}}%}"
+ COLOR_REMOTE_NOT_UPSTREAM="%{${GIT_RADAR_COLOR_REMOTE_NOT_UPSTREAM:-${fg_bold[red]}}%}"
- COLOR_LOCAL_AHEAD="%{${GIT_RADAR_COLOR_LOCAL_AHEAD:-$fg_bold[green]}%}"
- COLOR_LOCAL_BEHIND="%{${GIT_RADAR_COLOR_LOCAL_BEHIND:-$fg_bold[red]}%}"
- COLOR_LOCAL_DIVERGED="%{${GIT_RADAR_COLOR_LOCAL_DIVERGED:-$fg_bold[yellow]}%}"
+ COLOR_LOCAL_AHEAD="%{${GIT_RADAR_COLOR_LOCAL_AHEAD:-${fg_bold[green]}}%}"
+ COLOR_LOCAL_BEHIND="%{${GIT_RADAR_COLOR_LOCAL_BEHIND:-${fg_bold[red]}}%}"
+ COLOR_LOCAL_DIVERGED="%{${GIT_RADAR_COLOR_LOCAL_DIVERGED:-${fg_bold[yellow]}}%}"
- COLOR_CHANGES_STAGED="%{${GIT_RADAR_COLOR_CHANGES_STAGED:-$fg_bold[green]}%}"
- COLOR_CHANGES_UNSTAGED="%{${GIT_RADAR_COLOR_CHANGES_UNSTAGED:-$fg_bold[red]}%}"
- COLOR_CHANGES_CONFLICTED="%{${GIT_RADAR_COLOR_CHANGES_CONFLICTED:-$fg_bold[yellow]}%}"
- COLOR_CHANGES_UNTRACKED="%{${GIT_RADAR_COLOR_CHANGES_UNTRACKED:-$fg_bold[white]}%}"
+ COLOR_CHANGES_STAGED="%{${GIT_RADAR_COLOR_CHANGES_STAGED:-${fg_bold[green]}}%}"
+ COLOR_CHANGES_UNSTAGED="%{${GIT_RADAR_COLOR_CHANGES_UNSTAGED:-${fg_bold[red]}}%}"
+ COLOR_CHANGES_CONFLICTED="%{${GIT_RADAR_COLOR_CHANGES_CONFLICTED:-${fg_bold[yellow]}}%}"
+ COLOR_CHANGES_UNTRACKED="%{${GIT_RADAR_COLOR_CHANGES_UNTRACKED:-${fg_bold[white]}}%}"
- COLOR_STASH="%{${GIT_RADAR_COLOR_STASH:-$fg_bold[yellow]}%}"
+ COLOR_STASH="%{${GIT_RADAR_COLOR_STASH:-${fg_bold[yellow]}}%}"
- local italic_m="$(printf '\xF0\x9D\x98\xAE')"
+ local italic_m
+ italic_m="$(printf '\xF0\x9D\x98\xAE')"
COLOR_BRANCH="%{${GIT_RADAR_COLOR_BRANCH:-$reset_color}%}"
MASTER_SYMBOL="${GIT_RADAR_MASTER_SYMBOL:-"%{$reset_color%}$italic_m%{$reset_color%}"}"
- PROMPT_FORMAT="${GIT_RADAR_FORMAT:-" %{$fg_bold[grey]%}git:(%{$reset_color%}%{remote: }%{branch}%{ :local}%{$fg_bold[grey]%})%{$reset_color%}%{ :stash}%{ :changes}"}"
+ PROMPT_FORMAT="${GIT_RADAR_FORMAT:-" %{${fg_bold[grey]}%}git:(%{$reset_color%}%{remote: }%{branch}%{ :local}%{${fg_bold[grey]}%})%{$reset_color%}%{ :stash}%{ :changes}"}"
RESET_COLOR_LOCAL="%{${GIT_RADAR_COLOR_LOCAL_RESET:-$reset_color}%}"
RESET_COLOR_REMOTE="%{${GIT_RADAR_COLOR_REMOTE_RESET:-$reset_color}%}"
@@ -106,8 +107,9 @@ prepare_zsh_colors() {
}
in_current_dir() {
- local wd="$(pwd)"
- if [[ "$wd" == $cwd ]]; then
+ local wd
+ wd="$(pwd)"
+ if [[ "$wd" == "$cwd" ]]; then
cwd="$wd"
return 0
else
@@ -124,20 +126,20 @@ debug_print() {
local debug=$1
local message=$2
if [[ $debug == "debug" ]]; then
- echo $message
+ echo "$message"
fi
}
dot_git() {
if in_current_dir && [[ -n "$dot_git" ]]; then
# cache dot_git to save calls to rev-parse
- printf '%s' $dot_git
+ printf '%s' "$dot_git"
elif [ -d .git ]; then
dot_git=".git"
printf '%s' $dot_git
else
dot_git="$(git rev-parse --git-dir 2>/dev/null)"
- printf '%s' $dot_git
+ printf '%s' "$dot_git"
fi
}
@@ -207,7 +209,7 @@ fetch() {
# Gives $FETCH_TIME a value
get_fetch_time
- if time_to_update $FETCH_TIME; then
+ if time_to_update "$FETCH_TIME"; then
record_timestamp
git fetch --quiet > /dev/null 2>&1
fi
@@ -236,12 +238,15 @@ branch_ref() {
}
remote_branch_name() {
- local localRef="$(branch_name)"
- local remote="$(git config --get "branch.$localRef.remote")"
+ local localRef
+ local remote
+ localRef="$(branch_name)"
+ remote="$(git config --get "branch.$localRef.remote")"
if [[ -n $remote ]]; then
- local remoteBranch="$(git config --get "branch.${localRef}.merge" | sed -e 's/^refs\/heads\///')"
+ local remoteBranch
+ remoteBranch="$(git config --get "branch.${localRef}.merge" | sed -e 's/^refs\/heads\///')"
if [[ -n $remoteBranch ]]; then
- printf '%s/%s' $remote $remoteBranch
+ printf '%s/%s' "$remote" "$remoteBranch"
return 0
else
return 1
@@ -254,7 +259,7 @@ remote_branch_name() {
commits_behind_of_remote() {
remote_branch=${1:-"$(remote_branch_name)"}
if [[ -n "$remote_branch" ]]; then
- git rev-list --left-only --count ${remote_branch}...HEAD 2>/dev/null
+ git rev-list --left-only --count "${remote_branch}...HEAD" 2>/dev/null
else
printf '%s' "0"
fi
@@ -263,7 +268,7 @@ commits_behind_of_remote() {
commits_ahead_of_remote() {
remote_branch=${1:-"$(remote_branch_name)"}
if [[ -n "$remote_branch" ]]; then
- git rev-list --right-only --count ${remote_branch}...HEAD 2>/dev/null
+ git rev-list --right-only --count "${remote_branch}...HEAD" 2>/dev/null
else
printf '%s' "0"
fi
@@ -271,10 +276,10 @@ commits_ahead_of_remote() {
determine_tracked_remote() {
by_branch=$(git config --local branch."$(git rev-parse --abbrev-ref HEAD)".git-radar-tracked-remote)
- [[ ! -z $by_branch ]] && echo $by_branch && return 0
+ [[ ! -z "$by_branch" ]] && echo "$by_branch" && return 0
by_config=$(git config --local git-radar.tracked-remote)
- [[ ! -z $by_config ]] && echo $by_config && return 0
+ [[ ! -z "$by_config" ]] && echo "$by_config" && return 0
echo "origin/master"
}
@@ -283,7 +288,7 @@ remote_behind_of_master() {
remote_branch=${1:-"$(remote_branch_name)"}
tracked_remote=$(determine_tracked_remote)
if [[ -n "$remote_branch" && "$remote_branch" != "$tracked_remote" ]]; then
- git rev-list --left-only --count ${tracked_remote}...${remote_branch} 2>/dev/null || printf '%s' "0"
+ git rev-list --left-only --count "${tracked_remote}...${remote_branch}" 2>/dev/null || printf '%s' "0"
else
printf '%s' "0"
fi
@@ -293,7 +298,7 @@ remote_ahead_of_master() {
remote_branch=${1:-"$(remote_branch_name)"}
tracked_remote=$(determine_tracked_remote)
if [[ -n "$remote_branch" && "$remote_branch" != "$tracked_remote" ]]; then
- git rev-list --right-only --count ${tracked_remote}...${remote_branch} 2>/dev/null || printf '%s' "0"
+ git rev-list --right-only --count "${tracked_remote}...${remote_branch}" 2>/dev/null || printf '%s' "0"
else
printf '%s' "0"
fi
@@ -347,12 +352,18 @@ staged_status() {
local suffix=${3:-""}
local staged_string=""
- local filesModified="$(printf '%s' "$gitStatus" | grep -oE "M[ACDRM ] " | wc -l | grep -oEi '[1-9][0-9]*')"
- local filesAdded="$(printf '%s' "$gitStatus" | grep -oE "A[MCDR ] " | wc -l | grep -oEi '[1-9][0-9]*')"
- local filesDeleted="$(printf '%s' "$gitStatus" | grep -oE "D[AMCR ] " | wc -l | grep -oEi '[1-9][0-9]*')"
- local filesRenamed="$(printf '%s' "$gitStatus" | grep -oE "R[AMCD ] " | wc -l | grep -oEi '[1-9][0-9]*')"
- local filesCopied="$(printf '%s' "$gitStatus" | grep -oE "C[AMDR ] " | wc -l | grep -oEi '[1-9][0-9]*')"
- local typeChanged="$(printf '%s' "$gitStatus" | grep -oE "T[AMDR ] " | wc -l | grep -oEi '[1-9][0-9]*')"
+ local filesModified
+ local filesAdded
+ local filesDeleted
+ local filesRenamed
+ local filesCopied
+ local typeChanged
+ filesModified="$(printf '%s' "$gitStatus" | grep -oE "M[ACDRM ] " | wc -l | grep -oEi '[1-9][0-9]*')"
+ filesAdded="$(printf '%s' "$gitStatus" | grep -oE "A[MCDR ] " | wc -l | grep -oEi '[1-9][0-9]*')"
+ filesDeleted="$(printf '%s' "$gitStatus" | grep -oE "D[AMCR ] " | wc -l | grep -oEi '[1-9][0-9]*')"
+ filesRenamed="$(printf '%s' "$gitStatus" | grep -oE "R[AMCD ] " | wc -l | grep -oEi '[1-9][0-9]*')"
+ filesCopied="$(printf '%s' "$gitStatus" | grep -oE "C[AMDR ] " | wc -l | grep -oEi '[1-9][0-9]*')"
+ typeChanged="$(printf '%s' "$gitStatus" | grep -oE "T[AMDR ] " | wc -l | grep -oEi '[1-9][0-9]*')"
if [ -n "$filesAdded" ]; then
staged_string="$staged_string$filesAdded${prefix}A${suffix}"
@@ -381,9 +392,12 @@ conflicted_status() {
local suffix=${3:-""}
local conflicted_string=""
- local filesUs="$(printf '%s' "$gitStatus" | grep -oE "[AD]U " | wc -l | grep -oEi '[1-9][0-9]*')"
- local filesThem="$(printf '%s' "$gitStatus" | grep -oE "U[AD] " | wc -l | grep -oEi '[1-9][0-9]*')"
- local filesBoth="$(printf '%s' "$gitStatus" | grep -oE "(UU|AA|DD) " | wc -l | grep -oEi '[1-9][0-9]*')"
+ local filesUs
+ local filesThem
+ local filesBoth
+ filesUs="$(printf '%s' "$gitStatus" | grep -oE "[AD]U " | wc -l | grep -oEi '[1-9][0-9]*')"
+ filesThem="$(printf '%s' "$gitStatus" | grep -oE "U[AD] " | wc -l | grep -oEi '[1-9][0-9]*')"
+ filesBoth="$(printf '%s' "$gitStatus" | grep -oE "(UU|AA|DD) " | wc -l | grep -oEi '[1-9][0-9]*')"
if [ -n "$filesUs" ]; then
conflicted_string="$conflicted_string$filesUs${prefix}U${suffix}"
@@ -403,9 +417,12 @@ unstaged_status() {
local suffix=${3:-""}
local unstaged_string=""
- local filesModified="$(printf '%s' "$gitStatus" | grep -oE "[ACDRM ]M " | wc -l | grep -oEi '[1-9][0-9]*')"
- local filesDeleted="$(printf '%s' "$gitStatus" | grep -oE "[AMCR ]D " | wc -l | grep -oEi '[1-9][0-9]*')"
- local typeChanged="$(printf '%s' "$gitStatus" | grep -oE "[AMDR ]T " | wc -l | grep -oEi '[1-9][0-9]*')"
+ local filesModified
+ local filesDeleted
+ local typeChanged
+ filesModified="$(printf '%s' "$gitStatus" | grep -oE "[ACDRM ]M " | wc -l | grep -oEi '[1-9][0-9]*')"
+ filesDeleted="$(printf '%s' "$gitStatus" | grep -oE "[AMCR ]D " | wc -l | grep -oEi '[1-9][0-9]*')"
+ typeChanged="$(printf '%s' "$gitStatus" | grep -oE "[AMDR ]T " | wc -l | grep -oEi '[1-9][0-9]*')"
if [ -n "$filesDeleted" ]; then
unstaged_string="$unstaged_string$filesDeleted${prefix}D${suffix}"
@@ -425,7 +442,8 @@ untracked_status() {
local suffix=${3:-""}
local untracked_string=""
- local filesUntracked="$(printf '%s' "$gitStatus" | grep "?? " | wc -l | grep -oEi '[1-9][0-9]*')"
+ local filesUntracked
+ filesUntracked="$(printf '%s' "$gitStatus" | grep "?? " | wc -l | grep -oEi '[1-9][0-9]*')"
if [ -n "$filesUntracked" ]; then
untracked_string="$untracked_string$filesUntracked${prefix}?${suffix}"
@@ -436,14 +454,19 @@ untracked_status() {
color_changes_status() {
local separator="${1:- }"
- local porcelain="$(porcelain_status)"
+ local porcelain
+ porcelain="$(porcelain_status)"
local changes=""
if [[ -n "$porcelain" ]]; then
- local staged_changes="$(staged_status "$porcelain" "$COLOR_CHANGES_STAGED" "$RESET_COLOR_CHANGES")"
- local unstaged_changes="$(unstaged_status "$porcelain" "$COLOR_CHANGES_UNSTAGED" "$RESET_COLOR_CHANGES")"
- local untracked_changes="$(untracked_status "$porcelain" "$COLOR_CHANGES_UNTRACKED" "$RESET_COLOR_CHANGES")"
- local conflicted_changes="$(conflicted_status "$porcelain" "$COLOR_CHANGES_CONFLICTED" "$RESET_COLOR_CHANGES")"
+ local staged_changes
+ local unstaged_changes
+ local untracked_changes
+ local conflicted_changes
+ staged_changes="$(staged_status "$porcelain" "$COLOR_CHANGES_STAGED" "$RESET_COLOR_CHANGES")"
+ unstaged_changes="$(unstaged_status "$porcelain" "$COLOR_CHANGES_UNSTAGED" "$RESET_COLOR_CHANGES")"
+ untracked_changes="$(untracked_status "$porcelain" "$COLOR_CHANGES_UNTRACKED" "$RESET_COLOR_CHANGES")"
+ conflicted_changes="$(conflicted_status "$porcelain" "$COLOR_CHANGES_CONFLICTED" "$RESET_COLOR_CHANGES")"
if [[ -n "$staged_changes" ]]; then
staged_changes="$separator$staged_changes"
fi
@@ -549,7 +572,7 @@ bash_readable_branch_name() {
}
show_remote_status() {
- if [[ $@ == *$NO_REMOTE_STATUS* ]]; then
+ if [[ "$*" == "*$NO_REMOTE_STATUS*" ]]; then
return 1 # don't show the git remote status
fi
return 0
@@ -571,8 +594,9 @@ is_cwd_a_dot_git_directory() {
stash_status() {
if ! is_cwd_a_dot_git_directory "$PWD"; then
- local number_stashes="$(stashed_status)"
- if [ $number_stashes -gt 0 ]; then
+ local number_stashes
+ number_stashes="$(stashed_status)"
+ if [ "$number_stashes" -gt 0 ]; then
printf $PRINT_F_OPTION "${number_stashes}${COLOR_STASH}≡${RESET_COLOR_STASH}"
fi
fi