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
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
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-receiveThis 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-receivehook 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
<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-packon 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
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.