Branch policies are rules and restrictions that apply to specific branches and tags in a repository. With policies, you can manage changes, implement code reviews, enforce proper naming and conditions for creating branches and tags, and protect branches from accidental commits or direct pushes.
You specify the policy configuration for a particular repository and store it in the .sourcecraft/branches.yaml file. A configuration stored in the main branch, e.g., master or main, applies to the entire repository. You can also set the configuration at the SourceCraft organization level.
The general policy configuration format in .sourcecraft/branches.yaml is as follows:
Generate a branch policy configuration file named .sourcecraft/branches.yaml, for example:
branch_protection:policies:## Preventing commit history rewrites, edits without ## a pull request, and deletion of the main branch-target:default_branchmessage:"Direct push into main branch is forbidden, create PR first"rules:-prevent_force_push-prevent_non_pr_changes-prevent_deletion## Preventing the creation of branches with names that match filters-target:branchmatches: ["**", "!OO-**/**", "!hotfix/**", "!chore/**", "!release/**"]
message:"Please use proper branch naming"rules:-prevent_creation## Preventing the creation of tags with names that match filters-target:tagmatches:"gitcore-**"message:"Manual tag creation is forbidden, please use Releaser"rules:-prevent_creation
See also the branch policy example in the test-serverless-cube SourceCraft repository.
Tip
For group filtering, we recommend to use the ** pattern because the simple * pattern will not give you a match in expressions with /.
Add the branch policy configuration file to the git index, commit, and push the changes to the remote branch named main: