gitea源码

pushoptions.go 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package private
  4. import (
  5. "strconv"
  6. "strings"
  7. "code.gitea.io/gitea/modules/optional"
  8. )
  9. // GitPushOptions is a wrapper around a map[string]string
  10. type GitPushOptions map[string]string
  11. // GitPushOptions keys
  12. const (
  13. GitPushOptionRepoPrivate = "repo.private"
  14. GitPushOptionRepoTemplate = "repo.template"
  15. GitPushOptionForcePush = "force-push"
  16. )
  17. // Bool checks for a key in the map and parses as a boolean
  18. // An option without value is considered true, eg: "-o force-push" or "-o repo.private"
  19. func (g GitPushOptions) Bool(key string) optional.Option[bool] {
  20. if val, ok := g[key]; ok {
  21. if val == "" {
  22. return optional.Some(true)
  23. }
  24. if b, err := strconv.ParseBool(val); err == nil {
  25. return optional.Some(b)
  26. }
  27. }
  28. return optional.None[bool]()
  29. }
  30. // AddFromKeyValue adds a key value pair to the map by "key=value" format or "key" for empty value
  31. func (g GitPushOptions) AddFromKeyValue(line string) {
  32. kv := strings.SplitN(line, "=", 2)
  33. if len(kv) == 2 {
  34. g[kv[0]] = kv[1]
  35. } else {
  36. g[kv[0]] = ""
  37. }
  38. }