Example usage for org.eclipse.jgit.lib RefUpdate update

List of usage examples for org.eclipse.jgit.lib RefUpdate update

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib RefUpdate update.

Prototype

public Result update() throws IOException 

Source Link

Document

Gracefully update the ref to the new value.

Usage

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 w ww .j  a v  a2 s. c om*/

    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.svn.model.AbstractGitRespositoryTestCase.java

License:Educational Community License

protected Result createBranch(ObjectId objectId, String branchName) throws IOException {

    RefUpdate update = repo.updateRef(Constants.R_HEADS + branchName);

    update.setNewObjectId(objectId);/*from w ww .  j  a  va2  s. co  m*/

    update.setForceUpdate(true);

    return update.update();
}

From source file:org.nbgit.client.CommitBuilder.java

License:Open Source License

private boolean updateRef(RefUpdate ru, ObjectId id) throws IOException {
    ru.setNewObjectId(id);//  w  w  w .j  av a 2 s  . com
    ru.setRefLogMessage(buildReflogMessage(), false);
    ru.update();
    return ru.getOldObjectId() != null ? ru.getResult() == RefUpdate.Result.FAST_FORWARD
            : ru.getResult() == RefUpdate.Result.NEW;
}

From source file:org.test.RewriteGitHistory.java

License:Apache License

/**
 * Apply the commit on the current branch and update the head pointer.
 * //from   w  ww  .  j  a  v  a  2s . com
 * @param commitBuilder
 * @throws IOException
 * @throws NoHeadException
 */
protected ObjectId executeCommit(CommitBuilder commitBuilder) throws NoHeadException, IOException {

    ObjectInserter inserter = repository.getObjectDatabase().newInserter();

    ObjectId newBaseId = null;

    try {
        newBaseId = inserter.insert(commitBuilder);

        inserter.flush();

        Ref head = repository.getRef(Constants.HEAD);

        if (head == null)
            throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);

        // determine the current HEAD and the commit it is referring to
        ObjectId headId = repository.resolve(Constants.HEAD + "^{commit}");
        RevWalk revWalk = new RevWalk(repository);
        try {
            RevCommit newCommit = revWalk.parseCommit(newBaseId);
            RefUpdate ru = repository.updateRef(Constants.HEAD);
            ru.setNewObjectId(newBaseId);
            ru.setRefLogMessage("commit : " + newCommit.getShortMessage(), false);

            ru.setExpectedOldObjectId(headId);
            Result rc = ru.update();

            log.info("rc.type = " + rc.name());

        } finally {
            revWalk.release();
        }

    } finally {
        inserter.release();
    }

    return newBaseId;

}

From source file:playRepository.BareCommit.java

License:Apache License

private void refUpdate(ObjectId commitId, String refName) throws IOException {
    RefUpdate ru = this.repository.updateRef(refName);
    ru.setForceUpdate(false);// ww  w  .java  2s  .  com
    ru.setRefLogIdent(getPersonIdent());
    ru.setNewObjectId(commitId);
    if (hasOldCommit(refName)) {
        ru.setExpectedOldObjectId(getCurrentMomentHeadObjectId());
    }
    ru.setRefLogMessage(getCommitMessage(), false);
    ru.update();
}

From source file:playRepository.GitRepository.java

License:Apache License

/**
 * Clones a local repository./*from  www  . ja v a  2 s .  co m*/
 *
 * This doesn't copy Git objects but hardlink them to save disk space.
 *
 * @param originalProject
 * @param forkProject
 * @throws IOException
 */
protected static void cloneHardLinkedRepository(Project originalProject, Project forkProject)
        throws IOException {
    Repository origin = GitRepository.buildGitRepository(originalProject);
    Repository forked = GitRepository.buildGitRepository(forkProject);
    forked.create();

    final Path originObjectsPath = Paths.get(new File(origin.getDirectory(), "objects").getAbsolutePath());
    final Path forkedObjectsPath = Paths.get(new File(forked.getDirectory(), "objects").getAbsolutePath());

    // Hardlink files .git/objects/ directory to save disk space,
    // but copy .git/info/alternates because the file can be modified.
    SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
        public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws IOException {
            Path newPath = forkedObjectsPath.resolve(originObjectsPath.relativize(file.toAbsolutePath()));
            if (file.equals(forkedObjectsPath.resolve("/info/alternates"))) {
                Files.copy(file, newPath);
            } else {
                FileUtils.mkdirs(newPath.getParent().toFile(), true);
                Files.createLink(newPath, file);
            }
            return java.nio.file.FileVisitResult.CONTINUE;
        }
    };
    Files.walkFileTree(originObjectsPath, visitor);

    // Import refs.
    for (Map.Entry<String, Ref> entry : origin.getAllRefs().entrySet()) {
        RefUpdate updateRef = forked.updateRef(entry.getKey());
        Ref ref = entry.getValue();
        if (ref.isSymbolic()) {
            updateRef.link(ref.getTarget().getName());
        } else {
            updateRef.setNewObjectId(ref.getObjectId());
            updateRef.update();
        }
    }
}

From source file:svnserver.repository.git.GitRepository.java

License:GNU General Public License

