Use Git hook implementation code released

Use Git hook implementation code released

1. What is the git hook

And other version control systems, Gitcan trigger custom scripts when certain important actions occur. There are two such hooks: client and server side. The client calls the hooks such as a combined operation and submitted, and on receiving such submitted Such networking server push operation of the hook effect.

2, a mounting hook

Hooks are stored in the Gitdirectory hookssubdirectory. That most of the project .git/hooks. When you use git initwhen initializing a new repository, Gitthe default will put some sample scripts in this directory. These scripts can be invoked in addition to their outside, they also revealed that when triggered the argument passed. All examples are shellscripts, some of which mixed the Perlcode, but any properly named executable scripts can be used normally - you can use Rubyor Python, or write them in other languages. Examples of these names are to .sampleend, if you want to enable them, you must first remove the suffix. The right to a name and an executable file into the Gitdirectory hookssubdirectories, you can activate the hook scripts. As a result, it can be Gitcalled.

3, commonly used types of hook script

3.1 client hook

The client hook is divided into many. They are divided into the following: submission workflow hooks, e-mail workflow hooks and other hooks.

3.1.1 pre-commit

Run before typing submit information. It is used to check the snapshot to be submitted, for example, to check whether the omission, to ensure that the test run, and verification code. If the hook exits with a non-zero value, Gitit will give up this submission, but you can use git commit --no-verifyto bypass this link. You can use this hook to check if the code is the same style (similar to running linta program), if there is trailing whitespace characters (built-in hook is to do so), or document the new method is appropriate.

3.1.2 prepare-commit-msg

Before starting the commit message editor, after the default message is created. It allows you to edit the default information submitted by those who see. This hook takes a few options: submit information of some files in the current path, type, and submit patches submitted submitted by SHA-1check. It is generally submitted and no use; however, for those who will automatically generate the information submitted by default, such as submitting information templates, merge commit, compression and submit the revised submission and other very practical. You can submit a template to use it in conjunction with dynamically insert information.

3.1.3 commit-msg

Receiving a parameter, i.e., the above-mentioned, there is a current path of the temporary file information submitted. If the hook script exits with a non-zero value, Gitit will abandon submit, therefore, can be used to verify the status of the project prior to the submission or by submitting information.

3.1.4 post-commit

After running the entire commit process is completed. It does not receive any parameters, but you can easily run through git log -1 HEADto get the information submitted last time. The hook is generally used for things like notice.

3.1.5 E-mail workflow hook

You can set up three client-side hooks to e-mail workflow. They are the git amcommand is called, so if you do not use this command in your workflow, you can skip to the next section. If you need to receive e-mail via a git format-patchpatch produced, these hooks might need them.
The first hook is running applypatch-msg. It accepts a single parameter: the name of the temporary file that contains the request merge information. If the script returns a non-zero value, Gitit will give up the patch. You can use the script to ensure that the information submitted conform to the format, or directly with the script error correction format.
In the next git amto be called during the run Shi pre-applypatch. Somewhat difficult to understand it is that it just runs after applying the patch, produce before submitting, so you can use it to check before submitting a snapshot. You can use this script to run the test or inspection area. If there is anything missing, or failed to pass the test, the script will exit with a non-zero value, interruption git amof operation, so that the patch will not be submitted.
post-applypatchAfter submitting produce runs, is in git amthe hook during the run was finally called. On it you can use the results to inform a group or by pulling the patches. But you can not use it to stop the patching process.

3.1.6 Other clients hook

  • pre-rebaseRebase hook runs before, a non-zero value variable exit procedure can be aborted group. You can use this hook to prohibit the submission has been pushed rebase. GitBuilt-in pre-rebasehook example is to do so, but it made some of the assumptions may not match your workflow.

  • post-rewriteHook is called command replaces submit those records, for example, git commit --amendand git rebase(but not including git filter-branch). Its only parameter is the name of the command to rewrite the trigger, while receiving a series of rewriting submit records from standard input. Use this hook to a large extent with post-checkoutand post-mergeabout the same.

  • In git checkoutthe successful operation, post-checkoutthe hook will be called. You can use it to adjust your working directory according to your project environment. Including large binary file, automatically generating a document or other such similar operations.

  • In git mergethe successful operation, post-mergethe hook will be called. You can use it to restore Gitthe work area can not track data, such as permissions data. The hook can also be used to verify certain in Gitwhether a file exists outside the control of, so you can change the work area at the time, came to copy these files.

  • pre-pushIt will hook git pushduring operation, update the remote references but is called when the object has not been transferred. It accepts remote branch name and location as a parameter, while a series of references to be updated is read from the standard input. Before you can begin to push, use it to verify the operation of the update references (a non-zero exit code terminates the push process).

  • GitSome of the daily operations at run time, occasionally calling git gc --autofor garbage collection. pre-auto-gcHook will be called before the start garbage collection, you can use it to remind you now want to reclaim garbage, or judgment depends on how you want to interrupt the recovery.

