unrealsoul007 unrealsoul007 - 16 days ago 5
Git Question

Configure git to throw error on using certain characters in branch name

I recently figured that my organization's build process fails to succeed if your branch contains / in the branch name because the build process generates a file with filename

<blah>_<branch_name>.type
in folder say RootFolder.

eg. Branch name: dev/someDev/someBranch

FileName: `<blah>_dev/someDev/someBranch.type`


Since branch name contains /, the file actually gets created at
RootFolder\<blah>_dev\someDev\
with the file name being
someBranch.type
.
Now the process expecting the file in RootFolder doesn't find the file which causes the build process to fail.

So, is there a way I can configure git to throw an error in case the branch name contains certain illegal characters?

NOTE: At this is point I cannot change the build process. Also, I know there are several other ways to name a branch say:
dev-someDev-someBranch
by which we can avoid hitting this build failure but I am curious to see if I can configure git in the above manner.

Answer

Well, you did not tell us where you'd like the branch names to be checked against your policy—on the developers' machines or in the repository they push to or in the repository your build server fetches into (if eny) or somewhere else completely.

I think the simplest to configure point for such check is the repository the devs push their work to.

To implement such policy enforcement you'll need to write and enable a so-called hook in that repository; specifically, a pre-receive hook.

Hooks are scripts (or any other kind of executable programs) which Git calls when carrying out specific actions on a repository. See git help hooks to read an overview. Each hook must follow a certain convention to work with Git. Typically hooks read data supplied by Git from their standard input stream, write nothing to their standard output stream and signalize success (or "OK to continue") by exiting with the status code 0 and failure (or "not OK continue") by exiting with a non-zero exit code; in the latter case they may write an error message to their standard error stream.

The hook of interest, pre-receive rolls like this:

pre-receive This hook is invoked by git-receive-pack on the remote repository, which happens when a git push is done on a local repository. Just before starting to update refs on the remote repository, the pre-receive hook is invoked. Its exit status determines the success or failure of the update.

This hook executes once for the receive operation. It takes no arguments, but for each ref to be updated it receives on standard input a line of the format:

<old-value> SP <new-value> SP <ref-name> LF

where <old-value> is the old object name stored in the ref, <new-value> is the new object name to be stored in the ref and <ref-name> is the full name of the ref. When creating a new ref, <old-value> is 40 0.

If the hook exits with non-zero status, none of the refs will be updated. If the hook exits with zero, updating of individual refs can still be prevented by the update hook.

Both standard output and standard error output are forwarded to git send-pack on the other end, so you can simply echo messages for the user.

So, you'd need to write a hook program which reads the data from its standard input stream, interprets it as a set of LF-separated lines, break each into three fields separated by two SP (space) characters and check the third field—which will be a branch name—has no invalid characters as per your policy.

If your hook detects poilcy violation it should write a message to its standard error stream and exit with a non-zero exit code. This will make Git not update the repository with the data a developer attempted to push into it.