Mercurial > hg4j
annotate src/org/tmatesoft/hg/internal/ConfigFile.java @ 685:9897cbfd2790
Towards ssh remote repositories: use ganymed library for ssh transport
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 25 Jul 2013 21:32:09 +0200 |
parents | 6526d8adbc0f |
children |
rev | line source |
---|---|
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
1 /* |
608
e1b29756f901
Clean, organize and resolve some TODOs and FIXMEs: minor refactorings and comments
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
498
diff
changeset
|
2 * Copyright (c) 2011-2013 TMate Software Ltd |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
3 * |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
6 * the Free Software Foundation; version 2 of the License. |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
7 * |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
8 * This program is distributed in the hope that it will be useful, |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
11 * GNU General Public License for more details. |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
12 * |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
13 * For information on how to redistribute this software under |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
14 * the terms of a license other than GNU General Public License |
102
a3a2e5deb320
Updated contact address to support@hg4j.com
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
73
diff
changeset
|
15 * contact TMate Software at support@hg4j.com |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
16 */ |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
17 package org.tmatesoft.hg.internal; |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
18 |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
19 import java.io.ByteArrayOutputStream; |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
20 import java.io.File; |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
21 import java.io.FileOutputStream; |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
22 import java.io.IOException; |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
23 import java.io.PrintStream; |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
24 import java.nio.ByteBuffer; |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
25 import java.nio.channels.FileChannel; |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
26 import java.nio.channels.FileLock; |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
27 import java.util.ArrayList; |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
28 import java.util.Collections; |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
29 import java.util.Iterator; |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
30 import java.util.LinkedHashMap; |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
31 import java.util.List; |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
32 import java.util.Map; |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
33 |
628
6526d8adbc0f
Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
608
diff
changeset
|
34 import org.tmatesoft.hg.core.HgIOException; |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
35 import org.tmatesoft.hg.core.SessionContext; |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
36 import org.tmatesoft.hg.util.LogFacility; |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
37 |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
38 /** |
331
a37ce7145c3f
Access to repository configuration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
295
diff
changeset
|
39 * .ini / .rc file reader |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
40 * @author Artem Tikhomirov |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
41 * @author TMate Software Ltd. |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
42 */ |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
43 public class ConfigFile { |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
44 |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
45 private final SessionContext sessionContext; |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
46 private List<String> sections; |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
47 private List<Map<String,String>> content; |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
48 |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
49 public ConfigFile(SessionContext ctx) { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
50 sessionContext = ctx; |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
51 } |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
52 |
628
6526d8adbc0f
Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
608
diff
changeset
|
53 public void addLocation(File path) throws HgIOException { |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
54 read(path); |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
55 } |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
56 |
114
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
57 public boolean hasSection(String sectionName) { |
331
a37ce7145c3f
Access to repository configuration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
295
diff
changeset
|
58 return sections == null ? false : sections.indexOf(sectionName) != -1; |
114
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
59 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
60 |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
61 public List<String> getSectionNames() { |
114
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
62 return sections == null ? Collections.<String>emptyList() : Collections.unmodifiableList(sections); |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
63 } |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
64 |
498
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
487
diff
changeset
|
65 /** |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
487
diff
changeset
|
66 * Access map of section keys and values. |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
487
diff
changeset
|
67 * @param sectionName name of the section to retrieve |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
487
diff
changeset
|
68 * @return never <code>null</code>, empty map in case no section with specified name found |
0205a5c4566b
Issue 38: preserve user formatting and comments when updating configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
487
diff
changeset
|
69 */ |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
70 public Map<String,String> getSection(String sectionName) { |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
71 if (sections == null) { |
114
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
72 return Collections.emptyMap(); |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
73 } |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
74 int x = sections.indexOf(sectionName); |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
75 if (x == -1) { |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
76 return Collections.emptyMap(); |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
77 } |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
78 return Collections.unmodifiableMap(content.get(x)); |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
79 } |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
80 |
114
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
81 public boolean getBoolean(String sectionName, String key, boolean defaultValue) { |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
82 String value = getSection(sectionName).get(key); |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
83 if (value == null) { |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
84 return defaultValue; |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
85 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
86 for (String s : new String[] { "true", "yes", "on", "1" }) { |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
87 if (s.equalsIgnoreCase(value)) { |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
88 return true; |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
89 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
90 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
91 return false; |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
92 } |
128
44b97930570c
Introduced ChangelogHelper to look up changesets files were modified in
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
114
diff
changeset
|
93 |
44b97930570c
Introduced ChangelogHelper to look up changesets files were modified in
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
114
diff
changeset
|
94 public String getString(String sectionName, String key, String defaultValue) { |
44b97930570c
Introduced ChangelogHelper to look up changesets files were modified in
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
114
diff
changeset
|
95 String value = getSection(sectionName).get(key); |
44b97930570c
Introduced ChangelogHelper to look up changesets files were modified in
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
114
diff
changeset
|
96 return value == null ? defaultValue : value; |
44b97930570c
Introduced ChangelogHelper to look up changesets files were modified in
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
114
diff
changeset
|
97 } |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
98 |
487
db48a77ec8ff
Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
483
diff
changeset
|
99 public int getInteger(String sectionName, String key, int defaultValue) { |
db48a77ec8ff
Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
483
diff
changeset
|
100 String value = getSection(sectionName).get(key); |
db48a77ec8ff
Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
483
diff
changeset
|
101 return value == null ? defaultValue : Integer.parseInt(value); |
db48a77ec8ff
Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
483
diff
changeset
|
102 } |
db48a77ec8ff
Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
483
diff
changeset
|
103 |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
104 public void putString(String sectionName, String key, String newValue) { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
105 Map<String, String> section = null; |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
106 if (sections == null) { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
107 // init, in case we didn't read any file with prev config |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
108 sections = new ArrayList<String>(); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
109 content = new ArrayList<Map<String,String>>(); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
110 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
111 int x = sections.indexOf(sectionName); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
112 if (x == -1) { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
113 if (newValue == null) { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
114 return; |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
115 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
116 sections.add(sectionName); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
117 content.add(section = new LinkedHashMap<String, String>()); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
118 } else { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
119 section = content.get(x); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
120 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
121 if (newValue == null) { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
122 section.remove(key); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
123 } else { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
124 section.put(key, newValue); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
125 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
126 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
127 |
628
6526d8adbc0f
Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
608
diff
changeset
|
128 private void read(File f) throws HgIOException { |
114
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
129 if (f == null || !f.canRead()) { |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
130 return; |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
131 } |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
132 if (sections == null) { |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
133 sections = new ArrayList<String>(); |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
134 content = new ArrayList<Map<String,String>>(); |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
135 } |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
136 new Parser().go(f, this); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
137 ((ArrayList<?>) sections).trimToSize(); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
138 ((ArrayList<?>) content).trimToSize(); |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
139 assert sections.size() == content.size(); |
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
140 } |
378
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
141 |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
142 public void writeTo(File f) throws IOException { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
143 byte[] data = compose(); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
144 if (!f.exists()) { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
145 f.createNewFile(); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
146 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
147 FileChannel fc = new FileOutputStream(f).getChannel(); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
148 FileLock fl = fc.lock(); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
149 try { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
150 fc.write(ByteBuffer.wrap(data)); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
151 } finally { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
152 fl.release(); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
153 fc.close(); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
154 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
155 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
156 |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
157 private byte[] compose() { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
158 ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
159 PrintStream ps = new PrintStream(bos); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
160 Iterator<String> sectionNames = sections.iterator(); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
161 for (Map<String,String> s : content) { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
162 final String name = sectionNames.next(); // iterate through names despite section may be empty |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
163 if (s.isEmpty()) { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
164 continue; // do not write an empty section |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
165 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
166 ps.print('['); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
167 ps.print(name); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
168 ps.println(']'); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
169 for (Map.Entry<String, String> e : s.entrySet()) { |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
170 ps.print(e.getKey()); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
171 ps.print('='); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
172 ps.println(e.getValue()); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
173 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
174 ps.println(); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
175 } |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
176 ps.flush(); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
177 return bos.toByteArray(); |
9fb990c8a724
Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
178 } |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
179 |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
180 private static class Parser implements LineReader.LineConsumer<ConfigFile> { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
181 |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
182 private String sectionName = ""; |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
183 private Map<String,String> section = new LinkedHashMap<String, String>(); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
184 private File contextFile; |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
185 |
608
e1b29756f901
Clean, organize and resolve some TODOs and FIXMEs: minor refactorings and comments
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
498
diff
changeset
|
186 // TODO [post-1.1] "" and lists |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
187 // XXX perhaps, single string to keep whole section with substrings for keys/values to minimize number of arrays (String.value) |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
188 public boolean consume(String line, ConfigFile cfg) throws IOException { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
189 int x; |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
190 if ((x = line.indexOf('#')) != -1) { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
191 // do not keep comments in memory, get new, shorter string |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
192 line = new String(line.substring(0, x).trim()); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
193 } |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
194 if (line.length() <= 2) { // a=b or [a] are at least of length 3 |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
195 return true; |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
196 } |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
197 if (line.charAt(0) == '[' && line.charAt(line.length() - 1) == ']') { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
198 sectionName = line.substring(1, line.length() - 1); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
199 if (cfg.sections.indexOf(sectionName) == -1) { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
200 cfg.sections.add(sectionName); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
201 cfg.content.add(section = new LinkedHashMap<String, String>()); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
202 } else { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
203 section = null; // drop cached value |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
204 } |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
205 } else if (line.startsWith("%unset")) { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
206 if (section != null) { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
207 section.remove(line.substring(7).trim()); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
208 } |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
209 } else if (line.startsWith("%include")) { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
210 processInclude(line.substring(9).trim(), cfg); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
211 } else if ((x = line.indexOf('=')) != -1) { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
212 // share char[] of the original string |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
213 String key = line.substring(0, x).trim(); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
214 String value = line.substring(x+1).trim(); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
215 if (section == null) { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
216 int i = cfg.sections.indexOf(sectionName); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
217 assert i >= 0; |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
218 section = cfg.content.get(i); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
219 } |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
220 if (sectionName.length() == 0) { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
221 // add fake section only if there are any values |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
222 cfg.sections.add(sectionName); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
223 cfg.content.add(section); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
224 } |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
225 section.put(key, value); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
226 } |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
227 return true; |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
228 } |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
229 |
628
6526d8adbc0f
Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
608
diff
changeset
|
230 public void go(File f, ConfigFile cfg) throws HgIOException { |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
231 contextFile = f; |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
232 LineReader lr = new LineReader(f, cfg.sessionContext.getLog()); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
233 lr.ignoreLineComments("#"); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
234 lr.read(this, cfg); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
235 } |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
236 |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
237 // include failure doesn't propagate |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
238 private void processInclude(String includeValue, ConfigFile cfg) { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
239 File f; |
608
e1b29756f901
Clean, organize and resolve some TODOs and FIXMEs: minor refactorings and comments
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
498
diff
changeset
|
240 // TODO [post-1.1] handle environment variable expansion |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
241 if (includeValue.startsWith("~/")) { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
242 f = new File(System.getProperty("user.home"), includeValue.substring(2)); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
243 } else { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
244 f = new File(contextFile.getParentFile(), includeValue); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
245 } |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
246 try { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
247 if (f.canRead()) { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
248 new Parser().go(f, cfg); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
249 } else { |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
250 LogFacility lf = cfg.sessionContext.getLog(); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
251 lf.dump(ConfigFile.class, LogFacility.Severity.Debug, "Can't read file to include: %s", f); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
252 } |
628
6526d8adbc0f
Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
608
diff
changeset
|
253 } catch (HgIOException ex) { |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
254 LogFacility lf = cfg.sessionContext.getLog(); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
255 lf.dump(ConfigFile.class, LogFacility.Severity.Warn, "Can't include %s (%s)", f, includeValue); |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
256 } |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
257 } |
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
378
diff
changeset
|
258 } |
69
5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
259 } |