Example usage for org.eclipse.jgit.api Git getRepository

List of usage examples for org.eclipse.jgit.api Git getRepository

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git getRepository.

Prototype

public Repository getRepository() 

Source Link

Document

Get repository

Usage

From source file:org.fusesource.fabric.git.internal.GitHelpers.java

License:Apache License

protected static void configureBranch(Git git, String branch, String remote) {
    // lets update the merge config
    if (Strings.isNotBlank(branch)) {
        StoredConfig config = git.getRepository().getConfig();
        if (Strings.isNullOrBlank(config.getString("branch", branch, "remote"))
                || Strings.isNullOrBlank(config.getString("branch", branch, "merge"))) {
            config.setString("branch", branch, "remote", remote);
            config.setString("branch", branch, "merge", "refs/heads/" + branch);
            try {
                config.save();/*ww w .ja v a  2 s .  c  om*/
            } catch (IOException e) {
                LOG.error("Failed to configure the branch configuration to " + getRootGitDirectory(git)
                        + " with branch " + branch + " on remote repo: " + remote + ". " + e, e);
            }
        }
    }
}

From source file:org.fusesource.fabric.itests.basic.git.FabricGitTestSupport.java

License:Apache License

/**
 * Create a profile in git and check that its bridged to the registry.
 *
 * @param git     The git object of the test repository.
 * @param version The version of the profile.
 * @param profile The profile name.//from  w  ww .  j  a v  a 2 s.  c om
 * @throws Exception
 */
public void createAndTestProfileInGit(Git git, String version, String profile) throws Exception {
    //Create the test profile in git
    System.err.println("Create test profile:" + profile + " in git.");
    GitUtils.checkoutBranch(git, "origin", version);
    File testProfileDir = new File(git.getRepository().getWorkTree(), profile);
    testProfileDir.mkdirs();
    File testProfileConfig = new File(testProfileDir, "org.fusesource.fabric.agent.properties");
    testProfileConfig.createNewFile();
    Files.writeToFile(testProfileConfig, "", Charset.defaultCharset());
    git.add().addFilepattern(profile).call();
    git.commit().setMessage("Create " + profile).call();
    PullResult pullResult = git.pull().setCredentialsProvider(getCredentialsProvider()).setRebase(true).call();
    git.push().setCredentialsProvider(getCredentialsProvider()).setRemote("origin").call();
    GitUtils.waitForBranchUpdate(getCurator(), version);
    //Check that it has been bridged in zookeeper
    Thread.sleep(5000);
    assertNotNull(getCurator().checkExists().forPath(ZkPath.CONFIG_VERSIONS_PROFILE.getPath(version, profile)));
}

From source file:org.fusesource.fabric.itests.basic.git.FabricGitTestSupport.java

License:Apache License

/**
 * Create a profile in the registry and check that its bridged to git.
 *
 * @param git     The git object of the test repository.
 * @param version The version of the profile.
 * @param profile The profile name.//from   w w w .j av a 2s . c o m
 * @throws Exception
 */
public void createAndTestProfileInZooKeeper(Git git, String version, String profile) throws Exception {
    System.err.println("Create test profile:" + profile + " in zookeeper.");
    List<String> versions = Lists.transform(Arrays.<Version>asList(getFabricService().getVersions()),
            new Function<Version, String>() {

                @Override
                public String apply(Version version) {
                    return version.getId();
                }
            });

    if (!versions.contains(version)) {
        getFabricService().createVersion(version);
    }

    getFabricService().getVersion(version).createProfile(profile);
    GitUtils.waitForBranchUpdate(getCurator(), version);
    Thread.sleep(5000);
    GitUtils.checkoutBranch(git, "origin", version);
    PullResult pullResult = git.pull().setCredentialsProvider(getCredentialsProvider()).setRebase(true).call();
    assertTrue(pullResult.isSuccessful());
    File testProfileDir = new File(git.getRepository().getWorkTree(), profile);
    assertTrue(testProfileDir.exists());
    File testProfileConfig = new File(testProfileDir, "org.fusesource.fabric.agent.properties");
    assertTrue(testProfileConfig.exists());
}

