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 # 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 ## 4.8
- Add option to use git reset instead of git pull - Add option to use git reset instead of git pull
- Validate git origin URL - Validate git origin URL

View File

@@ -1,6 +1,6 @@
{ {
"name": "Git pull", "name": "Git pull",
"version": "4.8", "version": "4.9",
"slug": "git_pull", "slug": "git_pull",
"description": "Simple git pull to update the local configuration", "description": "Simple git pull to update the local configuration",
"url": "https://home-assistant.io/addons/git_pull/", "url": "https://home-assistant.io/addons/git_pull/",
@@ -16,6 +16,7 @@
"git_branch": "master", "git_branch": "master",
"git_command": "pull", "git_command": "pull",
"git_remote": "origin", "git_remote": "origin",
"git_prune": false,
"repository": null, "repository": null,
"auto_restart": false, "auto_restart": false,
"repeat": { "repeat": {
@@ -31,6 +32,7 @@
"git_branch": "str", "git_branch": "str",
"git_command": "match(pull|reset)", "git_command": "match(pull|reset)",
"git_remote": "str", "git_remote": "str",
"git_prune": "bool",
"repository": "match((?:.+):(\/\/)?(.*?)(\\.git)(\/?|\\#[-\\d\\w._]+?))", "repository": "match((?:.+):(\/\/)?(.*?)(\\.git)(\/?|\\#[-\\d\\w._]+?))",
"auto_restart": "bool", "auto_restart": "bool",
"repeat": { "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_BRANCH=$(jq --raw-output '.git_branch' $CONFIG_PATH)
GIT_COMMAND=$(jq --raw-output '.git_command' $CONFIG_PATH) GIT_COMMAND=$(jq --raw-output '.git_command' $CONFIG_PATH)
GIT_REMOTE=$(jq --raw-output '.git_remote' $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) REPOSITORY=$(jq --raw-output '.repository' $CONFIG_PATH)
AUTO_RESTART=$(jq --raw-output '.auto_restart' $CONFIG_PATH) AUTO_RESTART=$(jq --raw-output '.auto_restart' $CONFIG_PATH)
REPEAT_ACTIVE=$(jq --raw-output '.repeat.active' $CONFIG_PATH) REPEAT_ACTIVE=$(jq --raw-output '.repeat.active' $CONFIG_PATH)
@@ -111,7 +112,7 @@ fi
function git-synchronize { function git-synchronize {
# is /config a local git repo? # 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 then
echo "[Info] Local git repository exists" echo "[Info] Local git repository exists"
@@ -122,18 +123,36 @@ function git-synchronize {
echo "[Info] Git origin is correctly set to $REPOSITORY" echo "[Info] Git origin is correctly set to $REPOSITORY"
OLD_COMMIT=$(git rev-parse HEAD) 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 # Pull or reset depending on user preference
case "$GIT_COMMAND" in case "$GIT_COMMAND" in
pull) pull)
echo "[Info] Start git 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; } git pull || { echo "[Error] Git pull failed"; exit 1; }
;; ;;
reset) reset)
echo "[Info] Start git reset..." echo "[Info] Start git reset..."
git checkout "$GIT_BRANCH" || { echo "[Error] Git checkout failed"; exit 1; } git reset --hard "$GIT_REMOTE"/"$GIT_CURRENT_BRANCH" || { echo "[Error] Git reset 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; }
;; ;;
*) *)
echo "[Error] Git command is not set correctly. Should be either 'reset' or 'pull'" 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" echo "[Info] Restart Home-Assistant"
hassio homeassistant restart 2&> /dev/null hassio homeassistant restart 2&> /dev/null
else else
echo "[Info] Local configuration has changed. Restart requried." echo "[Info] Local configuration has changed. Restart required."
fi fi
else else
echo "[Error] Configuration updated but it does not pass the config check. Do not restart until this is fixed!" 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 #### #### Main program ####
cd /config || { echo "[Error] Failed to cd into /config"; exit 1; } cd /config || { echo "[Error] Failed to cd into /config"; exit 1; }
while true; do while true; do
check-ssh-key check-ssh-key
setup-user-password setup-user-password
git-synchronize git-synchronize
validate-config validate-config
# do we repeat? # do we repeat?
if [ -z "$REPEAT_ACTIVE" ]; then if [ ! "$REPEAT_ACTIVE" == "true" ]; then
exit 0 exit 0
fi fi
sleep "$REPEAT_INTERVAL" sleep "$REPEAT_INTERVAL"