From 851947bbf2fa89defc972b8991e612cd95f24566 Mon Sep 17 00:00:00 2001 From: Adam Warner Date: Mon, 13 Apr 2020 20:58:46 +0100 Subject: [PATCH 1/6] Add branch name to version output Signed-off-by: Adam Warner --- advanced/Scripts/version.sh | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index f6d4d344..16bad62a 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -97,28 +97,52 @@ getRemoteVersion(){ return 0 } +getLocalBranch(){ + # Local FTL btranch is stored in /etc/pihole/ftlbranch + if [[ "$1" == "FTL" ]]; then + cat /etc/pihole/ftlbranch + return 0 + fi + + # Get the checked out branch of the local directory + local directory="${1}" + local branch + + cd "${directory}" 2> /dev/null || { echo "${DEFAULT}"; return 1; } + branch=$(git rev-parse --abbrev-ref HEAD || echo "$DEFAULT") + if [[ ! "${branch}" =~ ^v ]]; then + echo "${branch}" + elif [[ "${branch}" == "${DEFAULT}" ]]; then + echo "ERROR" + return 1 + else + echo "No Checked out branch" + fi + return 0 +} + versionOutput() { [[ "$1" == "pi-hole" ]] && GITDIR=$COREGITDIR [[ "$1" == "AdminLTE" ]] && GITDIR=$WEBGITDIR [[ "$1" == "FTL" ]] && GITDIR="FTL" - [[ "$2" == "-c" ]] || [[ "$2" == "--current" ]] || [[ -z "$2" ]] && current=$(getLocalVersion $GITDIR) + [[ "$2" == "-c" ]] || [[ "$2" == "--current" ]] || [[ -z "$2" ]] && current=$(getLocalVersion $GITDIR) && branch=$(getLocalBranch $GITDIR) [[ "$2" == "-l" ]] || [[ "$2" == "--latest" ]] || [[ -z "$2" ]] && latest=$(getRemoteVersion "$1") if [[ "$2" == "-h" ]] || [[ "$2" == "--hash" ]]; then - [[ "$3" == "-c" ]] || [[ "$3" == "--current" ]] || [[ -z "$3" ]] && curHash=$(getLocalHash "$GITDIR") + [[ "$3" == "-c" ]] || [[ "$3" == "--current" ]] || [[ -z "$3" ]] && curHash=$(getLocalHash "$GITDIR") && branch=$(getLocalBranch $GITDIR) [[ "$3" == "-l" ]] || [[ "$3" == "--latest" ]] || [[ -z "$3" ]] && latHash=$(getRemoteHash "$1" "$(cd "$GITDIR" 2> /dev/null && git rev-parse --abbrev-ref HEAD)") fi if [[ -n "$current" ]] && [[ -n "$latest" ]]; then - output="${1^} version is $current (Latest: $latest)" + output="${1^} version is $current (Latest: $latest) Branch: $branch" elif [[ -n "$current" ]] && [[ -z "$latest" ]]; then - output="Current ${1^} version is $current" + output="Current ${1^} version is $current. Branch: $branch" elif [[ -z "$current" ]] && [[ -n "$latest" ]]; then output="Latest ${1^} version is $latest" elif [[ "$curHash" == "N/A" ]] || [[ "$latHash" == "N/A" ]]; then output="${1^} hash is not applicable" elif [[ -n "$curHash" ]] && [[ -n "$latHash" ]]; then - output="${1^} hash is $curHash (Latest: $latHash)" + output="${1^} hash is $curHash (Latest: $latHash) Branch: $branch" elif [[ -n "$curHash" ]] && [[ -z "$latHash" ]]; then output="Current ${1^} hash is $curHash" elif [[ -z "$curHash" ]] && [[ -n "$latHash" ]]; then From 6dc85c3527ee532888cf52d028f5da1fbc8a920e Mon Sep 17 00:00:00 2001 From: Adam Warner Date: Sat, 18 Apr 2020 12:51:04 +0100 Subject: [PATCH 2/6] Don't display branch name if it is on master. Prefer cached remote version over github API Signed-off-by: Adam Warner --- advanced/Scripts/version.sh | 43 ++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index 16bad62a..da017187 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -84,6 +84,21 @@ getRemoteVersion(){ # Get the version from the remote origin local daemon="${1}" local version + local cachedVersions + local arrCache + cachedVersions="/etc/pihole/GitHubVersions" + + #If the above file exists, then we can read from that. Prevents overuse of Github API + if [[ -f "$cachedVersions" ]]; then + IFS=' ' read -r -a arrCache <<< $(cat $cachedVersions) + case $daemon in + "pi-hole" ) echo ${arrCache[0]};; + "AdminLTE" ) echo ${arrCache[1]};; + "FTL" ) echo ${arrCache[2]};; + esac + + return 0 + fi version=$(curl --silent --fail "https://api.github.com/repos/pi-hole/${daemon}/releases/latest" | \ awk -F: '$1 ~/tag_name/ { print $2 }' | \ @@ -98,20 +113,23 @@ getRemoteVersion(){ } getLocalBranch(){ - # Local FTL btranch is stored in /etc/pihole/ftlbranch - if [[ "$1" == "FTL" ]]; then - cat /etc/pihole/ftlbranch - return 0 - fi - # Get the checked out branch of the local directory local directory="${1}" local branch - cd "${directory}" 2> /dev/null || { echo "${DEFAULT}"; return 1; } - branch=$(git rev-parse --abbrev-ref HEAD || echo "$DEFAULT") + # Local FTL btranch is stored in /etc/pihole/ftlbranch + if [[ "$1" == "FTL" ]]; then + branch=$(cat /etc/pihole/ftlbranch) + else + cd "${directory}" 2> /dev/null || { echo "${DEFAULT}"; return 1; } + branch=$(git rev-parse --abbrev-ref HEAD || echo "$DEFAULT") + fi if [[ ! "${branch}" =~ ^v ]]; then - echo "${branch}" + if [[ "${branch}" == "master" ]]; then + echo "" + else + echo "${branch} " + fi elif [[ "${branch}" == "${DEFAULT}" ]]; then echo "ERROR" return 1 @@ -132,17 +150,16 @@ versionOutput() { [[ "$3" == "-c" ]] || [[ "$3" == "--current" ]] || [[ -z "$3" ]] && curHash=$(getLocalHash "$GITDIR") && branch=$(getLocalBranch $GITDIR) [[ "$3" == "-l" ]] || [[ "$3" == "--latest" ]] || [[ -z "$3" ]] && latHash=$(getRemoteHash "$1" "$(cd "$GITDIR" 2> /dev/null && git rev-parse --abbrev-ref HEAD)") fi - if [[ -n "$current" ]] && [[ -n "$latest" ]]; then - output="${1^} version is $current (Latest: $latest) Branch: $branch" + output="${1^} version is $branch$current (Latest: $latest)" elif [[ -n "$current" ]] && [[ -z "$latest" ]]; then - output="Current ${1^} version is $current. Branch: $branch" + output="Current ${1^} version is $branch$current." elif [[ -z "$current" ]] && [[ -n "$latest" ]]; then output="Latest ${1^} version is $latest" elif [[ "$curHash" == "N/A" ]] || [[ "$latHash" == "N/A" ]]; then output="${1^} hash is not applicable" elif [[ -n "$curHash" ]] && [[ -n "$latHash" ]]; then - output="${1^} hash is $curHash (Latest: $latHash) Branch: $branch" + output="${1^} hash is $curHash (Latest: $latHash)" elif [[ -n "$curHash" ]] && [[ -z "$latHash" ]]; then output="Current ${1^} hash is $curHash" elif [[ -z "$curHash" ]] && [[ -n "$latHash" ]]; then From 3cc9ba4ee8f22d4af6837016174d798b6f1bab13 Mon Sep 17 00:00:00 2001 From: Adam Warner Date: Sat, 18 Apr 2020 12:53:19 +0100 Subject: [PATCH 3/6] stickler Signed-off-by: Adam Warner --- advanced/Scripts/version.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index da017187..2c4abd0b 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -90,11 +90,11 @@ getRemoteVersion(){ #If the above file exists, then we can read from that. Prevents overuse of Github API if [[ -f "$cachedVersions" ]]; then - IFS=' ' read -r -a arrCache <<< $(cat $cachedVersions) + IFS=' ' read -r -a arrCache < "$cachedVersions" case $daemon in - "pi-hole" ) echo ${arrCache[0]};; - "AdminLTE" ) echo ${arrCache[1]};; - "FTL" ) echo ${arrCache[2]};; + "pi-hole" ) echo "${arrCache[0]}";; + "AdminLTE" ) echo "${arrCache[1]}";; + "FTL" ) echo "${arrCache[2]}";; esac return 0 From 176fbaf83bb3034ba7b19656d23a1d147a0686da Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 21 Apr 2020 08:51:17 +0200 Subject: [PATCH 4/6] Ask pihole-FTL for the branch it was compiled from instead of trusting the checkout file to be present. Signed-off-by: DL6ER --- advanced/Scripts/version.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index 2c4abd0b..f79d5614 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -119,7 +119,7 @@ getLocalBranch(){ # Local FTL btranch is stored in /etc/pihole/ftlbranch if [[ "$1" == "FTL" ]]; then - branch=$(cat /etc/pihole/ftlbranch) + branch="$(pihole-FTL branch)" else cd "${directory}" 2> /dev/null || { echo "${DEFAULT}"; return 1; } branch=$(git rev-parse --abbrev-ref HEAD || echo "$DEFAULT") From 03431717030a3f627a4112b73e19c3f8e4458909 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 21 Apr 2020 08:54:28 +0200 Subject: [PATCH 5/6] Add correct displaying for detached HEAD state. Signed-off-by: DL6ER --- advanced/Scripts/version.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index f79d5614..c4788d99 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -127,6 +127,8 @@ getLocalBranch(){ if [[ ! "${branch}" =~ ^v ]]; then if [[ "${branch}" == "master" ]]; then echo "" + elif [[ "${branch}" == "HEAD" ]]; then + echo "in detached HEAD state at " else echo "${branch} " fi From 401c029dc4b6eff628159cc6fae164d7465eb12d Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 21 Apr 2020 16:08:32 +0200 Subject: [PATCH 6/6] Improve else condition of branch determination Signed-off-by: DL6ER --- advanced/Scripts/version.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index c4788d99..d2c41cba 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -132,11 +132,9 @@ getLocalBranch(){ else echo "${branch} " fi - elif [[ "${branch}" == "${DEFAULT}" ]]; then - echo "ERROR" - return 1 else - echo "No Checked out branch" + # Branch started in "v" + echo "release " fi return 0 }