Improved the `_git_time_since_commit` function.
Check for at least one commit using a `git log` limit of one.
Improves the performance inside git repos with a large history.
Diff
1diff --git a/themes/avit.zsh-theme b/themes/avit.zsh-theme
2index a21a9c8c4cbd2831d24fb35338583e1bdb7ea6ff..65466b82f4458e52c4728801c2c76a8b0b43291a 100644
3--- a/themes/avit.zsh-theme
4+++ b/themes/avit.zsh-theme
5@@ -38,34 +38,32 @@ function _ruby_version() {
6 # Determine the time since last commit. If branch is clean,
7 # use a neutral color, otherwise colors will vary according to time.
8 function _git_time_since_commit() {
9- if git rev-parse --git-dir > /dev/null 2>&1; then
10- # Only proceed if there is actually a commit.
11- if [[ $(git log 2>&1 > /dev/null | grep -c "^fatal: bad default revision") == 0 ]]; then
12- # Get the last commit.
13- last_commit=$(git log --pretty=format:'%at' -1 2> /dev/null)
14- now=$(date +%s)
15- seconds_since_last_commit=$((now-last_commit))
16-
17- # Totals
18- minutes=$((seconds_since_last_commit / 60))
19- hours=$((seconds_since_last_commit/3600))
20-
21- # Sub-hours and sub-minutes
22- days=$((seconds_since_last_commit / 86400))
23- sub_hours=$((hours % 24))
24- sub_minutes=$((minutes % 60))
25-
26- if [ $hours -gt 24 ]; then
27- commit_age="${days}d"
28- elif [ $minutes -gt 60 ]; then
29- commit_age="${sub_hours}h${sub_minutes}m"
30- else
31- commit_age="${minutes}m"
32- fi
33-
34- color=$ZSH_THEME_GIT_TIME_SINCE_COMMIT_NEUTRAL
35- echo "$color$commit_age%{$reset_color%}"
36+# Only proceed if there is actually a commit.
37+ if git log -1 > /dev/null 2>&1; then
38+ # Get the last commit.
39+ last_commit=$(git log --pretty=format:'%at' -1 2> /dev/null)
40+ now=$(date +%s)
41+ seconds_since_last_commit=$((now-last_commit))
42+
43+ # Totals
44+ minutes=$((seconds_since_last_commit / 60))
45+ hours=$((seconds_since_last_commit/3600))
46+
47+ # Sub-hours and sub-minutes
48+ days=$((seconds_since_last_commit / 86400))
49+ sub_hours=$((hours % 24))
50+ sub_minutes=$((minutes % 60))
51+
52+ if [ $hours -gt 24 ]; then
53+ commit_age="${days}d"
54+ elif [ $minutes -gt 60 ]; then
55+ commit_age="${sub_hours}h${sub_minutes}m"
56+ else
57+ commit_age="${minutes}m"
58 fi
59+
60+ color=$ZSH_THEME_GIT_TIME_SINCE_COMMIT_NEUTRAL
61+ echo "$color$commit_age%{$reset_color%}"
62 fi
63 }
6465@@ -99,4 +97,3 @@ ZSH_THEME_GIT_TIME_SINCE_COMMIT_NEUTRAL="%{$fg[grey]%}"
66 export LSCOLORS="exfxcxdxbxegedabagacad"
67 export LS_COLORS='di=34;40:ln=35;40:so=32;40:pi=33;40:ex=31;40:bd=34;46:cd=34;43:su=0;41:sg=0;46:tw=0;42:ow=0;43:'
68 export GREP_COLOR='1;33'
69-