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.debian.dependency.sources.TestJGitSource.java

License:Apache License

/** The second time a repository is setup and its on the correct branch, no changes should have to be made. */
@Test/*w ww .  j  a  va2  s.co  m*/
public void testInitGitSecondClean() throws Exception {
    Ref workRef = git.branchCreate().setName(WORK_BRANCH).call();

    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()));
    assertEquals("Work branch should not have moved", workRef.getObjectId(), headRef.getObjectId());
}

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

License:Apache License

/** On first initialization of a repository with bad files, they should be removed. */
@Test//from ww w  .  j  a v a 2  s . co m
public void testInitGitWithBadFiles() throws Exception {
    File.createTempFile("SomeClass", ".class", directory);
    File.createTempFile("some-jar", ".jar", directory);

    File subdir = new File(directory, "subdir");
    subdir.mkdirs();
    File.createTempFile("AnotherClass", ".class", subdir);
    File.createTempFile("another-jar", ".jar", subdir);

    git.add().addFilepattern(".").call();
    git.commit().setMessage("Add bad files for test").call();

    Ref oldHead = git.getRepository().getRef(Constants.HEAD).getLeaf();

    source.initialize(directory, ORIGIN);

    DirCache dirCache = git.getRepository().readDirCache();
    for (int i = 0; i < dirCache.getEntryCount(); ++i) {
        assertThat(dirCache.getEntry(i).getPathString(), not(endsWith(".jar")));
        assertThat(dirCache.getEntry(i).getPathString(), not(endsWith(".class")));
    }

    Ref oldBranch = git.getRepository().getRef(Repository.shortenRefName(oldHead.getName()));
    assertEquals("Keep history that jars were there", oldHead.getObjectId(), oldBranch.getObjectId());
}

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

License:Apache License

/**
 * If there are bad files on another branch, but the repository has been setup, i.e. bad files removed on the work branch,
 * then we should ignore them.//from   www . j av a 2s .c  o  m
 */
@Test
public void testInitBadFilesSecond() throws Exception {
    File.createTempFile("SomeClass", ".class", directory);
    File.createTempFile("some-jar", ".jar", directory);

    File subdir = new File(directory, "subdir");
    subdir.mkdirs();
    File.createTempFile("AnotherClass", ".class", subdir);
    File.createTempFile("another-jar", ".jar", subdir);

    git.add().addFilepattern(".").call();
    git.commit().setMessage("Add bad files for test").call();

    Ref oldHead = git.getRepository().getRef(Constants.HEAD).getLeaf();

    // setup work branch with bad files
    git.checkout().setCreateBranch(true).setName(WORK_BRANCH).call();

    RmCommand removeCommand = git.rm();
    DirCache dirCache = git.getRepository().readDirCache();
    for (int i = 0; i < dirCache.getEntryCount(); ++i) {
        String path = dirCache.getEntry(i).getPathString();
        if (path.endsWith(".jar") || path.endsWith(".class")) {
            removeCommand.addFilepattern(path);
        }
    }
    removeCommand.call();
    git.commit().setMessage("Remove bad jars for testing").call();

    // perform test
    source.initialize(directory, ORIGIN);

    dirCache = git.getRepository().readDirCache();
    for (int i = 0; i < dirCache.getEntryCount(); ++i) {
        assertThat(dirCache.getEntry(i).getPathString(), not(endsWith(".jar")));
        assertThat(dirCache.getEntry(i).getPathString(), not(endsWith(".class")));
    }

    Ref oldBranch = git.getRepository().getRef(Repository.shortenRefName(oldHead.getName()));
    assertEquals("Keep history that jars were there", oldHead.getObjectId(), oldBranch.getObjectId());
}

From source file:org.dstadler.jgit.api.JGitPrintContent.java

License:BSD License

