Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Every time the user types git commit -a,
I want to run it in my own script,
Then run original command git commit.. and have it run as it would have normally.

share|improve this question

migrated from stackoverflow.com Jan 28 '13 at 16:51

This question came from our site for professional and enthusiast programmers.

    
If this were possible, it would be a major security risk. –  cbuckley Jan 28 '13 at 10:52
add comment

4 Answers 4

up vote 5 down vote accepted

You could use ".git/hooks" to install a "pre-commit" hook, perhaps: http://git-scm.com/book/en/Customizing-Git-Git-Hooks

This is normally how you would do this. Of course, it would help if you explained exactly WHAT you want the "overload" to do.

share|improve this answer
add comment

If you want your "hook" is not limited to only git but you need to make it work with any executable, you can use alias

alias git='myhackyscript.sh'

then myhackyscript.sh may looks like

#!/bin/bash

# save params
params=("$@")

# do what i want to do
do_something

# launch git 
git "${params[@]}"

myhackyscript.sh must be stored in a $PATH folder like /usr/bin/

share|improve this answer
    
how does it re-attach to the "real" git command? –  Binny Zupnick Jan 28 '13 at 10:57
    
Look at the last line. It will call git with his parameters taken from the shell command –  Davide Berra Jan 28 '13 at 11:04
    
it will call the original git, or it will call the new alias? –  Binny Zupnick Jan 28 '13 at 11:10
    
It will call the original git. You won't fall in a recursion. –  Davide Berra Jan 28 '13 at 11:14
    
Aliases don't expand into a non-interactive shell. Try yourself –  Davide Berra Jan 28 '13 at 12:20
show 3 more comments

You can define a shell function that invokes a custom script which then passes things on to the original git command:

git() {
  case $1 in
    commit)
      echo "WUFF"
      ;;
  esac
  \git "$@"
}

Putting this function into one of your shell's RC files (e.g. ~/.profile or /etc/profile in the case of sh/bash/ksh shells) will make this available after the next login. Of course, you can get much more elaboare than this. Also, you might want to take a look at the git-sh-setup(1) and git-rev-parse(1) man-pages, in particular the section about parseopt.

share|improve this answer
    
You can source ~/.profile to update the current shell with the new function., –  iiSeymour Jan 28 '13 at 11:46
    
Sure, and on Mac it's actually enough to just open a new terminal/tab. But the logout/login method is the bullet proof way ;-) –  Michael Wild Jan 28 '13 at 11:48
    
That's because each new terminal/tab invokes a new shell so all relevant config files such as ~/.bashrc will be read. source ~/.bashrc will be bulletproof for updating the current shell with the changes. –  iiSeymour Jan 28 '13 at 11:53
add comment

If you create a script ls in a location such as ~/bin which is located on the path before /bin/ls your script will be executed instead.

user@wopr /home/user/bin/: $ cat ls
#!/bin/bash

echo "Security Breached!"

# Run original command
/bin/ls "$@"

Now when listing files the message Security Breached! is always displayed.

user@wopr /home/user/bin/: $ ls
Security Breached!
ls file.sh file myscript.sh

user@wopr /home/user/bin/: $ which ls
/home/user/bin/ls

You should be aware of the directories on your path and who can write to those directories.

share|improve this answer
add comment

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.