Update README.txt
[GitSharp.git] / GitSharp / Config.cs
blobf4763bf1e89d74361ac4bdbdbbbbe97c39da5602
1 /*
2 * Copyright (C) 2009, Henon <meinrad.recheis@gmail.com>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 using System;
39 using System.Collections.Generic;
40 using System.Linq;
41 using System.Text;
42 using System.Diagnostics;
44 namespace GitSharp
46 /// <summary>
47 /// Represents repository-, user-, and global-configuration for git
48 /// </summary>
49 public class Config : IEnumerable<KeyValuePair<string, string>>
51 private Repository _repo;
53 public Config(Repository repo)
55 Debug.Assert(repo != null);
56 _repo = repo;
59 /// <summary>
60 /// Direct config access via git style names (i.e. "user.name")
61 /// </summary>
62 /// <param name="key"></param>
63 /// <returns></returns>
64 public string this[string key]
66 get
68 var config = _repo._internal_repo.Config;
69 var token = key.Split('.');
71 if (token.Count() == 2)
73 return config.getString(token[0], null, token[1]);
76 if (token.Count() == 3)
78 return config.getString(token[0], token[1], token[2]);
81 return null;
83 set
85 var config = _repo._internal_repo.Config;
86 var token = key.Split('.');
87 if (token.Count() == 2)
88 config.setString(token[0], null, token[1], value);
89 else if (token.Count() == 3)
90 config.setString(token[0], token[1], token[2], value);
94 public IEnumerable<string> Keys
96 get
98 foreach (var pair in this)
99 yield return pair.Key;
103 public IEnumerable<string> Values
107 foreach (var pair in this)
108 yield return pair.Value;
112 #region IEnumerable<KeyValuePair<string,string>> Members
114 public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
116 var config = _repo._internal_repo.Config;
117 config.getState();
118 foreach (var entry in config._state.get().EntryList)
120 if (string.IsNullOrEmpty(entry.name))
121 continue;
122 var subsec = (entry.subsection != null ? "." + entry.subsection : "");
123 yield return new KeyValuePair<string, string>(entry.section + subsec + "." + entry.name, entry.value);
127 #endregion
129 #region IEnumerable Members
131 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
133 return GetEnumerator();
136 #endregion
138 /// <summary>
139 /// Saves the config to the file system.
140 /// </summary>
141 public void Persist()
143 _repo._internal_repo.Config.save();