View Javadoc
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  	 * @param repo
62  	 * @return merge configuration for the current branch of the repository
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  			// ignore
71  		}
72  		// use defaults if branch can't be determined
73  		return new MergeConfig();
74  	}
75  
76  	/**
77  	 * @param branch
78  	 *            short branch name to get the configuration for, as returned
79  	 *            e.g. by {@link Repository#getBranch()}
80  	 * @return a parser for use with {@link Config#get(SectionParser)}
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); //$NON-NLS-1$
97  		commit = !isMergeConfigOptionSet("--no-commit", mergeOptions); //$NON-NLS-1$
98  	}
99  
100 	private MergeConfig() {
101 		fastForwardMode = FastForwardMode.FF;
102 		squash = false;
103 		commit = true;
104 	}
105 
106 	/**
107 	 * @return the fast forward mode configured for this branch
108 	 */
109 	public FastForwardMode getFastForwardMode() {
110 		return fastForwardMode;
111 	}
112 
113 	/**
114 	 * @return true if merges into this branch are configured to be squash
115 	 *         merges, false otherwise
116 	 */
117 	public boolean isSquash() {
118 		return squash;
119 	}
120 
121 	/**
122 	 * @return false if --no-commit is configured for this branch, true
123 	 *         otherwise (event if --squash is configured)
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"); //$NON-NLS-1$
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 }