Skip to content

Commit 47e9760

Browse files
authored
Merge pull request #5 from SangwoonYun/feat/auto-update
fix: Modify last update check logic
2 parents 5f05d97 + 8065dc7 commit 47e9760

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Ignore last update check file for zsh-auto-venv plugin
2+
.last_update_check
3+

zsh-auto-venv.plugin.zsh

+27-13
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
# Plugin update check function
22
auto_venv_update_check() {
3-
local remote_tag local_tag repo_path user_input
4-
repo_path="${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-auto-venv"
5-
6-
# Check if git is available, if not, skip update check
7-
if ! command -v git &> /dev/null; then
8-
# Skip the update check if git is not installed
3+
local update_file="$ZSH_CUSTOM/plugins/zsh-auto-venv/.last_update_check"
4+
local current_time=$(date +%s)
5+
local last_update_check
6+
7+
if [[ -f "$update_file" ]]; then
8+
last_update_check=$(cat "$update_file")
9+
else
10+
last_update_check=0
11+
fi
12+
13+
# Check for updates every 24 hours (86400 seconds)
14+
if (( current_time - last_update_check < 86400 )); then
15+
# Last check was less than 24 hours ago, skip update check
916
return
1017
fi
1118

12-
# Fetch the latest tags from the remote, skip if fails
19+
# Record the time of this update check
20+
echo $current_time > "$update_file"
21+
22+
# Proceed with the rest of the update check process...
23+
local remote_tag local_tag repo_path user_input
24+
repo_path="${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-auto-venv"
25+
26+
# Skip update check if git is not installed
27+
if ! command -v git &> /dev/null; then return; fi
28+
29+
# Skip if fails to fetch the latest tags from the remote
1330
git -C "$repo_path" fetch --tags 2> /dev/null || return
1431

15-
# Get the latest tag name from the remote and local, continue only if both are retrieved
32+
# Continue only if both tags are retrieved successfully
1633
remote_tag=$(git -C "$repo_path" describe --tags `git -C "$repo_path" rev-list --tags --max-count=1` 2> /dev/null)
1734
local_tag=$(git -C "$repo_path" describe --tags 2> /dev/null)
18-
if [ -z "$remote_tag" ] || [ -z "$local_tag" ]; then
19-
# Skip the update process if unable to retrieve tags
20-
return
21-
fi
35+
if [ -z "$remote_tag" ] || [ -z "$local_tag" ]; then return; fi
2236

23-
# Check if the latest tag matches the currently checked-out version
37+
# Notify user of new version and ask for update permission
2438
if [ "$remote_tag" != "$local_tag" ]; then
2539
echo "New version available for zsh-auto-venv: $remote_tag (current version: $local_tag)"
2640
echo -n "Do you want to update the plugin now? [Y/n] "

0 commit comments

Comments
 (0)