Search in sources :

Example 1 with SubmoduleStatus

use of org.eclipse.jgit.submodule.SubmoduleStatus in project catma by forTEXT.

the class JGitRepoManager method getSubmoduleHeadRevisionHash.

/**
 * Gets the HEAD revision hash for the submodule with the name <code>submoduleName</code>.
 *
 * @param submoduleName the name of the submodule whose HEAD revision hash to get
 * @return a revision hash
 * @throws IOException if a submodule with the name <code>submoduleName</code> doesn't exist
 *                                            or if the operation failed for another reason
 */
@Override
public String getSubmoduleHeadRevisionHash(String submoduleName) throws IOException {
    if (!isAttached()) {
        throw new IllegalStateException("Can't call `getSubmoduleHeadRevisionHash` on a detached instance");
    }
    try {
        SubmoduleStatusCommand submoduleStatusCommand = this.gitApi.submoduleStatus();
        submoduleStatusCommand.addPath(submoduleName);
        Map<String, SubmoduleStatus> results = submoduleStatusCommand.call();
        if (!results.containsKey(submoduleName)) {
            throw new IOException(String.format("Failed to get HEAD revision hash for submodule `%s`. " + "A submodule with that name does not appear to exist.", submoduleName));
        }
        return results.get(submoduleName).getHeadId().getName();
    } catch (GitAPIException e) {
        throw new IOException("Failed to get submodule status", e);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) SubmoduleStatusCommand(org.eclipse.jgit.api.SubmoduleStatusCommand) SubmoduleStatus(org.eclipse.jgit.submodule.SubmoduleStatus)

Example 2 with SubmoduleStatus

use of org.eclipse.jgit.submodule.SubmoduleStatus in project JGitFS by centic9.

the class JGitHelper method getSubmoduleHead.

/**
 * Returns the path where the given submodule is linked.
 *
 * @param name the name of the Git submodule.
 * @return The path where the Git submodule is linked.
 * @throws NoSuchElementException if the given name is not known.
 * @throws IOException If accessing the Git repository fails
 */
public String getSubmoduleHead(String name) throws IOException {
    try {
        Map<String, SubmoduleStatus> set = git.submoduleStatus().call();
        for (Map.Entry<String, SubmoduleStatus> entry : set.entrySet()) {
            if (entry.getKey().equals(name)) {
                SubmoduleStatus value = entry.getValue();
                SubmoduleStatusType type = value.getType();
                if (type == SubmoduleStatusType.MISSING || type == SubmoduleStatusType.UNINITIALIZED) {
                    throw new NoSuchElementException("Could not read submodule " + name + " because it is in state " + type);
                }
                return value.getHeadId().getName();
            }
        }
        throw new NoSuchElementException("Could not read submodule " + name);
    } catch (GitAPIException e) {
        throw new IOException(e);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) SubmoduleStatusType(org.eclipse.jgit.submodule.SubmoduleStatusType) IOException(java.io.IOException) SubmoduleStatus(org.eclipse.jgit.submodule.SubmoduleStatus) Map(java.util.Map) ObjectIdSubclassMap(org.eclipse.jgit.lib.ObjectIdSubclassMap) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with SubmoduleStatus

use of org.eclipse.jgit.submodule.SubmoduleStatus in project oxygen-git-client-addon by oxygenxml.

the class DiffPresenterTest method testCompareSubmodule.

/**
 * <p><b>Description:</b> Checks the URL used in a rebase conflicts.</p>
 * <p><b>Bug ID:</b> EXM-45843</p>
 *
 * @author bogdan_dragici
 *
 * @throws Exception
 */
@Test
public void testCompareSubmodule() throws Exception {
    GitAccess gitAccess = GitAccess.getInstance();
    // PARENT repos
    String localTestRepositoryP = "target/test-resources/localCS";
    String remoteTestRepositoryP = "target/test-resources/remoteCS";
    Repository remoteRepoP = createRepository(remoteTestRepositoryP);
    Repository localRepoP = createRepository(localTestRepositoryP);
    bindLocalToRemote(localRepoP, remoteRepoP);
    // SUBMODULE repos
    String remoteTestRepositorySubModule = "target/test-resources/remoteCS-SubModule/";
    Repository remoteRepoSubModule = createRepository(remoteTestRepositorySubModule);
    // Commit (very important)
    gitAccess.commit("Commit");
    // Set the PARENT repo as the current one, to which we'll add the submodule
    gitAccess.setRepositorySynchronously(localTestRepositoryP);
    // Add SUBMODULE
    SubmoduleAddCommand addCommand = gitAccess.getGit().submoduleAdd();
    addCommand.setURI(remoteRepoSubModule.getDirectory().toURI().toString());
    addCommand.setPath("modules/submodule");
    Repository subRepo = addCommand.call();
    subRepo.close();
    File parentWorkDir = gitAccess.getRepository().getWorkTree();
    assertTrue(new File(parentWorkDir, "modules/submodule").isDirectory());
    assertTrue(new File(parentWorkDir, ".gitmodules").isFile());
    // Check the SUBMODULE
    Map<String, SubmoduleStatus> submodules = gitAccess.getGit().submoduleStatus().call();
    assertEquals(1, submodules.size());
    SubmoduleStatus status = submodules.get("modules/submodule");
    assertNotNull(status);
    assertEquals(SubmoduleStatusType.INITIALIZED, status.getType());
    // SHOW DIFF
    DiffPresenter.showDiff(// The submodule
    gitAccess.getStagedFiles().get(1), Mockito.mock(GitControllerBase.class));
    assertNotNull(leftDiff);
    assertNotNull(rightDiff);
    String left = "git://CurrentSubmodule/modules/submodule.txt";
    assertEquals(left, leftDiff.toString());
    assertTrue(TestUtil.read(new URL(left)).startsWith("Subproject commit "));
    String right = "git://PreviousSubmodule/modules/submodule.txt";
    assertEquals(right, rightDiff.toString());
    assertTrue(TestUtil.read(new URL(right)).startsWith("Subproject commit "));
}
Also used : GitControllerBase(com.oxygenxml.git.service.GitControllerBase) Repository(org.eclipse.jgit.lib.Repository) GitAccess(com.oxygenxml.git.service.GitAccess) SubmoduleAddCommand(org.eclipse.jgit.api.SubmoduleAddCommand) File(java.io.File) SubmoduleStatus(org.eclipse.jgit.submodule.SubmoduleStatus) URL(java.net.URL) Test(org.junit.Test)

Aggregations

SubmoduleStatus (org.eclipse.jgit.submodule.SubmoduleStatus)3 IOException (java.io.IOException)2 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 GitAccess (com.oxygenxml.git.service.GitAccess)1 GitControllerBase (com.oxygenxml.git.service.GitControllerBase)1 File (java.io.File)1 URL (java.net.URL)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 SubmoduleAddCommand (org.eclipse.jgit.api.SubmoduleAddCommand)1 SubmoduleStatusCommand (org.eclipse.jgit.api.SubmoduleStatusCommand)1 ObjectIdSubclassMap (org.eclipse.jgit.lib.ObjectIdSubclassMap)1 Repository (org.eclipse.jgit.lib.Repository)1 SubmoduleStatusType (org.eclipse.jgit.submodule.SubmoduleStatusType)1 Test (org.junit.Test)1