Use try-with-resource to avoid leaks with RevWalk and TreeWalk
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / commit / CommitHelper.java
blobcc7fcaa897caba82eb04cc76e2033f9339535594
1 /*******************************************************************************
2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2007, Jing Xue <jingxue@digizenstudio.com>
4 * Copyright (C) 2007, Robin Rosenberg <me@lathund.dewire.com>
5 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
6 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
7 * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com>
8 * Copyright (C) 2011, Jens Baumgart <jens.baumgart@sap.com>
10 * All rights reserved. This program and the accompanying materials
11 * are made available under the terms of the Eclipse Public License v1.0
12 * which accompanies this distribution, and is available at
13 * http://www.eclipse.org/legal/epl-v10.html
14 *******************************************************************************/
15 package org.eclipse.egit.ui.internal.commit;
17 import java.io.IOException;
19 import org.eclipse.egit.ui.Activator;
20 import org.eclipse.egit.ui.internal.UIText;
21 import org.eclipse.jgit.lib.Constants;
22 import org.eclipse.jgit.lib.ObjectId;
23 import org.eclipse.jgit.lib.PersonIdent;
24 import org.eclipse.jgit.lib.Repository;
25 import org.eclipse.jgit.lib.RepositoryState;
26 import org.eclipse.jgit.lib.UserConfig;
27 import org.eclipse.jgit.revwalk.RevCommit;
28 import org.eclipse.jgit.revwalk.RevWalk;
29 import org.eclipse.osgi.util.NLS;
30 import org.eclipse.swt.widgets.Text;
32 /**
33 * Helper class for preparing a commit in EGit UI
36 public class CommitHelper {
38 private Repository repository;
40 boolean canCommit;
42 String cannotCommitMessage;
44 private RevCommit previousCommit;
46 String author;
48 String committer;
50 boolean isMergedResolved;
52 boolean isCherryPickResolved;
54 private String commitMessage;
56 /**
57 * @param repository
59 public CommitHelper(Repository repository) {
60 this.repository = repository;
61 calculateCommitInfo();
64 private void calculateCommitInfo() {
65 Repository mergeRepository = null;
66 isMergedResolved = false;
67 isCherryPickResolved = false;
68 RepositoryState state = repository.getRepositoryState();
69 canCommit = state.canCommit();
70 if (!canCommit) {
71 cannotCommitMessage = NLS.bind(UIText.CommitAction_repositoryState,
72 state.getDescription());
73 return;
75 if (state.equals(RepositoryState.MERGING_RESOLVED)) {
76 isMergedResolved = true;
77 mergeRepository = repository;
78 } else if (state.equals(RepositoryState.CHERRY_PICKING_RESOLVED)) {
79 isCherryPickResolved = true;
80 mergeRepository = repository;
82 previousCommit = getHeadCommit(repository);
83 final UserConfig config = repository.getConfig().get(UserConfig.KEY);
84 author = config.getAuthorName();
85 final String authorEmail = config.getAuthorEmail();
86 author = author + " <" + authorEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$
88 committer = config.getCommitterName();
89 final String committerEmail = config.getCommitterEmail();
90 committer = committer + " <" + committerEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$
92 if (isMergedResolved || isCherryPickResolved) {
93 commitMessage = getMergeResolveMessage(mergeRepository);
96 if (isCherryPickResolved) {
97 author = getCherryPickOriginalAuthor(mergeRepository);
101 private static RevCommit getHeadCommit(Repository repository) {
102 RevCommit headCommit = null;
103 try (RevWalk rw = new RevWalk(repository)) {
104 ObjectId parentId = repository.resolve(Constants.HEAD);
105 if (parentId != null)
106 headCommit = rw.parseCommit(parentId);
107 } catch (IOException e) {
108 Activator.handleError(UIText.CommitAction_errorRetrievingCommit, e,
109 true);
111 return headCommit;
114 private String getMergeResolveMessage(Repository mergeRepository) {
115 try {
116 String message = mergeRepository.readMergeCommitMsg();
117 if (message != null)
118 return message;
119 } catch (IOException e) {
120 // Return "Could not find ..." below
122 return NLS.bind(UIText.CommitHelper_couldNotFindMergeMsg,
123 Constants.MERGE_MSG);
126 private static String getCherryPickOriginalAuthor(Repository mergeRepository) {
127 try (RevWalk rw = new RevWalk(mergeRepository)) {
128 ObjectId cherryPickHead = mergeRepository.readCherryPickHead();
129 PersonIdent author = rw.parseCommit(
130 cherryPickHead).getAuthorIdent();
131 return author.getName() + " <" + author.getEmailAddress() + ">"; //$NON-NLS-1$//$NON-NLS-2$
132 } catch (IOException e) {
133 Activator.handleError(UIText.CommitAction_errorRetrievingCommit, e,
134 true);
135 throw new IllegalStateException(e);
140 * @return true if committing is possible
142 public boolean canCommit() {
143 return canCommit;
147 * @return error message if committing is not possible
149 public String getCannotCommitMessage() {
150 return cannotCommitMessage;
154 * @return author
156 public String getAuthor() {
157 return author;
161 * @return committer
163 public String getCommitter() {
164 return committer;
168 * @return commit message
170 public String getCommitMessage() {
171 return commitMessage;
175 * @return isMergedResolved
177 public boolean isMergedResolved() {
178 return isMergedResolved;
182 * @return isCherryPickResolved
184 public boolean isCherryPickResolved() {
185 return isCherryPickResolved;
189 * @return previous commit
191 public RevCommit getPreviousCommit() {
192 return previousCommit;
196 * @return true if amending is allowed
198 public boolean amendAllowed() {
199 return previousCommit != null && repository.getRepositoryState().canAmend();
203 * @param repository
204 * to check
205 * @return true if an empty commit without files is allowed in the
206 * current state
208 public static boolean isCommitWithoutFilesAllowed(Repository repository) {
209 RepositoryState state = repository.getRepositoryState();
210 return state == RepositoryState.MERGING_RESOLVED;
214 * @param repository
215 * @return info related to the HEAD commit
217 public static CommitInfo getHeadCommitInfo(Repository repository) {
218 RevCommit headCommit = getHeadCommit(repository);
219 if (headCommit == null)
220 return null;
221 String commitMessage = headCommit.getFullMessage().replaceAll(
222 "\n", Text.DELIMITER); //$NON-NLS-1$
223 PersonIdent authorIdent = headCommit.getAuthorIdent();
224 String author = authorIdent.getName()
225 + " <" + authorIdent.getEmailAddress() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
226 PersonIdent committerIdent = headCommit.getCommitterIdent();
227 String committer = committerIdent.getName()
228 + " <" + committerIdent.getEmailAddress() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
229 return new CommitInfo(headCommit, author, committer, commitMessage);
233 * Commit Info
236 public static class CommitInfo {
237 private RevCommit commit;
238 private String author;
239 private String committer;
240 private String commitMessage;
243 * @param commit
244 * @param author
245 * @param committer
246 * @param commitMessage
248 public CommitInfo(RevCommit commit, String author, String committer, String commitMessage) {
249 super();
250 this.commit = commit;
251 this.author = author;
252 this.committer = committer;
253 this.commitMessage = commitMessage;
257 * @return commit
259 public RevCommit getCommit() {
260 return commit;
264 * @return author
266 public String getAuthor() {
267 return author;
271 * @return committer
273 public String getCommitter() {
274 return committer;
278 * @return commit message
280 public String getCommitMessage() {
281 return commitMessage;