PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/GitCommands/Settings/ConfigFileSettings.cs

https://github.com/qgppl/gitextensions
C# | 190 lines | 154 code | 33 blank | 3 comment | 8 complexity | 8e2fe4d1bf5f5d5d1f3b4e2ae439157f MD5 | raw file
Possible License(s): GPL-3.0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using GitCommands.Config;
  7. using GitUIPluginInterfaces;
  8. namespace GitCommands.Settings
  9. {
  10. public class ConfigFileSettings : SettingsContainer<ConfigFileSettings, ConfigFileSettingsCache>, ISettingsValueGetter
  11. {
  12. public ConfigFileSettings(ConfigFileSettings aLowerPriority, ConfigFileSettingsCache aSettingsCache)
  13. : base(aLowerPriority, aSettingsCache)
  14. {
  15. core = new CorePath(this);
  16. mergetool = new MergeToolPath(this);
  17. }
  18. public static ConfigFileSettings CreateEffective(GitModule aModule)
  19. {
  20. return CreateLocal(aModule, CreateGlobal(CreateSystemWide()));
  21. }
  22. public static ConfigFileSettings CreateLocal(GitModule aModule, bool allowCache = true)
  23. {
  24. return CreateLocal(aModule, null, allowCache);
  25. }
  26. private static ConfigFileSettings CreateLocal(GitModule aModule, ConfigFileSettings aLowerPriority, bool allowCache = true)
  27. {
  28. return new ConfigFileSettings(aLowerPriority,
  29. ConfigFileSettingsCache.Create(Path.Combine(aModule.GetGitDirectory(), "config"), true, allowCache));
  30. }
  31. public static ConfigFileSettings CreateGlobal(bool allowCache = true)
  32. {
  33. return CreateGlobal(null, allowCache);
  34. }
  35. public static ConfigFileSettings CreateGlobal(ConfigFileSettings aLowerPriority, bool allowCache = true)
  36. {
  37. string configPath = Path.Combine(GitCommandHelpers.GetHomeDir(), ".config", "git", "config");
  38. if (!File.Exists(configPath))
  39. configPath = Path.Combine(GitCommandHelpers.GetHomeDir(), ".gitconfig");
  40. return new ConfigFileSettings(aLowerPriority,
  41. ConfigFileSettingsCache.Create(configPath, false, allowCache));
  42. }
  43. public static ConfigFileSettings CreateSystemWide(bool allowCache = true)
  44. {
  45. // Git 2.xx
  46. string configPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Git", "config");
  47. if (!File.Exists(configPath))
  48. {
  49. // Git 1.xx
  50. configPath = Path.Combine(AppSettings.GitBinDir, "..", "etc", "gitconfig");
  51. if (!File.Exists(configPath))
  52. return null;
  53. }
  54. return new ConfigFileSettings(null,
  55. ConfigFileSettingsCache.Create(configPath, false, allowCache));
  56. }
  57. public readonly CorePath core;
  58. public readonly MergeToolPath mergetool;
  59. public string GetValue(string setting)
  60. {
  61. return this.GetString(setting, string.Empty);
  62. }
  63. public IList<string> GetValues(string setting)
  64. {
  65. return SettingsCache.GetValues(setting);
  66. }
  67. public void SetValue(string setting, string value)
  68. {
  69. if (value.IsNullOrEmpty())
  70. {
  71. //to remove setting
  72. value = null;
  73. }
  74. this.SetString(setting, value);
  75. }
  76. public void SetPathValue(string setting, string value)
  77. {
  78. SetValue(setting, ConfigSection.FixPath(value));
  79. }
  80. public IList<ConfigSection> GetConfigSections()
  81. {
  82. return SettingsCache.GetConfigSections();
  83. }
  84. public void RemoveConfigSection(string configSectionName)
  85. {
  86. SettingsCache.RemoveConfigSection(configSectionName);
  87. }
  88. public Encoding FilesEncoding
  89. {
  90. get
  91. {
  92. return GetEncoding("i18n.filesEncoding");
  93. }
  94. set
  95. {
  96. SetEncoding("i18n.filesEncoding", value);
  97. }
  98. }
  99. public Encoding CommitEncoding
  100. {
  101. get
  102. {
  103. return GetEncoding("i18n.commitEncoding");
  104. }
  105. }
  106. public Encoding LogOutputEncoding
  107. {
  108. get
  109. {
  110. return GetEncoding("i18n.logoutputencoding");
  111. }
  112. }
  113. private Encoding GetEncoding(string settingName)
  114. {
  115. Encoding result;
  116. string encodingName = GetValue(settingName);
  117. if (string.IsNullOrEmpty(encodingName))
  118. result = null;
  119. else if (!AppSettings.AvailableEncodings.TryGetValue(encodingName, out result))
  120. {
  121. try
  122. {
  123. result = Encoding.GetEncoding(encodingName);
  124. }
  125. catch (ArgumentException)
  126. {
  127. Debug.WriteLine("Unsupported encoding set in git config file: {0}\n" +
  128. "Please check the setting {1} in config file.", encodingName, settingName);
  129. result = null;
  130. }
  131. }
  132. return result;
  133. }
  134. private void SetEncoding(string settingName, Encoding encoding)
  135. {
  136. SetValue(settingName, encoding == null ? null : encoding.HeaderName);
  137. }
  138. }
  139. public class CorePath : SettingsPath
  140. {
  141. public readonly EnumNullableSetting<AutoCRLFType> autocrlf;
  142. public CorePath(ConfigFileSettings container)
  143. : base(container, "core")
  144. {
  145. autocrlf = new EnumNullableSetting<AutoCRLFType>("autocrlf", this);
  146. }
  147. }
  148. public class MergeToolPath : SettingsPath
  149. {
  150. public readonly BoolNullableSetting keepBackup;
  151. public MergeToolPath(ConfigFileSettings container)
  152. : base(container, "mergetool")
  153. {
  154. keepBackup = new BoolNullableSetting("keepBackup", this, true);
  155. }
  156. }
  157. }