Java Code Examples for org.eclipse.jgit.lib.RefUpdate#delete()

The following examples show how to use org.eclipse.jgit.lib.RefUpdate#delete() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitUtils.java    From onedev with MIT License 6 votes vote down vote up
public static void deleteRef(RefUpdate refUpdate) {
	try {
		refUpdate.setForceUpdate(true);
		RefUpdate.Result result = refUpdate.delete();
        if (result == RefUpdate.Result.LOCK_FAILURE
        		&& refUpdate.getExpectedOldObjectId() != null
        		&& !refUpdate.getExpectedOldObjectId().equals(refUpdate.getOldObjectId())) {
        	throw new ObsoleteCommitException(refUpdate.getOldObjectId());
        } else if (result != RefUpdate.Result.FAST_FORWARD && result != RefUpdate.Result.FORCED
        		&& result != RefUpdate.Result.NEW && result != RefUpdate.Result.NO_CHANGE) {
        	throw new RefUpdateException(result);
        } 
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example 2
Source File: DeleteTagCommand.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void run () throws GitException {
    Repository repository = getRepository();
    Ref currentRef = repository.getTags().get(tagName);
    if (currentRef == null) {
        throw new GitException.MissingObjectException(tagName, GitObjectType.TAG);
    }
    String fullName = currentRef.getName();
    try {
        RefUpdate update = repository.updateRef(fullName);
        update.setRefLogMessage("tag deleted", false);
        update.setForceUpdate(true);
        Result deleteResult = update.delete();

        switch (deleteResult) {
            case IO_FAILURE:
            case LOCK_FAILURE:
            case REJECTED:
                throw new GitException.RefUpdateException("Cannot delete tag " + tagName, GitRefUpdateResult.valueOf(deleteResult.name()));
        }
    } catch (IOException ex) {
        throw new GitException(ex);
    }
    
}
 
Example 3
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 6 votes vote down vote up
/** {@inheritDoc} */
   @Override
   public void deleteRef(String refName) throws GitException, InterruptedException {
refName = refName.replace(' ', '_');
try (Repository repo = getRepository()) {
    RefUpdate refUpdate = repo.updateRef(refName);
    // Required, even though this is a forced delete.
    refUpdate.setNewObjectId(repo.exactRef(Constants.HEAD).getObjectId());
    refUpdate.setForceUpdate(true);
    switch (refUpdate.delete()) {
    case NOT_ATTEMPTED:
    case LOCK_FAILURE:
    case REJECTED:
    case REJECTED_CURRENT_BRANCH:
    case IO_FAILURE:
    case RENAMED:
	throw new GitException("Could not delete " + refName);
    }
} catch (IOException e) {
    throw new GitException("Could not delete " + refName, e);
}
   }
 
Example 4
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void prune(RemoteConfig repository) throws GitException {
    try (Repository gitRepo = getRepository()) {
        String remote = repository.getName();
        String prefix = "refs/remotes/" + remote + "/";

        Set<String> branches = listRemoteBranches(remote);

        for (Ref r : new ArrayList<>(gitRepo.getAllRefs().values())) {
            if (r.getName().startsWith(prefix) && !branches.contains(r.getName())) {
                // delete this ref
                RefUpdate update = gitRepo.updateRef(r.getName());
                update.setRefLogMessage("remote branch pruned", false);
                update.setForceUpdate(true);
                update.delete();
            }
        }
    } catch (URISyntaxException | IOException e) {
        throw new GitException(e);
    }
}
 
Example 5
Source File: RepoMerger.java    From git-merge-repos with Apache License 2.0 5 votes vote down vote up
private void deleteOriginalRefs() throws IOException {
	try (RevWalk revWalk = new RevWalk(repository)) {
		Collection<Ref> refs = new ArrayList<>();
		RefDatabase refDatabase = repository.getRefDatabase();
		Map<String, Ref> originalBranches = refDatabase.getRefs("refs/heads/original/");
		Map<String, Ref> originalTags = refDatabase.getRefs("refs/tags/original/");
		refs.addAll(originalBranches.values());
		refs.addAll(originalTags.values());
		for (Ref originalRef : refs) {
			RefUpdate refUpdate = repository.updateRef(originalRef.getName());
			refUpdate.setForceUpdate(true);
			refUpdate.delete(revWalk);
		}
	}
}