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.craftercms.studio.impl.v1.repository.git.GitContentRepository.java

License:Open Source License

@Override
public String getRepoLastCommitId(final String site) {
    String toReturn = StringUtils.EMPTY;

    synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GitRepositories.GLOBAL : SANDBOX)) {
        Repository repo = helper.getRepository(site, SANDBOX);
        try {/*from  www . ja v  a  2s  . c om*/
            ObjectId commitId = repo.resolve(Constants.HEAD);
            toReturn = commitId.getName();
        } catch (IOException e) {
            logger.error("Error getting last commit ID for site " + site, e);
        }
    }

    return toReturn;
}

From source file:org.craftercms.studio.impl.v1.repository.git.GitContentRepository.java

License:Open Source License

@Override
public String getRepoFirstCommitId(final String site) {
    String toReturn = StringUtils.EMPTY;

    synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GitRepositories.GLOBAL : SANDBOX)) {
        Repository repo = helper.getRepository(site, SANDBOX);
        try (RevWalk rw = new RevWalk(repo)) {
            ObjectId head = repo.resolve(Constants.HEAD);
            RevCommit root = rw.parseCommit(head);
            rw.sort(RevSort.REVERSE);//from   w w w  .j  a v a  2 s .com
            rw.markStart(root);
            ObjectId first = rw.next();
            toReturn = first.getName();
            logger.error("FIRST COMMIT ID !!!: " + toReturn);
        } catch (IOException e) {
            logger.error("Error getting first commit ID for site " + site, e);
        }
    }

    return toReturn;
}

From source file:org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryHelper.java

License:Open Source License

public RevTree getTreeForLastCommit(Repository repository)
        throws AmbiguousObjectException, IncorrectObjectTypeException, IOException, MissingObjectException {
    ObjectId lastCommitId = repository.resolve(Constants.HEAD);

    // a RevWalk allows to walk over commits based on some filtering
    try (RevWalk revWalk = new RevWalk(repository)) {
        RevCommit commit = revWalk.parseCommit(lastCommitId);

        // and using commit's tree find the path
        RevTree tree = commit.getTree();
        return tree;
    }//from   ww  w  .  j a  va2 s.co  m
}

From source file:org.craftercms.studio.impl.v1.util.git.CherryPickCommandEx.java

License:Eclipse Distribution License

/**
 * Executes the {@code Cherry-Pick} command with all the options and
 * parameters collected by the setter methods (e.g. {@link #include(Ref)} of
 * this class. Each instance of this class should only be used for one
 * invocation of the command. Don't call this method twice on an instance.
 *
 * @return the result of the cherry-pick
 * @throws GitAPIException//from  w ww.  ja  v  a 2  s  .c  om
 * @throws WrongRepositoryStateException
 * @throws ConcurrentRefUpdateException
 * @throws UnmergedPathsException
 * @throws NoMessageException
 * @throws NoHeadException
 */
@Override
public CherryPickResult call() throws GitAPIException {
    RevCommit newHead = null;
    List<Ref> cherryPickedRefs = new LinkedList<>();
    checkCallable();

    try (RevWalk revWalk = new RevWalk(repo)) {

        // get the head commit
        Ref headRef = repo.exactRef(Constants.HEAD);
        if (headRef == null)
            throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);

        newHead = revWalk.parseCommit(headRef.getObjectId());

        // loop through all refs to be cherry-picked
        for (Ref src : commits) {
            // get the commit to be cherry-picked
            // handle annotated tags
            ObjectId srcObjectId = src.getPeeledObjectId();
            if (srcObjectId == null)
                srcObjectId = src.getObjectId();
            RevCommit srcCommit = revWalk.parseCommit(srcObjectId);

            Merger merger = strategy.newMerger(repo);
            if (merger.merge(newHead, srcCommit)) {
                if (AnyObjectId.equals(newHead.getTree().getId(), merger.getResultTreeId()))
                    continue;
                DirCacheCheckout dco = new DirCacheCheckout(repo, newHead.getTree(), repo.lockDirCache(),
                        merger.getResultTreeId());
                dco.setFailOnConflict(true);
                dco.checkout();
                if (!noCommit)
                    newHead = new Git(getRepository()).commit().setMessage(srcCommit.getFullMessage())
                            .setReflogComment(reflogPrefix + " " //$NON-NLS-1$
                                    + srcCommit.getShortMessage())
                            .setAuthor(srcCommit.getAuthorIdent()).setNoVerify(true).call();
                cherryPickedRefs.add(src);
            } else {
                return CherryPickResult.CONFLICT;
            }
        }
    } catch (IOException e) {
        throw new JGitInternalException(
                MessageFormat.format(JGitText.get().exceptionCaughtDuringExecutionOfCherryPickCommand, e), e);
    }
    return new CherryPickResult(newHead, cherryPickedRefs);
}

From source file:org.curioswitch.gradle.plugins.ci.CurioGenericCiPlugin.java

License:Open Source License

private static Set<String> computeAffectedFilesForBranch(Git git, String branch, Project rootProject)
        throws IOException {
    String masterRemote = git.getRepository().getRemoteNames().contains("upstream") ? "upstream" : "origin";
    CanonicalTreeParser oldTreeParser = parserForBranch(git,
            git.getRepository().exactRef("refs/remotes/" + masterRemote + "/master"));
    CanonicalTreeParser newTreeParser = parserForBranch(git, git.getRepository().exactRef(Constants.HEAD));
    return computeAffectedFiles(git, oldTreeParser, newTreeParser, rootProject);
}

From source file:org.curioswitch.gradle.release.ReleasePlugin.java

