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