]> git.donarmstrong.com Git - zsh.git/commitdiff
cleanup and prevent running as root
authormartin f. krafft <madduck@madduck.net>
Tue, 6 May 2008 07:19:28 +0000 (08:19 +0100)
committermartin f. krafft <madduck@madduck.net>
Tue, 6 May 2008 07:19:28 +0000 (08:19 +0100)
.zsh/zshrc/85_git_prompt [deleted file]
.zsh/zshrc/85_vcs_prompt [new file with mode: 0644]

diff --git a/.zsh/zshrc/85_git_prompt b/.zsh/zshrc/85_git_prompt
deleted file mode 100644 (file)
index 32d0e4f..0000000
+++ /dev/null
@@ -1,109 +0,0 @@
-# zshrc/85_git_prompt
-#
-# Make git information available to the prompt
-#
-# Copyright © 1994–2008 martin f. krafft <madduck@madduck.net>
-# Released under the terms of the Artistic Licence 2.0
-#
-# Source repository: http://git.madduck.net/v/etc/zsh.git
-#
-# Shamelessly based on http://glandium.org/blog/?p=170
-#
-
-__git_get_repo_root()
-{
-  local relroot
-  relroot="$(git rev-parse --show-cdup 2>/dev/null)" || return 1
-  if [ -n "$relroot" ]; then
-    readlink -f "$relroot"
-  else
-    echo $PWD
-  fi
-}
-
-__git_get_branch()
-{
-  local ref
-  ref=$(git symbolic-ref -q HEAD 2>/dev/null \
-     || git-name-rev --name-only HEAD 2>/dev/null)
-  echo "${ref#refs/heads/}"
-}
-
-__get_root_offsets()
-{
-  local pwda reporoot loc
-  pwda=(${(s:/:)PWD})
-  reporoot=(${(s:/:)1})
-  echo $((1 - $#reporoot)) $(($#pwda - $#reporoot))
-}
-
-__get_prompt_path_components()
-{
-  local reporoot
-  reporoot="$1"
-
-  set -- $(__get_root_offsets "$reporoot")
-  if [ "$2" -le 0 ]; then
-    echo %~
-  else
-    echo "%${1}~" "%${2}~"
-  fi
-}
-
-__vcs_get_repo_type()
-{
-  if __git_get_repo_root >/dev/null; then
-    echo git
-  else
-    echo NONE
-  fi
-}
-
-__vcs_set_prompt_variables()
-{
-  local pre branch post
-  local MAXLEN=25
-
-  case "${1:-$(__vcs_get_repo_type)}" in
-    git)
-      local reporoot="$(__git_get_repo_root)"
-      set -- $(__get_prompt_path_components "$reporoot")
-      branch="$(__git_get_branch)"
-      post="${(%)2}"
-      local prelen="$((MAXLEN - $#post - $#branch))"
-      [ $prelen -lt 10 ] && prelen=10
-      pre="%${prelen}<..<${1}%<<"
-      pre="${(%)pre}"
-      ;;
-    *)
-      local p="%${MAXLEN}<..<%~%<<"
-      #TODO find a better way so we don't have to nuke $psvar, but since the
-      #     %(nv.true.false) check for prompts checks element count, not
-      #     content, that's all we get for now
-      psvar=("${(%)p}")
-      return
-  esac
-
-  psvar[1]="$pre"
-  psvar[2]="$branch"
-  psvar[3]="$post"
-}
-
-update_git_vars_if_git_ran() {
-  local vcs="$(__vcs_get_repo_type)"
-  case "$(history $(($HISTCMD - 1)))" in
-    # $vcs appeared in last command, so be sure to update
-    *${vcs}*) __vcs_set_prompt_variables
-  esac
-}
-precmd_functions+=update_git_vars_if_git_ran
-
-update_git_vars() {
-  __vcs_set_prompt_variables
-}
-chpwd_functions+=update_git_vars
-
-# call it once
-update_git_vars
-
-# vim:ft=zsh
diff --git a/.zsh/zshrc/85_vcs_prompt b/.zsh/zshrc/85_vcs_prompt
new file mode 100644 (file)
index 0000000..63ccb65
--- /dev/null
@@ -0,0 +1,113 @@
+# zshrc/85_git_prompt
+#
+# Make git information available to the prompt
+#
+# Copyright © 1994–2008 martin f. krafft <madduck@madduck.net>
+# Released under the terms of the Artistic Licence 2.0
+#
+# Source repository: http://git.madduck.net/v/etc/zsh.git
+#
+# Shamelessly based on http://glandium.org/blog/?p=170
+#
+
+__git_get_repo_root()
+{
+  local relroot
+  relroot="$(git rev-parse --show-cdup 2>/dev/null)" || return 1
+  if [ -n "$relroot" ]; then
+    readlink -f "$relroot"
+  else
+    echo $PWD
+  fi
+}
+
+__git_get_branch()
+{
+  local ref
+  ref=$(git symbolic-ref -q HEAD 2>/dev/null \
+     || git-name-rev --name-only HEAD 2>/dev/null)
+  echo "${ref#refs/heads/}"
+}
+
+__get_root_offsets()
+{
+  local pwda reporoot loc
+  pwda=(${(s:/:)PWD})
+  reporoot=(${(s:/:)1})
+  echo $((1 - $#reporoot)) $(($#pwda - $#reporoot))
+}
+
+__get_prompt_path_components()
+{
+  local reporoot
+  reporoot="$1"
+
+  set -- $(__get_root_offsets "$reporoot")
+  if [ "$2" -le 0 ]; then
+    echo %~
+  else
+    echo "%${1}~" "%${2}~"
+  fi
+}
+
+__vcs_get_repo_type()
+{
+  if __git_get_repo_root >/dev/null; then
+    echo git
+  else
+    echo NONE
+  fi
+}
+
+__vcs_set_prompt_variables()
+{
+  local pre branch post
+  local MAXLEN=25
+
+  case "${1:-$(__vcs_get_repo_type)}" in
+    git)
+      local reporoot="$(__git_get_repo_root)"
+      set -- $(__get_prompt_path_components "$reporoot")
+      branch="$(__git_get_branch)"
+      post="${(%)2}"
+      local prelen="$((MAXLEN - $#post - $#branch))"
+      [ $prelen -lt 10 ] && prelen=10
+      pre="%${prelen}<..<${1}%<<"
+      pre="${(%)pre}"
+      ;;
+    *)
+      local p="%${MAXLEN}<..<%~%<<"
+      #TODO find a better way so we don't have to nuke $psvar, but since the
+      #     %(nv.true.false) check for prompts checks element count, not
+      #     content, that's all we get for now
+      psvar=("${(%)p}")
+      return
+  esac
+
+  psvar[1]="$pre"
+  psvar[2]="$branch"
+  psvar[3]="$post"
+}
+
+if ! is_root; then
+  # too dangerous to be run as root
+
+  _update_vcs_prompt_vars_if_vcs_ran() {
+    local vcs="$(__vcs_get_repo_type)"
+    case "$(history $(($HISTCMD - 1)))" in
+      # $vcs appeared in last command, so be sure to update
+      *${vcs}*) __vcs_set_prompt_variables
+    esac
+  }
+  precmd_functions+=_update_vcs prompt_vars_if_vcs_ran
+
+  _update_vcs_prompt_vars() {
+    __vcs_set_prompt_variables
+  }
+  chpwd_functions+=_update_vcs_prompt_vars
+fi
+
+# call it once
+_update_vcs_prompt_vars
+
+# vim:ft=zsh