/// <summary> /// Determines which configuration file has been found. /// </summary> public virtual bool HasConfig(ConfigurationLevel level) { using (ConfigurationHandle snapshot = Snapshot()) using (ConfigurationHandle handle = RetrieveConfigurationHandle(level, false, snapshot)) { return(handle != null); } }
/// <summary> /// Get a configuration value for a key. Keys are in the form 'section.name'. /// <para> /// The same escalation logic than in git.git will be used when looking for the key in the config files: /// - local: the Git file in the current repository /// - global: the Git file specific to the current interactive user (usually in `$HOME/.gitconfig`) /// - xdg: another Git file specific to the current interactive user (usually in `$HOME/.config/git/config`) /// - system: the system-wide Git file /// /// The first occurence of the key will be returned. /// </para> /// <para> /// For example in order to get the value for this in a .git\config file: /// /// <code> /// [core] /// bare = true /// </code> /// /// You would call: /// /// <code> /// bool isBare = repo.Config.Get<bool>("core.bare").Value; /// </code> /// </para> /// </summary> /// <typeparam name="T">The configuration value type</typeparam> /// <param name="key">The key</param> /// <returns>The <see cref="ConfigurationEntry{T}"/>, or null if not set</returns> public virtual ConfigurationEntry <T> Get <T>(string key) { Ensure.ArgumentNotNullOrEmptyString(key, "key"); using (ConfigurationHandle snapshot = Snapshot()) { return(Proxy.git_config_get_entry <T>(snapshot, key)); } }
/// <summary> /// Unset all configuration values in a multivar variable (key and value). /// </summary> /// <param name="key">The key to unset.</param> /// <param name="level">The configuration file which should be considered as the target of this operation</param> public virtual bool UnsetAll(string key, ConfigurationLevel level) { Ensure.ArgumentNotNullOrEmptyString(key, "key"); using (ConfigurationHandle h = RetrieveConfigurationHandle(level, true, configHandle)) { return(Proxy.git_config_delete_multivar(h, key)); } }
internal void UnsetMultivar(string key, ConfigurationLevel level) { Ensure.ArgumentNotNullOrEmptyString(key, "key"); using (ConfigurationHandle h = RetrieveConfigurationHandle(level, true, configHandle)) { Proxy.git_config_delete_multivar(h, key); } }
/// <summary> /// Find configuration entries matching <paramref name="regexp"/>. /// </summary> /// <param name="regexp">A regular expression.</param> /// <param name="level">The configuration file into which the key should be searched for.</param> /// <returns>Matching entries.</returns> public virtual IEnumerable <ConfigurationEntry <string> > Find(string regexp, ConfigurationLevel level) { Ensure.ArgumentNotNullOrEmptyString(regexp, "regexp"); using (ConfigurationHandle snapshot = Snapshot()) using (ConfigurationHandle h = RetrieveConfigurationHandle(level, true, snapshot)) { return(Proxy.git_config_iterator_glob(h, regexp).ToList()); } }
/// <summary> /// Adds a configuration value for a multivalue key. Keys are in the form 'section.name'. /// <para> /// For example in order to add the value for this in a .git\config file: /// /// [test] /// plugin = first /// /// You would call: /// /// repo.Config.Add("test.plugin", "first"); /// </para> /// </summary> /// <typeparam name="T">The configuration value type</typeparam> /// <param name="key">The key parts</param> /// <param name="value">The value</param> /// <param name="level">The configuration file which should be considered as the target of this operation</param> public virtual void Add(string key, string value, ConfigurationLevel level) { Ensure.ArgumentNotNull(value, "value"); Ensure.ArgumentNotNullOrEmptyString(key, "key"); using (ConfigurationHandle h = RetrieveConfigurationHandle(level, true, configHandle)) { Proxy.git_config_add_string(h, key, value); } }
public async Task Given_DisposedHandle_When_Dispose_Then_NothingHappens() { var internalHandle1 = A.Fake <IConfigurationHandle>(); var handle = new ConfigurationHandle(new[] { internalHandle1 }); await handle.DisposeAsync(); await handle.DisposeAsync(); A.CallTo(() => internalHandle1.DisposeAsync()).MustHaveHappenedOnceExactly(); }
public async Task Given_Disposables_When_Dispose_Then_AllDisposed() { var internalHandle1 = A.Fake <IConfigurationHandle>(); var internalHandle2 = A.Fake <IConfigurationHandle>(); var handle = new ConfigurationHandle(new[] { internalHandle1, internalHandle2 }); await handle.DisposeAsync(); A.CallTo(() => internalHandle1.DisposeAsync()).MustHaveHappenedOnceExactly(); A.CallTo(() => internalHandle2.DisposeAsync()).MustHaveHappenedOnceExactly(); }
/// <summary> /// Set a configuration value for a key. Keys are in the form 'section.name'. /// <para> /// For example in order to set the value for this in a .git\config file: /// /// [test] /// boolsetting = true /// /// You would call: /// /// repo.Config.Set("test.boolsetting", true); /// </para> /// </summary> /// <typeparam name="T">The configuration value type</typeparam> /// <param name="key">The key parts</param> /// <param name="value">The value</param> /// <param name="level">The configuration file which should be considered as the target of this operation</param> public virtual void Set <T>(string key, T value, ConfigurationLevel level) { Ensure.ArgumentNotNull(value, "value"); Ensure.ArgumentNotNullOrEmptyString(key, "key"); using (ConfigurationHandle h = RetrieveConfigurationHandle(level, true, configHandle)) { if (!configurationTypedUpdater.ContainsKey(typeof(T))) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Generic Argument of type '{0}' is not supported.", typeof(T).FullName)); } configurationTypedUpdater[typeof(T)](key, value, h); } }
/// <summary> /// Get a configuration value for a key. Keys are in the form 'section.name'. /// <para> /// For example in order to get the value for this in a .git\config file: /// /// <code> /// [core] /// bare = true /// </code> /// /// You would call: /// /// <code> /// bool isBare = repo.Config.Get<bool>("core.bare").Value; /// </code> /// </para> /// </summary> /// <typeparam name="T">The configuration value type</typeparam> /// <param name="key">The key</param> /// <param name="level">The configuration file into which the key should be searched for</param> /// <returns>The <see cref="ConfigurationEntry{T}"/>, or null if not set</returns> public virtual ConfigurationEntry <T> Get <T>(string key, ConfigurationLevel level) { Ensure.ArgumentNotNullOrEmptyString(key, "key"); using (ConfigurationHandle snapshot = Snapshot()) using (ConfigurationHandle handle = RetrieveConfigurationHandle(level, false, snapshot)) { if (handle == null) { return(null); } return(Proxy.git_config_get_entry <T>(handle, key)); } }
public async Task Given_DisposingHandle_When_Dispose_Then_Throws() { var internalHandle1 = A.Fake <IConfigurationHandle>(); // Force delay so the second dispose fails during dispose A.CallTo(() => internalHandle1.DisposeAsync()).Invokes(async() => await Task.Delay(100)); var handle = new ConfigurationHandle(new[] { internalHandle1 }); var disposeTask = Task.Run(() => handle.DisposeAsync().AsTask()); Assert.ThrowsAsync <InvalidOperationException>(() => handle.DisposeAsync().AsTask()); await disposeTask; A.CallTo(() => internalHandle1.DisposeAsync()).MustHaveHappenedOnceExactly(); }
private ConfigurationHandle RetrieveConfigurationHandle(ConfigurationLevel level, bool throwIfStoreHasNotBeenFound, ConfigurationHandle fromHandle) { ConfigurationHandle handle = null; if (fromHandle != null) { handle = Proxy.git_config_open_level(fromHandle, level); } if (handle == null && throwIfStoreHasNotBeenFound) { throw new LibGit2SharpException("No {0} configuration file has been found.", Enum.GetName(typeof(ConfigurationLevel), level)); } return(handle); }
private void Init(Repository repository) { configHandle = Proxy.git_config_new(); RepositoryHandle repoHandle = (repository != null) ? repository.Handle : null; if (repoHandle != null) { //TODO: push back this logic into libgit2. // As stated by @carlosmn "having a helper function to load the defaults and then allowing you // to modify it before giving it to git_repository_open_ext() would be a good addition, I think." // -- Agreed :) string repoConfigLocation = Path.Combine(repository.Info.Path, "config"); Proxy.git_config_add_file_ondisk(configHandle, repoConfigLocation, ConfigurationLevel.Local, repoHandle); Proxy.git_repository_set_config(repoHandle, configHandle); } else if (repoConfigPath != null) { Proxy.git_config_add_file_ondisk(configHandle, repoConfigPath, ConfigurationLevel.Local, repoHandle); } if (globalConfigPath != null) { Proxy.git_config_add_file_ondisk(configHandle, globalConfigPath, ConfigurationLevel.Global, repoHandle); } if (xdgConfigPath != null) { Proxy.git_config_add_file_ondisk(configHandle, xdgConfigPath, ConfigurationLevel.Xdg, repoHandle); } if (systemConfigPath != null) { Proxy.git_config_add_file_ondisk(configHandle, systemConfigPath, ConfigurationLevel.System, repoHandle); } if (programDataConfigPath != null) { Proxy.git_config_add_file_ondisk(configHandle, programDataConfigPath, ConfigurationLevel.ProgramData, repoHandle); } }