public static void main(String[] args) throws Exception {
    File gitWorkDir = new File("C:\\Users\\kishore\\git\\JavaRepos\\.git");
    Git git = Git.open(gitWorkDir);//ww w.  j a  v  a 2  s  . c o m
    Repository repo = git.getRepository();

    ObjectId lastCommitId = repo.resolve(Constants.HEAD);

    RevWalk revWalk = new RevWalk(repo);
    RevCommit commit = revWalk.parseCommit(lastCommitId);

    RevTree tree = commit.getTree();

    TreeWalk treeWalk = new TreeWalk(repo);
    treeWalk.addTree(tree);
    treeWalk.setRecursive(true);
    treeWalk.setFilter(PathFilter.ALL);
    if (!treeWalk.next()) {
        System.out.println("Nothing found!");
        return;
    }

    ObjectId objectId = treeWalk.getObjectId(0);
    System.err.println(treeWalk.getPathString());
    System.err.println(objectId.getName());

    ObjectLoader loader = repo.open(objectId);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    loader.copyTo(out);
    System.out.println("file1.txt:\n" + out.toString());
}

From source file:org.dstadler.jgit.unfinished.PullRemoteRepository.java

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    try (Repository repository = cloneRepository()) {
        System.out.println("Having repository: " + repository.getDirectory() + " with head: "
                + repository.findRef(Constants.HEAD) + "/" + repository.resolve("HEAD") + "/"
                + repository.resolve("refs/heads/master"));

        // TODO: why do we get null here for HEAD?!? See also
        // http://stackoverflow.com/questions/17979660/jgit-pull-noheadexception

        try (Git git = new Git(repository)) {
            PullResult call = git.pull().call();

            System.out.println("Pulled from the remote repository: " + call);
        }// w w w.j  a  v a2  s .  co m
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public List<Branch> branchList(BranchListRequest request) throws GitException {
    String listMode = request.getListMode();
    if (listMode != null && !BranchListRequest.LIST_ALL.equals(listMode)
            && !BranchListRequest.LIST_REMOTE.equals(listMode)) {
        throw new IllegalArgumentException(String.format(ERROR_UNSUPPORTED_LIST_MODE, listMode));
    }/*from   w w  w. j  a v  a  2s .com*/

    ListBranchCommand listBranchCommand = getGit().branchList();
    if (BranchListRequest.LIST_ALL.equals(listMode)) {
        listBranchCommand.setListMode(ListMode.ALL);
    } else if (BranchListRequest.LIST_REMOTE.equals(listMode)) {
        listBranchCommand.setListMode(ListMode.REMOTE);
    }
    List<Ref> refs;
    String currentRef;
    try {
        refs = listBranchCommand.call();
        String headBranch = getRepository().getBranch();
        Optional<Ref> currentTag = getGit().tagList().call().stream()
                .filter(tag -> tag.getObjectId().getName().equals(headBranch)).findFirst();
        if (currentTag.isPresent()) {
            currentRef = currentTag.get().getName();
        } else {
            currentRef = "refs/heads/" + headBranch;
        }

    } catch (GitAPIException | IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
    List<Branch> branches = new ArrayList<>();
    for (Ref ref : refs) {
        String refName = ref.getName();
        boolean isCommitOrTag = Constants.HEAD.equals(refName);
        String branchName = isCommitOrTag ? currentRef : refName;
        String branchDisplayName;
        if (isCommitOrTag) {
            branchDisplayName = "(detached from " + Repository.shortenRefName(currentRef) + ")";
        } else {
            branchDisplayName = Repository.shortenRefName(refName);
        }
        Branch branch = newDto(Branch.class).withName(branchName)
                .withActive(isCommitOrTag || refName.equals(currentRef)).withDisplayName(branchDisplayName)
                .withRemote(refName.startsWith("refs/remotes"));
        branches.add(branch);
    }
    return branches;
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public void fetch(FetchRequest request) throws GitException, UnauthorizedException {
    String remoteName = request.getRemote();
    String remoteUri;/* w w  w .j av  a2  s.com*/
    try {
        List<RefSpec> fetchRefSpecs;
        List<String> refSpec = request.getRefSpec();
        if (!refSpec.isEmpty()) {
            fetchRefSpecs = new ArrayList<>(refSpec.size());
            for (String refSpecItem : refSpec) {
                RefSpec fetchRefSpec = (refSpecItem.indexOf(':') < 0) //
                        ? new RefSpec(Constants.R_HEADS + refSpecItem + ":") //
                        : new RefSpec(refSpecItem);
                fetchRefSpecs.add(fetchRefSpec);
            }
        } else {
            fetchRefSpecs = Collections.emptyList();
        }

        FetchCommand fetchCommand = getGit().fetch();

        // If this an unknown remote with no refspecs given, put HEAD
        // (otherwise JGit fails)
        if (remoteName != null && refSpec.isEmpty()) {
            boolean found = false;
            List<Remote> configRemotes = remoteList(newDto(RemoteListRequest.class));
            for (Remote configRemote : configRemotes) {
                if (remoteName.equals(configRemote.getName())) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                fetchRefSpecs = Collections
                        .singletonList(new RefSpec(Constants.HEAD + ":" + Constants.FETCH_HEAD));
            }
        }

        if (remoteName == null) {
            remoteName = Constants.DEFAULT_REMOTE_NAME;
        }
        fetchCommand.setRemote(remoteName);
        remoteUri = getRepository().getConfig().getString(ConfigConstants.CONFIG_REMOTE_SECTION, remoteName,
                ConfigConstants.CONFIG_KEY_URL);
        fetchCommand.setRefSpecs(fetchRefSpecs);

        int timeout = request.getTimeout();
        if (timeout > 0) {
            fetchCommand.setTimeout(timeout);
        }
        fetchCommand.setRemoveDeletedRefs(request.isRemoveDeletedRefs());

        executeRemoteCommand(remoteUri, fetchCommand);
    } catch (GitException | GitAPIException exception) {
        String errorMessage;
        if (exception.getMessage().contains("Invalid remote: ")) {
            errorMessage = ERROR_NO_REMOTE_REPOSITORY;
        } else if ("Nothing to fetch.".equals(exception.getMessage())) {
            return;
        } else {
            errorMessage = exception.getMessage();
        }
        throw new GitException(errorMessage, exception);
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public Tag tagCreate(TagCreateRequest request) throws GitException {
    String commit = request.getCommit();
    if (commit == null) {
        commit = Constants.HEAD;
    }/*from  ww  w  . ja v a  2  s.c o  m*/

    try {
        RevWalk revWalk = new RevWalk(repository);
        RevObject revObject;
        try {
            revObject = revWalk.parseAny(repository.resolve(commit));
        } finally {
            revWalk.close();
        }

        TagCommand tagCommand = getGit().tag().setName(request.getName()).setObjectId(revObject)
                .setMessage(request.getMessage()).setForceUpdate(request.isForce());

        GitUser tagger = getUser();
        if (tagger != null) {
            tagCommand.setTagger(new PersonIdent(tagger.getName(), tagger.getEmail()));
        }

        Ref revTagRef = tagCommand.call();
        RevTag revTag = revWalk.parseTag(revTagRef.getLeaf().getObjectId());
        return newDto(Tag.class).withName(revTag.getTagName());
    } catch (IOException | GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

private String getCurrentBranch() throws GitException {
    try {//from  w ww .  j a  v a 2s .co m
        return Repository.shortenRefName(repository.exactRef(Constants.HEAD).getLeaf().getName());
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnectionTest.java

License:Open Source License

/**
 * Check branch using current repository reference is returned
 *
 * @throws Exception/*ww  w.  j ava 2 s. co  m*/
 *         if it fails
 */
@Test
public void checkCurrentBranch() throws Exception {
    String branchTest = "helloWorld";
    Ref ref = mock(Ref.class);
    when(repository.exactRef(Constants.HEAD)).thenReturn(ref);
    when(ref.getLeaf()).thenReturn(ref);
    when(ref.getName()).thenReturn(branchTest);
    String branchName = jGitConnection.getCurrentBranch();

    assertEquals(branchName, branchTest);
}