git_pull: bugfixes and enhancements (#386)

* enhancement, bugfixes, version bump

* fix travis ci errors

* Fixes #389 and enhancement: git_prune

* add double-quoting/fix travis

* Bugfix after fixing CI errors

* check_config bugfix: always do a git fetch

* Bugfix: always fetch; Code Optimization

* Code Optimization

* Prune not only when switching branch

* Verbosity
This commit is contained in:
Censored3
2018-08-29 00:30:21 +02:00
committed by Pascal Vizeli
parent d7b34cfdc1
commit 193b988174
3 changed files with 39 additions and 11 deletions

View File

@@ -1,5 +1,11 @@
# Changelog
## 4.9
- Fix git repo detection in config-dir - #372
- Fix repeat option detection - #375
- Allow to stay on the currently checked out branch - set "git_branch": ""
- Correct typo
## 4.8
- Add option to use git reset instead of git pull
- Validate git origin URL

View File

@@ -1,6 +1,6 @@
{
"name": "Git pull",
"version": "4.8",
"version": "4.9",
"slug": "git_pull",
"description": "Simple git pull to update the local configuration",
"url": "https://home-assistant.io/addons/git_pull/",
@@ -16,6 +16,7 @@
"git_branch": "master",
"git_command": "pull",
"git_remote": "origin",
"git_prune": false,
"repository": null,
"auto_restart": false,
"repeat": {
@@ -31,6 +32,7 @@
"git_branch": "str",
"git_command": "match(pull|reset)",
"git_remote": "str",
"git_prune": "bool",
"repository": "match((?:.+):(\/\/)?(.*?)(\\.git)(\/?|\\#[-\\d\\w._]+?))",
"auto_restart": "bool",
"repeat": {

View File

@@ -11,6 +11,7 @@ DEPLOYMENT_PASSWORD=$(jq --raw-output ".deployment_password" $CONFIG_PATH)
GIT_BRANCH=$(jq --raw-output '.git_branch' $CONFIG_PATH)
GIT_COMMAND=$(jq --raw-output '.git_command' $CONFIG_PATH)
GIT_REMOTE=$(jq --raw-output '.git_remote' $CONFIG_PATH)
GIT_PRUNE=$(jq --raw-output '.git_prune' $CONFIG_PATH)
REPOSITORY=$(jq --raw-output '.repository' $CONFIG_PATH)
AUTO_RESTART=$(jq --raw-output '.auto_restart' $CONFIG_PATH)
REPEAT_ACTIVE=$(jq --raw-output '.repeat.active' $CONFIG_PATH)
@@ -40,10 +41,10 @@ function git-clone {
# create backup
BACKUP_LOCATION="/tmp/config-$(date +%Y-%m-%d_%H-%M-%S)"
echo "[Info] Backup configuration to $BACKUP_LOCATION"
mkdir "${BACKUP_LOCATION}" || { echo "[Error] Creation of backup directory failed"; exit 1; }
cp -rf /config/* "${BACKUP_LOCATION}" || { echo "[Error] Copy files to backup directory failed"; exit 1; }
# remove config folder content
rm -rf /config/{,.[!.],..?}* || { echo "[Error] Clearing /config failed"; exit 1; }
@@ -111,7 +112,7 @@ fi
function git-synchronize {
# is /config a local git repo?
if git rev-parse --is-inside-git-dir &>/dev/null
if git rev-parse --is-inside-work-tree &>/dev/null
then
echo "[Info] Local git repository exists"
@@ -121,19 +122,37 @@ function git-synchronize {
then
echo "[Info] Git origin is correctly set to $REPOSITORY"
OLD_COMMIT=$(git rev-parse HEAD)
# Always do a fetch to update repos
echo "[Info] Start git fetch..."
git fetch "$GIT_REMOTE" || { echo "[Error] Git fetch failed"; exit 1; }
# Prune if configured
if [ "$GIT_PRUNE" == "true" ]
then
echo "[Info] Start git prune..."
git prune || { echo "[Error] Git prune failed"; exit 1; }
fi
# Do we switch branches?
GIT_CURRENT_BRANCH=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
if [ -z "$GIT_BRANCH" ] || [ "$GIT_BRANCH" == "$GIT_CURRENT_BRANCH" ]; then
echo "[Info] Staying on currently checked out branch: $GIT_CURRENT_BRANCH..."
else
echo "[Info] Switching branches - start git checkout of branch $GIT_BRANCH..."
git checkout "$GIT_BRANCH" || { echo "[Error] Git checkout failed"; exit 1; }
GIT_CURRENT_BRANCH=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
fi
# Pull or reset depending on user preference
case "$GIT_COMMAND" in
pull)
echo "[Info] Start git pull..."
git checkout "$GIT_BRANCH" || { echo "[Error] Git checkout failed"; exit 1; }
git pull || { echo "[Error] Git pull failed"; exit 1; }
;;
reset)
echo "[Info] Start git reset..."
git checkout "$GIT_BRANCH" || { echo "[Error] Git checkout failed"; exit 1; }
git fetch "$GIT_REMOTE" "$GIT_BRANCH" || { echo "[Error] Git fetch failed"; exit 1; }
git reset --hard "$GIT_REMOTE"/"$GIT_BRANCH" || { echo "[Error] Git reset failed"; exit 1; }
git reset --hard "$GIT_REMOTE"/"$GIT_CURRENT_BRANCH" || { echo "[Error] Git reset failed"; exit 1; }
;;
*)
echo "[Error] Git command is not set correctly. Should be either 'reset' or 'pull'"
@@ -161,7 +180,7 @@ function validate-config {
echo "[Info] Restart Home-Assistant"
hassio homeassistant restart 2&> /dev/null
else
echo "[Info] Local configuration has changed. Restart requried."
echo "[Info] Local configuration has changed. Restart required."
fi
else
echo "[Error] Configuration updated but it does not pass the config check. Do not restart until this is fixed!"
@@ -175,13 +194,14 @@ function validate-config {
#### Main program ####
cd /config || { echo "[Error] Failed to cd into /config"; exit 1; }
while true; do
check-ssh-key
setup-user-password
git-synchronize
validate-config
# do we repeat?
if [ -z "$REPEAT_ACTIVE" ]; then
if [ ! "$REPEAT_ACTIVE" == "true" ]; then
exit 0
fi
sleep "$REPEAT_INTERVAL"