License:Open Source License

private static String determineVersion(Project project) {
    String revisionId;/*from  w  w  w .  j  a  v a  2s.co  m*/
    if (project.hasProperty("curiostack.revisionId")) {
        revisionId = (String) project.property("curiostack.revisionId");
    } else if (System.getenv().containsKey("REVISION_ID")) {
        revisionId = System.getenv("REVISION_ID");
    } else if (!Strings.isNullOrEmpty(System.getenv().get("TAG_NAME"))) {
        revisionId = System.getenv("TAG_NAME");
    } else if (!Strings.isNullOrEmpty(System.getenv().get("BRANCH_NAME"))) {
        revisionId = System.getenv("BRANCH_NAME");
    } else {
        try (Git git = Git.open(project.getRootDir())) {
            revisionId = git.getRepository().resolve(Constants.HEAD).getName().substring(0, 12);
        } catch (IOException e) {
            revisionId = "unknown";
        }
    }

    checkNotNull(revisionId);

    boolean isRelease = "true".equals(project.findProperty("curiostack.release"));

    if (isRelease) {
        return revisionId;
    } else {
        return "0.0.0-" + TIMESTAMP_FORMATTER.format(LocalDateTime.now(ZoneOffset.UTC)) + "-" + revisionId;
    }
}

From source file:org.debian.dependency.sources.JGitSource.java

License:Apache License

private void openRepo(final File location, final String origin) throws IOException, GitAPIException {
    try {//  w ww  .j a  v  a  2s. co  m
        git = Git.open(location);

        if (git.getRepository().isBare()) {
            throw new IllegalArgumentException("Cannot work with bare git repos, convert to a non-bare repo");
        } else if (git.getRepository().getRef(Constants.HEAD).getObjectId() == null) {
            throw new IllegalArgumentException(
                    "Repository must have " + Constants.HEAD + " setup and pointing to a valid commit");
        }
    } catch (RepositoryNotFoundException e) {
        getLogger().debug("No existing git repository found, creating from " + location);
        git = Git.init().setDirectory(location).setBare(false).call();

        git.add().addFilepattern(".").call();

        // we don't want to track other SCM metadata files
        RmCommand removeAction = git.rm();
        for (String pattern : FileUtils.getDefaultExcludes()) {
            if (pattern.startsWith(".git")) {
                continue;
            }
            removeAction.addFilepattern(pattern);
        }
        removeAction.call();

        git.commit().setMessage(COMMIT_MESSAGE_PREFIX + " Import upstream from " + origin).call();
    }
}

From source file:org.debian.dependency.sources.JGitSource.java

License:Apache License

private void setupRepo() throws GitAPIException, IOException {
    if (git.status().call().hasUncommittedChanges()) {
        throw new IllegalStateException("Uncommitted changes detected in tree. Remove before continuing.");
    }/*www . j a  va 2 s  .  c o  m*/

    Ref branchRef = null;
    for (Ref ref : git.branchList().call()) {
        if (WORK_BRANCH.equals(Repository.shortenRefName(ref.getName()))) {
            branchRef = ref;
            break;
        }
    }

    // assume its setup correctly if its there
    if (branchRef != null) {
        return;
    }

    // assume the current branch is the correct one
    Ref headRef = git.getRepository().getRef(Constants.HEAD).getLeaf();
    try {
        // detach head so we don't modify any existing branches
        git.checkout().setName(ObjectId.toString(headRef.getObjectId())).call();

        removeFilesEndsWith(".class", COMMIT_MESSAGE_PREFIX + " Removing class files");
        RevCommit newRef = removeFilesEndsWith(".jar", COMMIT_MESSAGE_PREFIX + " Removing jar files");

        // now all automated setup routines have been accomplished
        git.branchCreate().setName(WORK_BRANCH).setStartPoint(newRef).call();
    } finally {
        // failsafe
        git.getRepository().updateRef(Constants.HEAD).link(headRef.getName());
    }
}

From source file:org.debian.dependency.sources.JGitSource.java

License:Apache License

private RevCommit removeFilesEndsWith(final String endsWith, final String message)
        throws IOException, GitAPIException {
    boolean containsFiles = false;
    RmCommand removeCommand = git.rm();//from w  w  w .  j  a  va 2s.c o m
    DirCache dirCache = git.getRepository().readDirCache();
    for (int i = 0; i < dirCache.getEntryCount(); ++i) {
        String path = dirCache.getEntry(i).getPathString();
        if (path.endsWith(endsWith)) {
            removeCommand.addFilepattern(path);
            containsFiles = true;
        }
    }

    if (containsFiles) {
        removeCommand.call();

        return git.commit().setMessage(message).call();
    }

    RevWalk walk = new RevWalk(git.getRepository());
    try {
        return walk.parseCommit(git.getRepository().resolve(Constants.HEAD));
    } finally {
        walk.release();
    }
}

From source file:org.debian.dependency.sources.TestJGitSource.java

License:Apache License

/** When initializing an existing git repository, we should preserve its history. */
@Test//  w  w w  .  j a  va 2 s  .co m
public void testInitGit() throws Exception {
    assertNull("Branch should not exist before first init", git.getRepository().resolve(WORK_BRANCH));

    source.initialize(directory, ORIGIN);

    assertNotNull("Branch must exist after init", git.getRepository().resolve(WORK_BRANCH));
    Ref headRef = git.getRepository().getRef(Constants.HEAD).getTarget();
    assertEquals("Head should point to the work branch", WORK_BRANCH,
            Repository.shortenRefName(headRef.getName()));
}