/GitCommands/Settings/RepoDistSettings.cs

https://github.com/fraga/gitextensions
C# | 139 lines | 104 code | 22 blank | 13 comment | 10 complexity | 05056648bbaa60a65ce1f4a14b3d7738 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. namespace GitCommands.Settings
  7. {
  8. /// <summary>
  9. /// Settings that can be distributed with repository
  10. /// they can be overriden for a particular repository
  11. /// </summary>
  12. public class RepoDistSettings : SettingsContainer<RepoDistSettings, GitExtSettingsCache>
  13. {
  14. public GitModule Module { get; private set; }
  15. public RepoDistSettings(RepoDistSettings aLowerPriority, GitExtSettingsCache aSettingsCache)
  16. : base(aLowerPriority, aSettingsCache)
  17. {
  18. BuildServer = new BuildServer(this);
  19. }
  20. #region CreateXXX
  21. public static RepoDistSettings CreateEffective(GitModule aModule)
  22. {
  23. return CreateLocal(aModule, CreateDistributed(aModule, CreateGlobal()));
  24. }
  25. private static RepoDistSettings CreateLocal(GitModule aModule, RepoDistSettings aLowerPriority, bool allowCache = true)
  26. {
  27. //if (aModule.IsBareRepository()
  28. return new RepoDistSettings(aLowerPriority,
  29. GitExtSettingsCache.Create(Path.Combine(aModule.GetGitDirectory(), AppSettings.SettingsFileName), allowCache));
  30. }
  31. public static RepoDistSettings CreateLocal(GitModule aModule, bool allowCache = true)
  32. {
  33. return CreateLocal(aModule, null, allowCache);
  34. }
  35. private static RepoDistSettings CreateDistributed(GitModule aModule, RepoDistSettings aLowerPriority, bool allowCache = true)
  36. {
  37. return new RepoDistSettings(aLowerPriority,
  38. GitExtSettingsCache.Create(Path.Combine(aModule.WorkingDir, AppSettings.SettingsFileName), allowCache));
  39. }
  40. public static RepoDistSettings CreateDistributed(GitModule aModule, bool allowCache = true)
  41. {
  42. return CreateDistributed(aModule, null, allowCache);
  43. }
  44. public static RepoDistSettings CreateGlobal(bool allowCache = true)
  45. {
  46. return new RepoDistSettings(null, GitExtSettingsCache.Create(AppSettings.SettingsFilePath, allowCache));
  47. }
  48. #endregion
  49. public override void SetValue<T>(string name, T value, Func<T, string> encode)
  50. {
  51. bool isEffectiveLevel = LowerPriority != null && LowerPriority.LowerPriority != null;
  52. bool isDetachedOrGlobal = LowerPriority == null;
  53. if (isDetachedOrGlobal || //there is no lower level
  54. SettingsCache.HasValue(name))//or the setting is assigned on this level
  55. {
  56. SettingsCache.SetValue(name, value, encode);
  57. }
  58. else if (isEffectiveLevel)
  59. {
  60. //Settings stored at the Distributed level always have to be set directly
  61. //so I do not pass the control to the LowerPriority(Distributed)
  62. //in order to not overwrite the setting
  63. if (LowerPriority.SettingsCache.HasValue(name))
  64. {
  65. //if the setting is set at the Distributed level, do not overwrite it
  66. //instead of that, set the setting at the Local level to make it effective
  67. //but only if the effective value is different from the new value
  68. if (LowerPriority.SettingsCache.HasADifferentValue(name, value, encode))
  69. {
  70. SettingsCache.SetValue(name, value, encode);
  71. }
  72. }
  73. else
  74. {
  75. //if the setting isn't set at the Distributed level, do not set it there
  76. //instead of that, set the setting at the Global level (it becomes effective then)
  77. LowerPriority.LowerPriority.SetValue(name, value, encode);
  78. }
  79. }
  80. else//the settings is not assigned on this level, recurse to the lower level
  81. {
  82. LowerPriority.SetValue(name, value, encode);
  83. }
  84. }
  85. public readonly BuildServer BuildServer;
  86. public bool NoFastForwardMerge
  87. {
  88. get { return this.GetBool("NoFastForwardMerge", false); }
  89. set { this.SetBool("NoFastForwardMerge", value); }
  90. }
  91. public string Dictionary
  92. {
  93. get { return this.GetString("dictionary", "en-US"); }
  94. set { this.SetString("dictionary", value); }
  95. }
  96. }
  97. public class BuildServer : SettingsPath
  98. {
  99. public readonly StringSetting Type;
  100. public readonly BoolNullableSetting EnableIntegration;
  101. public readonly BoolNullableSetting ShowBuildSummaryInGrid;
  102. public BuildServer(RepoDistSettings container)
  103. : base(container, "BuildServer")
  104. {
  105. Type = new StringSetting("Type", this, null);
  106. EnableIntegration = new BoolNullableSetting("EnableIntegration", this, false);
  107. ShowBuildSummaryInGrid = new BoolNullableSetting("ShowBuildSummaryInGrid", this, true);
  108. }
  109. public SettingsPath TypeSettings
  110. {
  111. get
  112. {
  113. return new SettingsPath(this, Type.Value);
  114. }
  115. }
  116. }
  117. }