3.2 server-side hook

In addition to the client hooks as a system administrator, you can also use a number of server-side hooks to enforce any kind of policy for the project. After the run and pushed to the server before the hook scripts. Hook ago pushed to the server is running a non-zero value can quit at any time, and refused to push to the client returns an error message, you can also set up sophisticated enough push policy according to what you want.

3.2.1 pre-receive

When processing a push operation from the client, the script first to be called is pre-receive. It gets to be pushed a series of references from the standard input. If it exits with a non-zero value, all push content will not be accepted. You can use this hook to prevent a reference to non-fast-forward (non-fast-forward) the update, or access control on all the references and file push modified.

3.2.2 update

updateThe script and pre-receivethe script is very similar, except that it will be ready for each branch of each update run once. If the pusher while pushing content to a plurality of branches, pre-receiverun only once, whereas updateit will be pushed to each branch of each run once. It does not read from standard input, but takes three parameters: a reference name (branch), citations pointed to before the push SHA-1value, and the user is ready to push content SHA-1value. If updatethe script exits with a nonzero value, the only appropriate that a reference is rejected; other references can still be updated.

3.2.3 post-receive

post-receiveHook is run after the end of the whole process can be used to update other services or notify users. It accepts the pre-receiveinput data of the same standard. Its uses include server to a mailing list letter informing continuous integration (continous integration), or update issue tracking system (ticket-tracking system) - may even decide to submit information by analyzing an issue (ticket) if it should be opened, modified, or closed. The script does not terminate push process, but the client before the end of the run it will remain connected, so if you want to do other operations need to be cautious to use it, because it will cost you for a long time.

4, using the hook to achieve code release

The hook type described above, the server may utilize post-receivehook code release is achieved, mainly comprising the following steps:

4.1 Creating a repository on the server

[root@server ~]# mkdir /usr/local/git-workspace
[root@server local]# cd /usr/local/git-workspace
[root@server local]# git init -bare wwwroot.git
[root@server local]# ls wwwroot.git/
branches  config  description  HEAD  hooks  index  info  objects  refs

It should be noted here:
initialize the repository using git init -barenot just git init, bareChinese meaning is: bare, bare. The reason is because this is called bare warehouse warehouse only saved version of the information submitted by the git history, git does not allow user to perform various operations on it. It means that the initialization of the repository (for the time being called bare repository) will generate a class file: used to record the history of the repository .gitdirectory of documents; and will not contain a copy of the actual project source files; so the repository can not be called work directory working tree; if you enter the version of the directory, you will find only the .gitfile directory, and no other files; that is, inside the repository files are .gitdirectory files in the original .gitdirectory of the file in the repository root directory; in other words, do not use the --bareoption, it will generate a .gitdirectory and its files under version history, the version history file is stored in the .gitdirectory; use --bareoption, no longer generates .gitcatalog, but only generate a .gitdirectory of version history file, these version history file is no longer stored in .gitthe following directory, but placed directly under the root directory of the repository. With git initthe repository user can also perform all the initialization in the directory gitoperational aspects. However, other users will be updated pushwhen they came up conflict prone.

4.2 Creating web directory

Sites using nginx and php provide services, web directories are as follows

[root@server]$ mkdir -p /home/website/webroot

4.3 local repository initialization and cloning

[root@localhost ~]$ cd /dev-workspace
[root@localhost dev-workspace]$ git init local.git
[root@localhost dev-workspace]$ cd local.git
[root@localhost local.git]$ git clone ssh://[email protected]/usr/local/git-workspace/wwwroot.git

Pull code via ssh protocol, if the key is not done directly enter the server password, if not using the default port 22, the command like this:

git clone ssh://[email protected]:端口号/usr/local/git-workspace/wwwroot.git

4.4 Setting the hook

[root@server ~]# cd /usr/local/git-workspace/wwwroot/hooks/
[root@server hooks]# vim post-receive
#!/bin/bash
git --work-tree=/home/website/webroot checkout -f

--work-treeThe corresponding file directory sites

4.5 Automatic detection code to forward to the web directory, the repository receiving a push

In the local repository workspace to develop and use the git pushcommand push to a remote repository bare hook will post-receiveautomatically take effect, the detected files to a --work-treedirectory, that is, the code will automatically synchronize upon submission to the webdirectory.
At this point, using the git hook to achieve complete code release.

Guess you like

Origin www.cnblogs.com/ssgeek/p/11487713.html