1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.merge;
11
12 import java.io.IOException;
13
14 import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
15 import org.eclipse.jgit.lib.Config;
16 import org.eclipse.jgit.lib.Config.SectionParser;
17 import org.eclipse.jgit.lib.ConfigConstants;
18 import org.eclipse.jgit.lib.Repository;
19
20
21
22
23
24
25 public class MergeConfig {
26
27
28
29
30
31
32
33
34 public static MergeConfig getConfigForCurrentBranch(Repository repo) {
35 try {
36 String branch = repo.getBranch();
37 if (branch != null)
38 return repo.getConfig().get(getParser(branch));
39 } catch (IOException e) {
40
41 }
42
43 return new MergeConfig();
44 }
45
46
47
48
49
50
51
52
53
54
55
56 public static final SectionParser<MergeConfig> getParser(
57 final String branch) {
58 return new MergeConfigSectionParser(branch);
59 }
60
61 private final FastForwardMode fastForwardMode;
62
63 private final boolean squash;
64
65 private final boolean commit;
66
67 private MergeConfig(String branch, Config config) {
68 String[] mergeOptions = getMergeOptions(branch, config);
69 fastForwardMode = getFastForwardMode(config, mergeOptions);
70 squash = isMergeConfigOptionSet("--squash", mergeOptions);
71 commit = !isMergeConfigOptionSet("--no-commit", mergeOptions);
72 }
73
74 private MergeConfig() {
75 fastForwardMode = FastForwardMode.FF;
76 squash = false;
77 commit = true;
78 }
79
80
81
82
83
84
85 public FastForwardMode getFastForwardMode() {
86 return fastForwardMode;
87 }
88
89
90
91
92
93
94
95
96 public boolean isSquash() {
97 return squash;
98 }
99
100
101
102
103
104
105
106 public boolean isCommit() {
107 return commit;
108 }
109
110 private static FastForwardMode getFastForwardMode(Config config,
111 String[] mergeOptions) {
112 for (String option : mergeOptions) {
113 for (FastForwardMode mode : FastForwardMode.values())
114 if (mode.matchConfigValue(option))
115 return mode;
116 }
117 FastForwardMode ffmode = FastForwardMode.valueOf(config.getEnum(
118 ConfigConstants.CONFIG_KEY_MERGE, null,
119 ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.TRUE));
120 return ffmode;
121 }
122
123 private static boolean isMergeConfigOptionSet(String optionToLookFor,
124 String[] mergeOptions) {
125 for (String option : mergeOptions) {
126 if (optionToLookFor.equals(option))
127 return true;
128 }
129 return false;
130 }
131
132 private static String[] getMergeOptions(String branch, Config config) {
133 String mergeOptions = config.getString(
134 ConfigConstants.CONFIG_BRANCH_SECTION, branch,
135 ConfigConstants.CONFIG_KEY_MERGEOPTIONS);
136 if (mergeOptions != null) {
137 return mergeOptions.split("\\s");
138 }
139 return new String[0];
140 }
141
142 private static class MergeConfigSectionParser implements
143 SectionParser<MergeConfig> {
144
145 private final String branch;
146
147 public MergeConfigSectionParser(String branch) {
148 this.branch = branch;
149 }
150
151 @Override
152 public MergeConfig parse(Config cfg) {
153 return new MergeConfig(branch, cfg);
154 }
155
156 @Override
157 public boolean equals(Object obj) {
158 if (obj instanceof MergeConfigSectionParser) {
159 return branch.equals(((MergeConfigSectionParser) obj).branch);
160 }
161 return false;
162 }
163
164 @Override
165 public int hashCode() {
166 return branch.hashCode();
167 }
168
169 }
170
171 }