From d4a487f1fa4cc048f9ca50a3d47c4aa8d6dee8bd Mon Sep 17 00:00:00 2001 From: Claudio Bandera Date: Sun, 24 Apr 2016 17:09:59 +0200 Subject: Merge remote-tracking branch 'upstream/master' into hotfix/unittests --- README.md | 22 +++++- git-radar | 2 + radar-base.sh | 58 ++++++++++---- test-directories.sh | 5 +- test-format-config.sh | 212 +++++++++++++++++++++++++------------------------- 5 files changed, 175 insertions(+), 124 deletions(-) diff --git a/README.md b/README.md index 2f508a8..2f4e39f 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ Add to your `config.fish` function fish_prompt set_color $fish_color_cwd echo -n (prompt_pwd) - git-radar --fish --fetch + echo -n (git-radar --fish --fetch) set_color normal echo -n ' > ' end @@ -190,6 +190,25 @@ export PROMPT="$PROMPT\$(git-radar --zsh --fetch) " ``` [(note: the `\` escaping the `$` is important)](#ensuring-prompt-execution) +You may also choose to fetch at a customized interval of time. To do so, add +this to your .bashrc, .zshrc: + +```bash +export GIT_RADAR_FETCH_TIME= +``` + +For example, to fetch every 30 seconds (instead of the default 5 minutes): + +```bash +export GIT_RADAR_FETCH_TIME=30 +``` + +You can also do this in the gitradarrc file: + +```bash +GIT_RADAR_FETCH_TIME=30 +``` + ## Customise your prompt Git Radar is highly customisable using a prompt format string. The 4 features @@ -235,6 +254,7 @@ The default prompt format uses this to add spaces only if the feature would render. In that way the prompt always looks well spaced out no matter how many features are rendering. + ## Support ### Ensuring prompt execution diff --git a/git-radar b/git-radar index a8dd72e..f736e41 100755 --- a/git-radar +++ b/git-radar @@ -95,9 +95,11 @@ while [[ $# > 0 ]];do if [[ "$command" == "--fetch" ]]; then nohup $dot/fetch.sh >/dev/null 2>&1 & fi + if [[ "$command" == "--zsh" ]]; then $dot/prompt.zsh $args fi + if [[ "$command" == "--bash" || "$command" == "--fish" ]]; then $dot/prompt.bash $args fi diff --git a/radar-base.sh b/radar-base.sh index 60d5bd3..9d337cc 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -14,6 +14,20 @@ timethis() { echo "$1 - $dur" >> $HOME/duration.dat } +get_fetch_time() { + if [ -f "$rcfile_path/.gitradarrc.bash" ]; then + source "$rcfile_path/.gitradarrc.bash" + elif [ -f "$rcfile_path/.gitradarrc.zsh" ]; then + source "$rcfile_path/.gitradarrc.zsh" + elif [ -f "$rcfile_path/.gitradarrc" ]; then + source "$rcfile_path/.gitradarrc" + fi + + FETCH_TIME="${GIT_RADAR_FETCH_TIME:-"$((5 * 60))"}" + echo $FETCH_TIME + +} + prepare_bash_colors() { if [ -f "$rcfile_path/.gitradarrc.bash" ]; then source "$rcfile_path/.gitradarrc.bash" @@ -49,7 +63,7 @@ prepare_bash_colors() { RESET_COLOR_CHANGES="\x01${GIT_RADAR_COLOR_CHANGES_RESET:-"\\033[0m"}\x02" RESET_COLOR_BRANCH="\x01${GIT_RADAR_COLOR_BRANCH_RESET:-"\\033[0m"}\x02" RESET_COLOR_STASH="\x01${GIT_RADAR_COLOR_STASH:-"\\033[0m"}\x02" - + } prepare_zsh_colors() { @@ -76,7 +90,7 @@ prepare_zsh_colors() { COLOR_CHANGES_UNTRACKED="%{${GIT_RADAR_COLOR_CHANGES_UNTRACKED:-$fg_bold[white]}%}" COLOR_STASH="%{${GIT_RADAR_COLOR_STASH:-$fg_bold[yellow]}%}" - + local italic_m="$(printf '\xF0\x9D\x98\xAE')" COLOR_BRANCH="%{${GIT_RADAR_COLOR_BRANCH:-$reset_color}%}" @@ -174,10 +188,10 @@ time_now() { } time_to_update() { + last_time_updated="${1:-$FETCH_TIME}" if is_repo; then local timesincelastupdate="$(($(time_now) - $(timestamp)))" - local fiveminutes="$((5 * 60))" - if (( $timesincelastupdate > $fiveminutes )); then + if (( $timesincelastupdate > $last_time_updated )); then # time to update return 0 (which is true) return 0 else @@ -190,7 +204,10 @@ time_to_update() { } fetch() { - if time_to_update; then + # Gives $FETCH_TIME a value + get_fetch_time + + if time_to_update $FETCH_TIME; then record_timestamp git fetch --quiet > /dev/null 2>&1 fi @@ -219,22 +236,25 @@ branch_ref() { } remote_branch_name() { - local localRef="\/$(branch_name)$" - if [[ -n "$localRef" ]]; then - local remoteBranch="$(git for-each-ref --format='%(upstream:short)' refs/heads $localRef 2>/dev/null | grep $localRef)" + local localRef="$(branch_name)" + local remote="$(git config --get "branch.$localRef.remote")" + if [[ -n $remote ]]; then + local remoteBranch="$(git config --get "branch.${localRef}.merge" | sed -e 's/^refs\/heads\///')" if [[ -n $remoteBranch ]]; then - printf '%s' $remoteBranch + printf '%s/%s' $remote $remoteBranch return 0 else - return 1 + return 1 fi + else + return 1 fi } commits_behind_of_remote() { remote_branch=${1:-"$(remote_branch_name)"} if [[ -n "$remote_branch" ]]; then - git rev-list --left-only --count ${remote_branch}...HEAD + git rev-list --left-only --count ${remote_branch}...HEAD 2>/dev/null else printf '%s' "0" fi @@ -243,7 +263,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 + git rev-list --right-only --count ${remote_branch}...HEAD 2>/dev/null else printf '%s' "0" fi @@ -521,10 +541,16 @@ stashed_status() { printf '%s' "$(git stash list | wc -l 2>/dev/null | grep -oEi '[0-9][0-9]*')" } +is_cwd_a_dot_git_directory() { + [[ "$(basename $PWD)" == ".git" ]]; return $? +} + stash_status() { - local number_stashes="$(stashed_status)" - if [ $number_stashes -gt 0 ]; then - printf $PRINT_F_OPTION "$number_stashes$COLOR_STASH≡$RESET_COLOR_STASH" + if ! is_cwd_a_dot_git_directory; then + local number_stashes="$(stashed_status)" + if [ $number_stashes -gt 0 ]; then + printf $PRINT_F_OPTION "${number_stashes}${COLOR_STASH}≡${RESET_COLOR_STASH}" + fi fi } @@ -551,7 +577,7 @@ render_prompt() { fi fi if [[ $PROMPT_FORMAT =~ ${if_pre}branch${if_post} ]]; then - branch_result="$(readable_branch_name | sed -e 's/\//\\\//')" + branch_result="$(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 diff --git a/test-directories.sh b/test-directories.sh index 1777e69..219a9f3 100755 --- a/test-directories.sh +++ b/test-directories.sh @@ -64,7 +64,8 @@ test_record_timestamp_in_repo() { test_time_to_update_when_timestamp_is_old() { cd $scriptDir - if [[ $OSTYPE == darwin* ]];then + FETCH_TIME="$((5 * 60))" # Fetch every 5 mins + if [[ $OSTYPE == darwin* ]];then touch -A "-010000" "$(dot_git)/lastupdatetime" else newtimestamp=$(date -d "now -1 hour" +%Y%m%d%H%M) @@ -75,6 +76,7 @@ test_time_to_update_when_timestamp_is_old() { test_not_time_to_update_when_just_recorded() { cd $scriptDir + FETCH_TIME="$((5 * 60))" # Fetch every 5 mins record_timestamp assertFalse time_to_update } @@ -83,6 +85,7 @@ test_time_to_update_when_no_timestamp() { cd_to_tmp git init --quiet + FETCH_TIME="$((5 * 60))" # Fetch every 5 mins time_to_update assertTrue time_to_update diff --git a/test-format-config.sh b/test-format-config.sh index 0bf01ac..78a1c34 100755 --- a/test-format-config.sh +++ b/test-format-config.sh @@ -125,111 +125,111 @@ test_all_options_set_config() { rm_tmp } -test_reorder_parts() { - prepare_test_repo - - export GIT_RADAR_FORMAT="%{branch}%{local}%{changes}%{remote}" - prepare_zsh_colors - unset_colours - - prompt="$(render_prompt)" - assertEquals "foo1↑1Am 1 →" "$prompt" - - export GIT_RADAR_FORMAT="%{local}%{changes}%{remote}%{branch}" - prepare_zsh_colors - unset_colours - - prompt="$(render_prompt)" - assertEquals "1↑1Am 1 →foo" "$prompt" - - export GIT_RADAR_FORMAT="%{changes}%{remote}%{branch}%{local}" - prepare_zsh_colors - unset_colours - - prompt="$(render_prompt)" - assertEquals "1Am 1 →foo1↑" "$prompt" - - rm_tmp -} - -test_prefix_and_suffix_changes() { - prepare_test_repo - - export GIT_RADAR_FORMAT="%{changes}" - prepare_zsh_colors - unset_colours - - prompt="$(render_prompt)" - assertEquals "1A" "$prompt" - - export GIT_RADAR_FORMAT="%{[:changes:]}" - prepare_zsh_colors - unset_colours - - prompt="$(render_prompt)" - assertEquals "[1A]" "$prompt" - - rm_tmp -} - -test_prefix_and_suffix_local() { - prepare_test_repo - - export GIT_RADAR_FORMAT="%{local}" - prepare_zsh_colors - unset_colours - - prompt="$(render_prompt)" - assertEquals "1↑" "$prompt" - - export GIT_RADAR_FORMAT="%{[:local:]}" - prepare_zsh_colors - unset_colours - - prompt="$(render_prompt)" - assertEquals "[1↑]" "$prompt" - - rm_tmp -} - -test_prefix_and_suffix_branch() { - prepare_test_repo - - export GIT_RADAR_FORMAT="%{branch}" - prepare_zsh_colors - unset_colours - - prompt="$(render_prompt)" - assertEquals "foo" "$prompt" - - export GIT_RADAR_FORMAT="%{[:branch:]}" - prepare_zsh_colors - unset_colours - - prompt="$(render_prompt)" - assertEquals "[foo]" "$prompt" - - rm_tmp -} - -test_prefix_and_suffix_remote() { - prepare_test_repo - - export GIT_RADAR_FORMAT="%{remote}" - prepare_zsh_colors - unset_colours - - prompt="$(render_prompt)" - assertEquals "m 1 →" "$prompt" - - export GIT_RADAR_FORMAT="%{[:remote:]}" - prepare_zsh_colors - unset_colours - - prompt="$(render_prompt)" - assertEquals "[m 1 →]" "$prompt" - - rm_tmp -} +#test_reorder_parts() { +# prepare_test_repo +# +# export GIT_RADAR_FORMAT="%{branch}%{local}%{changes}%{remote}" +# prepare_zsh_colors +# unset_colours +# +# prompt="$(render_prompt)" +# assertEquals "foo1↑1Am 1 →" "$prompt" +# +# export GIT_RADAR_FORMAT="%{local}%{changes}%{remote}%{branch}" +# prepare_zsh_colors +# unset_colours +# +# prompt="$(render_prompt)" +# assertEquals "1↑1Am 1 →foo" "$prompt" +# +# export GIT_RADAR_FORMAT="%{changes}%{remote}%{branch}%{local}" +# prepare_zsh_colors +# unset_colours +# +# prompt="$(render_prompt)" +# assertEquals "1Am 1 →foo1↑" "$prompt" +# +# rm_tmp +#} +# +#test_prefix_and_suffix_changes() { +# prepare_test_repo +# +# export GIT_RADAR_FORMAT="%{changes}" +# prepare_zsh_colors +# unset_colours +# +# prompt="$(render_prompt)" +# assertEquals "1A" "$prompt" +# +# export GIT_RADAR_FORMAT="%{[:changes:]}" +# prepare_zsh_colors +# unset_colours +# +# prompt="$(render_prompt)" +# assertEquals "[1A]" "$prompt" +# +# rm_tmp +#} +# +#test_prefix_and_suffix_local() { +# prepare_test_repo +# +# export GIT_RADAR_FORMAT="%{local}" +# prepare_zsh_colors +# unset_colours +# +# prompt="$(render_prompt)" +# assertEquals "1↑" "$prompt" +# +# export GIT_RADAR_FORMAT="%{[:local:]}" +# prepare_zsh_colors +# unset_colours +# +# prompt="$(render_prompt)" +# assertEquals "[1↑]" "$prompt" +# +# rm_tmp +#} +# +#test_prefix_and_suffix_branch() { +# prepare_test_repo +# +# export GIT_RADAR_FORMAT="%{branch}" +# prepare_zsh_colors +# unset_colours +# +# prompt="$(render_prompt)" +# assertEquals "foo" "$prompt" +# +# export GIT_RADAR_FORMAT="%{[:branch:]}" +# prepare_zsh_colors +# unset_colours +# +# prompt="$(render_prompt)" +# assertEquals "[foo]" "$prompt" +# +# rm_tmp +#} +# +#test_prefix_and_suffix_remote() { +# prepare_test_repo +# +# export GIT_RADAR_FORMAT="%{remote}" +# prepare_zsh_colors +# unset_colours +# +# prompt="$(render_prompt)" +# assertEquals "m 1 →" "$prompt" +# +# export GIT_RADAR_FORMAT="%{[:remote:]}" +# prepare_zsh_colors +# unset_colours +# +# prompt="$(render_prompt)" +# assertEquals "[m 1 →]" "$prompt" +# +# rm_tmp +#} . ./shunit/shunit2 -- cgit v1.2.3