forked from tangled.org/core
Monorepo for Tangled

knotserver: set default git committer user via config

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>

authored by boltless.me and committed by Tangled 0749c39a 033886cc

Changed files
+21 -14
knotserver
config
git
xrpc
+7
knotserver/config/config.go
··· 27 27 Dev bool `env:"DEV, default=false"` 28 28 } 29 29 30 + type Git struct { 31 + // user name & email used as committer 32 + UserName string `env:"USER_NAME, default=Tangled"` 33 + UserEmail string `env:"USER_EMAIL, default=noreply@tangled.sh"` 34 + } 35 + 30 36 func (s Server) Did() syntax.DID { 31 37 return syntax.DID(fmt.Sprintf("did:web:%s", s.Hostname)) 32 38 } ··· 34 40 type Config struct { 35 41 Repo Repo `env:",prefix=KNOT_REPO_"` 36 42 Server Server `env:",prefix=KNOT_SERVER_"` 43 + Git Git `env:",prefix=KNOT_GIT_"` 37 44 AppViewEndpoint string `env:"APPVIEW_ENDPOINT, default=https://tangled.sh"` 38 45 } 39 46
+12 -14
knotserver/git/merge.go
··· 85 85 86 86 // MergeOptions specifies the configuration for a merge operation 87 87 type MergeOptions struct { 88 - CommitMessage string 89 - CommitBody string 90 - AuthorName string 91 - AuthorEmail string 92 - FormatPatch bool 88 + CommitMessage string 89 + CommitBody string 90 + AuthorName string 91 + AuthorEmail string 92 + CommitterName string 93 + CommitterEmail string 94 + FormatPatch bool 93 95 } 94 96 95 97 func (e ErrMerge) Error() string { ··· 164 166 var stderr bytes.Buffer 165 167 var cmd *exec.Cmd 166 168 169 + // configure default git user before merge 170 + exec.Command("git", "-C", tmpDir, "config", "user.name", opts.CommitterName).Run() 171 + exec.Command("git", "-C", tmpDir, "config", "user.email", opts.CommitterEmail).Run() 167 172 exec.Command("git", "-C", tmpDir, "config", "advice.mergeConflict", "false").Run() 168 173 169 174 // if patch is a format-patch, apply using 'git am' ··· 188 193 authorName := opts.AuthorName 189 194 authorEmail := opts.AuthorEmail 190 195 191 - if authorEmail == "" { 192 - authorEmail = "noreply@tangled.sh" 193 - } 194 - 195 - if authorName == "" { 196 - authorName = "Tangled" 197 - } 198 - 199 - if authorName != "" { 196 + if authorName != "" && authorEmail != "" { 200 197 commitArgs = append(commitArgs, "--author", fmt.Sprintf("%s <%s>", authorName, authorEmail)) 201 198 } 199 + // else, will default to knot's global user.name & user.email configured via `KNOT_GIT_USER_*` env variables 202 200 203 201 commitArgs = append(commitArgs, "-m", opts.CommitMessage) 204 202
+2
knotserver/xrpc/merge.go
··· 81 81 mo.CommitMessage = *data.CommitMessage 82 82 } 83 83 84 + mo.CommitterName = x.Config.Git.UserName 85 + mo.CommitterEmail = x.Config.Git.UserEmail 84 86 mo.FormatPatch = patchutil.IsFormatPatch(data.Patch) 85 87 86 88 err = gr.MergeWithOptions([]byte(data.Patch), data.Branch, mo)