diff --git a/CHANGELOG.md b/CHANGELOG.md index fc31817c..a6c3ac27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: @@ -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. diff --git a/check-for-updates.sh b/check-for-updates.sh new file mode 100644 index 00000000..8bfba4f2 --- /dev/null +++ b/check-for-updates.sh @@ -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 diff --git a/emu.sh b/emu.sh index 3355c9a1..8dca66a3 100644 --- a/emu.sh +++ b/emu.sh @@ -27,3 +27,5 @@ function emu(){ # main wrapper function _updateohmycomma fi } + +source ${OH_MY_COMMA_PATH}/check-for-updates.sh diff --git a/install.sh b/install.sh index ee1fe34d..7e421830 100755 --- a/install.sh +++ b/install.sh @@ -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 diff --git a/log/.gitignore b/log/.gitignore new file mode 100644 index 00000000..5373f218 --- /dev/null +++ b/log/.gitignore @@ -0,0 +1,3 @@ +**/** +** +!.gitignore