RepositoryState state = repo.getRepositoryState();
processOptions(state);
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 (id == null) {
ObjectId objectId = repo.resolve(Constants.HEAD + "^{commit}");
if (objectId == null)
throw new NoHeadException(
JGitText.get().tagOnRepoWithoutHEADCurrentlyNotSupported);
newTag.setObjectId(objectId, Constants.OBJ_COMMIT);
} else {
newTag.setObjectId(id);
}
// write the tag object
ObjectInserter inserter = repo.newObjectInserter();
try {
ObjectId tagId = inserter.insert(newTag);
inserter.flush();
RevWalk revWalk = new RevWalk(repo);
try {
String refName = Constants.R_TAGS + newTag.getTag();
RefUpdate tagRef = repo.updateRef(refName);
tagRef.setNewObjectId(tagId);
tagRef.setForceUpdate(forceUpdate);
tagRef.setRefLogMessage("tagged " + name, false);
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();
}