From source file:org.fusesource.fabric.itests.basic.git.GitBridgeTest.java

License:Apache License

public void testWithProfiles() throws Exception {
    String testProfileNameBase = "mytestprofile-";
    System.err.println(executeCommand("fabric:create -n"));
    Set<Container> containers = ContainerBuilder.create(1, 1).withName("child").assertProvisioningResult()
            .build();/* w  w  w. j a  v  a 2 s.com*/
    String gitRepoUrl = GitUtils.getMasterUrl(getCurator());

    GitUtils.waitForBranchUpdate(getCurator(), "1.0");

    Git.cloneRepository().setURI(gitRepoUrl).setCloneAllBranches(true).setDirectory(testrepo)
            .setCredentialsProvider(getCredentialsProvider()).call();
    Git git = Git.open(testrepo);
    GitUtils.configureBranch(git, "origin", gitRepoUrl, "1.0");
    git.fetch().setCredentialsProvider(getCredentialsProvider());
    GitUtils.checkoutBranch(git, "origin", "1.0");

    //Check that the default profile exists
    assertTrue(new File(testrepo, "default").exists());

    //Create test profile
    for (int i = 1; i <= 3; i++) {
        String testProfileName = testProfileNameBase + i;
        System.err.println("Create test profile:" + testProfileName + " in zookeeper");
        getFabricService().getVersion("1.0").createProfile(testProfileName);
        GitUtils.waitForBranchUpdate(getCurator(), "1.0");
        git.pull().setRebase(true).setCredentialsProvider(getCredentialsProvider()).call();
        //Check that a newly created profile exists
        assertTrue(new File(testrepo, testProfileName).exists());
        //Delete test profile
        System.err.println("Delete test profile:" + testProfileName + " in git.");
        git.rm().addFilepattern(testProfileName).call();
        git.commit().setMessage("Delete " + testProfileName).call();
        git.push().setCredentialsProvider(getCredentialsProvider()).setRemote("origin").call();
        GitUtils.waitForBranchUpdate(getCurator(), "1.0");
        Thread.sleep(5000);
        assertFalse(new File(testrepo, testProfileName).exists());
        assertNull(getCurator().checkExists()
                .forPath(ZkPath.CONFIG_VERSIONS_PROFILE.getPath("1.0", testProfileName)));

        //Create the test profile in git
        System.err.println("Create test profile:" + testProfileName + " in git.");
        File testProfileDir = new File(testrepo, testProfileName);
        testProfileDir.mkdirs();
        File testProfileConfig = new File(testProfileDir, "org.fusesource.fabric.agent.properties");
        testProfileConfig.createNewFile();
        Files.writeToFile(testProfileConfig, "", Charset.defaultCharset());
        git.add().addFilepattern(testProfileName).call();
        RevCommit commit = git.commit().setMessage("Create " + testProfileName).call();
        FileTreeIterator fileTreeItr = new FileTreeIterator(git.getRepository());
        IndexDiff indexDiff = new IndexDiff(git.getRepository(), commit.getId(), fileTreeItr);
        System.out.println(indexDiff.getChanged());
        System.out.println(indexDiff.getAdded());
        git.push().setCredentialsProvider(getCredentialsProvider()).setRemote("origin").call();
        GitUtils.waitForBranchUpdate(getCurator(), "1.0");
        //Check that it has been bridged in zookeeper
        Thread.sleep(15000);
        assertNotNull(getCurator().checkExists()
                .forPath(ZkPath.CONFIG_VERSIONS_PROFILE.getPath("1.0", testProfileName)));
    }
}

From source file:org.gitective.tests.GitTestCase.java

License:Open Source License

