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) throws IOException 

Source Link

Document

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

Usage

From source file:org.nbgit.ui.clone.CloneAction.java

License:Open Source License

public static void doCheckout(Repository repo, Ref branch, OutputLogger logger) throws IOException {

    final GitIndex index = new GitIndex(repo);
    final Commit mapCommit = repo.mapCommit(branch.getObjectId());
    final Tree tree = mapCommit.getTree();
    final RefUpdate u;
    final WorkDirCheckout co;

    u = repo.updateRef(Constants.HEAD);
    u.setNewObjectId(mapCommit.getCommitId());
    u.forceUpdate();//  w  ww  .j av  a 2  s .  c  om

    // checking out files
    co = new WorkDirCheckout(repo, repo.getWorkDir(), index, tree);
    co.checkout();
    // writing index
    index.write();
}

From source file:org.webcat.core.git.GitCloner.java

License:Open Source License

private void doCheckout(Repository repository, Ref branch) throws IOException {
    if (!Constants.HEAD.equals(branch.getName())) {
        RefUpdate refUpdate = repository.updateRef(Constants.HEAD);
        refUpdate.disableRefLog();//from ww  w .  j  a  v a  2 s  .  c o  m
        refUpdate.link(branch.getName());
    }

    RevCommit commit = parseCommit(repository, branch);
    RefUpdate refUpdate = repository.updateRef(Constants.HEAD);
    refUpdate.setNewObjectId(commit);
    refUpdate.forceUpdate();

    DirCache dirCache = repository.lockDirCache();
    DirCacheCheckout checkout = new DirCacheCheckout(repository, dirCache, commit.getTree());
    checkout.checkout();
}

From source file:playRepository.GitRepository.java

License:Apache License

/**
 * Clones a local repository./*from   w w  w  .j  a va 2  s.  c om*/
 *
 * 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.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();//w  ww .j a va  2 s .c  o 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();//w  ww  .  ja va  2 s.  c om
    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()));
    }
}