From da96297d98b237491a8e49d1b83927cbb396e13f Mon Sep 17 00:00:00 2001 From: Seongmin Lee Date: Sun, 24 Aug 2025 16:15:09 +0900 Subject: [PATCH] knotserver: set default git committer user via config Change-Id: skprtmnmwuqnqxzrpzsomqsntyurzpzp 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 --- knotserver/config/config.go | 7 +++++++ knotserver/git/merge.go | 26 ++++++++++++-------------- knotserver/xrpc/merge.go | 2 ++ 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/knotserver/config/config.go b/knotserver/config/config.go index 3e056df..920e936 100644 --- a/knotserver/config/config.go +++ b/knotserver/config/config.go @@ -27,6 +27,12 @@ type Server struct { 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)) } @@ -34,6 +40,7 @@ func (s Server) Did() syntax.DID { 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"` } diff --git a/knotserver/git/merge.go b/knotserver/git/merge.go index 3c9d182..84679fa 100644 --- a/knotserver/git/merge.go +++ b/knotserver/git/merge.go @@ -85,11 +85,13 @@ type ConflictInfo struct { // 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 { @@ -164,6 +166,9 @@ func (g *GitRepo) applyPatch(tmpDir, patchFile string, opts MergeOptions) error var stderr bytes.Buffer var cmd *exec.Cmd + // configure default git user before merge + exec.Command("git", "-C", tmpDir, "config", "user.name", opts.CommitterName).Run() + exec.Command("git", "-C", tmpDir, "config", "user.email", opts.CommitterEmail).Run() exec.Command("git", "-C", tmpDir, "config", "advice.mergeConflict", "false").Run() // if patch is a format-patch, apply using 'git am' @@ -188,17 +193,10 @@ func (g *GitRepo) applyPatch(tmpDir, patchFile string, opts MergeOptions) error 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) diff --git a/knotserver/xrpc/merge.go b/knotserver/xrpc/merge.go index 4ba756f..8d6f14f 100644 --- a/knotserver/xrpc/merge.go +++ b/knotserver/xrpc/merge.go @@ -81,6 +81,8 @@ func (x *Xrpc) Merge(w http.ResponseWriter, r *http.Request) { mo.CommitMessage = *data.CommitMessage } + mo.CommitterName = x.Config.Git.UserName + mo.CommitterEmail = x.Config.Git.UserEmail mo.FormatPatch = patchutil.IsFormatPatch(data.Patch) err = gr.MergeWithOptions([]byte(data.Patch), data.Branch, mo) -- 2.43.0