/**
 * Create tag with name/*  w  ww .ja va  2  s.  com*/
 *
 * @param repo
 * @param name
 * @return tag ref
 * @throws Exception
 */
protected Ref tag(File repo, String name) throws Exception {
    Git git = Git.open(repo);
    git.tag().setName(name).setMessage(name).call();
    Ref tagRef = git.getRepository().getTags().get(name);
    assertNotNull(tagRef);
    return tagRef;
}

From source file:org.gitective.tests.GitTestCase.java

License:Open Source License

/**
 * Merge ref into current branch//from   w w  w .j  a  va2s.c  o m
 *
 * @param ref
 * @return result
 * @throws Exception
 */
protected MergeResult merge(String ref) throws Exception {
    Git git = Git.open(testRepo);
    return git.merge().setStrategy(MergeStrategy.RESOLVE)
            .include(CommitUtils.getCommit(git.getRepository(), ref)).call();
}

From source file:org.gitective.tests.GitTestCase.java

License:Open Source License

/**
 * Add note to latest commit with given content
 *
 * @param content//from  w  ww  . j a  va2s .co m
 * @param ref
 * @return note
 * @throws Exception
 */
protected Note note(String content, String ref) throws Exception {
    Git git = Git.open(testRepo);
    Note note = git.notesAdd().setMessage(content).setNotesRef(Constants.R_NOTES + ref)
            .setObjectId(CommitUtils.getHead(git.getRepository())).call();
    assertNotNull(note);
    return note;
}

From source file:org.gradle.vcs.git.internal.GitVersionControlSystem.java

License:Apache License

private static void updateSubModules(Git git) throws IOException, GitAPIException {
    SubmoduleWalk walker = SubmoduleWalk.forIndex(git.getRepository());
    try {/*from   w ww  .ja  va2s  .  c  o  m*/
        while (walker.next()) {
            Repository submodule = walker.getRepository();
            try {
                Git submoduleGit = Git.wrap(submodule);
                submoduleGit.fetch().call();
                git.submoduleUpdate().addPath(walker.getPath()).call();
                submoduleGit.reset().setMode(ResetCommand.ResetType.HARD).call();
                updateSubModules(submoduleGit);
            } finally {
                submodule.close();
            }
        }
    } finally {
        walker.close();
    }
}

From source file:org.impressivecode.depress.scm.git.GitOnlineLogParser.java

License:Open Source License

public static String getCurrentBranch(final String path) throws IOException, NoHeadException {
    Git git = initializeGit(path);
    return git.getRepository().getBranch();
}

From source file:org.impressivecode.depress.scm.git.GitOnlineLogParser.java

License:Open Source License

private List<GitCommit> processRepo(final String path, final GitParserOptions gitParserOptions)
        throws IOException, NoHeadException, GitAPIException {
    Git git = initializeGit(path);
    List<GitCommit> analyzedCommits = new ArrayList<GitCommit>();

    LogCommand log = git.log();/*from  w w w  . j av  a  2  s  . c  o m*/

    if (gitParserOptions.hasBranch()) {
        List<Ref> branches = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
        Ref branch = null;
        for (Ref b : branches) {
            if (b.getName().equals("refs/heads/" + gitParserOptions.getBranch())
                    || b.getName().equals("refs/remotes/" + gitParserOptions.getBranch())) {
                branch = b;
                break;
            }
        }
        if (branch == null) {
            throw new IOException("Specified branch was not found in git repository");
        }
        log.add(branch.getObjectId());
    }

    Iterable<RevCommit> loglines = log.call();
    Iterator<RevCommit> logIterator = loglines.iterator();
    while (logIterator.hasNext()) {
        RevCommit commit = logIterator.next();
        GitcommitProcessor proc = new GitcommitProcessor(gitParserOptions, git.getRepository());
        proc.setRevCommit(commit);
        proc.processCommitData();
        analyzedCommits.add(proc.getResult());
    }
    return analyzedCommits;
}