1 /*******************************************************************************
2 * Copyright (c) 2014 Konrad Kügler
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 * Holds configuration for merging into a given branch
55 *
56 * @since 3.3
57 */
58 public class MergeConfig {
59
60 /**
61 * Get merge configuration for the current branch of the repository
62 *
63 * @param repo
64 * a {@link org.eclipse.jgit.lib.Repository} object.
65 * @return merge configuration for the current branch of the repository
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 // ignore
74 }
75 // use defaults if branch can't be determined
76 return new MergeConfig();
77 }
78
79 /**
80 * Get a parser for use with
81 * {@link org.eclipse.jgit.lib.Config#get(SectionParser)}
82 *
83 * @param branch
84 * short branch name to get the configuration for, as returned
85 * e.g. by {@link org.eclipse.jgit.lib.Repository#getBranch()}
86 * @return a parser for use with
87 * {@link org.eclipse.jgit.lib.Config#get(SectionParser)}
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); //$NON-NLS-1$
104 commit = !isMergeConfigOptionSet("--no-commit", mergeOptions); //$NON-NLS-1$
105 }
106
107 private MergeConfig() {
108 fastForwardMode = FastForwardMode.FF;
109 squash = false;
110 commit = true;
111 }
112
113 /**
114 * Get the fast forward mode configured for this branch
115 *
116 * @return the fast forward mode configured for this branch
117 */
118 public FastForwardMode getFastForwardMode() {
119 return fastForwardMode;
120 }
121
122 /**
123 * Whether merges into this branch are configured to be squash merges, false
124 * otherwise
125 *
126 * @return true if merges into this branch are configured to be squash
127 * merges, false otherwise
128 */
129 public boolean isSquash() {
130 return squash;
131 }
132
133 /**
134 * Whether {@code --no-commit} option is not set.
135 *
136 * @return {@code false} if --no-commit is configured for this branch,
137 * {@code true} otherwise (even if --squash is configured)
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"); //$NON-NLS-1$
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 }