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
;
44 * An action for creating tag.
48 public class TagAction
extends RepositoryAction
{
50 private Repository repo
;
53 public boolean isEnabled() {
54 return getRepository(false) != null;
58 protected void execute(IAction action
) throws InvocationTargetException
,
59 InterruptedException
{
60 repo
= getRepository(true);
64 if (!repo
.getRepositoryState().canCheckout()) {
65 MessageDialog
.openError(getShell(),
66 UIText
.TagAction_cannotCheckout
, NLS
.bind(
67 UIText
.TagAction_repositoryState
, repo
68 .getRepositoryState().getDescription()));
72 String currentBranchName
;
74 currentBranchName
= repo
.getBranch();
75 } catch (IOException e
) {
76 throw new InvocationTargetException(e
,
77 UIText
.TagAction_cannotGetBranchName
);
80 CreateTagDialog dialog
= new CreateTagDialog(getShell(),
82 .getRefNameInputValidator(repo
, Constants
.R_TAGS
),
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
)
96 final Tag tag
= new Tag(repo
);
97 PersonIdent personIdent
= new PersonIdent(repo
);
98 String tagName
= dialog
.getTagName();
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
) {
113 new TagOperation(repo
, tag
, shouldMoveTag
).execute(monitor
);
114 } catch (CoreException e
) {
115 return Activator
.createErrorStatus(
116 UIText
.TagAction_taggingFailed
, e
);
118 GitLightweightDecorator
.refresh();
121 return Status
.OK_STATUS
;
126 tagJob
.setUser(true);
130 private List
<Tag
> getRevTags() {
131 Collection
<Ref
> revTags
= repo
.getTags().values();
132 List
<Tag
> tags
= new ArrayList
<Tag
>();
133 for (Ref ref
: revTags
) {
135 Tag tag
= repo
.mapTag(ref
.getName());
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
));
148 private RevWalk
getRevCommits() {
149 RevWalk revWalk
= new RevWalk(repo
);
150 revWalk
.sort(RevSort
.COMMIT_TIME_DESC
, true);
151 revWalk
.sort(RevSort
.BOUNDARY
, true);
154 AnyObjectId headId
= repo
.resolve(Constants
.HEAD
);
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
168 private ObjectId
getTagCommit(ObjectId objectId
)
169 throws InvocationTargetException
{
170 ObjectId result
= null;
171 if (objectId
== null) {
173 result
= repo
.resolve(Constants
.HEAD
);
174 } catch (IOException e
) {
175 throw new InvocationTargetException(e
,
176 UIText
.TagAction_unableToResolveHeadObjectId
);