Example usage for org.eclipse.jgit.lib Constants HEAD

List of usage examples for org.eclipse.jgit.lib Constants HEAD

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Constants HEAD.

Prototype

String HEAD

To view the source code for org.eclipse.jgit.lib Constants HEAD.

Click Source Link

Document

Special name for the "HEAD" symbolic-ref.

Usage

From source file:org.nbgit.util.GitUtils.java

License:Open Source License

/**
 * Loads the file in specified revision.
 *
 * @return null if the file does not exist in given revision
 *//*  www.  j  a va  2 s  .co m*/
public static File getFileRevision(File base, String revision) throws IOException {
    if (revision.equals("-1")) {
        return null;
    }
    if (GitRepository.REVISION_CURRENT.equals(revision)) {
        return base;
    }
    if (GitRepository.REVISION_BASE.equals(revision)) {
        revision = Constants.HEAD;
    }
    File tempFile = File.createTempFile("tmp", "-" + base.getName()); //NOI18N
    File repository = Git.getInstance().getTopmostManagedParent(base);

    CheckoutBuilder.create(repository).revision(revision).file(base, tempFile).checkout();
    if (tempFile.length() == 0) {
        return null;
    }
    return tempFile;
}

From source file:org.omegat.core.team2.impl.GITRemoteRepository2.java

License:Open Source License

private boolean indexIsEmpty(DirCache dc) throws Exception {
    DirCacheIterator dci = new DirCacheIterator(dc);
    AbstractTreeIterator old = prepareTreeParser(repository, repository.resolve(Constants.HEAD));
    try (Git git = new Git(repository)) {
        List<DiffEntry> diffs = git.diff().setOldTree(old).setNewTree(dci).call();
        return diffs.isEmpty();
    }/* w  w w  .ja va2 s.c  om*/
}

From source file:org.openehr.designer.repository.git.GitArchetypeRepository.java

License:Open Source License

@Override
public List<DiffItem> status() {
    try {//from   w  ww .  ja  v a  2 s .  c  o m
        AbstractTreeIterator oldTreeParser = prepareTreeParser(git.getRepository().resolve(Constants.HEAD));
        List<DiffEntry> diff = git.diff().setOldTree(oldTreeParser).call();

        List<DiffItem> result = new ArrayList<>();
        for (DiffEntry entry : diff) {
            DiffItem item = new DiffItem();
            item.setPath(firstNonNull(entry.getOldPath(), entry.getNewPath()));
            item.setChangeType(convertChangeType(entry.getChangeType()));
            result.add(item);
        }
        return result;
    } catch (IOException | GitAPIException e) {
        throw new ArchetypeRepositoryScmException("Couldn't diff", e);
    }
}

From source file:org.openengsb.connector.git.internal.GitServiceImpl.java

License:Apache License

@Override
public List<CommitRef> update() {
    List<CommitRef> commits = new ArrayList<CommitRef>();
    try {/*from   w w  w .java  2  s  .  c om*/
        if (repository == null) {
            prepareWorkspace();
            initRepository();
        }
        Git git = new Git(repository);
        AnyObjectId oldHead = repository.resolve(Constants.HEAD);
        if (oldHead == null) {
            LOGGER.debug("Local repository is empty. Fetching remote repository.");
            FetchResult fetchResult = doRemoteUpdate();
            if (fetchResult.getTrackingRefUpdate(Constants.R_REMOTES + "origin/" + watchBranch) == null) {
                LOGGER.debug("Nothing to fetch from remote repository.");
                return null;
            }
            doCheckout(fetchResult);
        } else {
            LOGGER.debug("Local repository exists. Pulling remote repository.");
            git.pull().call();
        }
        AnyObjectId newHead = repository.resolve(Constants.HEAD);
        if (newHead == null) {
            LOGGER.debug("New HEAD of local repository doesnt exist.");
            return null;
        }
        if (newHead != oldHead) {
            LogCommand logCommand = git.log();
            if (oldHead == null) {
                LOGGER.debug("Retrieving revisions from HEAD [{}] on", newHead.name());
                logCommand.add(newHead);
            } else {
                LOGGER.debug("Retrieving revisions in range [{}, {}]", newHead.name(), oldHead.name());
                logCommand.addRange(oldHead, newHead);
            }
            Iterable<RevCommit> revisions = logCommand.call();
            for (RevCommit revision : revisions) {
                commits.add(new GitCommitRef(revision));
            }
        }
    } catch (Exception e) {
        throw new ScmException(e);
    }
    return commits;
}

From source file:org.openengsb.connector.git.internal.GitServiceImpl.java

License:Apache License

protected void doCheckout(FetchResult fetchResult) throws IOException {
    final Ref head = fetchResult.getAdvertisedRef(Constants.R_HEADS + watchBranch);
    final RevWalk rw = new RevWalk(repository);
    final RevCommit mapCommit;
    try {/*from w ww  .j  a v  a2  s .c o  m*/
        LOGGER.debug("Mapping received reference to respective commit.");
        mapCommit = rw.parseCommit(head.getObjectId());
    } finally {
        rw.release();
    }

    final RefUpdate u;

    boolean detached = !head.getName().startsWith(Constants.R_HEADS);
    LOGGER.debug("Updating HEAD reference to revision [{}]", mapCommit.getId().name());
    u = repository.updateRef(Constants.HEAD, detached);
    u.setNewObjectId(mapCommit.getId());
    u.forceUpdate();

    DirCacheCheckout dirCacheCheckout = new DirCacheCheckout(repository, null, repository.lockDirCache(),
            mapCommit.getTree());
    dirCacheCheckout.setFailOnConflict(true);
    boolean checkoutResult = dirCacheCheckout.checkout();
    LOGGER.debug("Checked out new repository revision to working directory");
    if (!checkoutResult) {
        throw new IOException("Internal error occured on checking out files");
    }
}

