1 /*
2 * Copyright (C) 2017, 2020 Thomas Wolf <thomas.wolf@paranor.ch> and others
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0 which is available at
6 * https://www.eclipse.org/org/documents/edl-v10.php.
7 *
8 * SPDX-License-Identifier: BSD-3-Clause
9 */
10
11 package org.eclipse.jgit.lib;
12
13 import java.io.File;
14 import java.nio.file.InvalidPathException;
15 import java.nio.file.Path;
16 import java.util.List;
17 import java.util.concurrent.TimeUnit;
18
19 import org.eclipse.jgit.annotations.NonNull;
20 import org.eclipse.jgit.transport.RefSpec;
21 import org.eclipse.jgit.util.FS;
22
23 /**
24 * Something that knows how to convert plain strings from a git {@link Config}
25 * to typed values.
26 *
27 * @since 4.9
28 */
29 public interface TypedConfigGetter {
30
31 /**
32 * Get a boolean value from a git {@link Config}.
33 *
34 * @param config
35 * to get the value from
36 * @param section
37 * section the key is grouped within.
38 * @param subsection
39 * subsection name, such a remote or branch name.
40 * @param name
41 * name of the key to get.
42 * @param defaultValue
43 * default value to return if no value was present.
44 * @return true if any value or defaultValue is true, false for missing or
45 * explicit false
46 */
47 boolean getBoolean(Config config, String section, String subsection,
48 String name, boolean defaultValue);
49
50 /**
51 * Parse an enumeration from a git {@link Config}.
52 *
53 * @param config
54 * to get the value from
55 * @param all
56 * all possible values in the enumeration which should be
57 * recognized. Typically {@code EnumType.values()}.
58 * @param section
59 * section the key is grouped within.
60 * @param subsection
61 * subsection name, such a remote or branch name.
62 * @param name
63 * name of the key to get.
64 * @param defaultValue
65 * default value to return if no value was present.
66 * @return the selected enumeration value, or {@code defaultValue}.
67 */
68 <T extends Enum<?>> T getEnum(Config config, T[] all, String section,
69 String subsection, String name, T defaultValue);
70
71 /**
72 * Obtain an integer value from a git {@link Config}.
73 *
74 * @param config
75 * to get the value from
76 * @param section
77 * section the key is grouped within.
78 * @param subsection
79 * subsection name, such a remote or branch name.
80 * @param name
81 * name of the key to get.
82 * @param defaultValue
83 * default value to return if no value was present.
84 * @return an integer value from the configuration, or defaultValue.
85 */
86 int getInt(Config config, String section, String subsection, String name,
87 int defaultValue);
88
89 /**
90 * Obtain a long value from a git {@link Config}.
91 *
92 * @param config
93 * to get the value from
94 * @param section
95 * section the key is grouped within.
96 * @param subsection
97 * subsection name, such a remote or branch name.
98 * @param name
99 * name of the key to get.
100 * @param defaultValue
101 * default value to return if no value was present.
102 * @return a long value from the configuration, or defaultValue.
103 */
104 long getLong(Config config, String section, String subsection, String name,
105 long defaultValue);
106
107 /**
108 * Parse a numerical time unit, such as "1 minute", from a git
109 * {@link Config}.
110 *
111 * @param config
112 * to get the value from
113 * @param section
114 * section the key is in.
115 * @param subsection
116 * subsection the key is in, or null if not in a subsection.
117 * @param name
118 * the key name.
119 * @param defaultValue
120 * default value to return if no value was present.
121 * @param wantUnit
122 * the units of {@code defaultValue} and the return value, as
123 * well as the units to assume if the value does not contain an
124 * indication of the units.
125 * @return the value, or {@code defaultValue} if not set, expressed in
126 * {@code units}.
127 */
128 long getTimeUnit(Config config, String section, String subsection,
129 String name, long defaultValue, TimeUnit wantUnit);
130
131 /**
132 * Parse a string value from a git {@link Config} and treat it as a file
133 * path, replacing a ~/ prefix by the user's home directory.
134 * <p>
135 * <b>Note:</b> this may throw {@link InvalidPathException} if the string is
136 * not a valid path.
137 * </p>
138 *
139 * @param config
140 * to get the path from.
141 * @param section
142 * section the key is in.
143 * @param subsection
144 * subsection the key is in, or null if not in a subsection.
145 * @param name
146 * the key name.
147 * @param fs
148 * to use to convert the string into a path.
149 * @param resolveAgainst
150 * directory to resolve the path against if it is a relative
151 * path.
152 * @param defaultValue
153 * to return if no value was present
154 * @return the {@link Path}, or {@code defaultValue} if not set
155 * @since 5.10
156 */
157 default Path getPath(Config config, String section, String subsection,
158 String name, @NonNull FS fs, File resolveAgainst,
159 Path defaultValue) {
160 String value = config.getString(section, subsection, name);
161 if (value == null) {
162 return defaultValue;
163 }
164 File file;
165 if (value.startsWith("~/")) { //$NON-NLS-1$
166 file = fs.resolve(fs.userHome(), value.substring(2));
167 } else {
168 file = fs.resolve(resolveAgainst, value);
169 }
170 return file.toPath();
171 }
172
173 /**
174 * Parse a list of {@link RefSpec}s from a git {@link Config}.
175 *
176 * @param config
177 * to get the list from
178 * @param section
179 * section the key is in.
180 * @param subsection
181 * subsection the key is in, or null if not in a subsection.
182 * @param name
183 * the key name.
184 * @return a possibly empty list of
185 * {@link org.eclipse.jgit.transport.RefSpec}s
186 */
187 @NonNull
188 List<RefSpec> getRefSpecs(Config config, String section, String subsection,
189 String name);
190 }