From b3d9243f3500a0cb0116dcebdad2d67ca0cb4a0e Mon Sep 17 00:00:00 2001 From: bill-auger Date: Sat, 23 Feb 2019 17:30:45 -0500 Subject: [PATCH 1/6] admit '--dates' switch for arbitrary branch comparisons --- git-branch-status | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-branch-status b/git-branch-status index a4a37af..0ae6cb3 100755 --- a/git-branch-status +++ b/git-branch-status @@ -328,7 +328,7 @@ function Init # (cli_args) case "$1" in '-a'|'--all' ) show_all=1 ;; '-b'|'--branch' ) [ "$2" ] && branch_a="$2" || branch_a=$(GetCurrentBranch) ;; - '-d'|'--dates' ) show_dates=1 ;; + '-d'|'--dates' ) branch_a="$2" ; branch_b="$3" ; show_dates=1 ;; '-h'|'--help' ) EXIT "$USAGE" $EXIT_SUCCESS ;; '-l'|'--local' ) show_all_local=1 ; show_all_synced=1 ; ;; '-r'|'--remotes' ) show_all_remote=1 ; show_all_synced=1 ; ;; From 99b6fec797da5bc12b2c016b15e0fd1421d2b07c Mon Sep 17 00:00:00 2001 From: bill-auger Date: Wed, 15 May 2019 06:06:50 -0400 Subject: [PATCH 2/6] implement remote fetching per user-defined frequency --- config.ini.example | 8 ++++++++ git-branch-status | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 config.ini.example diff --git a/config.ini.example b/config.ini.example new file mode 100644 index 0000000..b2f8fcf --- /dev/null +++ b/config.ini.example @@ -0,0 +1,8 @@ +###################################################################### +# optional user-defined config for the 'git-branch-status' program # +# copy this file to: ./config.ini to customize # +###################################################################### + + +# number of minutes between automatic `git fetch --all` calls (0 => always, -1 => never) +FETCH_PERIOD=-1 diff --git a/git-branch-status b/git-branch-status index 0ae6cb3..09091cb 100755 --- a/git-branch-status +++ b/git-branch-status @@ -112,6 +112,18 @@ EXAMPLES: USAGE_MSG +## user-defined constants (see ./config.ini.example) ## + +readonly THIS_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || echo $0)")" +readonly CFG_FILE=${THIS_DIR}/config.ini.example +readonly LAST_FETCH_FILE=${THIS_DIR}/LAST_FETCH +readonly CFG_FETCH_REGEX='/^\s*FETCH_PERIOD\s*=\s*-?[0-9]+/' +readonly CFG_AWK_CMD='{ print ($2 + 0) }' + +declare -a cfg_fetch_period=($(awk -F '=' "${CFG_FETCH_REGEX}${CFG_AWK_CMD}" ${CFG_FILE} 2>/dev/null)) +readonly FETCH_PERIOD=$([ "${cfg_fetch_period[0]}" ] && echo ${cfg_fetch_period[0]} || echo '-1') + + ## constants ## readonly INNER_PADDING_W=13 # '| ' + ' | ' + ' | ' + ' | ' + ' |' @@ -354,6 +366,19 @@ function Init # (cli_args) readonly COMPARE_BRANCH=$branch_b } +function FetchRemotes +{ + if (( ${FETCH_PERIOD} >= 0 )) + then local now_ts=$(date +%s) + local last_fetch_ts=$(awk 'NR==1 { print ($1 + 0) }' ${LAST_FETCH_FILE} 2> /dev/null) + + if (( ${now_ts} - (${last_fetch_ts} + 0) >= ${FETCH_PERIOD} * 60 )) + then git fetch --all + echo ${now_ts} > ${LAST_FETCH_FILE} + fi + fi +} + function GenerateReports { if [ "$COMPARE_BRANCH" ] @@ -600,4 +625,5 @@ function Reset ## main entry ## Init $* +FetchRemotes GenerateReports From 5a39497e9d07cc01483f722f9f88b40f4903eaa7 Mon Sep 17 00:00:00 2001 From: bill-auger Date: Wed, 15 May 2019 06:13:59 -0400 Subject: [PATCH 3/6] add .gitignore --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..591a0ca --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +* +!config.ini.example +!git-branch-status +!LICENSE +!README.md From d3e50c33c8e55c9bde2b98364fb075cf46b6e9c0 Mon Sep 17 00:00:00 2001 From: bill-auger Date: Wed, 28 Aug 2019 03:44:50 -0400 Subject: [PATCH 4/6] sort by date when dates are shown --- git-branch-status | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/git-branch-status b/git-branch-status index 09091cb..0adcbe3 100755 --- a/git-branch-status +++ b/git-branch-status @@ -215,8 +215,10 @@ function AssertIsNotBareRepo function GetRefs # (refs_dir) { local refs_dir=$1 + local fmt='%(refname:short) %(upstream:short)' + local sort=$( (( $SHOW_DATES )) && echo '--sort=creatordate') - git for-each-ref --format="%(refname:short) %(upstream:short)" $refs_dir 2> /dev/null + git for-each-ref --format="$fmt" $sort $refs_dir 2> /dev/null } function GetLocalRefs From cacb997ae530655d101936926a8db61e76c00fe5 Mon Sep 17 00:00:00 2001 From: bill-auger Date: Mon, 25 May 2020 11:25:53 -0400 Subject: [PATCH 5/6] housekeeping --- README.md | 16 ++++----- git-branch-status | 90 +++++++++++++++++++++++------------------------ 2 files changed, 53 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index ecf3cb3..a0034dc 100644 --- a/README.md +++ b/README.md @@ -58,16 +58,16 @@ EXAMPLES: | feature-branch | (even) | (ahead 2) | origin/feature-branch | | master | (behind 1) | (even) | origin/master | - # compare two arbitrary branches (either local and either remote) + # compare two arbitrary branches - local or remote $ git-branch-status local-arbitrary-branch fork/arbitrary-branch | local-arbitrary-branch | (even) | (ahead 1) | fork/arbitrary-branch | $ git-branch-status fork/arbitrary-branch local-arbitrary-branch | fork/arbitrary-branch | (behind 1) | (even) | local-arbitrary-branch | - # show all branches - including those synchronized, non-tracking, or not checked-out + # show all branches - local and remote, regardless of state or relationship $ git-branch-status -a $ git-branch-status --all - | master | (even) | (ahead 1) | origin/master | + *| master | (even) | (ahead 1) | origin/master | | tracked-branch | (even) | (even) | origin/tracked-branch | | (no local) | n/a | n/a | origin/untracked-branch | | local-branch | n/a | n/a | (no upstream) | @@ -77,7 +77,7 @@ EXAMPLES: # show the current branch $ git-branch-status -b $ git-branch-status --branch - | current-branch | (even) | (ahead 2) | origin/current-branch | + *| current-branch | (even) | (ahead 2) | origin/current-branch | # show a specific branch $ git-branch-status specific-branch @@ -98,7 +98,7 @@ EXAMPLES: # show all local branches - including those synchronized or non-tracking $ git-branch-status -l $ git-branch-status --local - | master | (even) | (ahead 1) | origin/master | + *| master | (even) | (ahead 1) | origin/master | | tracked-branch | (even) | (even) | origin/tracked-branch | | local-branch | n/a | n/a | (no upstream) | @@ -111,9 +111,9 @@ EXAMPLES: # show all branches with timestamps (like -a -d) $ git-branch-status -v $ git-branch-status --verbose - | 1999-12-31 local | n/a | n/a | (no upstream) | - | 1999-12-31 master | (behind 1) | (even) | 2000-01-01 origin/master | - | 1999-12-31 tracked | (even) | (even) | 2000-01-01 origin/tracked | + | 1999-12-31 master | (behind 1) | (even) | 2000-01-01 origin/master | + | 1999-12-31 tracked | (even) | (even) | 2000-01-01 origin/tracked | + *| 1999-12-31 local-wip | n/a | n/a | (no upstream) | ``` diff --git a/git-branch-status b/git-branch-status index 0adcbe3..11340c1 100755 --- a/git-branch-status +++ b/git-branch-status @@ -5,7 +5,7 @@ # Copyright 2011 Jehiah Czebotar # Copyright 2013 Fredrik Strandin # Copyright 2014 Kristijan Novoselić -# Copyright 2014-2018 bill-auger +# Copyright 2014-2020 bill-auger # # git-branch-status is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3 @@ -53,16 +53,16 @@ EXAMPLES: | feature-branch | (even) | (ahead 2) | origin/feature-branch | | master | (behind 1) | (even) | origin/master | - # compare two arbitrary branches (either local and either remote) + # compare two arbitrary branches - local or remote $ git-branch-status local-arbitrary-branch fork/arbitrary-branch | local-arbitrary-branch | (even) | (ahead 1) | fork/arbitrary-branch | $ git-branch-status fork/arbitrary-branch local-arbitrary-branch | fork/arbitrary-branch | (behind 1) | (even) | local-arbitrary-branch | - # show all branches - including those synchronized, non-tracking, or not checked-out + # show all branches - local and remote, regardless of state or relationship $ git-branch-status -a $ git-branch-status --all - | master | (even) | (ahead 1) | origin/master | + *| master | (even) | (ahead 1) | origin/master | | tracked-branch | (even) | (even) | origin/tracked-branch | | (no local) | n/a | n/a | origin/untracked-branch | | local-branch | n/a | n/a | (no upstream) | @@ -72,7 +72,7 @@ EXAMPLES: # show the current branch $ git-branch-status -b $ git-branch-status --branch - | current-branch | (even) | (ahead 2) | origin/current-branch | + *| current-branch | (even) | (ahead 2) | origin/current-branch | # show a specific branch $ git-branch-status specific-branch @@ -93,7 +93,7 @@ EXAMPLES: # show all local branches - including those synchronized or non-tracking $ git-branch-status -l $ git-branch-status --local - | master | (even) | (ahead 1) | origin/master | + *| master | (even) | (ahead 1) | origin/master | | tracked-branch | (even) | (even) | origin/tracked-branch | | local-branch | n/a | n/a | (no upstream) | @@ -106,9 +106,9 @@ EXAMPLES: # show all branches with timestamps (like -a -d) $ git-branch-status -v $ git-branch-status --verbose - | 1999-12-31 local | n/a | n/a | (no upstream) | - | 1999-12-31 master | (behind 1) | (even) | 2000-01-01 origin/master | - | 1999-12-31 tracked | (even) | (even) | 2000-01-01 origin/tracked | + | 1999-12-31 master | (behind 1) | (even) | 2000-01-01 origin/master | + | 1999-12-31 tracked | (even) | (even) | 2000-01-01 origin/tracked | + *| 1999-12-31 local-wip | n/a | n/a | (no upstream) | USAGE_MSG @@ -131,7 +131,7 @@ readonly MARGIN_PAD_W=2 readonly MARGINS_PAD_W=$(( $MARGIN_PAD_W * 2 )) readonly ALL_PADDING_W=$(( $INNER_PADDING_W + $MARGINS_PAD_W )) readonly MAX_DIVERGENCE_W=12 # e.g. "(behind 999)" -readonly MIN_TTY_W=60 # ASSERT: (0 < ($ALL_PADDING_W + (MAX_DIVERGENCE_W * 2)) <= $MIN_TTY_W) +readonly MIN_TTY_W=60 # ASSERT: (0 < (ALL_PADDING_W + (MAX_DIVERGENCE_W * 2)) <= MIN_TTY_W) readonly MARGIN_PAD=$(printf "%$(( $MARGIN_PAD_W ))s") readonly CWHITE='\033[0;37m' readonly CRED='\033[0;31m' @@ -196,23 +196,23 @@ declare -a RemoteColors=() ## helpers ## -function AssertIsValidRepo +AssertIsValidRepo() { [ "$(git rev-parse --is-inside-work-tree 2> /dev/null)" == 'true' ] || \ ! (( $(AssertIsNotBareRepo) )) && echo 1 || echo 0 } -function AssertHasCommits +AssertHasCommits() { [ "$(git cat-file -t HEAD 2> /dev/null)" ] && echo 1 || echo 0 } -function AssertIsNotBareRepo +AssertIsNotBareRepo() { [ "$(git rev-parse --is-bare-repository 2> /dev/null)" != 'true' ] && echo 1 || echo 0 } -function GetRefs # (refs_dir) +GetRefs() # (refs_dir) { local refs_dir=$1 local fmt='%(refname:short) %(upstream:short)' @@ -221,19 +221,19 @@ function GetRefs # (refs_dir) git for-each-ref --format="$fmt" $sort $refs_dir 2> /dev/null } -function GetLocalRefs +GetLocalRefs() { GetRefs refs/heads } -function GetRemoteRefs # (remote_repo_name) +GetRemoteRefs() # (remote_repo_name) { local remote_repo=$1 GetRefs refs/remotes/$remote_repo } -function GetStatus # (base_commit compare_commit) +GetStatus() # (base_commit compare_commit) { local base_commit=$1 local compare_commit=$2 @@ -241,19 +241,19 @@ function GetStatus # (base_commit compare_commit) git rev-list --left-right ${base_commit}...${compare_commit} -- 2>/dev/null } -function GetCurrentBranch +GetCurrentBranch() { git rev-parse --abbrev-ref HEAD } -function GetUpstreamBranch # (local_branch) +GetUpstreamBranch() # (local_branch) { local local_branch=$1 git rev-parse --abbrev-ref $local_branch@{upstream} 2> /dev/null } -function IsCurrentBranch # (branch_name) +IsCurrentBranch() # (branch_name) { local branch=$1 local this_branch=$(AppendHeadDate $branch) @@ -262,7 +262,7 @@ function IsCurrentBranch # (branch_name) [ "$this_branch" == "$current_branch" ] && echo 1 || echo 0 } -function IsLocalBranch # (branch_name) +IsLocalBranch() # (branch_name) { local branch=$1 local is_local_branch=$(git branch -a | grep -E "^.* $branch$") @@ -270,7 +270,7 @@ function IsLocalBranch # (branch_name) [ "$is_local_branch" ] && echo 1 || echo 0 } -function IsTrackedBranch # (base_branch_name compare_branch_name) +IsTrackedBranch() # (base_branch_name compare_branch_name) { local base_branch=$1 local compare_branch=$2 @@ -279,7 +279,7 @@ function IsTrackedBranch # (base_branch_name compare_branch_name) [ "$compare_branch" == "$upstream_branch" ] && echo 1 || echo 0 } -function DoesBranchExist # (branch_name) +DoesBranchExist() # (branch_name) { local branch=$1 local is_known_branch=$(git branch -a | grep -E "^.* (remotes\/)?$branch$") @@ -287,7 +287,7 @@ function DoesBranchExist # (branch_name) [ "$is_known_branch" ] && echo 1 || echo 0 } -function AppendHeadDate # (commit_ref) +AppendHeadDate() # (commit_ref) { local commit=$1 local author_date=$(git log -n 1 --format=format:"%ai" $commit 2> /dev/null) @@ -298,7 +298,7 @@ function AppendHeadDate # (commit_ref) echo $date$commit } -function CurrentTtyW +CurrentTtyW() { local tty_dims=$(stty -F /dev/tty size 2> /dev/null || stty -f /dev/tty size 2> /dev/null) local tty_w=$(echo $tty_dims | cut -d ' ' -f 2) @@ -306,7 +306,7 @@ function CurrentTtyW (( $tty_w )) && echo "$tty_w" || echo "$MIN_TTY_W" } -function PrintHRule # (rule_width) +PrintHRule() # (rule_width) { local rule_w=$1 local h_rule="$(dd if=/dev/zero bs=$rule_w count=1 2> /dev/null | tr '\0' $HRULE_CHAR)" @@ -314,7 +314,7 @@ function PrintHRule # (rule_width) echo "$MARGIN_PAD$h_rule" } -function EXIT # (exit_msg exit_status) +Exit() # (exit_msg exit_status) { local exit_msg=$1 local exit_status=$2 @@ -328,7 +328,7 @@ function EXIT # (exit_msg exit_status) ## business ## -function Init # (cli_args) +Init() # (cli_args) { local show_dates=0 local show_all=0 @@ -343,7 +343,7 @@ function Init # (cli_args) '-a'|'--all' ) show_all=1 ;; '-b'|'--branch' ) [ "$2" ] && branch_a="$2" || branch_a=$(GetCurrentBranch) ;; '-d'|'--dates' ) branch_a="$2" ; branch_b="$3" ; show_dates=1 ;; - '-h'|'--help' ) EXIT "$USAGE" $EXIT_SUCCESS ;; + '-h'|'--help' ) Exit "$USAGE" $EXIT_SUCCESS ;; '-l'|'--local' ) show_all_local=1 ; show_all_synced=1 ; ;; '-r'|'--remotes' ) show_all_remote=1 ; show_all_synced=1 ; ;; '-v'|'--verbose' ) show_all=1 ; show_dates=1 ; ;; @@ -351,13 +351,13 @@ function Init # (cli_args) esac # sanity checks - (( $(AssertIsValidRepo ) )) || EXIT "$NOT_REPO_ERR" - (( $(AssertIsNotBareRepo) )) || EXIT "$BARE_REPO_MSG" - (( $(AssertHasCommits ) )) || EXIT "$NO_COMMITS_MSG" - (( $(CurrentTtyW) >= $MIN_TTY_W )) || EXIT "$TTY_W_MSG" - [ -z "$branch_a" ] || (($(DoesBranchExist $branch_a))) || EXIT "$INVALID_BRANCH_MSG '$branch_a'" - [ -z "$branch_b" ] || (($(DoesBranchExist $branch_b))) || EXIT "$INVALID_BRANCH_MSG '$branch_b'" - [ -z "$branch_a" ] || (($(IsLocalBranch $branch_a))) || [ "$branch_b" ] || EXIT "$INVALID_LOCAL_BRANCH_MSG '$branch_a'" + (( $(AssertIsValidRepo ) )) || Exit "$NOT_REPO_ERR" + (( $(AssertIsNotBareRepo) )) || Exit "$BARE_REPO_MSG" + (( $(AssertHasCommits ) )) || Exit "$NO_COMMITS_MSG" + (( $(CurrentTtyW) >= $MIN_TTY_W )) || Exit "$TTY_W_MSG" + [ -z "$branch_a" ] || (($(DoesBranchExist $branch_a))) || Exit "$INVALID_BRANCH_MSG '$branch_a'" + [ -z "$branch_b" ] || (($(DoesBranchExist $branch_b))) || Exit "$INVALID_BRANCH_MSG '$branch_b'" + [ -z "$branch_a" ] || (($(IsLocalBranch $branch_a))) || [ "$branch_b" ] || Exit "$INVALID_LOCAL_BRANCH_MSG '$branch_a'" [ -z "$branch_a" ] || show_all_local=1 # force "no upstream" message for non-tracking branches readonly SHOW_DATES=$show_dates @@ -368,7 +368,7 @@ function Init # (cli_args) readonly COMPARE_BRANCH=$branch_b } -function FetchRemotes +FetchRemotes() { if (( ${FETCH_PERIOD} >= 0 )) then local now_ts=$(date +%s) @@ -381,7 +381,7 @@ function FetchRemotes fi } -function GenerateReports +GenerateReports() { if [ "$COMPARE_BRANCH" ] then CustomReport $(IsTrackedBranch $FILTER_BRANCH $COMPARE_BRANCH) @@ -390,7 +390,7 @@ function GenerateReports fi } -function CustomReport # (is_tracked_branch) +CustomReport() # (is_tracked_branch) { local is_tracked_branch=$1 local synchronized_msg @@ -401,7 +401,7 @@ function CustomReport # (is_tracked_branch) PrintReport "$FILTER_BRANCH <-> $COMPARE_BRANCH" "$synchronized_msg" } -function LocalReport +LocalReport() { local synchronized_msg [ "$FILTER_BRANCH" ] && synchronized_msg="$BRANCH_SYNCED_MSG" || synchronized_msg="$LOCALS_SYNCED_MSG" @@ -411,7 +411,7 @@ function LocalReport PrintReport "local <-> upstream" "$synchronized_msg" } -function RemoteReport +RemoteReport() { local synchronized_msg="$REMOTES_SYNCED_MSG" local remote_repo @@ -428,7 +428,7 @@ function RemoteReport done } -function GenerateReport # (base_branch compare_branch) +GenerateReport() # (base_branch compare_branch) { local base_branch=$1 local compare_branch=$2 @@ -506,7 +506,7 @@ function GenerateReport # (base_branch compare_branch) if [ ${#remote_msg} -gt $RemoteW ] ; then RemoteW=${#remote_msg} ; fi ; } -function PrintReport # (table_header_line synchronized_msg) +PrintReport() # (table_header_line synchronized_msg) { local table_header_line=$1 local synchronized_msg=$2 @@ -573,7 +573,7 @@ function PrintReport # (table_header_line synchronized_msg) Reset } -function PrintReportLine +PrintReportLine() { # select data local local_msg=$( echo ${LocalMsgs[ $result_n]} | sed "$JOIN_REGEX") @@ -605,7 +605,7 @@ function PrintReportLine printf "$local_msg$behind_msg$ahead_msg$remote_msg$end_msg\n" } -function Reset +Reset() { WereAnyDivergences=0 WereAnyCompared=0 From 512daf428a885b7d06f3d9b857f822597ae469cf Mon Sep 17 00:00:00 2001 From: bill-auger Date: Mon, 25 May 2020 11:27:04 -0400 Subject: [PATCH 6/6] support user-defined config via cfg file or env vars --- .gitignore | 2 +- config.ini.example | 8 -------- gbs-config.sh.inc.example | 28 +++++++++++++++++++++++++++ git-branch-status | 40 ++++++++++++++++++++++----------------- 4 files changed, 52 insertions(+), 26 deletions(-) delete mode 100644 config.ini.example create mode 100644 gbs-config.sh.inc.example diff --git a/.gitignore b/.gitignore index 591a0ca..a06670d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ * -!config.ini.example +!gbs-config.sh.inc.example !git-branch-status !LICENSE !README.md diff --git a/config.ini.example b/config.ini.example deleted file mode 100644 index b2f8fcf..0000000 --- a/config.ini.example +++ /dev/null @@ -1,8 +0,0 @@ -###################################################################### -# optional user-defined config for the 'git-branch-status' program # -# copy this file to: ./config.ini to customize # -###################################################################### - - -# number of minutes between automatic `git fetch --all` calls (0 => always, -1 => never) -FETCH_PERIOD=-1 diff --git a/gbs-config.sh.inc.example b/gbs-config.sh.inc.example new file mode 100644 index 0000000..f6f12e0 --- /dev/null +++ b/gbs-config.sh.inc.example @@ -0,0 +1,28 @@ +############################################################################### +# optional user-defined config for the 'git-branch-status' program # +#-----------------------------------------------------------------------------# +# * copy this file to: ./gbs-config.sh.inc (or $GBS_CFG_FILE) to customize # +# * the text in this file must be valid BASH # +# * you can also export these configuration options from the environment # +# as well as an alternate location of this file (as $GBS_CFG_FILE) # +# * environment variables will over-ride those defined in this file # +# * environment variables should be named similarly to those in this file # +# with the CFG_* prefix replaced with GBS_* # +# eg: export GBS_CFG_FILE=~/.config/git-branch-status/config.sh.inc # +# eg: export GBS_FETCH_PERIOD=0 # +############################################################################### + + +# number of minutes between automatic `git fetch --all` calls +# 0 => always, -1 => never (default: -1) +readonly CFG_FETCH_PERIOD=-1 + + +# writable filesystem location for storage of the last-fetch timestamp +# (default: ~/.GBS_LAST_FETCH) +readonly CFG_LAST_FETCH_FILE=~/.GBS_LAST_FETCH + + +# whether or not to present ANSI colors +# 0 => no, 1 => yes (default: 1) +readonly CFG_USE_ANSI_COLOR=1 diff --git a/git-branch-status b/git-branch-status index 11340c1..8c237fd 100755 --- a/git-branch-status +++ b/git-branch-status @@ -112,16 +112,22 @@ EXAMPLES: USAGE_MSG -## user-defined constants (see ./config.ini.example) ## +## user-defined configuration (see ./gbs-config.sh.inc.example) ## readonly THIS_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || echo $0)")" -readonly CFG_FILE=${THIS_DIR}/config.ini.example -readonly LAST_FETCH_FILE=${THIS_DIR}/LAST_FETCH -readonly CFG_FETCH_REGEX='/^\s*FETCH_PERIOD\s*=\s*-?[0-9]+/' -readonly CFG_AWK_CMD='{ print ($2 + 0) }' - -declare -a cfg_fetch_period=($(awk -F '=' "${CFG_FETCH_REGEX}${CFG_AWK_CMD}" ${CFG_FILE} 2>/dev/null)) -readonly FETCH_PERIOD=$([ "${cfg_fetch_period[0]}" ] && echo ${cfg_fetch_period[0]} || echo '-1') +readonly DEF_CFG_FILE="${THIS_DIR}"/gbs-config.sh.inc +readonly CFG_FILE=$( ( [[ "$GBS_CFG_FILE" && -f "$GBS_CFG_FILE" ]] && echo -n "$GBS_CFG_FILE" ) || \ + ( [[ "$DEF_CFG_FILE" && -f "$DEF_CFG_FILE" ]] && echo -n "$DEF_CFG_FILE" ) ) +source "$CFG_FILE" 2> /dev/null +readonly FETCH_PERIOD=$( ( [[ "$GBS_FETCH_PERIOD" =~ ^-?[0-9]+$ ]] && echo -n "$GBS_FETCH_PERIOD" ) || \ + ( [[ "$CFG_FETCH_PERIOD" =~ ^-?[0-9]+$ ]] && echo -n "$CFG_FETCH_PERIOD" ) || \ + echo -n '-1' ) +readonly LAST_FETCH_FILE=$( ( touch "$GBS_LAST_FETCH_FILE" 2> /dev/null && echo -n "$GBS_LAST_FETCH_FILE" ) || \ + ( touch "$CFG_LAST_FETCH_FILE" 2> /dev/null && echo -n "$CFG_LAST_FETCH_FILE" ) || \ + echo -n ~/.GBS_LAST_FETCH ) +readonly USE_ANSI_COLOR=$( ( [[ "$GBS_USE_ANSI_COLOR" =~ ^[01]$ ]] && echo -n "$GBS_USE_ANSI_COLOR" ) || \ + ( [[ "$CFG_USE_ANSI_COLOR" =~ ^[01]$ ]] && echo -n "$CFG_USE_ANSI_COLOR" ) || \ + echo -n '1' ) ## constants ## @@ -133,12 +139,12 @@ readonly ALL_PADDING_W=$(( $INNER_PADDING_W + $MARGINS_PAD_W )) readonly MAX_DIVERGENCE_W=12 # e.g. "(behind 999)" readonly MIN_TTY_W=60 # ASSERT: (0 < (ALL_PADDING_W + (MAX_DIVERGENCE_W * 2)) <= MIN_TTY_W) readonly MARGIN_PAD=$(printf "%$(( $MARGIN_PAD_W ))s") -readonly CWHITE='\033[0;37m' -readonly CRED='\033[0;31m' -readonly CGREEN='\033[0;32m' -readonly CYELLOW='\033[1;33m' -readonly CBLUE='\033[1;34m' -readonly CEND='\033[0m' +readonly CWHITE=$( (( $USE_ANSI_COLOR )) && echo '\033[0;37m' ) +readonly CRED=$( (( $USE_ANSI_COLOR )) && echo '\033[0;31m' ) +readonly CGREEN=$( (( $USE_ANSI_COLOR )) && echo '\033[0;32m' ) +readonly CYELLOW=$( (( $USE_ANSI_COLOR )) && echo '\033[1;33m' ) +readonly CBLUE=$( (( $USE_ANSI_COLOR )) && echo '\033[1;34m' ) +readonly CEND=$( (( $USE_ANSI_COLOR )) && echo '\033[0m' ) readonly CDEFAULT=$CWHITE readonly CTRACKING=$CBLUE readonly CAHEAD=$CYELLOW @@ -372,11 +378,11 @@ FetchRemotes() { if (( ${FETCH_PERIOD} >= 0 )) then local now_ts=$(date +%s) - local last_fetch_ts=$(awk 'NR==1 { print ($1 + 0) }' ${LAST_FETCH_FILE} 2> /dev/null) + local last_fetch_ts=$(( $(cat ${LAST_FETCH_FILE} 2> /dev/null) + 0 )) - if (( ${now_ts} - (${last_fetch_ts} + 0) >= ${FETCH_PERIOD} * 60 )) + if (( ${now_ts} - ${last_fetch_ts} >= ${FETCH_PERIOD} * 60 )) then git fetch --all - echo ${now_ts} > ${LAST_FETCH_FILE} + echo -n ${now_ts} > ${LAST_FETCH_FILE} fi fi }