Skip to content

Commit

Permalink
Check for updates once a week, on session start (#39)
Browse files Browse the repository at this point in the history
* update checker test

* tweak update checker

* test

* test

* fix

* test

* test

* test

* uhh

* debug

* test exit

* test

* update last check

* test

* update check

* test

* fix

* test

* test

* test

* test

* test

* test

* test

* test

* test

* test

* test

* lmao

* test

* test

* fake update

* bump version

* 1 day lock

* test

* add branch status

* update branch status

* yeet

* remove text

* yeet

* prettier on changelong

* update changelog
  • Loading branch information
AskAlice authored Jul 24, 2020
1 parent be55f6c commit f91520b
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
Release 0.1.7 (2020-07-24)
=====

* Auto updater will check for updates. This is based on .oh-my-zsh's [check_for_upgrade.sh](https://github.com/ohmyzsh/ohmyzsh/blob/master/tools/check_for_upgrade.sh)

Release 0.1.6 (2020-07-23)
=====

* Add device command aliases: battery, settings, shutdown

Release 0.1.5 (2020-07-22)
=====

* Make the username argument under `emu fork switch` optional. If not specified, it will use the current fork switched to
* Add `--branch` (`-b`) flag for specifying the branch
* This means you must supply `-b` or `--branch` when switching branches, even when supplying the username:
Expand All @@ -16,10 +23,12 @@ Release 0.1.5 (2020-07-22)

Release 0.1.4 (2020-07-12)
=====

* Add `emu device settings` command to open the settings app

Release 0.1.3 (2020-07-06)
=====

* Make flags/arguments more robust. Optional non-positional arguments are now supported, as long as they are the last arguments.
* `emu fork switch` and `emu fork list` commands added. Uses one singular git repo and adds remotes of forks so that the time to install a new fork is reduced significantly since git is able to re-use blobs.
* A one-time setup is required when using the fork command, this full clones commaai/openpilot which may take a bit of time on first use.
Expand Down
75 changes: 75 additions & 0 deletions check-for-updates.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

#Based on https://github.com/ohmyzsh/ohmyzsh/blob/master/tools/check_for_upgrade.sh
if [[ "$OMC_DISABLE_AUTO_UPDATE" = true ]] \
|| ! command -v git &>/dev/null; then
return
fi
OMC_EPOCH=$(date +%s)
set +x
function current_epoch() {
echo $(($OMC_EPOCH/60/60/24))
}

function update_last_updated_file() {
echo "LAST_EPOCH=$(current_epoch)" > "${OH_MY_COMMA_PATH}/log/.omc-update"
}

# Remove lock directory if older than a day
if mtime=$(date +%s -r "$OH_MY_COMMA_PATH/log/update.lock" 2>/dev/null); then
if (( (mtime + 3600 * 24) < OMC_EPOCH )); then
command rm -rf "$OH_MY_COMMA_PATH/log/update.lock"
fi
fi

# Check for lock directory
if ! command mkdir "$OH_MY_COMMA_PATH/log/update.lock" 2>/dev/null; then
return
fi

# Remove lock directory on exit. `return 1` is important for when trapping a SIGINT:
# The return status from the function is handled specially. If it is zero, the signal is
# assumed to have been handled, and execution continues normally. Otherwise, the shell
# will behave as interrupted except that the return status of the trap is retained.
trap "command rm -rf '$OH_MY_COMMA_PATH/log/update.lock'; return" EXIT INT QUIT

# Create or update .omc-update file if missing or malformed
if ! source "${OH_MY_COMMA_PATH}/log/.omc-update" 2>/dev/null || [[ -z "$LAST_EPOCH" ]]; then
touch ${OH_MY_COMMA_PATH}/log/.omc-update
update_last_updated_file
fi

# Number of days before trying to update again
epoch_target=${OMC_AUTOUPDATE_DAYS:-7}
# Test if enough time has passed until the next update
if (( ( $(current_epoch) - LAST_EPOCH ) < $epoch_target )); then
return
fi

cd ${OH_MY_COMMA_PATH}

git fetch
OMC_UPSTREAM=${1:-'@{u}'}
OMC_LOCAL=$(git rev-parse @)
OMC_REMOTE=$(git rev-parse "$OMC_UPSTREAM")

if [ $OMC_LOCAL != $OMC_REMOTE ]; then
# Ask for confirmation before updating unless disabled
if [[ "$OMC_DISABLE_UPDATE_PROMPT" = true ]]; then
emu update
else
echo "[emu.sh] Current .oh-my-comma branch: $(git branch | head -n 1)"
echo "$(git status | head -n 2 | tail -n 1)"
# input sink to swallow all characters typed before the prompt
# and add a newline if there wasn't one after characters typed
read -r -p "[emu.sh] Update .oh-my-comma? [Y/n] " option
[[ "$option" != $'\n' ]] && echo
case "$option" in
[yY$'\n']) emu update && update_last_updated_file ;;
[nN]) update_last_updated_file ;;
*) emu update ;;
esac
fi
fi
cd -
unset -f current_epoch update_last_updated_file
2 changes: 2 additions & 0 deletions emu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ function emu(){ # main wrapper function
_updateohmycomma
fi
}

source ${OH_MY_COMMA_PATH}/check-for-updates.sh
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ COMMUNITY_BASHRC_PATH=/data/community/.bashrc
OH_MY_COMMA_PATH=/data/community/.oh-my-comma
GIT_BRANCH_NAME=master
GIT_REMOTE_URL=https://github.com/emu-sh/.oh-my-comma.git
OMC_VERSION=0.1.6
OMC_VERSION=0.1.7

update=false
if [ $# -ge 1 ] && [ $1 = "update" ]; then
Expand Down
3 changes: 3 additions & 0 deletions log/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/**
**
!.gitignore

0 comments on commit f91520b

Please sign in to comment.