go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth/authctx/git.go (about) 1 // Copyright 2019 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package authctx 16 17 import ( 18 "os" 19 "text/template" 20 ) 21 22 // gitConfigTempl is a template for .gitconfig file used inside a LUCI context. 23 // 24 // Note that we have to do two insteadOf rewrites: / => /a/ and /a/ => /a/. 25 // The later is needed to allow https://.../a/ to be passed to git explicitly. 26 // Without it, git will rewrite the url into https://.../a/a/ which doesn't 27 // work. 28 var gitConfigTempl = template.Must(template.New(".gitconfig").Parse(`# Autogenerated. 29 30 [user] 31 email = {{.UserEmail}} 32 name = {{.UserName}} 33 34 [core] 35 deltaBaseCacheLimit = 2g 36 {{- if .IsWindows}} 37 fscache = true 38 symlinks = false 39 autocrlf = false 40 filemode = false{{end}} 41 42 [pack] 43 packSizeLimit = 2g 44 45 # Reduce noise. 46 [advice] 47 detachedHead = false 48 49 [http] 50 version = HTTP/1.1 51 # Request the GFE return debug headers as an encrypted blob in 52 # X-Encrypted-Debug-Headers. 53 extraheader = X-Return-Encrypted-Headers: all 54 {{if .IsWindows}} 55 [diff "astextplain"] 56 textconv = astextplain 57 {{end}} 58 [gc] 59 autodetach = false 60 {{if .UseCredentialHelper}} 61 [credential] 62 helper = 63 helper = luci 64 {{end -}} 65 {{- range .KnownGerritHosts}} 66 [url "https://{{.}}/a/"] 67 insteadOf = https://{{.}}/a/ 68 insteadOf = https://{{.}}/ 69 {{end -}} 70 `)) 71 72 // gitConfig is used to setup .gitconfig used by subprocesses. 73 // 74 // This is used only if git authentication is enabled. Otherwise we don't mess 75 // with git config at all. 76 // 77 // Assumes 'git' binary is actually gitwrapper and that 'git-credential-luci' 78 // binary is in PATH. 79 type gitConfig struct { 80 IsWindows bool // true if running on Windows 81 UserEmail string // value of user.email 82 UserName string // value of user.name 83 UseCredentialHelper bool // if true, use git-credential-luci helper for auth 84 KnownGerritHosts []string // hosts to use '/a/' paths on to force auth 85 } 86 87 // Write actually writes the config to 'path'. 88 func (gc *gitConfig) Write(path string) error { 89 f, err := os.Create(path) 90 if err != nil { 91 return err 92 } 93 defer f.Close() 94 if err = gitConfigTempl.Execute(f, gc); err != nil { 95 return err 96 } 97 return f.Close() // failure to close the file is an overall failure 98 }