Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8745328f17d8215784d71974eb939d31aaa695e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*******************************************************************************
 * Copyright (C) 2015, Max Hohenegger <eclipse@hohenegger.eu>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.egit.gitflow;

import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;

import static org.eclipse.egit.gitflow.Activator.error;

/**
 * Checks if name is valid and branch does not exist.
 *
 * @since 4.0
 */
public class BranchNameValidator {
	/**
	 * Characters not allowed in git flow branches.
	 */
	public static final String ILLEGAL_CHARS = "/ "; //$NON-NLS-1$

	/**
	 * @param repository
	 * @param featureName
	 * @return Whether featureName corresponds to existing branch.
	 * @throws CoreException
	 */
	public static boolean featureExists(GitFlowRepository repository,
			String featureName) throws CoreException {
		return branchExists(repository,
				repository.getConfig().getFullFeatureBranchName(featureName));
	}

	/**
	 * @param repository
	 * @param hotfixName
	 * @return Whether hotfixName corresponds to existing branch.
	 * @throws CoreException
	 */
	public static boolean hotfixExists(GitFlowRepository repository,
			String hotfixName) throws CoreException {
		return branchExists(repository,
				repository.getConfig().getFullHotfixBranchName(hotfixName));
	}

	/**
	 * @param repository
	 * @param releaseName
	 * @return Whether releaseName corresponds to existing branch.
	 * @throws CoreException
	 */
	public static boolean releaseExists(GitFlowRepository repository,
			String releaseName) throws CoreException {
		return branchExists(repository,
				repository.getConfig().getFullReleaseBranchName(releaseName));
	}

	private static boolean branchExists(GitFlowRepository repository,
			String fullBranchName) throws CoreException {
		List<Ref> branches;
		try {
			branches = Git.wrap(repository.getRepository()).branchList().call();
		} catch (GitAPIException e) {
			throw new CoreException(error(e.getMessage(), e));
		}
		for (Ref ref : branches) {
			if (fullBranchName.equals(ref.getTarget().getName())) {
				return true;
			}
		}

		return false;
	}

	/**
	 * @param name
	 * @return Whether or not name would be a valid name for a branch.
	 */
	public static boolean isBranchNameValid(String name) {
		return Repository.isValidRefName(Constants.R_HEADS + name);
	}
}

Back to the top