Skip to content

Commit 99b6fec

Browse files
committed
implement remote fetching per user-defined frequency
1 parent b3d9243 commit 99b6fec

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

config.ini.example

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
######################################################################
2+
# optional user-defined config for the 'git-branch-status' program #
3+
# copy this file to: ./config.ini to customize #
4+
######################################################################
5+
6+
7+
# number of minutes between automatic `git fetch --all` calls (0 => always, -1 => never)
8+
FETCH_PERIOD=-1

git-branch-status

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ EXAMPLES:
112112
USAGE_MSG
113113

114114

115+
## user-defined constants (see ./config.ini.example) ##
116+
117+
readonly THIS_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || echo $0)")"
118+
readonly CFG_FILE=${THIS_DIR}/config.ini.example
119+
readonly LAST_FETCH_FILE=${THIS_DIR}/LAST_FETCH
120+
readonly CFG_FETCH_REGEX='/^\s*FETCH_PERIOD\s*=\s*-?[0-9]+/'
121+
readonly CFG_AWK_CMD='{ print ($2 + 0) }'
122+
123+
declare -a cfg_fetch_period=($(awk -F '=' "${CFG_FETCH_REGEX}${CFG_AWK_CMD}" ${CFG_FILE} 2>/dev/null))
124+
readonly FETCH_PERIOD=$([ "${cfg_fetch_period[0]}" ] && echo ${cfg_fetch_period[0]} || echo '-1')
125+
126+
115127
## constants ##
116128

117129
readonly INNER_PADDING_W=13 # '| ' + ' | ' + ' | ' + ' | ' + ' |'
@@ -354,6 +366,19 @@ function Init # (cli_args)
354366
readonly COMPARE_BRANCH=$branch_b
355367
}
356368

369+
function FetchRemotes
370+
{
371+
if (( ${FETCH_PERIOD} >= 0 ))
372+
then local now_ts=$(date +%s)
373+
local last_fetch_ts=$(awk 'NR==1 { print ($1 + 0) }' ${LAST_FETCH_FILE} 2> /dev/null)
374+
375+
if (( ${now_ts} - (${last_fetch_ts} + 0) >= ${FETCH_PERIOD} * 60 ))
376+
then git fetch --all
377+
echo ${now_ts} > ${LAST_FETCH_FILE}
378+
fi
379+
fi
380+
}
381+
357382
function GenerateReports
358383
{
359384
if [ "$COMPARE_BRANCH" ]
@@ -600,4 +625,5 @@ function Reset
600625
## main entry ##
601626

602627
Init $*
628+
FetchRemotes
603629
GenerateReports

0 commit comments

Comments
 (0)