Browse Source

feat(cli): add `omz version` command

Marc Cornellà 2 years ago
parent
commit
f0f792fa6b
1 changed files with 23 additions and 0 deletions
  1. 23 0
      lib/cli.zsh

+ 23 - 0
lib/cli.zsh

@@ -29,6 +29,7 @@ function _omz {
     'reload:Reload the current zsh session'
     'theme:Manage themes'
     'update:Update Oh My Zsh'
+    'version:Show the version'
   )
 
   if (( CURRENT == 2 )); then
@@ -164,6 +165,7 @@ Available commands:
   reload              Reload the current zsh session
   theme  <command>    Manage themes
   update              Update Oh My Zsh
+  version             Show the version
 
 EOF
 }
@@ -777,3 +779,24 @@ function _omz::update {
     [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
   fi
 }
+
+function _omz::version {
+  (
+    cd "$ZSH"
+
+    # Get the version name:
+    # 1) try tag-like version
+    # 2) try name-rev
+    # 3) try branch name
+    local version
+    version=$(command git describe --tags HEAD 2>/dev/null) \
+    || version=$(command git name-rev --no-undefined --name-only --exclude="remotes/*" HEAD 2>/dev/null) \
+    || version=$(command git symbolic-ref --quiet --short HEAD 2>/dev/null)
+
+    # Get short hash for the current HEAD
+    local commit=$(command git rev-parse --short HEAD 2>/dev/null)
+
+    # Show version and commit hash
+    printf "%s (%s)\n" "$version" "$commit"
+  )
+}