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