View Javadoc
1   /*
2    * Copyright (C) 2011, Robin Stocker <robin@nibor.org>
3    * Copyright (C) 2012, Matthias Sohn <matthias.sohn@sap.com> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.lib;
13  
14  import java.net.URISyntaxException;
15  
16  import org.eclipse.jgit.transport.RefSpec;
17  import org.eclipse.jgit.transport.RemoteConfig;
18  
19  /**
20   * Branch section of a Git configuration file.
21   */
22  public class BranchConfig {
23  
24  	/**
25  	 * Config values for branch.[name].rebase (and pull.rebase).
26  	 *
27  	 * @since 4.5
28  	 */
29  	public enum BranchRebaseMode implements Config.ConfigEnum {
30  
31  		/** Value for rebasing */
32  		REBASE("true"), //$NON-NLS-1$
33  		/** Value for rebasing preserving local merge commits */
34  		PRESERVE("preserve"), //$NON-NLS-1$
35  		/** Value for rebasing interactively */
36  		INTERACTIVE("interactive"), //$NON-NLS-1$
37  		/** Value for not rebasing at all but merging */
38  		NONE("false"); //$NON-NLS-1$
39  
40  		private final String configValue;
41  
42  		private BranchRebaseMode(String configValue) {
43  			this.configValue = configValue;
44  		}
45  
46  		@Override
47  		public String toConfigValue() {
48  			return configValue;
49  		}
50  
51  		@Override
52  		public boolean matchConfigValue(String s) {
53  			return configValue.equals(s);
54  		}
55  	}
56  
57  	/**
58  	 * The value that means "local repository" for {@link #getRemote()}:
59  	 * {@value}
60  	 *
61  	 * @since 3.5
62  	 */
63  	public static final String LOCAL_REPOSITORY = "."; //$NON-NLS-1$
64  
65  	private final Config config;
66  	private final String branchName;
67  
68  	/**
69  	 * Create a new branch config, which will read configuration from config
70  	 * about specified branch.
71  	 *
72  	 * @param config
73  	 *            the config to read from
74  	 * @param branchName
75  	 *            the short branch name of the section to read
76  	 */
77  	public BranchConfig(Config config, String branchName) {
78  		this.config = config;
79  		this.branchName = branchName;
80  	}
81  
82  	/**
83  	 * Get the full tracking branch name
84  	 *
85  	 * @return the full tracking branch name or <code>null</code> if it could
86  	 *         not be determined
87  	 */
88  	public String getTrackingBranch() {
89  		String remote = getRemoteOrDefault();
90  		String mergeRef = getMerge();
91  		if (remote == null || mergeRef == null)
92  			return null;
93  
94  		if (isRemoteLocal())
95  			return mergeRef;
96  
97  		return findRemoteTrackingBranch(remote, mergeRef);
98  	}
99  
100 	/**
101 	 * Get the full remote-tracking branch name
102 	 *
103 	 * @return the full remote-tracking branch name or {@code null} if it could
104 	 *         not be determined. If you also want local tracked branches use
105 	 *         {@link #getTrackingBranch()} instead.
106 	 */
107 	public String getRemoteTrackingBranch() {
108 		String remote = getRemoteOrDefault();
109 		String mergeRef = getMerge();
110 		if (remote == null || mergeRef == null)
111 			return null;
112 
113 		return findRemoteTrackingBranch(remote, mergeRef);
114 	}
115 
116 	/**
117 	 * Whether the "remote" setting points to the local repository (with
118 	 * {@value #LOCAL_REPOSITORY})
119 	 *
120 	 * @return {@code true} if the "remote" setting points to the local
121 	 *         repository (with {@value #LOCAL_REPOSITORY}), false otherwise
122 	 * @since 3.5
123 	 */
124 	public boolean isRemoteLocal() {
125 		return LOCAL_REPOSITORY.equals(getRemote());
126 	}
127 
128 	/**
129 	 * Get the remote this branch is configured to fetch from/push to
130 	 *
131 	 * @return the remote this branch is configured to fetch from/push to, or
132 	 *         {@code null} if not defined
133 	 * @since 3.5
134 	 */
135 	public String getRemote() {
136 		return config.getString(ConfigConstants.CONFIG_BRANCH_SECTION,
137 				branchName, ConfigConstants.CONFIG_KEY_REMOTE);
138 	}
139 
140 	/**
141 	 * Get the name of the upstream branch as it is called on the remote
142 	 *
143 	 * @return the name of the upstream branch as it is called on the remote, or
144 	 *         {@code null} if not defined
145 	 * @since 3.5
146 	 */
147 	public String getMerge() {
148 		return config.getString(ConfigConstants.CONFIG_BRANCH_SECTION,
149 				branchName, ConfigConstants.CONFIG_KEY_MERGE);
150 	}
151 
152 	/**
153 	 * Whether the branch is configured to be rebased
154 	 *
155 	 * @return {@code true} if the branch is configured to be rebased
156 	 * @since 3.5
157 	 */
158 	public boolean isRebase() {
159 		return getRebaseMode() != BranchRebaseMode.NONE;
160 	}
161 
162 	/**
163 	 * Retrieves the config value of branch.[name].rebase.
164 	 *
165 	 * @return the {@link org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode}
166 	 * @since 4.5
167 	 */
168 	public BranchRebaseMode getRebaseMode() {
169 		return config.getEnum(BranchRebaseMode.values(),
170 				ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
171 				ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.NONE);
172 	}
173 
174 	/**
175 	 * Finds the tracked remote tracking branch
176 	 *
177 	 * @param remote
178 	 *            Remote name
179 	 * @param mergeRef
180 	 *            merge Ref of the local branch tracking the remote tracking
181 	 *            branch
182 	 * @return full remote tracking branch name or null
183 	 */
184 	private String findRemoteTrackingBranch(String remote, String mergeRef) {
185 		RemoteConfig remoteConfig;
186 		try {
187 			remoteConfig = new RemoteConfig(config, remote);
188 		} catch (URISyntaxException e) {
189 			return null;
190 		}
191 		for (RefSpec refSpec : remoteConfig.getFetchRefSpecs()) {
192 			if (refSpec.matchSource(mergeRef)) {
193 				RefSpec expanded = refSpec.expandFromSource(mergeRef);
194 				return expanded.getDestination();
195 			}
196 		}
197 		return null;
198 	}
199 
200 	private String getRemoteOrDefault() {
201 		String remote = getRemote();
202 		if (remote == null) {
203 			return Constants.DEFAULT_REMOTE_NAME;
204 		}
205 		return remote;
206 	}
207 }