From source file:org.openengsb.connector.git.internal.GitServiceImpl.java

License:Apache License

@Override
public File export(CommitRef ref) {
    RevWalk rw = null;//  www . jav a  2  s.com
    File tmp = null;
    try {
        if (repository == null) {
            initRepository();
        }

        LOGGER.debug("Resolving HEAD and reference [{}]", ref.getStringRepresentation());
        AnyObjectId headId = repository.resolve(Constants.HEAD);
        AnyObjectId refId = repository.resolve(ref.getStringRepresentation());
        if (headId == null || refId == null) {
            throw new ScmException("HEAD or reference [" + ref.getStringRepresentation() + "] doesn't exist.");
        }
        rw = new RevWalk(repository);
        RevCommit head = rw.parseCommit(headId);
        RevCommit commit = rw.parseCommit(refId);

        LOGGER.debug("Checking out working copy of revision");
        checkoutIndex(commit);

        tmp = File.createTempFile("repository", ".zip");
        LOGGER.debug("Exporting repository to archive");
        ZipArchiveOutputStream zos = new ZipArchiveOutputStream(tmp);
        packRepository(localWorkspace, zos);
        zos.close();

        LOGGER.debug("Checking out working copy of former HEAD revision");
        checkoutIndex(head);
    } catch (IOException e) {
        throw new ScmException(e);
    } finally {
        if (rw != null) {
            rw.release();
        }
    }
    return tmp;
}

From source file:org.openengsb.connector.git.internal.GitServiceImpl.java

License:Apache License

@Override
public boolean exists(String arg0) {
    try {/*from w w w. j a  va  2s . c  o m*/
        AnyObjectId id = repository.resolve(Constants.HEAD);
        RevCommit commit = new RevWalk(repository).parseCommit(id);
        LOGGER.debug("Looking up file {} in HEAD revision", arg0);
        TreeWalk treeWalk = TreeWalk.forPath(repository, arg0, new AnyObjectId[] { commit.getTree() });
        if (treeWalk == null) {
            return false;
        }
        ObjectId objectId = treeWalk.getObjectId(treeWalk.getTreeCount() - 1);
        LOGGER.debug("File {} received commit id {} at commit", arg0, objectId.name());
        return !objectId.equals(ObjectId.zeroId());
    } catch (Exception e) {
        throw new ScmException(e);
    }
}

From source file:org.openengsb.connector.git.internal.GitServiceImpl.java

License:Apache License

@Override
public File get(String file) {
    try {// www.  jav  a  2 s  .com
        if (repository == null) {
            initRepository();
        }
        AnyObjectId id = repository.resolve(Constants.HEAD);
        RevCommit commit = new RevWalk(repository).parseCommit(id);
        LOGGER.debug("Looking up file {} in HEAD revision", file);
        TreeWalk treeWalk = TreeWalk.forPath(repository, file, new AnyObjectId[] { commit.getTree() });
        if (treeWalk == null) {
            return null;
        }
        ObjectId objectId = treeWalk.getObjectId(treeWalk.getTreeCount() - 1);
        if (objectId == ObjectId.zeroId()) {
            LOGGER.debug("File {} couldn't be found in HEAD revision", file);
            return null;
        }
        String fileName = getFilename(file);
        LOGGER.debug("Creating file from saved repository content");
        File tmp = File.createTempFile(fileName, null);
        tmp.deleteOnExit();
        OutputStream os = new FileOutputStream(tmp);
        os.write(repository.open(objectId).getCachedBytes());
        os.close();
        return tmp;
    } catch (Exception e) {
        throw new ScmException(e);
    }
}

From source file:org.openengsb.connector.git.internal.GitServiceImpl.java

License:Apache License

@Override
public CommitRef getHead() {
    try {//from   w  w w  . ja v  a  2 s .c  om
        if (repository == null) {
            initRepository();
        }
        AnyObjectId id = repository.resolve(Constants.HEAD);
        RevCommit commit = new RevWalk(repository).parseCommit(id);
        LOGGER.debug("Resolved HEAD to commit {}", commit.getId().name());
        return new GitCommitRef(commit);
    } catch (IOException e) {
        if (repository != null) {
            repository.close();
            repository = null;
        }
        throw new ScmException(e);
    }
}

From source file:org.openengsb.connector.git.internal.GitServiceImplTest.java

License:Apache License

@Test
public void updateWithEmptyWorkspace_shouldCloneRemoteRepository() throws Exception {
    List<CommitRef> commits = service.update();
    assertThat(commits.size(), is(1));/*from  w  ww . j  av  a  2 s  .  co m*/
    ObjectId remote = service.getRepository().resolve("refs/remotes/origin/master");
    assertThat(remote, notNullValue());
    assertThat(remote, is(remoteRepository.resolve("refs/heads/master")));
    assertThat(commits.get(0).getStringRepresentation(),
            is(service.getRepository().resolve(Constants.HEAD).name()));
}