Skip to content

Commit

Permalink
Add project specific gitconfig and more
Browse files Browse the repository at this point in the history
This introduces primarily project specific config that includes new
scripts and hook.

Hook checks if commit we push to Gitlab server are correct. There are
multiple checks:
* Check naming convention
* Check base (master vs develop)
* Protect against forgotten develop push when pushing master

In terms of aliases this adds:
git new-mr:
  This opens web browser with new merge request on https://gitlab.nic.cz
  for current branch.
git new-banch:
  This creates new branch interactively and should help users with
  naming convention.
  • Loading branch information
Cynerd committed Oct 29, 2020
1 parent 313ad54 commit 848af2d
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# To use this config run: git config --local include.path ../.gitconfig
# You also need bash to interpret hooks and scripts

[core]
hooksPath = .githooks

[alias]
new-mr = "!.gitscripts/new-mr"
new-branch = "!.gitscripts/new-branch"

[sendemail]
to = [email protected]
76 changes: 76 additions & 0 deletions .githooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash
server="gitlab.nic.cz"
server_old="gitlab.labs.nic.cz"

zero_sha1="0000000000000000000000000000000000000000"


is_new_branch() {
local remote_sha1="$1"
[ "$remote_sha1" = "$zero_sha1" ]
}

compare_ancestors() {
local local_hash="$1"
[ "$local_hash" != "$zero_sha1" ] || return 0 # ignore removals
git merge-base --is-ancestor \
"$(git merge-base "$local_hash" "$2")" \
"$(git merge-base "$local_hash" "$3")"
}


remote_name="$1"
remote_url="$2"


if [[ "$remote_url" != *$server* && "$remote_url" != *$server_old* ]]; then
# We are interested only in pushes to our server
exit 0
fi

push_master=
push_develop=

while read -r local_ref local_sha1 remote_ref remote_sha1; do
remote_ref="${remote_ref#refs/heads/}"
case "$remote_ref" in
master)
push_master="$local_sha1"
;;
develop)
push_develop="$local_sha1"
;;
hotfix/*)
if compare_ancestors "$local_sha1" master develop; then
echo "Reference has invalid ancestor, please base it on top of master: $local_ref" >&2
exit 1
fi
;;
feature/*|bugfix/*|refactor/*|hack/*)
if compare_ancestors "$local_sha1" develop master; then
echo "Reference has invalid ancestor, please base it on top of develop: $local_ref" >&2
exit 1
fi
;;
*)
# We terminate push only if this tries to create new branch of invalid
# name. This allows push to existing branches.
if is_new_branch "$remote_sha1"; then
echo "Creation of new branch of this name is not allowed: $remote_ref" >&2
exit 1
fi
;;
esac
done

if [ -n "$push_master" ]; then
if [ -z "$push_develop" ]; then
echo "Develop branch has to be always updated with master branch." >&2
echo "Push both at the same time with: git push origin master develop" >&2
exit 1
fi
if ! git merge-base --is-ancestor "$push_master" "$push_develop"; then
echo "Tip commit of master is not merged to develop branch." >&2
exit 1
fi
fi
76 changes: 76 additions & 0 deletions .gitscripts/new-branch
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash

branch_name="$1"
branch_base="$2"
branch_desig="$3"

valid_base() {
[[ "$1" == "master" || "$1" == "develop" ]]
}

valid_desig() {
local base="$1"
local desig="$2"
case "$base" in
master)
[ "$desig" = "hotfix" ]
return
;;
develop)
[[ "$desig" =~ ^(bugfix|feature|refactor|hack)$ ]]
return
;;
*)
return 1
;;
esac
}

while [ -z "$branch_name" ]; do
read -r -p "Branch name: " branch_name
done

while ! valid_base "$branch_base"; do
read -r -p "Branch base ([d]evelop/[m]aster): " branch_base
case "$branch_base" in
d|dev)
branch_base="develop"
;;
m)
branch_base="master"
;;
esac
done

while ! valid_desig "$branch_base" "$branch_desig"; do
case "$branch_base" in
master)
branch_desig="hotfix"
continue
;;
develop)
read -r -p "Branch designation (bugfix/feature/refactor/hack): " \
branch_desig
case "$branch_desig" in
b|bug)
branch_desig="bugfix"
;;
f)
branch_desig="feature"
;;
r|ref)
branch_desig="refactor"
;;
h)
branch_desig="hack"
;;
esac
;;
esac
done

branch="$branch_desig/$branch_name"
echo "Creating branch: $branch"

git branch "$branch" "$branch_base"
git switch "$branch"
36 changes: 36 additions & 0 deletions .gitscripts/new-mr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
repo="https://gitlab.nic.cz/turris/turris-os-packages"
cur_branch="$(git branch --show-current)"


open_merge_request() {
local target="$1"
local url="$repo/-/merge_requests/new?merge_request%5Bsource_branch%5D=$cur_branch&merge_request%5Btarget_branch%5D=$target"
if command -v xdg-open >/dev/null; then
xdg-open "$url"
else
echo "Open following URL to create merge request for branch: $cur_branch"
echo "$url"
fi
}


case "$cur_branch" in
master)
echo "Master branch is the most stable branch. There is no merge target for it." &2
exit 1
;;
develop)
echo "Develop branch is merged only on new Turris OS release." >&2
exit 1
;;
hotfix/*)
open_merge_request master
;;
feature/*|bugfix/*|refacotr/*|hack/*)
open_merge_request develop
;;
*)
echo "Merge requests are not supported for this branch." >&2
;;
esac
8 changes: 8 additions & 0 deletions WORKFLOW.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ Tips for developers and maintainers
This is collection of various tips and primarily configuration options you can use
to simplify commands described in this flow.
Use project specific git configuration::
It is highly advised to use project specific git config. You can apply it by
running following command:
[,sh]
----------------------------------------------------------------------------------
git config --local include.path ../.gitconfig
----------------------------------------------------------------------------------
Sign commits and tags with GPG without using `--gpg-sign` and `-s`::
You can configure global or local git option `commit.gpgSign` and `tag.gpgSign`.
[,sh]
Expand Down

0 comments on commit 848af2d

Please sign in to comment.