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 package org.eclipse.jgit.util;
48
49 import java.io.File;
50 import java.io.IOException;
51 import java.net.InetAddress;
52 import java.net.UnknownHostException;
53 import java.security.AccessController;
54 import java.security.PrivilegedAction;
55 import java.text.DateFormat;
56 import java.text.SimpleDateFormat;
57 import java.util.Locale;
58 import java.util.TimeZone;
59 import java.util.concurrent.atomic.AtomicReference;
60
61 import org.eclipse.jgit.errors.ConfigInvalidException;
62 import org.eclipse.jgit.errors.CorruptObjectException;
63 import org.eclipse.jgit.lib.Config;
64 import org.eclipse.jgit.lib.Constants;
65 import org.eclipse.jgit.lib.ObjectChecker;
66 import org.eclipse.jgit.lib.StoredConfig;
67 import org.eclipse.jgit.storage.file.FileBasedConfig;
68 import org.eclipse.jgit.util.time.MonotonicClock;
69 import org.eclipse.jgit.util.time.MonotonicSystemClock;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
72
73
74
75
76
77
78
79
80
81 public abstract class SystemReader {
82
83 private final static Logger LOG = LoggerFactory
84 .getLogger(SystemReader.class);
85
86 private static final SystemReader DEFAULT;
87
88 private static Boolean isMacOS;
89
90 private static Boolean isWindows;
91
92 static {
93 SystemReader r = new Default();
94 r.init();
95 DEFAULT = r;
96 }
97
98 private static class Default extends SystemReader {
99 private volatile String hostname;
100
101 private AtomicReference<FileBasedConfig> systemConfig = new AtomicReference<>();
102
103 private volatile AtomicReference<FileBasedConfig> userConfig = new AtomicReference<>();
104
105 @Override
106 public String getenv(String variable) {
107 return System.getenv(variable);
108 }
109
110 @Override
111 public String getProperty(String key) {
112 return System.getProperty(key);
113 }
114
115 @Override
116 public FileBasedConfig openSystemConfig(Config parent, FS fs) {
117 FileBasedConfig c = systemConfig.get();
118 if (c == null) {
119 systemConfig.compareAndSet(null,
120 createSystemConfig(parent, fs));
121 c = systemConfig.get();
122 }
123 return c;
124 }
125
126 protected FileBasedConfig createSystemConfig(Config parent, FS fs) {
127 if (StringUtils.isEmptyOrNull(getenv(Constants.GIT_CONFIG_NOSYSTEM_KEY))) {
128 File configFile = fs.getGitSystemConfig();
129 if (configFile != null) {
130 return new FileBasedConfig(parent, configFile, fs);
131 }
132 }
133 return new FileBasedConfig(parent, null, fs) {
134 @Override
135 public void load() {
136
137 }
138
139 @Override
140 public boolean isOutdated() {
141
142 return false;
143 }
144 };
145 }
146
147 @Override
148 public FileBasedConfig openUserConfig(Config parent, FS fs) {
149 FileBasedConfig c = userConfig.get();
150 if (c == null) {
151 userConfig.compareAndSet(null, new FileBasedConfig(parent,
152 new File(fs.userHome(), ".gitconfig"), fs));
153 c = userConfig.get();
154 }
155 return c;
156 }
157
158 @Override
159 public StoredConfig getSystemConfig()
160 throws IOException, ConfigInvalidException {
161 FileBasedConfig c = openSystemConfig(null, FS.DETECTED);
162 if (c.isOutdated()) {
163 LOG.debug("loading system config {}", systemConfig);
164 c.load();
165 }
166 return c;
167 }
168
169 @Override
170 public StoredConfig getUserConfig()
171 throws IOException, ConfigInvalidException {
172 FileBasedConfig c = openUserConfig(getSystemConfig(), FS.DETECTED);
173 if (c.isOutdated()) {
174 LOG.debug("loading user config {}", userConfig);
175 c.load();
176 }
177 return c;
178 }
179
180 @Override
181 public String getHostname() {
182 if (hostname == null) {
183 try {
184 InetAddress localMachine = InetAddress.getLocalHost();
185 hostname = localMachine.getCanonicalHostName();
186 } catch (UnknownHostException e) {
187
188 hostname = "localhost";
189 }
190 assert hostname != null;
191 }
192 return hostname;
193 }
194
195 @Override
196 public long getCurrentTime() {
197 return System.currentTimeMillis();
198 }
199
200 @Override
201 public int getTimezone(long when) {
202 return getTimeZone().getOffset(when) / (60 * 1000);
203 }
204 }
205
206 private static volatile SystemReader INSTANCE = DEFAULT;
207
208
209
210
211
212
213 public static SystemReader getInstance() {
214 return INSTANCE;
215 }
216
217
218
219
220
221
222
223
224 public static void setInstance(SystemReader newReader) {
225 isMacOS = null;
226 isWindows = null;
227 if (newReader == null)
228 INSTANCE = DEFAULT;
229 else {
230 newReader.init();
231 INSTANCE = newReader;
232 }
233 }
234
235 private ObjectChecker platformChecker;
236
237 private void init() {
238
239
240 if (platformChecker == null)
241 setPlatformChecker();
242 }
243
244
245
246
247
248
249 protected final void setPlatformChecker() {
250 platformChecker = new ObjectChecker()
251 .setSafeForWindows(isWindows())
252 .setSafeForMacOS(isMacOS());
253 }
254
255
256
257
258
259
260
261 public abstract String getHostname();
262
263
264
265
266
267
268
269
270 public abstract String getenv(String variable);
271
272
273
274
275
276
277
278
279 public abstract String getProperty(String key);
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294 public abstract FileBasedConfig openUserConfig(Config parent, FS fs);
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311 public abstract FileBasedConfig openSystemConfig(Config parent, FS fs);
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326 public abstract StoredConfig getUserConfig()
327 throws IOException, ConfigInvalidException;
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342 public abstract StoredConfig getSystemConfig()
343 throws IOException, ConfigInvalidException;
344
345
346
347
348
349
350 public abstract long getCurrentTime();
351
352
353
354
355
356
357
358 public MonotonicClock getClock() {
359 return new MonotonicSystemClock();
360 }
361
362
363
364
365
366
367
368
369 public abstract int getTimezone(long when);
370
371
372
373
374
375
376
377 public TimeZone getTimeZone() {
378 return TimeZone.getDefault();
379 }
380
381
382
383
384
385
386
387 public Locale getLocale() {
388 return Locale.getDefault();
389 }
390
391
392
393
394
395
396
397
398
399
400 public SimpleDateFormat getSimpleDateFormat(String pattern) {
401 return new SimpleDateFormat(pattern);
402 }
403
404
405
406
407
408
409
410
411
412
413
414
415 public SimpleDateFormat getSimpleDateFormat(String pattern, Locale locale) {
416 return new SimpleDateFormat(pattern, locale);
417 }
418
419
420
421
422
423
424
425
426
427
428
429
430
431 public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
432 return DateFormat.getDateTimeInstance(dateStyle, timeStyle);
433 }
434
435
436
437
438
439
440 public boolean isWindows() {
441 if (isWindows == null) {
442 String osDotName = getOsName();
443 isWindows = Boolean.valueOf(osDotName.startsWith("Windows"));
444 }
445 return isWindows.booleanValue();
446 }
447
448
449
450
451
452
453 public boolean isMacOS() {
454 if (isMacOS == null) {
455 String osDotName = getOsName();
456 isMacOS = Boolean.valueOf(
457 "Mac OS X".equals(osDotName) || "Darwin".equals(osDotName));
458 }
459 return isMacOS.booleanValue();
460 }
461
462 private String getOsName() {
463 return AccessController.doPrivileged(
464 (PrivilegedAction<String>) () -> getProperty("os.name")
465 );
466 }
467
468
469
470
471
472
473
474
475
476
477 public void checkPath(String path) throws CorruptObjectException {
478 platformChecker.checkPath(path);
479 }
480
481
482
483
484
485
486
487
488
489
490
491
492 public void checkPath(byte[] path) throws CorruptObjectException {
493 platformChecker.checkPath(path, 0, path.length);
494 }
495 }