Example usage for org.eclipse.jgit.lib StoredConfig getString

List of usage examples for org.eclipse.jgit.lib StoredConfig getString

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib StoredConfig getString.

Prototype

public String getString(final String section, String subsection, final String name) 

Source Link

Document

Get string value or null if not found.

Usage

From source file:org.springframework.cloud.config.server.environment.JGitEnvironmentRepositoryTests.java

License:Apache License

@Test
public void testMergeException() throws Exception {

    Git git = mock(Git.class);
    CloneCommand cloneCommand = mock(CloneCommand.class);
    MockGitFactory factory = new MockGitFactory(git, cloneCommand);
    JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment);
    this.repository.setGitFactory(factory);

    //refresh()->shouldPull
    StatusCommand statusCommand = mock(StatusCommand.class);
    Status status = mock(Status.class);
    when(git.status()).thenReturn(statusCommand);
    Repository repository = mock(Repository.class);
    when(git.getRepository()).thenReturn(repository);
    StoredConfig storedConfig = mock(StoredConfig.class);
    when(repository.getConfig()).thenReturn(storedConfig);
    when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
    when(statusCommand.call()).thenReturn(status);
    when(status.isClean()).thenReturn(true);

    //refresh()->fetch
    FetchCommand fetchCommand = mock(FetchCommand.class);
    FetchResult fetchResult = mock(FetchResult.class);
    when(git.fetch()).thenReturn(fetchCommand);
    when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand);
    when(fetchCommand.call()).thenReturn(fetchResult);
    when(fetchResult.getTrackingRefUpdates()).thenReturn(Collections.EMPTY_LIST);

    //refresh()->checkout
    CheckoutCommand checkoutCommand = mock(CheckoutCommand.class);
    //refresh()->checkout->containsBranch
    ListBranchCommand listBranchCommand = mock(ListBranchCommand.class);
    when(git.checkout()).thenReturn(checkoutCommand);
    when(git.branchList()).thenReturn(listBranchCommand);
    List<Ref> refs = new ArrayList<>();
    Ref ref = mock(Ref.class);
    refs.add(ref);//www.  j a  v  a 2  s  .  c om
    when(ref.getName()).thenReturn("/master");
    when(listBranchCommand.call()).thenReturn(refs);

    //refresh()->merge
    MergeCommand mergeCommand = mock(MergeCommand.class);
    when(git.merge()).thenReturn(mergeCommand);
    when(mergeCommand.call()).thenThrow(new NotMergedException()); //here is our exception we are testing

    //refresh()->return git.getRepository().getRef("HEAD").getObjectId().getName();
    Ref headRef = mock(Ref.class);
    when(repository.getRef(anyString())).thenReturn(headRef);

    ObjectId newObjectId = ObjectId.fromRaw(new int[] { 1, 2, 3, 4, 5 });
    when(headRef.getObjectId()).thenReturn(newObjectId);

    SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", "master");
    assertEquals(locations.getVersion(), newObjectId.getName());
}

From source file:org.springframework.cloud.config.server.environment.JGitEnvironmentRepositoryTests.java

License:Apache License

@Test
public void testResetHardException() throws Exception {

    Git git = mock(Git.class);
    CloneCommand cloneCommand = mock(CloneCommand.class);
    MockGitFactory factory = new MockGitFactory(git, cloneCommand);
    JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment);
    this.repository.setGitFactory(factory);

    //refresh()->shouldPull
    StatusCommand statusCommand = mock(StatusCommand.class);
    Status status = mock(Status.class);
    when(git.status()).thenReturn(statusCommand);
    Repository repository = mock(Repository.class);
    when(git.getRepository()).thenReturn(repository);
    StoredConfig storedConfig = mock(StoredConfig.class);
    when(repository.getConfig()).thenReturn(storedConfig);
    when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
    when(statusCommand.call()).thenReturn(status);
    when(status.isClean()).thenReturn(true).thenReturn(false);

    //refresh()->fetch
    FetchCommand fetchCommand = mock(FetchCommand.class);
    FetchResult fetchResult = mock(FetchResult.class);
    when(git.fetch()).thenReturn(fetchCommand);
    when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand);
    when(fetchCommand.call()).thenReturn(fetchResult);
    when(fetchResult.getTrackingRefUpdates()).thenReturn(Collections.EMPTY_LIST);

    //refresh()->checkout
    CheckoutCommand checkoutCommand = mock(CheckoutCommand.class);
    //refresh()->checkout->containsBranch
    ListBranchCommand listBranchCommand = mock(ListBranchCommand.class);
    when(git.checkout()).thenReturn(checkoutCommand);
    when(git.branchList()).thenReturn(listBranchCommand);
    List<Ref> refs = new ArrayList<>();
    Ref ref = mock(Ref.class);
    refs.add(ref);//from  w  w w .j  a v  a 2s.com
    when(ref.getName()).thenReturn("/master");
    when(listBranchCommand.call()).thenReturn(refs);

    //refresh()->merge
    MergeCommand mergeCommand = mock(MergeCommand.class);
    when(git.merge()).thenReturn(mergeCommand);
    when(mergeCommand.call()).thenThrow(new NotMergedException()); //here is our exception we are testing

    //refresh()->hardReset
    ResetCommand resetCommand = mock(ResetCommand.class);
    when(git.reset()).thenReturn(resetCommand);
    when(resetCommand.call()).thenReturn(ref);

    //refresh()->return git.getRepository().getRef("HEAD").getObjectId().getName();
    Ref headRef = mock(Ref.class);
    when(repository.getRef(anyString())).thenReturn(headRef);

    ObjectId newObjectId = ObjectId.fromRaw(new int[] { 1, 2, 3, 4, 5 });
    when(headRef.getObjectId()).thenReturn(newObjectId);

    SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", "master");
    assertEquals(locations.getVersion(), newObjectId.getName());
}

