|
|
@ -66,18 +66,24 @@ import org.eclipse.jgit.revwalk.RevObject; |
|
|
|
import org.eclipse.jgit.revwalk.RevWalk; |
|
|
|
import org.eclipse.jgit.revwalk.RevWalk; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Create/update an annotated tag object. |
|
|
|
* Create/update an annotated tag object or a simple unannotated tag |
|
|
|
* <p> |
|
|
|
* <p> |
|
|
|
* Examples (<code>git</code> is a {@link Git} instance): |
|
|
|
* Examples (<code>git</code> is a {@link Git} instance): |
|
|
|
* <p> |
|
|
|
* <p> |
|
|
|
* Create a new annotated tag for the current commit: |
|
|
|
* Create a new tag for the current commit: |
|
|
|
* |
|
|
|
* |
|
|
|
* <pre> |
|
|
|
* <pre> |
|
|
|
* git.tag().setName("v1.0").setMessage("First stable release").call(); |
|
|
|
* git.tag().setName("v1.0").setMessage("First stable release").call(); |
|
|
|
* </pre> |
|
|
|
* </pre> |
|
|
|
* <p> |
|
|
|
* <p> |
|
|
|
* Use {@link Repository#updateRef(String)} to create a lightweight tag (just a |
|
|
|
* |
|
|
|
* named reference to a commit). |
|
|
|
* <p> |
|
|
|
|
|
|
|
* Create a new unannotated tag for the current commit: |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* <pre> |
|
|
|
|
|
|
|
* git.tag().setName("v1.0").setAnnotated(false).call(); |
|
|
|
|
|
|
|
* </pre> |
|
|
|
|
|
|
|
* <p> |
|
|
|
* |
|
|
|
* |
|
|
|
* @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-tag.html" |
|
|
|
* @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-tag.html" |
|
|
|
* >Git documentation about Tag</a> |
|
|
|
* >Git documentation about Tag</a> |
|
|
@ -95,6 +101,8 @@ public class TagCommand extends GitCommand<Ref> { |
|
|
|
|
|
|
|
|
|
|
|
private boolean forceUpdate; |
|
|
|
private boolean forceUpdate; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean annotated = true; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @param repo |
|
|
|
* @param repo |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -120,13 +128,8 @@ public class TagCommand extends GitCommand<Ref> { |
|
|
|
RepositoryState state = repo.getRepositoryState(); |
|
|
|
RepositoryState state = repo.getRepositoryState(); |
|
|
|
processOptions(state); |
|
|
|
processOptions(state); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RevWalk revWalk = new RevWalk(repo); |
|
|
|
try { |
|
|
|
try { |
|
|
|
// create the tag object
|
|
|
|
|
|
|
|
TagBuilder newTag = new TagBuilder(); |
|
|
|
|
|
|
|
newTag.setTag(name); |
|
|
|
|
|
|
|
newTag.setMessage(message); |
|
|
|
|
|
|
|
newTag.setTagger(tagger); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if no id is set, we should attempt to use HEAD
|
|
|
|
// if no id is set, we should attempt to use HEAD
|
|
|
|
if (id == null) { |
|
|
|
if (id == null) { |
|
|
|
ObjectId objectId = repo.resolve(Constants.HEAD + "^{commit}"); //$NON-NLS-1$
|
|
|
|
ObjectId objectId = repo.resolve(Constants.HEAD + "^{commit}"); //$NON-NLS-1$
|
|
|
@ -134,47 +137,33 @@ public class TagCommand extends GitCommand<Ref> { |
|
|
|
throw new NoHeadException( |
|
|
|
throw new NoHeadException( |
|
|
|
JGitText.get().tagOnRepoWithoutHEADCurrentlyNotSupported); |
|
|
|
JGitText.get().tagOnRepoWithoutHEADCurrentlyNotSupported); |
|
|
|
|
|
|
|
|
|
|
|
newTag.setObjectId(objectId, Constants.OBJ_COMMIT); |
|
|
|
id = revWalk.parseCommit(objectId); |
|
|
|
} else { |
|
|
|
|
|
|
|
newTag.setObjectId(id); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!annotated) { |
|
|
|
|
|
|
|
if (message != null || tagger != null) |
|
|
|
|
|
|
|
throw new JGitInternalException( |
|
|
|
|
|
|
|
JGitText.get().messageAndTaggerNotAllowedInUnannotatedTags); |
|
|
|
|
|
|
|
return updateTagRef(id, revWalk, name, |
|
|
|
|
|
|
|
"SimpleTag[" + name + " : " + id //$NON-NLS-1$ //$NON-NLS-2$
|
|
|
|
|
|
|
|
+ "]"); //$NON-NLS-1$
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// create the tag object
|
|
|
|
|
|
|
|
TagBuilder newTag = new TagBuilder(); |
|
|
|
|
|
|
|
newTag.setTag(name); |
|
|
|
|
|
|
|
newTag.setMessage(message); |
|
|
|
|
|
|
|
newTag.setTagger(tagger); |
|
|
|
|
|
|
|
newTag.setObjectId(id); |
|
|
|
|
|
|
|
|
|
|
|
// write the tag object
|
|
|
|
// write the tag object
|
|
|
|
ObjectInserter inserter = repo.newObjectInserter(); |
|
|
|
ObjectInserter inserter = repo.newObjectInserter(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
ObjectId tagId = inserter.insert(newTag); |
|
|
|
ObjectId tagId = inserter.insert(newTag); |
|
|
|
inserter.flush(); |
|
|
|
inserter.flush(); |
|
|
|
|
|
|
|
|
|
|
|
RevWalk revWalk = new RevWalk(repo); |
|
|
|
String tag = newTag.getTag(); |
|
|
|
try { |
|
|
|
return updateTagRef(tagId, revWalk, tag, newTag.toString()); |
|
|
|
String refName = Constants.R_TAGS + newTag.getTag(); |
|
|
|
|
|
|
|
RefUpdate tagRef = repo.updateRef(refName); |
|
|
|
|
|
|
|
tagRef.setNewObjectId(tagId); |
|
|
|
|
|
|
|
tagRef.setForceUpdate(forceUpdate); |
|
|
|
|
|
|
|
tagRef.setRefLogMessage("tagged " + name, false); //$NON-NLS-1$
|
|
|
|
|
|
|
|
Result updateResult = tagRef.update(revWalk); |
|
|
|
|
|
|
|
switch (updateResult) { |
|
|
|
|
|
|
|
case NEW: |
|
|
|
|
|
|
|
case FORCED: |
|
|
|
|
|
|
|
return repo.getRef(refName); |
|
|
|
|
|
|
|
case LOCK_FAILURE: |
|
|
|
|
|
|
|
throw new ConcurrentRefUpdateException( |
|
|
|
|
|
|
|
JGitText.get().couldNotLockHEAD, |
|
|
|
|
|
|
|
tagRef.getRef(), updateResult); |
|
|
|
|
|
|
|
case REJECTED: |
|
|
|
|
|
|
|
throw new RefAlreadyExistsException( |
|
|
|
|
|
|
|
MessageFormat.format( |
|
|
|
|
|
|
|
JGitText.get().tagAlreadyExists, |
|
|
|
|
|
|
|
newTag.toString())); |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
throw new JGitInternalException(MessageFormat.format( |
|
|
|
|
|
|
|
JGitText.get().updatingRefFailed, refName, |
|
|
|
|
|
|
|
newTag.toString(), updateResult)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
revWalk.release(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
inserter.release(); |
|
|
|
inserter.release(); |
|
|
@ -184,6 +173,35 @@ public class TagCommand extends GitCommand<Ref> { |
|
|
|
throw new JGitInternalException( |
|
|
|
throw new JGitInternalException( |
|
|
|
JGitText.get().exceptionCaughtDuringExecutionOfTagCommand, |
|
|
|
JGitText.get().exceptionCaughtDuringExecutionOfTagCommand, |
|
|
|
e); |
|
|
|
e); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
revWalk.release(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Ref updateTagRef(ObjectId tagId, RevWalk revWalk, |
|
|
|
|
|
|
|
String tagName, String newTagToString) throws IOException, |
|
|
|
|
|
|
|
ConcurrentRefUpdateException, RefAlreadyExistsException { |
|
|
|
|
|
|
|
String refName = Constants.R_TAGS + tagName; |
|
|
|
|
|
|
|
RefUpdate tagRef = repo.updateRef(refName); |
|
|
|
|
|
|
|
tagRef.setNewObjectId(tagId); |
|
|
|
|
|
|
|
tagRef.setForceUpdate(forceUpdate); |
|
|
|
|
|
|
|
tagRef.setRefLogMessage("tagged " + name, false); //$NON-NLS-1$
|
|
|
|
|
|
|
|
Result updateResult = tagRef.update(revWalk); |
|
|
|
|
|
|
|
switch (updateResult) { |
|
|
|
|
|
|
|
case NEW: |
|
|
|
|
|
|
|
case FORCED: |
|
|
|
|
|
|
|
return repo.getRef(refName); |
|
|
|
|
|
|
|
case LOCK_FAILURE: |
|
|
|
|
|
|
|
throw new ConcurrentRefUpdateException( |
|
|
|
|
|
|
|
JGitText.get().couldNotLockHEAD, tagRef.getRef(), |
|
|
|
|
|
|
|
updateResult); |
|
|
|
|
|
|
|
case REJECTED: |
|
|
|
|
|
|
|
throw new RefAlreadyExistsException(MessageFormat.format( |
|
|
|
|
|
|
|
JGitText.get().tagAlreadyExists, newTagToString)); |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
throw new JGitInternalException(MessageFormat.format( |
|
|
|
|
|
|
|
JGitText.get().updatingRefFailed, refName, newTagToString, |
|
|
|
|
|
|
|
updateResult)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -201,7 +219,7 @@ public class TagCommand extends GitCommand<Ref> { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void processOptions(RepositoryState state) |
|
|
|
private void processOptions(RepositoryState state) |
|
|
|
throws InvalidTagNameException { |
|
|
|
throws InvalidTagNameException { |
|
|
|
if (tagger == null) |
|
|
|
if (tagger == null && annotated) |
|
|
|
tagger = new PersonIdent(repo); |
|
|
|
tagger = new PersonIdent(repo); |
|
|
|
if (name == null || !Repository.isValidRefName(Constants.R_TAGS + name)) |
|
|
|
if (name == null || !Repository.isValidRefName(Constants.R_TAGS + name)) |
|
|
|
throw new InvalidTagNameException(MessageFormat.format(JGitText |
|
|
|
throw new InvalidTagNameException(MessageFormat.format(JGitText |
|
|
@ -323,4 +341,22 @@ public class TagCommand extends GitCommand<Ref> { |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @param annotated |
|
|
|
|
|
|
|
* @return {@code this} |
|
|
|
|
|
|
|
* @since 3.0 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public TagCommand setAnnotated(boolean annotated) { |
|
|
|
|
|
|
|
this.annotated = annotated; |
|
|
|
|
|
|
|
return this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @return true if this command will create an annotated tag (default is |
|
|
|
|
|
|
|
* true) |
|
|
|
|
|
|
|
* @since 3.0 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public boolean isAnnotated() { |
|
|
|
|
|
|
|
return annotated; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|