Skip to main content
summaryrefslogtreecommitdiffstats
blob: 20879eee59bfa88b8ccb76c061a5ee1fecf10401 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*******************************************************************************
 * 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.op;

import java.io.IOException;

import static java.lang.String.format;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;

import static org.eclipse.egit.gitflow.Activator.error;
import static org.eclipse.jgit.lib.Constants.*;

import org.eclipse.egit.gitflow.GitFlowRepository;
import org.eclipse.egit.gitflow.WrongGitFlowStateException;
import org.eclipse.egit.gitflow.internal.CoreText;
import org.eclipse.jgit.errors.AmbiguousObjectException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.RevisionSyntaxException;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.osgi.util.NLS;

/**
 * git flow release start
 */
public final class ReleaseStartOperation extends AbstractReleaseOperation {
	private String startCommitSha1;

	private boolean isHead;

	/**
	 * start release from given commit
	 *
	 * @param repository
	 * @param startCommitSha1
	 * @param releaseName
	 */
	public ReleaseStartOperation(GitFlowRepository repository,
			String startCommitSha1, String releaseName) {
		this(repository, startCommitSha1, releaseName, isHead(repository, startCommitSha1));
	}

	/**
	 * start release from HEAD
	 *
	 * @param repository
	 * @param releaseName
	 */
	public ReleaseStartOperation(GitFlowRepository repository,
			String releaseName) {
		this(repository, findHead(repository), releaseName, true);
	}

	private ReleaseStartOperation(GitFlowRepository repository,
			String startCommitSha1, String releaseName, boolean isHead) {
		super(repository, releaseName);
		this.startCommitSha1 = startCommitSha1;
		this.isHead = isHead;
	}

	@Override
	public void execute(IProgressMonitor monitor) throws CoreException {
		String branchName = repository.getConfig().getReleaseBranchName(versionName);

		try {
			if (releaseExists()) {
				throw new CoreException(
						error(format(
								CoreText.ReleaseStartOperation_releaseNameAlreadyExists,
								versionName)));
			}
			if (!repository.isDevelop()) {
				throw new CoreException(
						error(NLS.bind(CoreText.ReleaseStartOperation_notOn, repository.getConfig().getDevelop())));
			}
		} catch (IOException e) {
			throw new CoreException(error(e.getMessage(), e));
		}

		RevCommit commit = repository.findCommit(startCommitSha1);
		if (commit == null) {
			throw new IllegalStateException(NLS.bind(CoreText.ReleaseStartOperation_unableToFindCommit, startCommitSha1));
		}
		start(monitor, branchName, commit);
	}

	/**
	 * @return whether or not the given versionName exists
	 * @throws RevisionSyntaxException
	 * @throws AmbiguousObjectException
	 * @throws IncorrectObjectTypeException
	 * @throws IOException
	 */
	public boolean releaseExists()
			throws RevisionSyntaxException, AmbiguousObjectException,
			IncorrectObjectTypeException, IOException {
		return null != repository.getRepository().resolve(
				R_TAGS + repository.getConfig().getVersionTagPrefix() + versionName);
	}

	@Override
	public ISchedulingRule getSchedulingRule() {
		if (isHead) {
			return null;
		} else {
			return super.getSchedulingRule();
		}
	}

	private static boolean isHead(final GitFlowRepository gfRepo, final String sha1) {
		try {
			RevCommit head = gfRepo.findHead();
			return sha1.equals(head.getName());
		} catch (WrongGitFlowStateException e) {
			return false;
		}
	}

	private static String findHead(GitFlowRepository repository) {
		try {
			return repository.findHead().getName();
		} catch (WrongGitFlowStateException e) {
			throw new IllegalStateException(e);
		}
	}
}

Back to the top