Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have two directories: src and projects. I would like to prevent myself from running git ... unless I am specifically inside src or projects. Is this possible?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

It is difficult to prevent the binary from being run but for the typical situations an easy protection method exists:

  1. You define a shell function that overwrites the name. This will obviously not work in another (also another user's) shell.
  2. You take the binary out of the $PATH and replace it by a wrapper script. This is safer but will obviously cause problems with software updates (the script gets overwritten).

The shell function could look like this:

git () {
  local cwd="$(pwd -P)"
  if ! [ "/path/to/src" = "$cwd" -o "/path/to/projects" = "$cwd" ]; then
    echo "The current working directory is: '${cwd}'"
    echo "git must not be run from here; from src and projects only."
    echo "Aborting."
  else
    command git "$@"
  fi
}
share|improve this answer
    
This is an acceptable solution for me - I'm going to wait a bit and see if any other options pop up. If not, I'll accept. Thanks! –  sdasdadas May 30 '13 at 20:26

An unusual one: if using zsh, you could use region highlighting to flag in bold red the git word if used while $PWD is not where you want. Like:

self-insert() {
  zle .$WIDGET
  if [[ $BUFFER = git(| *) && $PWD != */src && $PWD != */projects ]]; then
    region_highlight=("0 3 fg=red,bold,standout")
  else
    region_highlight=()
  fi
}
zle -N self-insert

That way, it doesn't get in your way, but let you know of a potential problem.

To flag git anywhere on the line:

self-insert() {
  zle .$WIDGET
  if [[ $BUFFER =~ '\<git\>' && $PWD != */src && $PWD != */projects ]]; then
    region_highlight=("$((MBEGIN-1)) $MEND fg=red,bold,standout")
  else
    region_highlight=()
  fi
}
zle -N self-insert

Example:

example

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.