From 6a840970944957f5b500c74098224b243ee2e523 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/git.go | 4 ++++ knotserver/git/merge.go | 37 ++++++++++++++++++++++--------------- knotserver/routes.go | 10 ++++++---- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/knotserver/config/config.go b/knotserver/config/config.go index a98137b..c564754 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/git.go b/knotserver/git/git.go index a5fc5ef..2b6fedc 100644 --- a/knotserver/git/git.go +++ b/knotserver/git/git.go @@ -109,6 +109,10 @@ func Open(path string, ref string) (*GitRepo, error) { } g.h = *hash } + _, err = g.runGitCmd("config", "user.name", "", "user.email", "") + if err != nil { + return nil, err + } return &g, nil } diff --git a/knotserver/git/merge.go b/knotserver/git/merge.go index 3c9d182..09c2497 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,7 +166,19 @@ func (g *GitRepo) applyPatch(tmpDir, patchFile string, opts MergeOptions) error 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 { @@ -188,17 +202,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/routes.go b/knotserver/routes.go index e2ddeb4..d42ca87 100644 --- a/knotserver/routes.go +++ b/knotserver/routes.go @@ -986,10 +986,12 @@ func (h *Handle) Merge(w http.ResponseWriter, r *http.Request) { } 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), } -- 2.43.0