Questions tagged [hook]

A hook refers to replacing or extending a system or application's default behavior with a custom behavior for a specific event. For keyboard hooks prefer the tag [keyhook]. For git related hooking use the tag [githooks] alone. For hooking web services use the tag [webhooks] instead. For React hooks use tag[react-hooks].

What is it?

A hook refers to replacing or extending the default behavior with a custom behavior for specific events. The events may be operating system events, but also application events that are meant to be extended with hooks.

The hook may fully replace the default behavior. It may also just extend the default behavior for example by adding a listening or monitoring activity without changing the default behavior, or add an extra effect on the top of it.

Hooking and programming techniques

"Hook" is commonly used to describe replacing a function pointer in an existing data structure with a new function pointer that points to new code. This new code can extend the default behavior by calling the old function pointer before or after the new code executes, or the new code can replace / disable the existing behavior by not calling the old pointer.

Hooking is similar in concept to OOP inheritance, but is usually more dynamic (determined at runtime) than OOP inheritance.

Essentially it's a place in code that allows you to tap in to a module to either provide different behavior or to react when something happens. Hooks often (but not always) use callback functions. For example, you might hook an event system using hookEvent(Events.STARTUP, myCallbackFunction). You are passing a function pointer to the hookEvent function, so it knows what function to call when the event occurs.

How does it work?

Typically hooks are inserted while software is already running, but hooking is a tactic that can also be employed prior to the application being started. There are two techniques to achieve this:

  • Physical modification
  • Runtime modification

Related tags and disambiguation

  • For the hooks related to keyboard events, prefer the tag
  • For the hooking git events, you must use the tag preferably without the
  • For hooking web services with web service callbacks prefer the tag
  • For React hooks, prefer the tag

More Information

5710 questions
378
votes
16 answers

What is meant by the term "hook" in programming?

I recently heard the term "hook" while talking to some people about a program I was writing. I'm unsure exactly what this term implies although I inferred from the conversation that a hook is a type of function. I searched for a definition but was…
Chris
  • 4,355
  • 5
  • 20
  • 12
319
votes
4 answers

Applying a git post-commit hook to all current and future repositories

I've written a Git post-commit hook and it works correctly. However, I want to add this hook to apply to all current (and future) Git repositories I am working on. I tried adding the hook to my ~/.git/hooks/ directory instead of in the hooks…
swanson
  • 7,377
  • 4
  • 32
  • 34
306
votes
10 answers

Putting Git hooks into a repository

Is it considered to be a bad practice - to put .git/hooks into the projects repository (using symlinks, for example). If yes, what is the best way to deliver same hooks to different Git users?
shabunc
  • 23,119
  • 19
  • 77
  • 102
293
votes
8 answers

Best way to allow plugins for a PHP application

I am starting a new web application in PHP and this time around I want to create something that people can extend by using a plugin interface. How does one go about writing 'hooks' into their code so that plugins can attach to specific events?
Wally Lawless
  • 7,539
  • 7
  • 37
  • 53
192
votes
4 answers

Is there any git hook for pull?

I need to perform some actions (prepare gettext *.mo message files) on my project everytime I run git pull. Is there any suitable git hook, which I could use for this purpose please?
msgre
  • 2,253
  • 3
  • 14
  • 12
172
votes
8 answers

In Git, how can I write the current commit hash to a file in the same commit

I'm trying to do a fancy stuff here with Git hooks, but I don't really know how to do it (or if it's possible). What I need to do is: in every commit I want to take its hash and then update a file in the commit with this hash. Any ideas?
Felipe Kamakura
  • 2,774
  • 3
  • 19
  • 12
152
votes
7 answers

Git pre-push hooks

I would like to run a unit-tests before every git push and if tests fails, cancel the push, but I can't even find pre-push hook, there is pre-commit and pre-rebase only.
sheepwalker
  • 1,690
  • 2
  • 13
  • 17
136
votes
4 answers

How to configure Git post commit hook

How to trigger a build remotely from Jenkins? How to configure Git post commit hook? My requirement is whenever changes are made in the Git repository for a particular project it will automatically start Jenkins build for that project. In Jenkins…
phanikumar Raja
  • 1,539
  • 3
  • 14
  • 18
118
votes
6 answers

Adding console.log to every function automatically

Is there a way to make any function output a console.log statement when it's called by registering a global hook somewhere (that is, without modifying the actual function itself) or via some other means?
JRL
  • 76,767
  • 18
  • 98
  • 146
117
votes
7 answers

Writing a git post-receive hook to deal with a specific branch

Here's my current hook in a bare repo that lives in the company's server: git push origin master This hooks pushes to Assembla. What i need is to push only one branch (master, ideally) when someone pushes changes to that branch on our server, and…
Jorge Guberte
  • 10,464
  • 8
  • 37
  • 56
94
votes
6 answers

Git pre-commit hook : changed/added files

I am writing a pre-commit hook. I want to run php -l against all files with .php extension. However I am stuck. I need to obtain a list of new/changed files that are staged. deleted files should be excluded. I have tried using git diff and git…
igorw
  • 27,759
  • 5
  • 78
  • 90
92
votes
2 answers

automated django receive hook on server: respond to collectstatic with "yes"

I'm using a Github post-recieve hook to run a bash file that pulls both my repos. #!/bin/sh cd ~/public_html/repo_static env -i /usr/bin/git pull origin master cd ~/django-code/repo_django env -i /usr/bin/git pull origin master I also want to…
Artur Sapek
  • 2,425
  • 6
  • 27
  • 29
86
votes
6 answers

How do I prompt the user from within a commit-msg hook?

I want to warn the user if their commit message doesn't follow a certain set of guidelines, and then give them the option to edit their commit message, ignore the warning, or cancel the commit. The problem is that I don't seem to have access to…
Ben Hocking
  • 7,790
  • 5
  • 37
  • 52
78
votes
5 answers

Linux Kernel: System call hooking example

I'm trying to write some simple test code as a demonstration of hooking the system call table. "sys_call_table" is no longer exported in 2.6, so I'm just grabbing the address from the System.map file, and I can see it is correct (Looking through the…
Stephen
  • 4,176
  • 2
  • 24
  • 29
71
votes
6 answers

how do aim bots in fps games work?

I was curious if anyone had any experience/knowledge about aim bots in online FPS games such as Counter-Strike. I am curious and would like to learn more about how the cursor knows how to lock on to an opposing player. Obviously if I wanted to cheat…
user105033
  • 18,800
  • 19
  • 58
  • 69
1
2 3
99 100