Example usage for org.eclipse.jgit.lib Repository updateRef

List of usage examples for org.eclipse.jgit.lib Repository updateRef

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository updateRef.

Prototype

@NonNull
public RefUpdate updateRef(String ref, boolean detach) throws IOException 

Source Link

Document

Create a command to update, create or delete a ref in this repository.

Usage

From source file:com.gitblit.utils.JGitUtils.java

License:Apache License

/**
 * Sets the symbolic ref HEAD to the specified target ref. The
 * HEAD will be detached if the target ref is not a branch.
 *
 * @param repository//www.j  a  v  a 2s . c  o  m
 * @param targetRef
 * @return true if successful
 */
public static boolean setHEADtoRef(Repository repository, String targetRef) {
    try {
        // detach HEAD if target ref is not a branch
        boolean detach = !targetRef.startsWith(Constants.R_HEADS);
        RefUpdate.Result result;
        RefUpdate head = repository.updateRef(Constants.HEAD, detach);
        if (detach) { // Tag
            RevCommit commit = getCommit(repository, targetRef);
            head.setNewObjectId(commit.getId());
            result = head.forceUpdate();
        } else {
            result = head.link(targetRef);
        }
        switch (result) {
        case NEW:
        case FORCED:
        case NO_CHANGE:
        case FAST_FORWARD:
            return true;
        default:
            LOGGER.error(MessageFormat.format("{0} HEAD update to {1} returned result {2}",
                    repository.getDirectory().getAbsolutePath(), targetRef, result));
        }
    } catch (Throwable t) {
        error(t, repository, "{0} failed to set HEAD to {1}", targetRef);
    }
    return false;
}

From source file:com.gitblit.utils.JGitUtils.java

License:Apache License

/**
 * Sets the local branch ref to point to the specified commit id.
 *
 * @param repository/* w  w w.j a  v  a  2s  .  co  m*/
 * @param branch
 * @param commitId
 * @return true if successful
 */
public static boolean setBranchRef(Repository repository, String branch, String commitId) {
    String branchName = branch;
    if (!branchName.startsWith(Constants.R_REFS)) {
        branchName = Constants.R_HEADS + branch;
    }

    try {
        RefUpdate refUpdate = repository.updateRef(branchName, false);
        refUpdate.setNewObjectId(ObjectId.fromString(commitId));
        RefUpdate.Result result = refUpdate.forceUpdate();

        switch (result) {
        case NEW:
        case FORCED:
        case NO_CHANGE:
        case FAST_FORWARD:
            return true;
        default:
            LOGGER.error(MessageFormat.format("{0} {1} update to {2} returned result {3}",
                    repository.getDirectory().getAbsolutePath(), branchName, commitId, result));
        }
    } catch (Throwable t) {
        error(t, repository, "{0} failed to set {1} to {2}", branchName, commitId);
    }
    return false;
}

From source file:com.gitblit.utils.JGitUtils.java

License:Apache License

/**
 * Deletes the specified branch ref.//from   w ww  . j  a  v a2s  .  c om
 *
 * @param repository
 * @param branch
 * @return true if successful
 */
public static boolean deleteBranchRef(Repository repository, String branch) {

    try {
        RefUpdate refUpdate = repository.updateRef(branch, false);
        refUpdate.setForceUpdate(true);
        RefUpdate.Result result = refUpdate.delete();
        switch (result) {
        case NEW:
        case FORCED:
        case NO_CHANGE:
        case FAST_FORWARD:
            return true;
        default:
            LOGGER.error(MessageFormat.format("{0} failed to delete to {1} returned result {2}",
                    repository.getDirectory().getAbsolutePath(), branch, result));
        }
    } catch (Throwable t) {
        error(t, repository, "{0} failed to delete {1}", branch);
    }
    return false;
}

From source file:com.google.gerrit.server.edit.ChangeEditUtil.java

License:Apache License

