knotserver: set default git committer user via config #529

merged
opened by boltless.me targeting master from boltless.me/core: push-skprtmnmwuqn

Introducing KNOT_GIT_USER_NAME and KNOT_GIT_USER_EMAIL environment variables.

This will prevent silently inheritting global gitconfig's user

Signed-off-by: Seongmin Lee boltlessengineer@proton.me

Changed files
+35 -19
knotserver
+7
knotserver/config/config.go
···
Dev bool `env:"DEV, default=false"`
}
+
type Git struct {
+
// user name & email used as committer
+
UserName string `env:"USER_NAME, default=Tangled"`
+
UserEmail string `env:"USER_EMAIL, default=noreply@tangled.sh"`
+
}
+
func (s Server) Did() syntax.DID {
return syntax.DID(fmt.Sprintf("did:web:%s", s.Hostname))
}
···
type Config struct {
Repo Repo `env:",prefix=KNOT_REPO_"`
Server Server `env:",prefix=KNOT_SERVER_"`
+
Git Git `env:",prefix=KNOT_GIT_"`
AppViewEndpoint string `env:"APPVIEW_ENDPOINT, default=https://tangled.sh"`
}
+22 -15
knotserver/git/merge.go
···
// MergeOptions specifies the configuration for a merge operation
type MergeOptions struct {
-
CommitMessage string
-
CommitBody string
-
AuthorName string
-
AuthorEmail string
-
FormatPatch bool
+
CommitMessage string
+
CommitBody string
+
AuthorName string
+
AuthorEmail string
+
CommitterName string
+
CommitterEmail string
+
FormatPatch bool
}
func (e ErrMerge) Error() string {
···
var stderr bytes.Buffer
var cmd *exec.Cmd
-
exec.Command("git", "-C", tmpDir, "config", "advice.mergeConflict", "false").Run()
+
// configure default git user before merge
+
exec.Command(
+
"git",
+
"-C",
+
tmpDir,
+
"config",
+
"user.name",
+
opts.CommitterName,
+
"user.email",
+
opts.CommitterEmail,
+
"advice.mergeConflict",
+
"false",
+
).Run()
// if patch is a format-patch, apply using 'git am'
if opts.FormatPatch {
···
authorName := opts.AuthorName
authorEmail := opts.AuthorEmail
-
if authorEmail == "" {
-
authorEmail = "noreply@tangled.sh"
-
}
-
-
if authorName == "" {
-
authorName = "Tangled"
-
}
-
-
if authorName != "" {
+
if authorName != "" && authorEmail != "" {
commitArgs = append(commitArgs, "--author", fmt.Sprintf("%s <%s>", authorName, authorEmail))
}
+
// else, will default to knot's global user.name & user.email configured via `KNOT_GIT_USER_*` env variables
commitArgs = append(commitArgs, "-m", opts.CommitMessage)
+6 -4
knotserver/routes.go
···
}
mo := git.MergeOptions{
-
AuthorName: data.AuthorName,
-
AuthorEmail: data.AuthorEmail,
-
CommitBody: data.CommitBody,
-
CommitMessage: data.CommitMessage,
+
AuthorName: data.AuthorName,
+
AuthorEmail: data.AuthorEmail,
+
CommitterName: h.c.Git.UserName,
+
CommitterEmail: h.c.Git.UserEmail,
+
CommitBody: data.CommitBody,
+
CommitMessage: data.CommitMessage,
FormatPatch: patchutil.IsFormatPatch(patch),
}