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
|
/*******************************************************************************
* Copyright (C) 2010, Dariusz Luksza <dariusz@luksza.org>
*
* 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.core.op;
import java.io.IOException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.egit.core.CoreText;
import org.eclipse.egit.core.internal.util.ProjectUtil;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.ObjectWriter;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.Tag;
import org.eclipse.jgit.lib.RefUpdate.Result;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.core.TeamException;
/**
* Tags repository with given {@link Tag} object.
*/
public class TagOperation implements IEGitOperation {
private final Tag tag;
private final Repository repo;
private final boolean shouldMoveTag;
/**
* Construct TagOperation
*
* @param repo
* @param tag
* @param shouldMoveTag if <code>true</code> it will replace tag with same name
*/
public TagOperation(Repository repo, Tag tag, boolean shouldMoveTag) {
this.tag = tag;
this.repo = repo;
this.shouldMoveTag = shouldMoveTag;
}
public void execute(IProgressMonitor m) throws CoreException {
IProgressMonitor monitor;
if (m == null)
monitor = new NullProgressMonitor();
else
monitor = m;
try {
monitor.beginTask(NLS.bind(CoreText.TagOperation_performingTagging,
tag.getTag()), 3);
updateTagObject();
monitor.worked(1);
updateRepo();
monitor.worked(1);
ProjectUtil.refreshProjects(repo, SubMonitor.convert(monitor, 1));
} finally {
monitor.done();
}
}
private void updateRepo() throws TeamException {
String refName = Constants.R_TAGS + tag.getTag();
try {
RefUpdate tagRef = repo.updateRef(refName);
tagRef.setNewObjectId(tag.getTagId());
tagRef.setForceUpdate(shouldMoveTag);
Result updateResult = tagRef.update();
if (updateResult != Result.NEW && updateResult != Result.FORCED)
throw new TeamException(NLS.bind(CoreText.TagOperation_taggingFailure,
tag.getTag(), updateResult));
} catch (IOException e) {
throw new TeamException(NLS.bind(CoreText.TagOperation_taggingFailure,
tag.getTag(), e.getMessage()), e);
}
}
private void updateTagObject() throws TeamException {
ObjectId startPointRef = tag.getObjId();
try {
ObjectLoader object = repo.openObject(startPointRef);
tag.setType(Constants.typeString(object.getType()));
ObjectWriter objWriter = new ObjectWriter(repo);
tag.setTagId(objWriter.writeTag(tag));
} catch (IOException e) {
throw new TeamException(NLS.bind(CoreText.TagOperation_objectIdNotFound,
tag.getTag(), e.getMessage()), e);
}
}
public ISchedulingRule getSchedulingRule() {
return null;
}
}
|