1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.lib;
11
12 import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;
13 import static org.eclipse.jgit.lib.TypedConfigGetter.UNSET_INT;
14
15 import java.text.MessageFormat;
16
17 import org.eclipse.jgit.api.errors.InvalidConfigurationException;
18 import org.eclipse.jgit.internal.JGitText;
19
20
21
22
23
24
25
26
27 public final class AbbrevConfig {
28 private static final String VALUE_NO = "no";
29
30 private static final String VALUE_AUTO = "auto";
31
32
33
34
35 public static final int MIN_ABBREV = 4;
36
37
38
39
40
41
42
43
44
45
46 public static int capAbbrev(int len) {
47 return Math.min(Math.max(MIN_ABBREV, len),
48 Constants.OBJECT_ID_STRING_LENGTH);
49 }
50
51
52
53
54 public final static AbbrevConfig NO = new AbbrevConfig(
55 Constants.OBJECT_ID_STRING_LENGTH);
56
57
58
59
60
61
62
63
64
65
66 public static AbbrevConfig parseFromConfig(Repository repo)
67 throws InvalidConfigurationException {
68 Config config = repo.getConfig();
69 String value = config.getString(ConfigConstants.CONFIG_CORE_SECTION,
70 null, ConfigConstants.CONFIG_KEY_ABBREV);
71 if (value == null || value.equalsIgnoreCase(VALUE_AUTO)) {
72 return auto(repo);
73 }
74 if (value.equalsIgnoreCase(VALUE_NO)) {
75 return NO;
76 }
77 try {
78 int len = config.getIntInRange(ConfigConstants.CONFIG_CORE_SECTION,
79 ConfigConstants.CONFIG_KEY_ABBREV, MIN_ABBREV,
80 Constants.OBJECT_ID_STRING_LENGTH, UNSET_INT);
81 if (len == UNSET_INT) {
82
83
84
85
86
87
88 len = OBJECT_ID_ABBREV_STRING_LENGTH;
89 }
90 return new AbbrevConfig(len);
91 } catch (IllegalArgumentException e) {
92 throw new InvalidConfigurationException(MessageFormat
93 .format(JGitText.get().invalidCoreAbbrev, value), e);
94 }
95 }
96
97
98
99
100
101
102
103
104
105
106 private static AbbrevConfig auto(Repository repo) {
107 long count = repo.getObjectDatabase().getApproximateObjectCount();
108 if (count == -1) {
109 return new AbbrevConfig(OBJECT_ID_ABBREV_STRING_LENGTH);
110 }
111
112 int len = 63 - Long.numberOfLeadingZeros(count) + 1;
113
114
115
116
117
118 len = (len + 1) / 2;
119
120 return new AbbrevConfig(Math.max(len, OBJECT_ID_ABBREV_STRING_LENGTH));
121 }
122
123
124
125
126
127
128 private int abbrev;
129
130
131
132
133 private AbbrevConfig(int abbrev) {
134 this.abbrev = capAbbrev(abbrev);
135 }
136
137
138
139
140
141
142 public int get() {
143 return abbrev;
144 }
145
146 @Override
147 public String toString() {
148 return Integer.toString(abbrev);
149 }
150 }