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 package org.eclipse.jgit.merge;
44
45 import java.io.IOException;
46
47 import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
48 import org.eclipse.jgit.lib.Config;
49 import org.eclipse.jgit.lib.Config.SectionParser;
50 import org.eclipse.jgit.lib.ConfigConstants;
51 import org.eclipse.jgit.lib.Repository;
52
53
54
55
56
57
58 public class MergeConfig {
59
60
61
62
63
64
65
66
67 public static MergeConfig getConfigForCurrentBranch(Repository repo) {
68 try {
69 String branch = repo.getBranch();
70 if (branch != null)
71 return repo.getConfig().get(getParser(branch));
72 } catch (IOException e) {
73
74 }
75
76 return new MergeConfig();
77 }
78
79
80
81
82
83
84
85
86
87
88
89 public static final SectionParser<MergeConfig> getParser(
90 final String branch) {
91 return new MergeConfigSectionParser(branch);
92 }
93
94 private final FastForwardMode fastForwardMode;
95
96 private final boolean squash;
97
98 private final boolean commit;
99
100 private MergeConfig(String branch, Config config) {
101 String[] mergeOptions = getMergeOptions(branch, config);
102 fastForwardMode = getFastForwardMode(config, mergeOptions);
103 squash = isMergeConfigOptionSet("--squash", mergeOptions);
104 commit = !isMergeConfigOptionSet("--no-commit", mergeOptions);
105 }
106
107 private MergeConfig() {
108 fastForwardMode = FastForwardMode.FF;
109 squash = false;
110 commit = true;
111 }
112
113
114
115
116
117
118 public FastForwardMode getFastForwardMode() {
119 return fastForwardMode;
120 }
121
122
123
124
125
126
127
128
129 public boolean isSquash() {
130 return squash;
131 }
132
133
134
135
136
137
138
139 public boolean isCommit() {
140 return commit;
141 }
142
143 private static FastForwardMode getFastForwardMode(Config config,
144 String[] mergeOptions) {
145 for (String option : mergeOptions) {
146 for (FastForwardMode mode : FastForwardMode.values())
147 if (mode.matchConfigValue(option))
148 return mode;
149 }
150 FastForwardMode ffmode = FastForwardMode.valueOf(config.getEnum(
151 ConfigConstants.CONFIG_KEY_MERGE, null,
152 ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.TRUE));
153 return ffmode;
154 }
155
156 private static boolean isMergeConfigOptionSet(String optionToLookFor,
157 String[] mergeOptions) {
158 for (String option : mergeOptions) {
159 if (optionToLookFor.equals(option))
160 return true;
161 }
162 return false;
163 }
164
165 private static String[] getMergeOptions(String branch, Config config) {
166 String mergeOptions = config.getString(
167 ConfigConstants.CONFIG_BRANCH_SECTION, branch,
168 ConfigConstants.CONFIG_KEY_MERGEOPTIONS);
169 if (mergeOptions != null)
170 return mergeOptions.split("\\s");
171 else
172 return new String[0];
173 }
174
175 private static class MergeConfigSectionParser implements
176 SectionParser<MergeConfig> {
177
178 private final String branch;
179
180 public MergeConfigSectionParser(String branch) {
181 this.branch = branch;
182 }
183
184 @Override
185 public MergeConfig parse(Config cfg) {
186 return new MergeConfig(branch, cfg);
187 }
188
189 @Override
190 public boolean equals(Object obj) {
191 if (obj instanceof MergeConfigSectionParser)
192 return branch.equals(((MergeConfigSectionParser) obj).branch);
193 else
194 return false;
195 }
196
197 @Override
198 public int hashCode() {
199 return branch.hashCode();
200 }
201
202 }
203
204 }