private static void deleteRef(Repository repo, ChangeEdit edit) throws IOException {
    String refName = edit.getRefName();
    RefUpdate ru = repo.updateRef(refName, true);
    ru.setExpectedOldObjectId(edit.getRef().getObjectId());
    ru.setForceUpdate(true);// w w  w  .  j a va2  s  .c o  m
    RefUpdate.Result result = ru.delete();
    switch (result) {
    case FORCED:
    case NEW:
    case NO_CHANGE:
        break;
    default:
        throw new IOException(String.format("Failed to delete ref %s: %s", refName, result));
    }
}

From source file:com.google.gerrit.server.notedb.ChangeRebuilder.java

License:Apache License

private void deleteRef(Change change, Repository changeRepo) throws IOException {
    String refName = ChangeNoteUtil.changeRefName(change.getId());
    RefUpdate ru = changeRepo.updateRef(refName, true);
    ru.setForceUpdate(true);//  w w  w. ja  v a 2 s. c om
    RefUpdate.Result result = ru.delete();
    switch (result) {
    case FORCED:
    case NEW:
    case NO_CHANGE:
        break;
    default:
        throw new IOException(String.format("Failed to delete ref %s: %s", refName, result));
    }
}

From source file:org.jenkinsci.git.RepositoryCheckoutOperation.java

License:Open Source License

public Boolean invoke(File file, VirtualChannel channel) throws IOException, InterruptedException {
    CommitLogWriter writer = new CommitLogWriter(new OutputStreamWriter(log.write()));
    CommitLogWriterFilter filter = new CommitLogWriterFilter(writer);
    StreamProgressMonitor monitor = null;
    if (listener != null)
        monitor = new StreamProgressMonitor(listener.getLogger());
    try {/*from  ww w.ja va 2  s .  c  o m*/
        for (BuildRepository repo : repos) {
            Repository gitRepo = new FileRepositoryOperation(repo).invoke(file, channel);
            RevCommit current = null;
            if (gitRepo == null)
                gitRepo = new InitOperation(repo).invoke(file, channel);
            else
                current = CommitUtils.getLatest(gitRepo);

            RevCommit fetched = new FetchOperation(repo, gitRepo, monitor).call();
            if (fetched == null)
                return false;

            if (current != null)
                new CommitFinder(gitRepo).setFilter(filter).findBetween(fetched, current);
            else
                writer.write(new Commit(gitRepo, fetched));

            new TreeCheckoutOperation(gitRepo, fetched).call();
            RefUpdate refUpdate = gitRepo.updateRef(Constants.HEAD, true);
            refUpdate.setNewObjectId(fetched);
            Result result = refUpdate.forceUpdate();
            if (result == null)
                throw new IOException("Null ref update result");
            switch (result) {
            case NEW:
            case FORCED:
            case FAST_FORWARD:
            case NO_CHANGE:
                // These are the acceptable results
                break;
            default:
                throw new IOException(result.name());
            }
        }
    } finally {
        writer.close();
    }
    return true;
}

From source file:org.kuali.student.git.importer.FixImportRepo.java

License:Educational Community License

private static void updateRef(Repository repo, String branchName, String revision, AnyObjectId branchId)
        throws IOException {

    RefUpdate u = repo.updateRef(branchName, true);

    u.setForceUpdate(true);/*from www . j  ava2 s.co  m*/

    String resultMessage = "resetting branch " + branchName + " to revision " + revision + " at "
            + branchId.name();

    u.setNewObjectId(branchId);

    u.setRefLogMessage(resultMessage, true);

    Result result = u.update();

    log.info(resultMessage + " result = " + result);

}

From source file:org.kuali.student.git.importer.FixImportRepo.java

License:Educational Community License

private static void deleteRef(Repository repo, String branchName, String revision) throws IOException {

    RefUpdate u = repo.updateRef(branchName, true);

    u.setForceUpdate(true);//from   w  w  w  .j  ava2 s.co  m

    String resultMessage = "deleting branch " + branchName + " at revision " + revision;

    u.setRefLogMessage(resultMessage, true);

    Result result = u.delete();

    log.info(resultMessage + " result = " + result);

}