From source file:org.uberfire.java.nio.fs.jgit.JGitCloneTest.java

License:Apache License

@Test
public void cloneNotMirrorRepoConfigTest() throws IOException {
    final File parentFolder = createTempDirectory();

    final File gitSource = new File(parentFolder, SOURCE_GIT + ".git");

    final File gitTarget = new File(parentFolder, TARGET_GIT + ".git");

    final Git origin = setupGitRepo(gitSource, null);

    boolean isMirror = false;
    boolean sslVerify = true;
    final Git clonedNotMirror = new Clone(gitTarget, gitSource.getAbsolutePath(), isMirror, null,
            CredentialsProvider.getDefault(), null, null, sslVerify).execute().get();

    assertThat(clonedNotMirror).isNotNull();

    StoredConfig config = clonedNotMirror.getRepository().getConfig();

    assertNotEquals(Clone.REFS_MIRRORED, config.getString("remote", "origin", "fetch"));
    assertNull(config.getString("remote", "origin", "mirror"));
    assertEquals(gitSource.getAbsolutePath(), config.getString("remote", "origin", "url"));

    boolean missingDefaultValue = true;
    assertEquals(missingDefaultValue, config.getBoolean("http", null, "sslVerify", missingDefaultValue));
}

From source file:org.uberfire.java.nio.fs.jgit.JGitCloneTest.java

License:Apache License

@Test
public void cloneMirrorRepoNoSSLVerifyConfigTest() throws IOException {
    final File parentFolder = createTempDirectory();

    final File gitSource = new File(parentFolder, SOURCE_GIT + ".git");

    final File gitTarget = new File(parentFolder, TARGET_GIT + ".git");

    final Git origin = setupGitRepo(gitSource, null);

    assertTrue(provider.config.isSslVerify());

    boolean isMirror = true;
    boolean sslVerify = false;
    final Git clonedMirror = new Clone(gitTarget, gitSource.getAbsolutePath(), isMirror, null,
            CredentialsProvider.getDefault(), null, null, sslVerify).execute().get();

    assertThat(clonedMirror).isNotNull();

    StoredConfig config = clonedMirror.getRepository().getConfig();

    assertEquals(Clone.REFS_MIRRORED, config.getString("remote", "origin", "fetch"));
    assertNull(config.getString("remote", "origin", "mirror"));
    assertEquals(gitSource.getAbsolutePath(), config.getString("remote", "origin", "url"));
    assertEquals(sslVerify, config.getBoolean("http", null, "sslVerify", !sslVerify));

}

From source file:org.uberfire.java.nio.fs.jgit.util.commands.UpdateRemoteConfig.java

License:Apache License

public List<RefSpec> execute() throws IOException, URISyntaxException {
    final List<RefSpec> specs = new ArrayList<>();
    if (refSpecs == null || refSpecs.isEmpty()) {
        specs.add(new RefSpec("+refs/heads/*:refs/remotes/" + remote.getK1() + "/*"));
        specs.add(new RefSpec("+refs/tags/*:refs/tags/*"));
        specs.add(new RefSpec("+refs/notes/*:refs/notes/*"));
    } else {/*  w  w w  .  j  av  a2 s  .c  om*/
        specs.addAll(refSpecs);
    }

    final StoredConfig config = git.getRepository().getConfig();
    final String url = config.getString("remote", remote.getK1(), "url");
    if (url == null) {
        final RemoteConfig remoteConfig = new RemoteConfig(git.getRepository().getConfig(), remote.getK1());
        remoteConfig.addURI(new URIish(remote.getK2()));
        specs.forEach(remoteConfig::addFetchRefSpec);
        remoteConfig.update(git.getRepository().getConfig());
        git.getRepository().getConfig().save();
    }
    return specs;
}

From source file:pt.up.fe.specs.psfbuilder.GitBranch.java

License:Apache License

public static GitBranch newInstance(File location) {
    FileRepositoryBuilder repoBuilder = new FileRepositoryBuilder().findGitDir(location.getAbsoluteFile());

    if (repoBuilder.getGitDir() == null) {
        throw new RuntimeException("Could not find a git repository for folder '"
                + SpecsIo.getWorkingDir().getAbsolutePath() + "'");
    }//from w w w  .  j  a  va 2s  .c om

    // Open an existing repository
    try (Repository repo = repoBuilder.build()) {
        StoredConfig config = repo.getConfig();
        Set<String> remotes = config.getSubsections("remote");

        if (remotes.isEmpty()) {
            throw new RuntimeException("Could not find a remote in '" + repo.getWorkTree() + "'");
        }

        // Get a remote. Try origin first, if not found, get the first on the list
        String remoteName = getRemoteName(remotes);
        String remote = config.getString("remote", remoteName, "url");

        Set<String> branches = config.getSubsections("branch");
        if (branches.isEmpty()) {
            throw new RuntimeException("Could not find a branch in '" + repo.getWorkTree() + "'");
        }

        String branch = getBranchName(branches);

        return new GitBranch(repo.getWorkTree(), remote, branch);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

}