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
+39 -19
knotserver
+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
+4
knotserver/git/git.go
··· 109 } 110 g.h = *hash 111 } 112 return &g, nil 113 } 114
··· 109 } 110 g.h = *hash 111 } 112 + _, err = g.runGitCmd("config", "user.name", "<name>", "user.email", "<email>") 113 + if err != nil { 114 + return nil, err 115 + } 116 return &g, nil 117 } 118
+22 -15
knotserver/git/merge.go
··· 86 87 // MergeOptions specifies the configuration for a merge operation 88 type MergeOptions struct { 89 - CommitMessage string 90 - CommitBody string 91 - AuthorName string 92 - AuthorEmail string 93 - FormatPatch bool 94 } 95 96 func (e ErrMerge) Error() string { ··· 165 var stderr bytes.Buffer 166 var cmd *exec.Cmd 167 168 - exec.Command("git", "-C", tmpDir, "config", "advice.mergeConflict", "false").Run() 169 170 // if patch is a format-patch, apply using 'git am' 171 if opts.FormatPatch { ··· 189 authorName := opts.AuthorName 190 authorEmail := opts.AuthorEmail 191 192 - if authorEmail == "" { 193 - authorEmail = "noreply@tangled.sh" 194 - } 195 - 196 - if authorName == "" { 197 - authorName = "Tangled" 198 - } 199 - 200 - if authorName != "" { 201 commitArgs = append(commitArgs, "--author", fmt.Sprintf("%s <%s>", authorName, authorEmail)) 202 } 203 204 commitArgs = append(commitArgs, "-m", opts.CommitMessage) 205
··· 86 87 // MergeOptions specifies the configuration for a merge operation 88 type MergeOptions struct { 89 + CommitMessage string 90 + CommitBody string 91 + AuthorName string 92 + AuthorEmail string 93 + CommitterName string 94 + CommitterEmail string 95 + FormatPatch bool 96 } 97 98 func (e ErrMerge) Error() string { ··· 167 var stderr bytes.Buffer 168 var cmd *exec.Cmd 169 170 + // configure default git user before merge 171 + exec.Command( 172 + "git", 173 + "-C", 174 + tmpDir, 175 + "config", 176 + "user.name", 177 + opts.CommitterName, 178 + "user.email", 179 + opts.CommitterEmail, 180 + "advice.mergeConflict", 181 + "false", 182 + ).Run() 183 184 // if patch is a format-patch, apply using 'git am' 185 if opts.FormatPatch { ··· 203 authorName := opts.AuthorName 204 authorEmail := opts.AuthorEmail 205 206 + if authorName != "" && authorEmail != "" { 207 commitArgs = append(commitArgs, "--author", fmt.Sprintf("%s <%s>", authorName, authorEmail)) 208 } 209 + // else, will default to knot's global user.name & user.email configured via `KNOT_GIT_USER_*` env variables 210 211 commitArgs = append(commitArgs, "-m", opts.CommitMessage) 212
+6 -4
knotserver/routes.go
··· 986 } 987 988 mo := git.MergeOptions{ 989 - AuthorName: data.AuthorName, 990 - AuthorEmail: data.AuthorEmail, 991 - CommitBody: data.CommitBody, 992 - CommitMessage: data.CommitMessage, 993 FormatPatch: patchutil.IsFormatPatch(patch), 994 } 995
··· 986 } 987 988 mo := git.MergeOptions{ 989 + AuthorName: data.AuthorName, 990 + AuthorEmail: data.AuthorEmail, 991 + CommitterName: h.c.Git.UserName, 992 + CommitterEmail: h.c.Git.UserEmail, 993 + CommitBody: data.CommitBody, 994 + CommitMessage: data.CommitMessage, 995 FormatPatch: patchutil.IsFormatPatch(patch), 996 } 997