2 * Copyright (C) 2009, Henon <meinrad.recheis@gmail.com>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
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
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.
39 using System
.Collections
.Generic
;
42 using System
.Diagnostics
;
47 /// Represents repository-, user-, and global-configuration for git
49 public class Config
: IEnumerable
<KeyValuePair
<string, string>>
51 private Repository _repo
;
53 public Config(Repository repo
)
55 Debug
.Assert(repo
!= null);
60 /// Direct config access via git style names (i.e. "user.name")
62 /// <param name="key"></param>
63 /// <returns></returns>
64 public string this[string key
]
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]);
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
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
;
118 foreach (var entry
in config
._state
.get().EntryList
)
120 if (string.IsNullOrEmpty(entry
.name
))
122 var subsec
= (entry
.subsection
!= null ? "." + entry
.subsection
: "");
123 yield return new KeyValuePair
<string, string>(entry
.section
+ subsec
+ "." + entry
.name
, entry
.value);
129 #region IEnumerable Members
131 System
.Collections
.IEnumerator System
.Collections
.IEnumerable
.GetEnumerator()
133 return GetEnumerator();
139 /// Saves the config to the file system.
141 public void Persist()
143 _repo
._internal_repo
.Config
.save();