/**
 * Create cache for new revisions.//  w  ww.  ja  v  a 2s.c o  m
 *
 * @throws IOException
 * @throws SVNException
 */
public boolean cacheRevisions() throws IOException, SVNException {
    // Fast check.
    lock.readLock().lock();
    try {
        final int lastRevision = revisions.size() - 1;
        if (lastRevision >= 0) {
            final ObjectId lastCommitId = revisions.get(lastRevision).getGitNewCommit();
            final Ref master = repository.getRef(gitBranch);
            if ((master == null) || (master.getObjectId().equals(lastCommitId))) {
                return false;
            }
        }
    } finally {
        lock.readLock().unlock();
    }
    // Real update.
    final ObjectInserter inserter = repository.newObjectInserter();
    lock.writeLock().lock();
    try {
        final Ref master = repository.getRef(gitBranch);
        final List<RevCommit> newRevs = new ArrayList<>();
        final RevWalk revWalk = new RevWalk(repository);
        ObjectId objectId = master.getObjectId();
        while (true) {
            if (revisionByHash.containsKey(objectId)) {
                break;
            }
            final RevCommit commit = revWalk.parseCommit(objectId);
            newRevs.add(commit);
            if (commit.getParentCount() == 0)
                break;
            objectId = commit.getParent(0);
        }
        if (!newRevs.isEmpty()) {
            final long beginTime = System.currentTimeMillis();
            int processed = 0;
            long reportTime = beginTime;
            log.info("Loading revision changes: {} revision", newRevs.size());
            int revisionId = revisions.size();
            ObjectId cacheId = revisions.get(revisions.size() - 1).getCacheCommit();
            for (int i = newRevs.size() - 1; i >= 0; i--) {
                final RevCommit revCommit = newRevs.get(i);
                cacheId = LayoutHelper.createCacheCommit(inserter, cacheId, revCommit, revisionId,
                        Collections.emptyMap());
                inserter.flush();

                processed++;
                long currentTime = System.currentTimeMillis();
                if (currentTime - reportTime > REPORT_DELAY) {
                    log.info("  processed revision: {} ({} rev/sec)", newRevs.size() - i,
                            1000.0f * processed / (currentTime - reportTime));
                    reportTime = currentTime;
                    processed = 0;

                    final RefUpdate refUpdate = repository.updateRef(svnBranch);
                    refUpdate.setNewObjectId(cacheId);
                    refUpdate.update();
                }
                revisionId++;
            }
            final long endTime = System.currentTimeMillis();
            log.info("Revision changes loaded: {} ms", endTime - beginTime);

            final RefUpdate refUpdate = repository.updateRef(svnBranch);
            refUpdate.setNewObjectId(cacheId);
            refUpdate.update();
        }
        return !newRevs.isEmpty();
    } finally {
        lock.writeLock().unlock();
    }
}

From source file:svnserver.repository.git.LayoutHelper.java

License:GNU General Public License

@NotNull
public static Ref initRepository(@NotNull Repository repository, String branch) throws IOException {
    Ref ref = repository.getRef(PREFIX_REF + branch);
    if (ref == null) {
        Ref old = repository.getRef(OLD_CACHE_REF);
        if (old != null) {
            final RefUpdate refUpdate = repository.updateRef(PREFIX_REF + branch);
            refUpdate.setNewObjectId(old.getObjectId());
            refUpdate.update();
        }/* ww w.j  a v  a2 s.co m*/
    }
    if (ref == null) {
        final ObjectId revision = createFirstRevision(repository);
        final RefUpdate refUpdate = repository.updateRef(PREFIX_REF + branch);
        refUpdate.setNewObjectId(revision);
        refUpdate.update();
        ref = repository.getRef(PREFIX_REF + branch);
        if (ref == null) {
            throw new IOException("Can't initialize repository.");
        }
    }
    Ref old = repository.getRef(OLD_CACHE_REF);
    if (old != null) {
        final RefUpdate refUpdate = repository.updateRef(OLD_CACHE_REF);
        refUpdate.setForceUpdate(true);
        refUpdate.delete();
    }
    return ref;
}

From source file:svnserver.repository.git.push.GitPushEmbedded.java

License:GNU General Public License

@Override
public boolean push(@NotNull Repository repository, @NotNull ObjectId ReceiveId, @NotNull String branch,
        @NotNull User userInfo) throws SVNException, IOException {
    final RefUpdate refUpdate = repository.updateRef(branch);
    refUpdate.getOldObjectId();//from w  ww .ja  v  a 2 s. co m
    refUpdate.setNewObjectId(ReceiveId);
    runReceiveHook(repository, refUpdate, preReceive, userInfo);
    runUpdateHook(repository, refUpdate, update, userInfo);
    final RefUpdate.Result result = refUpdate.update();
    switch (result) {
    case REJECTED:
        return false;
    case NEW:
    case FAST_FORWARD:
        runReceiveHook(repository, refUpdate, postReceive, userInfo);
        return true;
    default:
        log.error("Unexpected push error: {}", result);
        throw new SVNException(SVNErrorMessage.create(SVNErrorCode.IO_WRITE_ERROR, result.name()));
    }
}