From 7d46f27d6087e6e238af0963634e43e91ec8fcc6 Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Wed, 21 Oct 2015 09:57:05 +0100 Subject: [PATCH 01/12] ZSH only version of async files changes --- radar-base.sh | 84 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 23 deletions(-) diff --git a/radar-base.sh b/radar-base.sh index 099debd..b7ea508 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -25,7 +25,28 @@ get_fetch_time() { FETCH_TIME="${GIT_RADAR_FETCH_TIME:-"$((5 * 60))"}" echo $FETCH_TIME +} + +function top_zsh_pid { + # Look up the parent of the given PID. + pid="${1:-$$}" + found_zsh="${2}" + ppid="$(ps -p $pid -o ppid=)" + ppid_name="$(ps -p $ppid -o comm=)" + + #printf "${ppid}:${ppid_name} -> " + # /sbin/init always has a PID of 1, so if you reach that, the current PID is + # the top-level parent. Otherwise, keep looking. + if [[ ${ppid} -eq 1 ]] ; then + printf "${ppid}" + elif [[ ${ppid_name} == *zsh* ]]; then + top_zsh_pid "${ppid}" "true" + elif [[ $found_zsh == "true" ]]; then + printf "${pid}" + else + top_zsh_pid "${ppid}" "false" + fi } prepare_bash_colors() { @@ -411,35 +432,52 @@ untracked_status() { } color_changes_status() { - local separator="${1:- }" - - local 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")" - if [[ -n "$staged_changes" ]]; then - staged_changes="$separator$staged_changes" - fi + _async_changes() { + local zsh_parent_pid="$1" + local separator="${2:- }" + + local 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")" + if [[ -n "$staged_changes" ]]; then + staged_changes="$separator$staged_changes" + fi - if [[ -n "$unstaged_changes" ]]; then - unstaged_changes="$separator$unstaged_changes" - fi + if [[ -n "$unstaged_changes" ]]; then + unstaged_changes="$separator$unstaged_changes" + fi - if [[ -n "$conflicted_changes" ]]; then - conflicted_changes="$separator$conflicted_changes" - fi + if [[ -n "$conflicted_changes" ]]; then + conflicted_changes="$separator$conflicted_changes" + fi + + if [[ -n "$untracked_changes" ]]; then + untracked_changes="$separator$untracked_changes" + fi - if [[ -n "$untracked_changes" ]]; then - untracked_changes="$separator$untracked_changes" + changes="$staged_changes$conflicted_changes$unstaged_changes$untracked_changes" fi - changes="$staged_changes$conflicted_changes$unstaged_changes$untracked_changes" + printf $PRINT_F_OPTION "${changes:1}" + kill -s USR1 "$zsh_parent_pid" + } + + zsh_parent_pid="$(top_zsh_pid "$PPID")" + + if [[ -f $(dot_git)/git_radar_changes ]]; then + cat $(dot_git)/git_radar_changes + fi + if [[ ! -f $(dot_git)/git_radar_working ]]; then + (_async_changes "$zsh_parent_pid")&> $(dot_git)/git_radar_changes & + touch $(dot_git)/git_radar_working + else + rm $(dot_git)/git_radar_working fi - printf $PRINT_F_OPTION "${changes:1}" } bash_color_changes_status() { From 475e83b1f6bc4ab04fe7bcab0eabbf7416904d9c Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Wed, 4 Nov 2015 12:20:15 +0000 Subject: [PATCH 02/12] Cleaned up async functionality, no more messy pid finding --- radar-base.sh | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/radar-base.sh b/radar-base.sh index b7ea508..8667dd7 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -433,7 +433,7 @@ untracked_status() { color_changes_status() { _async_changes() { - local zsh_parent_pid="$1" + local parent_pid="$1" local separator="${2:- }" local porcelain="$(porcelain_status)" @@ -464,19 +464,16 @@ color_changes_status() { fi printf $PRINT_F_OPTION "${changes:1}" - kill -s USR1 "$zsh_parent_pid" + kill -s USR1 "$parent_pid" # Tell the parent we are finished } - zsh_parent_pid="$(top_zsh_pid "$PPID")" - if [[ -f $(dot_git)/git_radar_changes ]]; then cat $(dot_git)/git_radar_changes fi - if [[ ! -f $(dot_git)/git_radar_working ]]; then - (_async_changes "$zsh_parent_pid")&> $(dot_git)/git_radar_changes & - touch $(dot_git)/git_radar_working - else - rm $(dot_git)/git_radar_working + + GIT_RADAR_REDRAW=${GIT_RADAR_REDRAW:-false} + if ! $GIT_RADAR_REDRAW; then + (_async_changes "$GIT_RADAR_ZSH_PID")&> $(dot_git)/git_radar_changes & fi } From ca6b050931d02a4edb1f02958190a4b04fb51112 Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Wed, 4 Nov 2015 12:22:49 +0000 Subject: [PATCH 03/12] Remove unneeded pid finding function --- radar-base.sh | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/radar-base.sh b/radar-base.sh index 8667dd7..7e18df0 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -27,28 +27,6 @@ get_fetch_time() { echo $FETCH_TIME } -function top_zsh_pid { - # Look up the parent of the given PID. - pid="${1:-$$}" - found_zsh="${2}" - ppid="$(ps -p $pid -o ppid=)" - ppid_name="$(ps -p $ppid -o comm=)" - - #printf "${ppid}:${ppid_name} -> " - - # /sbin/init always has a PID of 1, so if you reach that, the current PID is - # the top-level parent. Otherwise, keep looking. - if [[ ${ppid} -eq 1 ]] ; then - printf "${ppid}" - elif [[ ${ppid_name} == *zsh* ]]; then - top_zsh_pid "${ppid}" "true" - elif [[ $found_zsh == "true" ]]; then - printf "${pid}" - else - top_zsh_pid "${ppid}" "false" - fi -} - prepare_bash_colors() { if [ -f "$rcfile_path/.gitradarrc.bash" ]; then source "$rcfile_path/.gitradarrc.bash" From 8645c05d417202e5fa684ba2b1405e8454b99e2a Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Wed, 4 Nov 2015 14:26:33 +0000 Subject: [PATCH 04/12] Generalise the async approach --- radar-base.sh | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/radar-base.sh b/radar-base.sh index 7e18df0..7370348 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -409,6 +409,24 @@ untracked_status() { printf '%s' "$untracked_string" } +async_or_not() { + local function_to_run="$1" + local file="$(dot_git)/async_git_radar_$2" + local working_file="$(dot_git)/async_git_radar_workers" + + if [[ -f $file ]]; then + cat $file + fi + + GIT_RADAR_REDRAW=${GIT_RADAR_REDRAW:-false} + if ! $GIT_RADAR_REDRAW; then + ( + eval $function_to_run > $file + kill -s USR1 "$GIT_RADAR_ZSH_PID" # Tell the parent we are finished + ) > /dev/null & + fi +} + color_changes_status() { _async_changes() { local parent_pid="$1" @@ -442,17 +460,9 @@ color_changes_status() { fi printf $PRINT_F_OPTION "${changes:1}" - kill -s USR1 "$parent_pid" # Tell the parent we are finished } - if [[ -f $(dot_git)/git_radar_changes ]]; then - cat $(dot_git)/git_radar_changes - fi - - GIT_RADAR_REDRAW=${GIT_RADAR_REDRAW:-false} - if ! $GIT_RADAR_REDRAW; then - (_async_changes "$GIT_RADAR_ZSH_PID")&> $(dot_git)/git_radar_changes & - fi + async_or_not "_async_changes '$GIT_RADAR_ZSH_PID'" "changes" } bash_color_changes_status() { From 96d6dc5e96980302bc5a4c274952142ee2ee9d0c Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Wed, 4 Nov 2015 15:59:45 +0000 Subject: [PATCH 05/12] Only run async if the user wants us to --- radar-base.sh | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/radar-base.sh b/radar-base.sh index 7370348..a7856e8 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -411,19 +411,25 @@ untracked_status() { async_or_not() { local function_to_run="$1" - local file="$(dot_git)/async_git_radar_$2" - local working_file="$(dot_git)/async_git_radar_workers" - if [[ -f $file ]]; then - cat $file - fi + GIT_RADAR_ASYNC_EXEC=${GIT_RADAR_ASYNC_EXEC:-false} + if $GIT_RADAR_ASYNC_EXEC; then + local file="$(dot_git)/async_git_radar_$2" + local working_file="$(dot_git)/async_git_radar_workers" + + if [[ -f $file ]]; then + cat $file + fi - GIT_RADAR_REDRAW=${GIT_RADAR_REDRAW:-false} - if ! $GIT_RADAR_REDRAW; then - ( - eval $function_to_run > $file - kill -s USR1 "$GIT_RADAR_ZSH_PID" # Tell the parent we are finished - ) > /dev/null & + GIT_RADAR_REDRAW=${GIT_RADAR_REDRAW:-false} + if ! $GIT_RADAR_REDRAW; then + ( + eval $function_to_run > $file + kill -s USR1 "$GIT_RADAR_ZSH_PID" # Tell the parent we are finished + ) > /dev/null & + fi + else + eval $function_to_run fi } From 30ff4793776ac4662ba18ec52e743088f949f769 Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Wed, 4 Nov 2015 16:00:18 +0000 Subject: [PATCH 06/12] Don't need the PID now --- radar-base.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radar-base.sh b/radar-base.sh index a7856e8..a5f963c 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -468,7 +468,7 @@ color_changes_status() { printf $PRINT_F_OPTION "${changes:1}" } - async_or_not "_async_changes '$GIT_RADAR_ZSH_PID'" "changes" + async_or_not "_async_changes" "changes" } bash_color_changes_status() { From 42448c4449def00049071f405ef1f20b8d7971e1 Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Wed, 4 Nov 2015 16:05:35 +0000 Subject: [PATCH 07/12] Simplify the changes needed to make a function async --- radar-base.sh | 59 +++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/radar-base.sh b/radar-base.sh index a5f963c..59f0130 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -414,8 +414,7 @@ async_or_not() { GIT_RADAR_ASYNC_EXEC=${GIT_RADAR_ASYNC_EXEC:-false} if $GIT_RADAR_ASYNC_EXEC; then - local file="$(dot_git)/async_git_radar_$2" - local working_file="$(dot_git)/async_git_radar_workers" + local file="$(dot_git)/async_git_radar_$1" if [[ -f $file ]]; then cat $file @@ -434,41 +433,37 @@ async_or_not() { } color_changes_status() { - _async_changes() { - local parent_pid="$1" - local separator="${2:- }" - - local 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")" - if [[ -n "$staged_changes" ]]; then - staged_changes="$separator$staged_changes" - fi - - if [[ -n "$unstaged_changes" ]]; then - unstaged_changes="$separator$unstaged_changes" - fi + local parent_pid="$1" + local separator="${2:- }" + + local 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")" + if [[ -n "$staged_changes" ]]; then + staged_changes="$separator$staged_changes" + fi - if [[ -n "$conflicted_changes" ]]; then - conflicted_changes="$separator$conflicted_changes" - fi + if [[ -n "$unstaged_changes" ]]; then + unstaged_changes="$separator$unstaged_changes" + fi - if [[ -n "$untracked_changes" ]]; then - untracked_changes="$separator$untracked_changes" - fi + if [[ -n "$conflicted_changes" ]]; then + conflicted_changes="$separator$conflicted_changes" + fi - changes="$staged_changes$conflicted_changes$unstaged_changes$untracked_changes" + if [[ -n "$untracked_changes" ]]; then + untracked_changes="$separator$untracked_changes" fi - printf $PRINT_F_OPTION "${changes:1}" - } + changes="$staged_changes$conflicted_changes$unstaged_changes$untracked_changes" + fi - async_or_not "_async_changes" "changes" + printf $PRINT_F_OPTION "${changes:1}" } bash_color_changes_status() { @@ -611,7 +606,7 @@ render_prompt() { fi fi if [[ $PROMPT_FORMAT =~ ${if_pre}changes${if_post} ]]; then - changes_result="$(color_changes_status)" + changes_result="$(async_or_not "color_changes_status")" if [[ -n "$changes_result" ]]; then changes_sed="s/${sed_pre}changes${sed_post}/\2${changes_result}\4/" else From 142e448ebdff1d1989b33bddb2666600f10cafe2 Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Wed, 4 Nov 2015 16:22:59 +0000 Subject: [PATCH 08/12] Ensure outdated output isn't preferred --- radar-base.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/radar-base.sh b/radar-base.sh index 59f0130..8196bef 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -416,17 +416,18 @@ async_or_not() { if $GIT_RADAR_ASYNC_EXEC; then local file="$(dot_git)/async_git_radar_$1" - if [[ -f $file ]]; then - cat $file - fi - GIT_RADAR_REDRAW=${GIT_RADAR_REDRAW:-false} if ! $GIT_RADAR_REDRAW; then ( - eval $function_to_run > $file + output="$($function_to_run)" + printf '%s' "$output" > $file kill -s USR1 "$GIT_RADAR_ZSH_PID" # Tell the parent we are finished ) > /dev/null & fi + + if [[ -f $file ]]; then + cat $file + fi else eval $function_to_run fi From ab2ed210da462bdaa4d4a26eb0714493f294583e Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Wed, 4 Nov 2015 22:27:29 +0000 Subject: [PATCH 09/12] Make remote commits async --- radar-base.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radar-base.sh b/radar-base.sh index 8196bef..2e09573 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -583,7 +583,7 @@ render_prompt() { sed_post="\(\:\([^%^{^}]*\)\)\{0,1\}}" if [[ $output =~ ${if_pre}remote${if_post} ]]; then - remote_result="$(color_remote_commits)" + remote_result="$(async_or_not "color_remote_commits")" if [[ -n "$remote_result" ]]; then remote_sed="s/${sed_pre}remote${sed_post}/\2${remote_result}\4/" else From d5941d46dabdd1e34538d90c38b5d8a6b44640cb Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Wed, 4 Nov 2015 22:27:56 +0000 Subject: [PATCH 10/12] Make local commits async --- radar-base.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radar-base.sh b/radar-base.sh index 2e09573..1fd73fe 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -599,7 +599,7 @@ render_prompt() { fi fi if [[ $PROMPT_FORMAT =~ ${if_pre}local${if_post} ]]; then - local_result="$(color_local_commits)" + local_result="$(async_or_not "color_local_commits")" if [[ -n "$local_result" ]]; then local_sed="s/${sed_pre}local${sed_post}/\2$local_result\4/" else From 962e7d5eb351acf2630fd2cfbd94cae41c93ac58 Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Wed, 4 Nov 2015 22:28:46 +0000 Subject: [PATCH 11/12] Make stash status async --- radar-base.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radar-base.sh b/radar-base.sh index 1fd73fe..c27a120 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -615,7 +615,7 @@ render_prompt() { fi fi if [[ $PROMPT_FORMAT =~ ${if_pre}stash${if_post} ]]; then - stash_result="$(stash_status)" + stash_result="$(async_or_not "stash_status")" if [[ -n "$stash_result" ]]; then stash_sed="s/${sed_pre}stash${sed_post}/\2${stash_result}\4/" else From fec7e01595e8e3762ef28ea6225dbb08e0b2690c Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Wed, 4 Nov 2015 22:29:48 +0000 Subject: [PATCH 12/12] Make branch name async --- radar-base.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radar-base.sh b/radar-base.sh index c27a120..85be5d9 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -591,7 +591,7 @@ render_prompt() { fi fi if [[ $PROMPT_FORMAT =~ ${if_pre}branch${if_post} ]]; then - branch_result="$(readable_branch_name | sed -e 's/\//\\\//g')" + branch_result="$(async_or_not "readable_branch_name" | sed -e 's/\//\\\//g')" if [[ -n "$branch_result" ]]; then branch_sed="s/${sed_pre}branch${sed_post}/\2${branch_result}\4/" else