View Javadoc
1   /*******************************************************************************
2    * Copyright (c) 2014 Konrad Kügler and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
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   * Holds configuration for merging into a given branch
22   *
23   * @since 3.3
24   */
25  public class MergeConfig {
26  
27  	/**
28  	 * Get merge configuration for the current branch of the repository
29  	 *
30  	 * @param repo
31  	 *            a {@link org.eclipse.jgit.lib.Repository} object.
32  	 * @return merge configuration for the current branch of the repository
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  			// ignore
41  		}
42  		// use defaults if branch can't be determined
43  		return new MergeConfig();
44  	}
45  
46  	/**
47  	 * Get a parser for use with
48  	 * {@link org.eclipse.jgit.lib.Config#get(SectionParser)}
49  	 *
50  	 * @param branch
51  	 *            short branch name to get the configuration for, as returned
52  	 *            e.g. by {@link org.eclipse.jgit.lib.Repository#getBranch()}
53  	 * @return a parser for use with
54  	 *         {@link org.eclipse.jgit.lib.Config#get(SectionParser)}
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); //$NON-NLS-1$
71  		commit = !isMergeConfigOptionSet("--no-commit", mergeOptions); //$NON-NLS-1$
72  	}
73  
74  	private MergeConfig() {
75  		fastForwardMode = FastForwardMode.FF;
76  		squash = false;
77  		commit = true;
78  	}
79  
80  	/**
81  	 * Get the fast forward mode configured for this branch
82  	 *
83  	 * @return the fast forward mode configured for this branch
84  	 */
85  	public FastForwardMode getFastForwardMode() {
86  		return fastForwardMode;
87  	}
88  
89  	/**
90  	 * Whether merges into this branch are configured to be squash merges, false
91  	 * otherwise
92  	 *
93  	 * @return true if merges into this branch are configured to be squash
94  	 *         merges, false otherwise
95  	 */
96  	public boolean isSquash() {
97  		return squash;
98  	}
99  
100 	/**
101 	 * Whether {@code --no-commit} option is not set.
102 	 *
103 	 * @return {@code false} if --no-commit is configured for this branch,
104 	 *         {@code true} otherwise (even if --squash is configured)
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"); //$NON-NLS-1$
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 }