Git Repositories View: Simple fetch and push
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / actions / TagAction.java
blobef6d198315656eecda10d337a51f5a4c68f14feb
1 /*******************************************************************************
2 * Copyright (C) 2010, Dariusz Luksza <dariusz@luksza.org>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org.eclipse.egit.ui.internal.actions;
11 import java.io.IOException;
12 import java.lang.reflect.InvocationTargetException;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.core.runtime.jobs.Job;
22 import org.eclipse.egit.core.op.TagOperation;
23 import org.eclipse.egit.ui.Activator;
24 import org.eclipse.egit.ui.UIText;
25 import org.eclipse.egit.ui.internal.ValidationUtils;
26 import org.eclipse.egit.ui.internal.decorators.GitLightweightDecorator;
27 import org.eclipse.egit.ui.internal.dialogs.CreateTagDialog;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.dialogs.ErrorDialog;
30 import org.eclipse.jface.dialogs.IDialogConstants;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.jgit.lib.AnyObjectId;
33 import org.eclipse.jgit.lib.Constants;
34 import org.eclipse.jgit.lib.ObjectId;
35 import org.eclipse.jgit.lib.PersonIdent;
36 import org.eclipse.jgit.lib.Ref;
37 import org.eclipse.jgit.lib.Repository;
38 import org.eclipse.jgit.lib.Tag;
39 import org.eclipse.jgit.revwalk.RevSort;
40 import org.eclipse.jgit.revwalk.RevWalk;
41 import org.eclipse.osgi.util.NLS;
43 /**
44 * An action for creating tag.
46 * @see TagOperation
48 public class TagAction extends RepositoryAction {
50 private Repository repo;
52 @Override
53 public boolean isEnabled() {
54 return getRepository(false) != null;
57 @Override
58 protected void execute(IAction action) throws InvocationTargetException,
59 InterruptedException {
60 repo = getRepository(true);
61 if (repo == null)
62 return;
64 if (!repo.getRepositoryState().canCheckout()) {
65 MessageDialog.openError(getShell(),
66 UIText.TagAction_cannotCheckout, NLS.bind(
67 UIText.TagAction_repositoryState, repo
68 .getRepositoryState().getDescription()));
69 return;
72 String currentBranchName;
73 try {
74 currentBranchName = repo.getBranch();
75 } catch (IOException e) {
76 throw new InvocationTargetException(e,
77 UIText.TagAction_cannotGetBranchName);
80 CreateTagDialog dialog = new CreateTagDialog(getShell(),
81 ValidationUtils
82 .getRefNameInputValidator(repo, Constants.R_TAGS),
83 currentBranchName);
85 // get and set commits
86 RevWalk revCommits = getRevCommits();
87 dialog.setRevCommitList(revCommits);
89 // get and set existing tags
90 List<Tag> tags = getRevTags();
91 dialog.setExistingTags(tags);
93 if (dialog.open() != IDialogConstants.OK_ID)
94 return;
96 final Tag tag = new Tag(repo);
97 PersonIdent personIdent = new PersonIdent(repo);
98 String tagName = dialog.getTagName();
100 tag.setTag(tagName);
101 tag.setTagger(personIdent);
102 tag.setMessage(dialog.getTagMessage());
104 ObjectId tagCommit = getTagCommit(dialog.getTagCommit());
105 tag.setObjId(tagCommit);
107 String tagJobName = NLS.bind(UIText.TagAction_creating, tagName);
108 final boolean shouldMoveTag = dialog.shouldOverWriteTag();
110 Job tagJob = new Job(tagJobName) {
111 protected IStatus run(IProgressMonitor monitor) {
112 try {
113 new TagOperation(repo, tag, shouldMoveTag).execute(monitor);
114 } catch (CoreException e) {
115 return Activator.createErrorStatus(
116 UIText.TagAction_taggingFailed, e);
117 } finally {
118 GitLightweightDecorator.refresh();
121 return Status.OK_STATUS;
126 tagJob.setUser(true);
127 tagJob.schedule();
130 private List<Tag> getRevTags() {
131 Collection<Ref> revTags = repo.getTags().values();
132 List<Tag> tags = new ArrayList<Tag>();
133 for (Ref ref : revTags) {
134 try {
135 Tag tag = repo.mapTag(ref.getName());
136 tags.add(tag);
137 } catch (IOException e) {
138 ErrorDialog.openError(getShell(),
139 UIText.TagAction_errorDuringTagging, NLS.bind(
140 UIText.TagAction_errorWhileMappingRevTag, ref
141 .getName()), new Status(IStatus.ERROR,
142 Activator.getPluginId(), e.getMessage(), e));
145 return tags;
148 private RevWalk getRevCommits() {
149 RevWalk revWalk = new RevWalk(repo);
150 revWalk.sort(RevSort.COMMIT_TIME_DESC, true);
151 revWalk.sort(RevSort.BOUNDARY, true);
153 try {
154 AnyObjectId headId = repo.resolve(Constants.HEAD);
155 if (headId != null)
156 revWalk.markStart(revWalk.parseCommit(headId));
157 } catch (IOException e) {
158 ErrorDialog.openError(getShell(),
159 UIText.TagAction_errorDuringTagging,
160 UIText.TagAction_errorWhileGettingRevCommits, new Status(
161 IStatus.ERROR, Activator.getPluginId(), e
162 .getMessage(), e));
165 return revWalk;
168 private ObjectId getTagCommit(ObjectId objectId)
169 throws InvocationTargetException {
170 ObjectId result = null;
171 if (objectId == null) {
172 try {
173 result = repo.resolve(Constants.HEAD);
174 } catch (IOException e) {
175 throw new InvocationTargetException(e,
176 UIText.TagAction_unableToResolveHeadObjectId);
178 } else {
179 result = objectId;
181 return result;