You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TypedConfigGetter.java 5.9KB

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