Selaa lähdekoodia

add commit id and git mode to zsh prompt, add decorations to git log

Commit ids are really useful, especially with reflog and resets.
Git modes are shown during bisects, merges and rebases.
Decorations show where the various refs point to (very cool!)
We also change git log's relative time stamp color to differentiate it from the
subject line. This should give us an easier overview of commit ages.
Weiyi Lou 13 vuotta sitten
vanhempi
commit
56e40ef2c6
2 muutettua tiedostoa jossa 25 lisäystä ja 10 poistoa
  1. 21 8
      zsh/omz-custom/cinaeco.zsh-theme
  2. 4 2
      zsh/omz-custom/git.zsh

+ 21 - 8
zsh/omz-custom/cinaeco.zsh-theme

@@ -8,7 +8,7 @@ RPROMPT='$(vi_mode_prompt_info) %{$reset_color%}%T %{$fg[white]%}%h%{$reset_colo
 
 MODE_INDICATOR="%{$fg[green]%}vi-mode%{$reset_color%}"
 
-ZSH_THEME_GIT_PROMPT_PREFIX="[git:"
+ZSH_THEME_GIT_PROMPT_PREFIX="["
 ZSH_THEME_GIT_PROMPT_SUFFIX="]%{$reset_color%}"
 ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[green]%}"
 ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}"
@@ -28,16 +28,29 @@ ZSH_THEME_GIT_INDEX_COPIED="c"
 ##############################
 
 ## Override the default `git_prompt_info` function
-## We decide if we show nothing, status with no branch (like in submodules) or
-## with a branch using regex comparisons.
-## Is there a better way than relying on error output?
+## Git commit id and mode code taken from:
+## https://github.com/benhoskings/dot-files/blob/master/files/bin/git_cwd_info
 function git_prompt_info() {
-  ref=$(git symbolic-ref HEAD 2>&1)
-  [[ $ref =~ "Not a git" ]] && return
-  [[ $ref =~ "not a symbolic" ]] && echo "$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_PREFIX$(current_repository):%{$fg[red]%}no branch$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX $(git_prompt_status)" && return
-  echo "$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_PREFIX$(current_repository):$(current_branch)$ZSH_THEME_GIT_PROMPT_SUFFIX $(git_prompt_status)"
+  GIT_REPO_PATH=$(git rev-parse --git-dir 2>/dev/null)
+  [[ $GIT_REPO_PATH == "" ]] && return
+
+  GIT_COMMIT_ID=`git rev-parse --short HEAD 2>/dev/null`
+
+  GIT_MODE=""
+  if [[ -e "$GIT_REPO_PATH/BISECT_LOG" ]]; then
+    GIT_MODE=" BISECT"
+  elif [[ -e "$GIT_REPO_PATH/MERGE_HEAD" ]]; then
+    GIT_MODE=" MERGE"
+  elif [[ -e "$GIT_REPO_PATH/rebase" || -e "$GIT_REPO_PATH/rebase-apply" || -e "$GIT_REPO_PATH/rebase-merge" || -e "$GIT_REPO_PATH/../.dotest" ]]; then
+    GIT_MODE=" REBASE"
+  fi
+
+  GIT_BRANCH=$(current_branch)
+  [[ $GIT_BRANCH == '' ]] && GIT_BRANCH="%{$fg[red]%}no branch$(parse_git_dirty)"
+  echo "$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_PREFIX$(current_repository):$GIT_BRANCH:$GIT_COMMIT_ID$ZSH_THEME_GIT_PROMPT_SUFFIX%{$fg[magenta]%}$GIT_MODE $(git_prompt_status)"
 }
 
+
 ## Override the default `git_prompt_status` function
 ## Preferably only use with multiline prompts
 ## Try to print a each change instead of just indicating if each type exists.

+ 4 - 2
zsh/omz-custom/git.zsh

@@ -10,11 +10,13 @@ alias gf='git fetch'
 
 alias gbd='git branch -D'
 
-alias gl='git log --graph --date=relative --pretty="format:%C(yellow)%h %Cblue[%cn] %Creset%s... %cd"'
+alias gl='git log --graph --pretty="format:%C(yellow)%h %Cblue[%cn]%Cgreen%d %Creset%s... %C(cyan)%ar"'
 alias gls='git log --graph --stat'
 alias glp='git log -p'
 
-alias gr='git rebase'
+alias grb='git rebase'
+alias grh='git reset --hard'
+alias grs='git reset --soft'
 
 alias gtrack='git branch --set-upstream $(current_branch) origin/$(current_branch) && git fetch'