Skip to content

Commit

Permalink
feat: improve git-verify (#46)
Browse files Browse the repository at this point in the history
Signed-off-by: Tronje Krop <[email protected]>
  • Loading branch information
tkrop committed Feb 1, 2024
1 parent 55f4067 commit 30a1bd0
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 21 deletions.
14 changes: 11 additions & 3 deletions MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ make git-create(-*) # creates and pushes a branch with the current change set
make git-commit(-*) # commits the current change set to the current branch
make git-fix [...] # pushes the latest changes to the previous commit
make git-push # pushes the current branch to the upstream repository
make git-verify # checks git log to follow commit conventions
```

The `git-create(-*)` targets support `<branch>` and a `<message...>` argument
Expand All @@ -590,11 +591,18 @@ The `git-reset` and `git-clean` targets support an optional `all` argument
to define whether also pushed branches should be cleaned up instead of only
merged branches.

The `git-fix` supports `(no-)edit` and `(no)-verify` arguments to selectively
enable or disable commit verification and comment editing. The default is to
using verification but disable editing. The default behavior can be defined
The `git-fix` target supports `(no-)edit` and `(no)-verify` arguments to enable
and disable commit verification and comment editing. The default is to using
verification enabled but disable editing. The default behavior can be defined
by setting providing the `GITFIX` environment variable.

The `git-verify` target verifies that commit messages in git log entries are
following a common commit convention containing a [commit types](#commit-types)
as well as a github issue references in the title and are signed-off. The
target allows to validate full git `logs` as well as a single `message` file.
As default the target verifies all commit added to the current branch in
relation to the main branch.


## Commit types

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export GO ?= go
export GOPATH ?= $(shell $(GO) env GOPATH)
export GOBIN ?= $(GOPATH)/bin
# Setup go-make to utilize desired build and config scripts.
GOMAKE_DEP ?= github.com/tkrop/[email protected].48
GOMAKE_DEP ?= github.com/tkrop/[email protected].49
# Request targets from go-make targets target.
TARGETS := $(shell command -v $(GOBIN)/go-make >/dev/null || \
$(GO) install $(GOMAKE_DEP) >/dev/stderr && \
Expand Down
3 changes: 3 additions & 0 deletions Makefile.vars
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ RUN_DEPS := run-db
LINTERS_CUSTOM := nonamedreturns tagliatelle
# Linters swithed off to complete next level (default: <empty>).
LINTERS_DISABLED :=

# Customize the build pipeline (default: init lint test build run clean).
TARGETS_ALL := init git-verify lint test build run clean
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.48
0.0.49
2 changes: 1 addition & 1 deletion config/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export GO ?= go
export GOPATH ?= $(shell $(GO) env GOPATH)
export GOBIN ?= $(GOPATH)/bin
# Setup go-make to utilize desired build and config scripts.
GOMAKE_DEP ?= github.com/tkrop/[email protected].48
GOMAKE_DEP ?= github.com/tkrop/[email protected].49
# Request targets from go-make targets target.
TARGETS := $(shell command -v $(GOBIN)/go-make >/dev/null || \
$(GO) install $(GOMAKE_DEP) >/dev/stderr && \
Expand Down
35 changes: 20 additions & 15 deletions config/Makefile.base
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ go-pkg = $(shell awk -v mode="$(2)" -v filter="$(3)" \
}' <<<"$(1)")

# Setup go-make to use desired build and config scripts.
GOMAKE_DEP := github.com/tkrop/[email protected].48
GOMAKE_DEP := github.com/tkrop/[email protected].49
GOMAKE_MAKEFILE := $(realpath $(firstword $(MAKEFILE_LIST)))
GOMAKE_CONFIG := $(patsubst %/,%,$(dir $(GOMAKE_MAKEFILE)))

Expand Down Expand Up @@ -211,7 +211,7 @@ DELIVERY_REGEX ?= (cdp-runtime\/go-|go-version: \^?)[0-9.]*

# Setup default git command and repository information.
GIT ?= git
GITHOOKS ?= pre-commit commit-msg
GITHOOKS ?= pre-commit # commit-msg
GITAUTHOR ?= $$(echo -n "$$(git config --get user.name) <$$(git config --get user.email)>")
GITREPO ?= $(shell $(GIT) remote get-url origin 2>/dev/null | \
sed "s/^https:\/\///; s/^git@//; s/.git$$//; s/:/\//")
Expand Down Expand Up @@ -523,23 +523,23 @@ git-verify = awk -v mode="$(1)" -v author="$(GITAUTHOR)" ' \
function verify() { \
title = (i = index(msg,"\n")) ? substr(msg, 0, i - 1) : msg; \
if (title !~ re_type_missing) { \
printf("$(call merror,commit type missing [title=%s])\n", title); error++ \
printf("$(call merror,commit type missing [title=%s])\n", title); errors++ \
} else if (title !~ re_type_invalid) { \
printf("$(call merror,commit type invalid [title=%s])\n", title); error++ \
printf("$(call merror,commit type invalid [title=%s])\n", title); errors++ \
} \
if (title !~ re_issue_missing) { \
printf("$(call merror,issue missing [titel=%s])\n", title); error++ \
printf("$(call merror,issue missing [titel=%s])\n", title); errors++ \
} else if (title !~ re_issue_invalid) { \
printf("$(call merror,issue invalid [titel=%s])\n", title); error++ \
printf("$(call merror,issue invalid [titel=%s])\n", title); errors++ \
} \
sign = (i = index(msg, signed_by)) ? substr(msg, i) : ""; \
if (sign !~ re_signed_missing) { \
printf("$(call merror,signed-off-by missing [msg=%s])\n", msg); error++ \
printf("$(call merror,signed-off-by missing [msg=%s])\n", msg); errors++ \
} else if (sign !~ "^" re_signed_missing) { \
printf("$(call merror,signed-off-by by to many [sign=%s])\n", sign); error++ \
printf("$(call merror,signed-off-by by to many [sign=%s])\n", sign); errors++ \
} else if (mode == "message" && sign !~ re_signed_author) { \
printf("$(call merror,signed-off-by not the author \
[sign=%s; author=%s])\n", sign, author); error++ \
[sign=%s; author=%s])\n", sign, author); errors++ \
} msg = "" \
} \
(mode == "message") { \
Expand All @@ -553,7 +553,13 @@ git-verify = awk -v mode="$(1)" -v author="$(GITAUTHOR)" ' \
} else { msg = substr($$0, 5) } next \
} \
(mode != "message") { if (msg) { verify(); } } \
END { verify(); if (error) { exit 1 } }'
END { \
verify(); if (errors) { \
printf("$(call mfailure,git-verify $(RUNARGS) [errors=%d])\n", \
errors); exit 1 \
} \
printf("$(call msuccess,git-verify $(RUNARGS) [errors=%d])\n", errors) \
}'

#@ prints the git log as pretty graph.
git-graph::
Expand All @@ -573,15 +579,14 @@ git-reset::
$(GIT) pull && $(GIT) stash apply \
) \
fi || exit 1; $(call git-clean,$${MAIN});
#@ check whether git log is compliant with conventional commit messages and signed.
#@ <mode> [msg|log-file] # checks whether git log follows the commit conventions.
git-verify::
@MODE="$(firstword $(RUNARGS))"; case "$${MODE}" in \
( "log" ) cat "$(wordlist 2,$(words $(RUNARGS)),$(RUNARGS))";; \
( "message" ) cat "$(wordlist 2,$(words $(RUNARGS)),$(RUNARGS))";; \
( "pull" ) BRANCH="$$($(GITBRANCH))"; TARGET="$(git-main)"; \
$(GIT) log --no-merges --format=raw $${BRANCH} ^$${TARGET};; \
( "branch" ) BRANCH="$$($(GITBRANCH))"; \
$(GIT) log --no-merges --format=raw $${BRANCH};; \
( "branch" ) $(GIT) log --no-merges --format=raw "$$($(GITBRANCH))";; \
( "pull" | * ) MAIN="$(git-main)" $(GIT) fetch origin --all --verbose && \
$(GIT) log --no-merges --format=raw "$$($(GITBRANCH))" "^origin/$${MAIN}";; \
esac | $(call git-verify,$${MODE}) >/dev/stderr

#@ <branch> <message> # creates a branch with the current change set using next issue.
Expand Down
1 change: 1 addition & 0 deletions internal/make/fixtures/git-verify/log-all.err
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
error: signed-off-by missing [msg=feat[service]!: initial commit (#11)]
error: signed-off-by by to many [sign=Signed-off-by: John Doe <[email protected]>
Signed-off-by: John Doe <[email protected]>]
failure: git-verify log ../internal/make/fixtures/git-verify/log-all.in [errors=13]
make: *** [go-make/config/Makefile.base: git-verify] Error 1
error: execute make: call failed [path=go-make/run, call=[make --file go-make/config/Makefile.base --no-print-directory git-verify log ../internal/make/fixtures/git-verify/log-all.in]]: exit status 2
1 change: 1 addition & 0 deletions internal/make/fixtures/git-verify/msg-failed.err
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
error: commit type missing [title=feat(wrong): all is somehow wrong (org#1)]
error: issue missing [titel=feat(wrong): all is somehow wrong (org#1)]
error: signed-off-by not the author [sign=Signed-off-by: Alice Doe <[email protected]>; author=John Doe <[email protected]>]
failure: git-verify message ../internal/make/fixtures/git-verify/msg-failed.in [errors=3]
make: *** [go-make/config/Makefile.base: git-verify] Error 1
error: execute make: call failed [path=go-make/run, call=[make --file go-make/config/Makefile.base --no-print-directory git-verify message ../internal/make/fixtures/git-verify/msg-failed.in]]: exit status 2
1 change: 1 addition & 0 deletions internal/make/fixtures/git-verify/msg-okay.err
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
info: captured arguments [message ../internal/make/fixtures/git-verify/msg-okay.in]
success: git-verify message ../internal/make/fixtures/git-verify/msg-okay.in [errors=0]

0 comments on commit 30a1bd0

Please sign in to comment.