-
Notifications
You must be signed in to change notification settings - Fork 147
/
SystemReader.cs
237 lines (204 loc) · 7.67 KB
/
SystemReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.IO;
using System.Net;
using GitSharp.Core.Util;
namespace GitSharp.Core
{
[Serializable]
public enum PlatformType
{
Windows = 1,
Unix = 4,
Xbox = 5,
MacOSX = 6,
Suse = 20,
Ubuntu = 21,
RedHat = 22,
Unknown = 127,
UnixMono = 128
}
[Serializable]
public enum ConfigFileType
{
System = 1,
Global = 2,
Repo = 3
}
public abstract class SystemReader
{
private static SystemReader _instance = new InternalSystemReader();
/** @return the live instance to read system properties. */
public static SystemReader getInstance()
{
return _instance;
}
/**
* @param newReader
* the new instance to use when accessing properties.
*/
public static void setInstance(SystemReader newReader)
{
_instance = newReader;
}
/**
* Gets the hostname of the local host. If no hostname can be found, the
* hostname is set to the default value "localhost".
*
* @return the canonical hostname
*/
public abstract string getHostname();
/**
* @param variable system variable to read
* @return value of the system variable
*/
public abstract string getenv(string variable);
/**
* @param key of the system property to read
* @return value of the system property
*/
public abstract string getProperty(string key);
/**
* @return the git configuration found in the user home
*/
public abstract FileBasedConfig openUserConfig();
/**
* @return the current system time
*/
public abstract long getCurrentTime();
/**
* @param when TODO
* @return the local time zone
*/
public abstract int getTimezone(long when);
/// <summary>
/// Returns Windows, Linux or Mac for identification of the OS in use
/// </summary>
/// <returns>Operating System name</returns>
public abstract PlatformType getOperatingSystem();
/// <summary>
/// Returns the GitSharp configuration file from the OS-dependant location.
/// </summary>
/// <returns></returns>
public abstract FileBasedConfig getConfigFile(ConfigFileType configType);
/// <summary>
/// Returns the GitSharp configuration file based on a user-specified location.
/// </summary>
/// <returns></returns>
public abstract FileBasedConfig getConfigFile(string fileLocation);
private class InternalSystemReader : SystemReader
{
public override string getenv(string variable)
{
return Environment.GetEnvironmentVariable(variable);
}
public override string getProperty(string key)
{
//[java] return System.getProperty(key);
string result = string.Empty;
switch (key)
{
case Constants.OS_USER_NAME_KEY:
using (System.Security.Principal.WindowsIdentity ident = System.Security.Principal.WindowsIdentity.GetCurrent())
{
result = ident.Name;
int index = result.LastIndexOf('\\');
if (result.Length >= index+1)
result = result.Substring(index+1);
break;
}
default:
throw new NotImplementedException("The " + key + " property has not been implemented. This was a Java feature.");
}
return result;
}
public override FileBasedConfig openUserConfig()
{
DirectoryInfo home = FS.userHome();
return new FileBasedConfig(new FileInfo(Path.Combine(home.FullName, ".gitconfig")));
}
public override string getHostname()
{
return Dns.GetHostName();
}
public override long getCurrentTime()
{
return DateTime.UtcNow.ToMillisecondsSinceEpoch();
}
public override int getTimezone(long when)
{
return (int)TimeZone.CurrentTimeZone.GetUtcOffset(when.MillisToUtcDateTime()).TotalMinutes;
}
public override PlatformType getOperatingSystem()
{
OperatingSystem os = Environment.OSVersion;
PlatformID pid = os.Platform;
PlatformType pType = PlatformType.Unknown;
switch (pid)
{
case PlatformID.Win32NT:
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.WinCE:
pType = PlatformType.Windows;
break;
case PlatformID.Unix:
pType = PlatformType.Unix;
break;
//case PlatformID.MacOSX:
// pType = PlatformType.MacOSX;
// break;
//case PlatformID.Xbox:
// pType = PlatformType.Xbox;
// break;
default:
// Mono used 128 as its internal Unix identifier before it was added by MS.
if ((int)pid == (int)PlatformType.UnixMono)
{
pType = PlatformType.Unix;
break;
}
throw new ArgumentException("OS support for '" + os.VersionString + " ' is not implemented.");
}
//Testing should be added here to identify the flavor of *nix in use.
//This is primarily useful because *nix does not have a standardized install location
//or function call to identify "special folders".
if (pType == PlatformType.Unix)
{
}
return pType;
}
public override FileBasedConfig getConfigFile(ConfigFileType configType)
{
string filename = string.Empty;
switch (configType)
{
case ConfigFileType.Global:
filename = Path.Combine(FS.globalHome().FullName, ".gitconfig");
break;
case ConfigFileType.System:
filename = Path.Combine(FS.systemHome().FullName, "gitconfig");
break;
case ConfigFileType.Repo:
filename = Path.Combine(FS.userHome().FullName, Constants.DOT_GIT);
filename = Path.Combine(filename, "config");
break;
default:
throw new ArgumentException("getConfigFile used unknown Config filetype.");
}
FileInfo info = new FileInfo(filename);
if (info != null)
return (new FileBasedConfig(info));
else
throw new FileNotFoundException();
}
public override FileBasedConfig getConfigFile(string fileLocation)
{
FileInfo info = new FileInfo(fileLocation);
if (info != null)
return (new FileBasedConfig(info));
else
throw new FileNotFoundException();
}
}
}
}