OverviewÁttekintés
ih-cgh is a set of shared git hooks for projects that put a ticket key in branch names and commit messages. By default the key is JIRA-style (ABC-123), but the pattern is configurable, so it does not depend on JIRA. The hooks are copied into your repository's .githooks directory and activated with core.hooksPath, so they are committed with your code and every clone runs the same version.
Az ih-cgh közös git hookok gyűjteménye olyan projektekhez, ahol az ágnevekben és a commit üzenetekben ticketkulcs szerepel. A kulcs alapértelmezésben JIRA-stílusú (ABC-123), de a minta beállítható, így nem függ a JIRA-tól. A hookok a projekt .githooks könyvtárába másolódnak, és a core.hooksPath állítja őket működésbe, így a kóddal együtt kerülnek commitba, és minden klón ugyanazt a verziót futtatja.
What the hooks doMit csinálnak a hookokTOCTart.
- commit-msg: puts the ticket key of the branch name in front of the commit subject (with the default settings ABC-123: Fix it, or NOJIRA: ... when there is no ticket).
- commit-msg: az ágnév ticketkulcsát a commit tárgya elé teszi (az alapbeállításokkal ABC-123: Fix it, ticket hiányában pedig NOJIRA: ...).
- pre-commit: checks the branch name (with the default settings ABC-123-some-topic or NOJIRA-some-topic) and asks for confirmation before a commit to a protected branch (master or main by default).
- pre-commit: ellenőrzi az ágnevet (az alapbeállításokkal ABC-123-valami-tema vagy NOJIRA-valami-tema), és megerősítést kér, mielőtt védett ágra (alapértelmezésben master vagy main) commitolnál.
- post-merge: after a git merge into master or main (configurable), a branch whose name ends with -version-major, -version-minor, -version-patch or -version-fix bumps the npm version, tags it and pushes (Node projects).
- post-merge: ha a master vagy main ágba (beállítható) git merge történik, és az egyesített ág neve -version-major, -version-minor, -version-patch vagy -version-fix végű, akkor megnöveli az npm verziót, címkét tesz rá és pusholja (Node projektek).
- Several scripts per hook: every hook runs the scripts of its hooks.d directory in order, so you can add your own next to ours.
- Több szkript hookonként: minden hook sorban lefuttatja a saját hooks.d könyvtárának szkriptjeit, így a saját szkriptjeidet a miénk mellé teheted.
InstallationTelepítés
With the installerA telepítővelTOCTart.
Run the installer from the repository you want to set up. Any clone of this repository will do; there is no need to keep it inside your project.
Futtasd a telepítőt abból a repóból, amelyet be akarsz állítani. A repó bármelyik klónja megfelel, nem kell a projekten belül tartanod.
Install into a projectTelepítés egy projektbe
git clone git@github.com:inebhedj/ih-cgh.git ~/tools/ih-cgh # oncecd /path/to/your/projectsh ~/tools/ih-cgh/install.shgit add .githooks && git commitAfter cloningKlónozás után
After cloning, the committed hooks only need to be enabled (the installer does the same, and also updates them):
Klónozás után csak a commitolt hookokat kell engedélyezni (a telepítő is ezt teszi, és frissíti is őket):
git config core.hooksPath .githooksWith npmnpm-melTOCTart.
In a Node project you can add the package and let npm install set up the hooks.
Node projektben hozzáadhatod a csomagot, és az npm install beállítja a hookokat.
Install as a development dependencyTelepítés fejlesztői függőségként
npm install --save-dev @inebhedj/ih-cgh{ "scripts": { "prepare": "cgh prepare" }}cgh prepare installs quietly. It does nothing on CI servers (when CI is set), outside of git repositories or with CGH_SKIP_INSTALL=1, and it never makes npm install fail. Other commands: cgh install, cgh uninstall, cgh check, cgh version.
A cgh prepare csendben telepít. CI szervereken (ha a CI be van állítva), git repón kívül vagy CGH_SKIP_INSTALL=1 esetén nem csinál semmit, és az npm install-t soha nem buktatja el. További parancsok: cgh install, cgh uninstall, cgh check, cgh version.
Installer optionsA telepítő kapcsolóiTOCTart.
OptionsKapcsolók
Running the installer again updates the hooks. Your cgh.conf and your own scripts are never overwritten.
A telepítő újrafuttatása frissíti a hookokat. A cgh.conf és a saját szkriptjeid sosem íródnak felül.
- --dry-run
- Show what would happen, change nothing.
- --dry-run
- Megmutatja, mi történne, de nem változtat semmit.
- --uninstall
- Remove everything again.
- --uninstall
- Mindent újra eltávolít.
- --dir NAME
- Use another directory than .githooks.
- --dir NAME
- A .githooks helyett másik könyvtárat használ.
- --force
- Take over an existing core.hooksPath.
- --force
- Átveszi a meglévő core.hooksPath beállítást.
ConfigurationBeállítás
cgh.confcgh.confTOCTart.
The file is .githooks/cgh.conf, with one key = value per line. Every value can also be set with git config cgh.<key-with-dashes> or with the environment variable CGH_<KEY>, which take precedence.
A fájl a .githooks/cgh.conf, soronként egy key = value párral. Minden érték megadható git config cgh.<kulcs-kötőjellel> paranccsal vagy CGH_<KULCS> környezeti változóval is, ezek elsőbbséget élveznek.
SettingsBeállítások
| keykulcs | defaultalapértelmezés | meaningjelentés |
|---|---|---|
ticket_regex | [A-Z][A-Z0-9]+-[0-9]+ | An extended regular expression that matches a ticket key. The default accepts keys such as ABC-123 (capital letters or digits, a hyphen and a number). Change it to use another ticket format.Kiterjesztett reguláris kifejezés, amely egy ticketkulcsra illeszkedik. Az alapértelmezés az olyan kulcsokat fogadja el, mint az ABC-123 (nagybetűk vagy számjegyek, kötőjel és egy szám). Más ticketformátumhoz írd át. |
no_ticket | NOJIRA | The token used when there is no ticket.A ticket hiányakor használt jelölő. |
branch_regex | ^(<ticket>|<no_ticket>)-[a-zA-Z0-9-]+$ | An extended regular expression that every new branch name has to match.Kiterjesztett reguláris kifejezés, amelyre minden új ágnévnek illeszkednie kell. |
protected_branches | master main | Space separated globs, for example release/*. The value none protects nothing.Szóközzel elválasztott minták, például release/*. A none érték semmit sem véd. |
version_branches | master main | Space separated globs: a merge into these branches can bump the version.Szóközzel elválasztott minták: az ezekbe történő merge megnövelheti a verziót. |
version_push | yes | Set no to not push the version commit and the tag.A no érték esetén a verzió-commit és a címke nem kerül pusholásra. |
Another ticket formatMásik ticketformátum
The default ticket key is JIRA-style, so a branch such as PROJ_12-fix-it or #123-fix-it is rejected until you change ticket_regex. Two examples of other formats (the branch name has to start with the key):
Az alapértelmezett ticketkulcs JIRA-stílusú, ezért az olyan ág, mint a PROJ_12-fix-it vagy a #123-fix-it, addig el lesz utasítva, amíg át nem írod a ticket_regex-et. Két példa másik formátumra (az ágnévnek a kulccsal kell kezdődnie):
git config cgh.ticket-regex '[A-Z]+_[0-9]+' # PROJ_12-fix-it -> PROJ_12: Fix itgit config cgh.ticket-regex '#[0-9]+' # #123-fix-it -> #123: Fix itHooks in detailA hookok részletesen
The scriptsA szkriptekTOCTart.
Scripts of the hooksA hookok szkriptjei
- commit-msg/00-ticket-prefix
- Takes the ticket from the start of the branch name and prepends it to the commit subject. The body and comments are left alone. Nothing happens if the subject already starts with a ticket, for fixup and squash commits, merges, rebases, cherry-picks and a detached HEAD.
- commit-msg/00-ticket-prefix
- Az ágnév elejéről átveszi a ticketet, és a commit tárgya elé teszi. A törzset és a megjegyzéseket nem bántja. Nem csinál semmit, ha a tárgy már tickettel kezdődik, fixup és squash commitoknál, merge-nél, rebase-nél, cherry-picknél és leválasztott HEAD esetén.
- pre-commit/00-branch-name
- A new branch (one without commits of its own) whose name does not match branch_regex is rejected, with a suggestion how to rename it. An old branch with a bad name only gets a notice.
- pre-commit/00-branch-name
- Az új ágat (amelynek még nincs saját commitja) elutasítja, ha a neve nem illeszkedik a branch_regex-re, és javaslatot tesz az átnevezésre. A rossz nevű régi ágra csak figyelmeztet.
- pre-commit/10-protected-branch
- Asks for confirmation before a commit to a protected branch. Without a terminal (IDE, scripts) the commit is rejected, unless you override it explicitly.
- pre-commit/10-protected-branch
- Megerősítést kér, mielőtt védett ágra commitolnál. Terminál nélkül (IDE, szkriptek) a commit elutasításra kerül, hacsak kifejezetten felül nem bírálod.
- post-merge/00-version-bump
- After a git merge into a version branch it looks at the branches that point at the merged commit and bumps the version if one of them ends with -version-major, -version-minor, -version-patch or -version-fix.
- post-merge/00-version-bump
- Verzióágba történő git merge után megnézi az egyesített commitra mutató ágakat, és megnöveli a verziót, ha valamelyik -version-major, -version-minor, -version-patch vagy -version-fix végű.
Protected branchesVédett ágakTOCTart.
Committing to a protected branchCommit védett ágra
To commit to a protected branch without a terminal, override the check explicitly:
Ha terminál nélkül akarsz védett ágra commitolni, kifejezetten bíráld felül az ellenőrzést:
CGH_ALLOW_PROTECTED=1 git commit ...Version bumpVerziónövelésTOCTart.
Bumping the version with a mergeVerziónövelés merge-dzsel
If several branches match, the highest bump wins. The hook runs npm version (commit NOJIRA: Releasing version X, tag vX) and pushes the commit and the tag together, so either both arrive or neither.
Ha több ág is illeszkedik, a legnagyobb növelés nyer. A hook lefuttatja az npm version parancsot (commit NOJIRA: Releasing version X, címke vX), és a commitot meg a címkét együtt pusholja, így vagy mindkettő megérkezik, vagy egyik sem.
git switch -c ABC-5-new-login-version-minorgit commit -m "..."git switch mastergit merge -It needs a package.json and npm. It does not run after git pull (also not when you pull a merge that was made on GitHub; for that, the release-on-merge.yml template does the same on the server: use the hook or the template, not both) and not for git merge --squash. If the push fails, the commit and the tag stay local and the message tells you the command to retry.
package.json és npm kell hozzá. Nem fut le git pull után (akkor sem, ha a GitHubon készült merge-öt húzod le; ehhez a release-on-merge.yml sablon ugyanezt végzi el a szerveren: vagy a hookot, vagy a sablont használd, a kettőt együtt ne) és git merge --squash esetén sem. Ha a push sikertelen, a commit és a címke helyben marad, az üzenet pedig megadja az újrapróbálás parancsát.
Using the hooksA hookok használata
Skipping and debuggingKihagyás és hibakeresésTOCTart.
Skipping and debuggingKihagyás és hibakeresés
- git commit --no-verify
- Skips pre-commit and commit-msg as usual. Client-side hooks can always be bypassed, so enforce important rules in CI as well.
- git commit --no-verify
- A szokásos módon kihagyja a pre-commit és a commit-msg hookot. A kliensoldali hookok mindig megkerülhetők, ezért a fontos szabályokat CI-ben is kényszerítsd ki.
- CGH_SKIP=1 git ...
- Skips all scripts of ih-cgh for one command.
- CGH_SKIP=1 git ...
- Egyetlen parancs erejéig kihagyja az ih-cgh összes szkriptjét.
- CGH_DEBUG=1 git ...
- Prints every script that is run.
- CGH_DEBUG=1 git ...
- Kiír minden lefuttatott szkriptet.
Your own hooksSaját hookokTOCTart.
Adding your own scriptsSaját szkriptek hozzáadása
Put an executable script into .githooks/hooks.d/<hook-name>/ and run the installer again, which creates the missing stub for that hook. For example:
Tegyél egy futtatható szkriptet a .githooks/hooks.d/<hook-neve>/ könyvtárba, és futtasd újra a telepítőt, amely létrehozza a hiányzó csonkot ahhoz a hookhoz. Például:
.githooks/hooks.d/pre-push/50-testsScripts run in lexical order and the first one that fails stops the hook. Files ending in .sample, .backup or .disabled are ignored. A script gets the hook's arguments and its standard input. A hook that already existed in .git/hooks when you installed keeps running after the custom scripts.
A szkriptek lexikális sorrendben futnak, és az első, amelyik hibázik, leállítja a hookot. A .sample, .backup vagy .disabled végű fájlokat figyelmen kívül hagyja. A szkript megkapja a hook argumentumait és a szabványos bemenetét. Az a hook, amely már létezett a .git/hooks könyvtárban a telepítéskor, a saját szkriptek után is tovább fut.
Checking on the serverEllenőrzés a szerverenTOCTart.
Server-side checkSzerveroldali ellenőrzés
Client-side hooks can be skipped, so the installer also puts .githooks/cgh-check into your repository (cgh check from npm). It checks the branch name and the subject of every commit in BASE..HEAD with the same configuration as the hooks:
A kliensoldali hookok kihagyhatók, ezért a telepítő a .githooks/cgh-check fájlt is a repóba teszi (npm-ből: cgh check). Ugyanazzal a beállítással, mint a hookok, ellenőrzi az ágnevet és a BASE..HEAD közötti minden commit tárgyát:
sh .githooks/cgh-check --base origin/main --branch "$BRANCH"Exit status 0 means fine, 1 means something is wrong. A ready-to-copy GitHub workflow is in templates/check-pull-request.yml. The files in templates/ are not active: they are only templates to copy into your project.
A 0-s kilépési kód azt jelenti, hogy minden rendben, az 1-es azt, hogy valami hibás. Egy másolásra kész GitHub workflow a templates/check-pull-request.yml fájlban van. A templates/ fájljai nem aktívak: csak sablonok, amelyeket a saját projektedbe kell másolni.
NotesJegyzetekTOCTart.
Good to knowJó tudni
The hooks are POSIX shell scripts. The tests need only git and a POSIX shell, and each runs in a fresh temporary repository.
A hookok POSIX shell szkriptek. A tesztekhez csak git és POSIX shell kell, és mindegyik friss, ideiglenes repóban fut.
Run all tests:
Az összes teszt futtatása:
sh tests/run.shContributing and the layout of the repository: CONTRIBUTING.md.
Közreműködés és a repó felépítése: CONTRIBUTING.md.
License: ISC.
Licenc: ISC.