Monorepo for Tangled tangled.org

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>

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