1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
|
// Package config collects together all configuration settings
// NOTE: Subject to change, do not rely on this package from outside git-lfs source
package config
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"time"
"unicode"
"github.com/git-lfs/git-lfs/v3/errors"
"github.com/git-lfs/git-lfs/v3/fs"
"github.com/git-lfs/git-lfs/v3/git"
"github.com/git-lfs/git-lfs/v3/tools"
"github.com/git-lfs/git-lfs/v3/tr"
"github.com/rubyist/tracerx"
)
var (
ShowConfigWarnings = false
defaultRemote = "origin"
gitConfigWarningPrefix = "lfs."
)
type Configuration struct {
// Os provides a `*Environment` used to access to the system's
// environment through os.Getenv. It is the point of entry for all
// system environment configuration.
Os Environment
// Git provides a `*Environment` used to access to the various levels of
// `.gitconfig`'s. It is the point of entry for all Git environment
// configuration.
Git Environment
currentRemote *string
pushRemote *string
// gitConfig can fetch or modify the current Git config and track the Git
// version.
gitConfig *git.Configuration
ref *git.Ref
remoteRef *git.Ref
fs *fs.Filesystem
gitDir *string
workDir string
loading sync.Mutex // guards initialization of gitConfig and remotes
loadingGit sync.Mutex // guards initialization of local git and working dirs
remotes []string
extensions map[string]Extension
mask int
maskOnce sync.Once
timestamp time.Time
}
func New() *Configuration {
return NewIn("", "")
}
func NewIn(workdir, gitdir string) *Configuration {
gitConf := git.NewConfig(workdir, gitdir)
c := &Configuration{
Os: EnvironmentOf(NewOsFetcher()),
gitConfig: gitConf,
timestamp: time.Now(),
}
if len(gitConf.WorkDir) > 0 {
c.gitDir = &gitConf.GitDir
c.workDir = gitConf.WorkDir
}
c.Git = &delayedEnvironment{
callback: func() Environment {
sources, err := gitConf.Sources(c.LocalWorkingDir(), ".lfsconfig")
if err != nil {
fmt.Fprintln(os.Stderr, tr.Tr.Get("Error reading `git config`: %s", err))
}
return c.readGitConfig(sources...)
},
}
return c
}
func (c *Configuration) getMask() int {
// This logic is necessarily complex because Git's logic is complex.
c.maskOnce.Do(func() {
val, ok := c.Git.Get("core.sharedrepository")
if !ok {
val = "umask"
} else if Bool(val, false) {
val = "group"
}
switch strings.ToLower(val) {
case "group", "true", "1":
c.mask = 007
case "all", "world", "everybody", "2":
c.mask = 002
case "umask", "false", "0":
c.mask = umask()
default:
if mode, err := strconv.ParseInt(val, 8, 16); err != nil {
// If this doesn't look like an octal number, then it
// could be a falsy value, in which case we should use
// the umask, or it's just invalid, in which case the
// umask is a safe bet.
c.mask = umask()
} else {
c.mask = 0666 & ^int(mode)
}
}
})
return c.mask
}
func (c *Configuration) readGitConfig(gitconfigs ...*git.ConfigurationSource) Environment {
gf, extensions, uniqRemotes := readGitConfig(gitconfigs...)
c.extensions = extensions
c.remotes = make([]string, 0, len(uniqRemotes))
for remote := range uniqRemotes {
c.remotes = append(c.remotes, remote)
}
return EnvironmentOf(gf)
}
// Values is a convenience type used to call the NewFromValues function. It
// specifies `Git` and `Env` maps to use as mock values, instead of calling out
// to real `.gitconfig`s and the `os.Getenv` function.
type Values struct {
// Git and Os are the stand-in maps used to provide values for their
// respective environments.
Git, Os map[string][]string
}
// NewFrom returns a new `*config.Configuration` that reads both its Git
// and Environment-level values from the ones provided instead of the actual
// `.gitconfig` file or `os.Getenv`, respectively.
//
// This method should only be used during testing.
func NewFrom(v Values) *Configuration {
c := &Configuration{
Os: EnvironmentOf(mapFetcher(v.Os)),
gitConfig: git.NewConfig("", ""),
timestamp: time.Now(),
}
c.Git = &delayedEnvironment{
callback: func() Environment {
source := &git.ConfigurationSource{
Lines: make([]string, 0, len(v.Git)),
}
for key, values := range v.Git {
parts := strings.Split(key, ".")
isCaseSensitive := len(parts) >= 3
hasUpper := strings.IndexFunc(key, unicode.IsUpper) > -1
// This branch should only ever trigger in
// tests, and only if they'd be broken.
if !isCaseSensitive && hasUpper {
panic(tr.Tr.Get("key %q has uppercase, shouldn't", key))
}
for _, value := range values {
fmt.Printf("Config: %s=%s\n", key, value)
source.Lines = append(source.Lines, fmt.Sprintf("%s=%s", key, value))
}
}
return c.readGitConfig(source)
},
}
return c
}
// BasicTransfersOnly returns whether to only allow "basic" HTTP transfers.
// Default is false, including if the lfs.basictransfersonly is invalid
func (c *Configuration) BasicTransfersOnly() bool {
return c.Git.Bool("lfs.basictransfersonly", false)
}
// TusTransfersAllowed returns whether to only use "tus.io" HTTP transfers.
// Default is false, including if the lfs.tustransfers is invalid
func (c *Configuration) TusTransfersAllowed() bool {
return c.Git.Bool("lfs.tustransfers", false)
}
func (c *Configuration) TransferBatchSize() int {
return c.Git.Int("lfs.transfer.batchSize", 0)
}
func (c *Configuration) FetchIncludePaths() []string {
patterns, _ := c.Git.Get("lfs.fetchinclude")
return tools.CleanPaths(patterns, ",")
}
func (c *Configuration) FetchExcludePaths() []string {
patterns, _ := c.Git.Get("lfs.fetchexclude")
return tools.CleanPaths(patterns, ",")
}
func (c *Configuration) CurrentRef() *git.Ref {
c.loading.Lock()
defer c.loading.Unlock()
if c.ref == nil {
r, err := git.CurrentRef()
if err != nil {
tracerx.Printf("Error loading current ref: %s", err)
c.ref = &git.Ref{}
} else {
c.ref = r
}
}
return c.ref
}
func (c *Configuration) IsDefaultRemote() bool {
return c.Remote() == defaultRemote
}
func (c *Configuration) AutoDetectRemoteEnabled() bool {
return c.Git.Bool("lfs.remote.autodetect", false)
}
func (c *Configuration) SearchAllRemotesEnabled() bool {
return c.Git.Bool("lfs.remote.searchall", false)
}
// Remote returns the default remote based on:
// 1. The currently tracked remote branch, if present
// 2. The value of remote.lfsdefault.
// 3. Any other SINGLE remote defined in .git/config
// 4. Use "origin" as a fallback.
// Results are cached after the first hit.
func (c *Configuration) Remote() string {
ref := c.CurrentRef()
c.loading.Lock()
defer c.loading.Unlock()
if c.currentRemote == nil {
if remote, ok := c.Git.Get(fmt.Sprintf("branch.%s.remote", ref.Name)); len(ref.Name) != 0 && ok {
// try tracking remote
c.currentRemote = &remote
} else if remote, ok := c.Git.Get("remote.lfsdefault"); ok {
// try default remote
c.currentRemote = &remote
} else if remotes := c.Remotes(); len(remotes) == 1 {
// use only remote if there is only 1
c.currentRemote = &remotes[0]
} else {
// fall back to default :(
c.currentRemote = &defaultRemote
}
}
return *c.currentRemote
}
func (c *Configuration) PushRemote() string {
ref := c.CurrentRef()
c.loading.Lock()
defer c.loading.Unlock()
if c.pushRemote == nil {
if remote, ok := c.Git.Get(fmt.Sprintf("branch.%s.pushRemote", ref.Name)); ok {
c.pushRemote = &remote
} else if remote, ok := c.Git.Get("remote.lfspushdefault"); ok {
c.pushRemote = &remote
} else if remote, ok := c.Git.Get("remote.pushDefault"); ok {
c.pushRemote = &remote
} else {
c.loading.Unlock()
remote := c.Remote()
c.loading.Lock()
c.pushRemote = &remote
}
}
return *c.pushRemote
}
func (c *Configuration) SetValidRemote(name string) error {
if err := git.ValidateRemote(name); err != nil {
name := git.RewriteLocalPathAsURL(name)
if err := git.ValidateRemote(name); err != nil {
return err
}
}
c.SetRemote(name)
return nil
}
func (c *Configuration) SetValidPushRemote(name string) error {
if err := git.ValidateRemote(name); err != nil {
name := git.RewriteLocalPathAsURL(name)
if err := git.ValidateRemote(name); err != nil {
return err
}
}
c.SetPushRemote(name)
return nil
}
func (c *Configuration) SetRemote(name string) {
c.currentRemote = &name
}
func (c *Configuration) SetPushRemote(name string) {
c.pushRemote = &name
}
func (c *Configuration) Remotes() []string {
c.loadGitConfig()
return c.remotes
}
func (c *Configuration) Extensions() map[string]Extension {
c.loadGitConfig()
return c.extensions
}
// SortedExtensions gets the list of extensions ordered by Priority
func (c *Configuration) SortedExtensions() ([]Extension, error) {
return SortExtensions(c.Extensions())
}
func (c *Configuration) SkipDownloadErrors() bool {
return c.Os.Bool("GIT_LFS_SKIP_DOWNLOAD_ERRORS", false) || c.Git.Bool("lfs.skipdownloaderrors", false)
}
func (c *Configuration) SetLockableFilesReadOnly() bool {
return c.Os.Bool("GIT_LFS_SET_LOCKABLE_READONLY", true) && c.Git.Bool("lfs.setlockablereadonly", true)
}
func (c *Configuration) ForceProgress() bool {
return c.Os.Bool("GIT_LFS_FORCE_PROGRESS", false) || c.Git.Bool("lfs.forceprogress", false)
}
// HookDir returns the location of the hooks owned by this repository. If the
// core.hooksPath configuration variable is supported, we prefer that and expand
// paths appropriately.
func (c *Configuration) HookDir() (string, error) {
if git.IsGitVersionAtLeast("2.9.0") {
hp, ok := c.Git.Get("core.hooksPath")
if ok {
path, err := tools.ExpandPath(hp, false)
if err != nil {
return "", err
}
if filepath.IsAbs(path) {
return path, nil
}
return filepath.Join(c.LocalWorkingDir(), path), nil
}
}
return filepath.Join(c.LocalGitStorageDir(), "hooks"), nil
}
func (c *Configuration) InRepo() bool {
return len(c.LocalGitDir()) > 0
}
func (c *Configuration) LocalWorkingDir() string {
c.loadGitDirs()
return c.workDir
}
func (c *Configuration) LocalGitDir() string {
c.loadGitDirs()
return *c.gitDir
}
func (c *Configuration) loadGitDirs() {
c.loadingGit.Lock()
defer c.loadingGit.Unlock()
if c.gitDir != nil {
return
}
gitdir, workdir, err := git.GitAndRootDirs()
if err != nil {
errMsg := err.Error()
tracerx.Printf("Error running 'git rev-parse': %s", errMsg)
if errors.ExitStatus(err) != 128 {
fmt.Fprintln(os.Stderr, tr.Tr.Get("Error: %s", errMsg))
}
c.gitDir = &gitdir
}
gitdir = tools.ResolveSymlinks(gitdir)
c.gitDir = &gitdir
c.workDir = tools.ResolveSymlinks(workdir)
}
func (c *Configuration) LocalGitStorageDir() string {
return c.Filesystem().GitStorageDir
}
func (c *Configuration) LocalReferenceDirs() []string {
return c.Filesystem().ReferenceDirs
}
func (c *Configuration) LFSStorageDir() string {
return c.Filesystem().LFSStorageDir
}
func (c *Configuration) LFSObjectDir() string {
return c.Filesystem().LFSObjectDir()
}
func (c *Configuration) LFSObjectExists(oid string, size int64) bool {
return c.Filesystem().ObjectExists(oid, size)
}
func (c *Configuration) EachLFSObject(fn func(fs.Object) error) error {
return c.Filesystem().EachObject(fn)
}
func (c *Configuration) LocalLogDir() string {
return c.Filesystem().LogDir()
}
func (c *Configuration) TempDir() string {
return c.Filesystem().TempDir()
}
func (c *Configuration) Filesystem() *fs.Filesystem {
c.loadGitDirs()
c.loading.Lock()
defer c.loading.Unlock()
if c.fs == nil {
lfsdir, _ := c.Git.Get("lfs.storage")
c.fs = fs.New(
c.Os,
c.LocalGitDir(),
c.LocalWorkingDir(),
lfsdir,
c.RepositoryPermissions(false),
)
}
return c.fs
}
func (c *Configuration) Cleanup() error {
if c == nil {
return nil
}
c.loading.Lock()
defer c.loading.Unlock()
return c.fs.Cleanup()
}
func (c *Configuration) OSEnv() Environment {
return c.Os
}
func (c *Configuration) GitEnv() Environment {
return c.Git
}
func (c *Configuration) GitConfig() *git.Configuration {
return c.gitConfig
}
func (c *Configuration) FindGitGlobalKey(key string) string {
return c.gitConfig.FindGlobal(key)
}
func (c *Configuration) FindGitSystemKey(key string) string {
return c.gitConfig.FindSystem(key)
}
func (c *Configuration) FindGitLocalKey(key string) string {
return c.gitConfig.FindLocal(key)
}
func (c *Configuration) FindGitWorktreeKey(key string) string {
return c.gitConfig.FindWorktree(key)
}
func (c *Configuration) SetGitGlobalKey(key, val string) (string, error) {
return c.gitConfig.SetGlobal(key, val)
}
func (c *Configuration) SetGitSystemKey(key, val string) (string, error) {
return c.gitConfig.SetSystem(key, val)
}
func (c *Configuration) SetGitLocalKey(key, val string) (string, error) {
return c.gitConfig.SetLocal(key, val)
}
func (c *Configuration) SetGitWorktreeKey(key, val string) (string, error) {
return c.gitConfig.SetWorktree(key, val)
}
func (c *Configuration) UnsetGitGlobalSection(key string) (string, error) {
return c.gitConfig.UnsetGlobalSection(key)
}
func (c *Configuration) UnsetGitSystemSection(key string) (string, error) {
return c.gitConfig.UnsetSystemSection(key)
}
func (c *Configuration) UnsetGitLocalSection(key string) (string, error) {
return c.gitConfig.UnsetLocalSection(key)
}
func (c *Configuration) UnsetGitWorktreeSection(key string) (string, error) {
return c.gitConfig.UnsetWorktreeSection(key)
}
func (c *Configuration) UnsetGitLocalKey(key string) (string, error) {
return c.gitConfig.UnsetLocalKey(key)
}
// loadGitConfig is a temporary measure to support legacy behavior dependent on
// accessing properties set by ReadGitConfig, namely:
// - `c.extensions`
// - `c.uniqRemotes`
// - `c.gitConfig`
//
// Since the *gitEnvironment is responsible for setting these values on the
// (*config.Configuration) instance, we must call that method, if it exists.
//
// loadGitConfig returns a bool returning whether or not `loadGitConfig` was
// called AND the method did not return early.
func (c *Configuration) loadGitConfig() {
if g, ok := c.Git.(*delayedEnvironment); ok {
g.Load()
}
}
var (
// dateFormats is a list of all the date formats that Git accepts,
// except for the built-in one, which is handled below.
dateFormats = []string{
"Mon, 02 Jan 2006 15:04:05 -0700",
"2006-01-02T15:04:05-0700",
"2006-01-02 15:04:05-0700",
"2006.01.02T15:04:05-0700",
"2006.01.02 15:04:05-0700",
"01/02/2006T15:04:05-0700",
"01/02/2006 15:04:05-0700",
"02.01.2006T15:04:05-0700",
"02.01.2006 15:04:05-0700",
"2006-01-02T15:04:05Z",
"2006-01-02 15:04:05Z",
"2006.01.02T15:04:05Z",
"2006.01.02 15:04:05Z",
"01/02/2006T15:04:05Z",
"01/02/2006 15:04:05Z",
"02.01.2006T15:04:05Z",
"02.01.2006 15:04:05Z",
}
// defaultDatePattern is the regexp for Git's native date format.
defaultDatePattern = regexp.MustCompile(`\A(\d+) ([+-])(\d{2})(\d{2})\z`)
)
// findUserData returns the name/email that should be used in the commit header.
// We use the same technique as Git for finding this information, except that we
// don't fall back to querying the system for defaults if no values are found in
// the Git configuration or environment.
//
// envType should be "author" or "committer".
func (c *Configuration) findUserData(envType string) (name, email string) {
var filter = func(r rune) rune {
switch r {
case '<', '>', '\n':
return -1
default:
return r
}
}
envType = strings.ToUpper(envType)
name, ok := c.Os.Get("GIT_" + envType + "_NAME")
if !ok {
name, _ = c.Git.Get("user.name")
}
email, ok = c.Os.Get("GIT_" + envType + "_EMAIL")
if !ok {
email, ok = c.Git.Get("user.email")
}
if !ok {
email, _ = c.Os.Get("EMAIL")
}
// Git filters certain characters out of the name and email fields.
name = strings.Map(filter, name)
email = strings.Map(filter, email)
return
}
func (c *Configuration) findUserTimestamp(envType string) time.Time {
date, ok := c.Os.Get(fmt.Sprintf("GIT_%s_DATE", strings.ToUpper(envType)))
if !ok {
return c.timestamp
}
// time.Parse doesn't parse seconds from the Epoch, like we use in the
// Git native format, so we have to do it ourselves.
strs := defaultDatePattern.FindStringSubmatch(date)
if strs != nil {
unixSecs, _ := strconv.ParseInt(strs[1], 10, 64)
hours, _ := strconv.Atoi(strs[3])
offset, _ := strconv.Atoi(strs[4])
offset = (offset + hours*60) * 60
if strs[2] == "-" {
offset = -offset
}
return time.Unix(unixSecs, 0).In(time.FixedZone("", offset))
}
for _, format := range dateFormats {
if t, err := time.Parse(format, date); err == nil {
return t
}
}
// The user provided an invalid value, so default to the current time.
return c.timestamp
}
// CurrentCommitter returns the name/email that would be used to commit a change
// with this configuration. In particular, the "user.name" and "user.email"
// configuration values are used
func (c *Configuration) CurrentCommitter() (name, email string) {
return c.findUserData("committer")
}
// CurrentCommitterTimestamp returns the timestamp that would be used to commit
// a change with this configuration.
func (c *Configuration) CurrentCommitterTimestamp() time.Time {
return c.findUserTimestamp("committer")
}
// CurrentAuthor returns the name/email that would be used to author a change
// with this configuration. In particular, the "user.name" and "user.email"
// configuration values are used
func (c *Configuration) CurrentAuthor() (name, email string) {
return c.findUserData("author")
}
// CurrentCommitterTimestamp returns the timestamp that would be used to commit
// a change with this configuration.
func (c *Configuration) CurrentAuthorTimestamp() time.Time {
return c.findUserTimestamp("author")
}
// RepositoryPermissions returns the permissions that should be used to write
// files in the repository.
func (c *Configuration) RepositoryPermissions(executable bool) os.FileMode {
perms := os.FileMode(0666 & ^c.getMask())
if executable {
return tools.ExecutablePermissions(perms)
}
return perms
}
|