From 9d1656b5c1abc9db4079f67cc75325fb28b063fc Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Mon, 28 Jun 2021 18:24:33 +0300 Subject: [PATCH] Pull request: all: imp hacking, hooks Merge in DNS/adguard-home from imp-hacking to master Squashed commit of the following: commit fcc0740526948256b5b356a3a7969e00baa35ff7 Author: Ainar Garipov Date: Mon Jun 28 18:16:57 2021 +0300 all: imp hacking, hooks --- HACKING.md | 23 ++++++++++++++++++----- scripts/hooks/pre-commit | 4 ++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/HACKING.md b/HACKING.md index 101a7757..bde2ab90 100644 --- a/HACKING.md +++ b/HACKING.md @@ -77,7 +77,8 @@ attributes to make it work in Markdown renderers that strip "id". --> * Avoid `init` and use explicit initialization functions instead. - * Avoid `new`, especially with structs. + * Avoid `new`, especially with structs, unless a temporary value is needed, + for example when checking the type of an error using `errors.As`. * Check against empty strings like this: @@ -119,6 +120,15 @@ attributes to make it work in Markdown renderers that strip "id". --> ) ``` + Or, with a struct literal: + + ```go + err := functionWithALongName(arg, structType{ + field1: val1, + field2: val2, + }) + ``` + But **never** this: ```go @@ -132,8 +142,8 @@ attributes to make it work in Markdown renderers that strip "id". --> as well. * Don't use `fmt.Sprintf` where a more structured approach to string - conversion could be used. For example, `net.JoinHostPort` or - `url.(*URL).String`. + conversion could be used. For example, `aghnet.JoinHostPort`, + `net.JoinHostPort` or `url.(*URL).String`. * Don't use naked `return`s. @@ -155,6 +165,9 @@ attributes to make it work in Markdown renderers that strip "id". --> * Prefer constants to variables where possible. Avoid global variables. Use [constant errors] instead of `errors.New`. + * Prefer defining `Foo.String` and `ParseFoo` in terms of `Foo.MarshalText` + and `Foo.UnmarshalText` correspondingly and not the other way around. + * Prefer to use named functions for goroutines. * Program code lines should not be longer than one hundred (**100**) columns. @@ -198,8 +211,8 @@ attributes to make it work in Markdown renderers that strip "id". --> ### Formatting - * Decorate `break`, `continue`, `fallthrough`, `return`, and other terminating - statements with empty lines unless it's the only statement in that block. + * Decorate `break`, `continue`, `return`, and other terminating statements + with empty lines unless it's the only statement in that block. * Don't group type declarations together. Unlike with blocks of `const`s, where a `iota` may be used or where all constants belong to a certain type, diff --git a/scripts/hooks/pre-commit b/scripts/hooks/pre-commit index 41fd72fc..e38e9aa5 100755 --- a/scripts/hooks/pre-commit +++ b/scripts/hooks/pre-commit @@ -2,6 +2,10 @@ set -e -f -u +# Show all temporary todos to the programmer but don't fail the commit +# if there are any, because the commit could be in a temporary branch. +git grep -e 'TODO.*!!' -- ':!HACKING.md' ':!scripts/hooks/pre-commit' | more || : + if [ "$( git diff --cached --name-only -- 'client/*.js' )" ] then make js-lint js-test