Example usage for org.eclipse.jgit.lib Repository getFullBranch

List of usage examples for org.eclipse.jgit.lib Repository getFullBranch

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository getFullBranch.

Prototype

@Nullable
public String getFullBranch() throws IOException 

Source Link

Document

Get the name of the reference that HEAD points to.

Usage

From source file:net.erdfelt.android.sdkfido.git.internal.GitInfo.java

License:Apache License

public static void infoAll(Repository db) throws IOException {
    System.out.printf("Repository - %s%n", db.getDirectory());
    System.out.printf("      state: %s%n", db.getRepositoryState());
    System.out.printf("     branch: %s%n", db.getBranch());
    System.out.printf("full-branch: %s%n", db.getFullBranch());
    infoRefs(db);// www .j  av  a  2 s  .co m
    infoBranches(db);
    infoTags(db);
}

From source file:net.tietema.versioning.GitJavaVersioning.java

License:Apache License

public String getRevision(File projectDir) throws MojoExecutionException {
    // XXX we use our own findGitDir because they JGit one doesn't find the git dir in a multi project build
    File gitDir = findGitDir(projectDir);
    String revision = "Unknown";
    if (gitDir == null)
        return revision;

    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = null;
    try {//from   w w w  .java 2s .  c  o m
        repository = builder.setGitDir(gitDir).readEnvironment() // scan environment GIT_* variables
                .findGitDir(projectDir) // scan up the file system tree
                .build();

        log.info("Git dir: " + gitDir.toString());
        RepositoryState state = repository.getRepositoryState();
        log.info(state.getDescription());
        String branch = repository.getBranch();
        log.info("Branch is: " + branch);
        Git git = new Git(repository);
        String fullBranch = repository.getFullBranch();
        log.info("Full branch is: " + fullBranch);
        ObjectId id = repository.resolve(fullBranch);
        log.info("Branch " + repository.getBranch() + " points to " + id.name());

        Status status = git.status().call();
        boolean strictClean = status.isClean();
        // no untracked files
        boolean loseClean = status.getAdded().isEmpty() && status.getChanged().isEmpty()
                && status.getConflicting().isEmpty() && status.getMissing().isEmpty()
                && status.getModified().isEmpty() && status.getRemoved().isEmpty();

        StringWriter buffer = new StringWriter();
        JavaWriter writer = new JavaWriter(buffer);
        writer.emitPackage(packageName)
                .beginType(packageName + "." + className, "class", Modifier.PUBLIC | Modifier.FINAL)
                .emitField("String", "BRANCH", Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC,
                        String.format(Locale.US, "\"%s\"", branch))
                .emitField("String", "REVISION", Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC,
                        String.format(Locale.US, "\"%s\"", id.name()))
                .emitField("String", "REVISION_SHORT", Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC,
                        String.format(Locale.US, "\"%s\"", id.name().substring(0, 8)))
                .emitJavadoc("Strict Clean means no changes, not even untracked files")
                .emitField("boolean", "STRICT_CLEAN", Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC,
                        (strictClean ? "true" : "false"))
                .emitJavadoc("Lose Clean means no changes except untracked files.")
                .emitField("boolean", "LOSE_CLEAN", Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC,
                        (loseClean ? "true" : "false"))
                .endType();
        revision = buffer.toString();

        return revision;
    } catch (IOException e) {
        log.error(e);
        throw new MojoExecutionException(e.getMessage());
    } catch (GitAPIException e) {
        log.error(e);
        throw new MojoExecutionException(e.getMessage());
    } finally {
        if (repository != null)
            repository.close();
    }
}

From source file:org.ajoberstar.gradle.git.tasks.GitBranchList.java

License:Apache License

/**
 * Execute command to list branches.//from w w  w . jav  a 2 s .  c o m
 */
@TaskAction
void branchList() {
    ListBranchCommand cmd = getGit().branchList();
    if (getBranchType() != null) {
        switch (getBranchType()) {
        case REMOTE:
            cmd.setListMode(ListBranchCommand.ListMode.REMOTE);
            break;
        case ALL:
            cmd.setListMode(ListBranchCommand.ListMode.ALL);
            break;
        case LOCAL:
            break; //use default
        default:
            throw new AssertionError("Illegal branch type: " + getBranchType());
        }
    }
    try {
        Repository repo = getGit().getRepository();
        branches = new ArrayList<Branch>();
        for (Ref ref : cmd.call()) {
            branches.add(GitUtil.refToBranch(repo, ref));
        }
        workingBranch = GitUtil.gitNameToBranch(repo, repo.getFullBranch());
    } catch (IOException e) {
        throw new GradleException("Problem listing branches", e);
    } catch (GitAPIException e) {
        throw new GradleException("Problem listing branches", e);
    }
}

From source file:org.eclipse.egit.gitflow.op.FeatureCheckoutOperationTest.java

License:Open Source License

@Test
public void testFeatureCheckout() throws Exception {
    Repository repository = testRepository.getRepository();
    GitFlowRepository gfRepo = init("testFeatureCheckout\n\nfirst commit\n");

    new FeatureStartOperation(gfRepo, MY_FEATURE).execute(null);
    new BranchOperation(repository, gfRepo.getConfig().getDevelop()).execute(null);

    new FeatureCheckoutOperation(gfRepo, MY_FEATURE).execute(null);

    assertEquals(gfRepo.getConfig().getFullFeatureBranchName(MY_FEATURE), repository.getFullBranch());
}

From source file:org.eclipse.egit.gitflow.op.FeatureCheckoutOperationTest.java

License:Open Source License

@Test
public void testFeatureCheckoutConflicts() throws Exception {
    Repository repository = testRepository.getRepository();
    GitFlowRepository gfRepo = init("testFeatureCheckoutConflicts\n\nfirst commit\n");

    // setup something we can modify later
    IFile file = testUtils.addFileToProject(project.getProject(), "folder1/file1.txt", "Hello world");
    testRepository.connect(project.getProject());
    testRepository.trackAllFiles(project.getProject());
    testRepository.commit("Initial commit");

    new FeatureStartOperation(gfRepo, MY_FEATURE).execute(null);
    // modify on first branch
    testUtils.changeContentOfFile(project.getProject(), file, "Hello Feature");
    testRepository.addToIndex(file);//w w w. j  ava2 s  .c o  m
    testRepository.commit("Feature commit");
    new BranchOperation(repository, gfRepo.getConfig().getDevelop()).execute(null);
    assertEquals(gfRepo.getConfig().getDevelopFull(), repository.getFullBranch());

    // modify on second branch
    testUtils.changeContentOfFile(project.getProject(), file, "Hello Develop");
    testRepository.addToIndex(file);

    FeatureCheckoutOperation featureCheckoutOperation = new FeatureCheckoutOperation(gfRepo, MY_FEATURE);
    featureCheckoutOperation.execute(null);

    assertEquals(Status.CONFLICTS, featureCheckoutOperation.getResult().getStatus());
    assertEquals(gfRepo.getConfig().getDevelopFull(), repository.getFullBranch());
}

From source file:org.eclipse.egit.gitflow.op.FeatureFinishOperationTest.java

License:Open Source License

@Test
public void testFeatureFinishFastForward() throws Exception {
    String fileName = "theFirstFile.txt";
    Repository repository = testRepository.getRepository();
    GitFlowRepository gfRepo = init("testFeatureFinish\n\nfirst commit\n");

    new FeatureStartOperation(gfRepo, MY_FEATURE).execute(null);
    RevCommit branchCommit = addFileAndCommit(fileName, "adding file on feature branch");
    new FeatureFinishOperation(gfRepo).execute(null);
    assertEquals(gfRepo.getConfig().getDevelopFull(), repository.getFullBranch());

    String branchName = gfRepo.getConfig().getFeatureBranchName(MY_FEATURE);
    assertNull(findBranch(repository, branchName));

    RevCommit developHead = gfRepo.findHead();
    assertEquals(branchCommit, developHead);

    assertEquals(2, countCommits(repository));
    assertTrue(new File(repository.getDirectory() + "/../" + fileName).exists());
}

From source file:org.eclipse.egit.gitflow.op.FeatureFinishOperationTest.java

License:Open Source License

@Test
public void testFeatureFinishSquash() throws Exception {
    String fileName = "theFirstFile.txt";
    String fileName2 = "theSecondFile.txt";

    Repository repository = testRepository.getRepository();
    GitFlowRepository gfRepo = init("testFeatureFinishSquash\n\nfirst commit\n");

    new FeatureStartOperation(gfRepo, MY_FEATURE).execute(null);

    String branchName = gfRepo.getConfig().getFeatureBranchName(MY_FEATURE);
    addFileAndCommit(fileName, "adding first file on feature branch");
    addFileAndCommit(fileName2, "adding second file on feature branch");
    FeatureFinishOperation featureFinishOperation = new FeatureFinishOperation(gfRepo);
    featureFinishOperation.setSquash(true);
    featureFinishOperation.execute(null);
    assertEquals(gfRepo.getConfig().getDevelopFull(), repository.getFullBranch());
    assertEquals(null, findBranch(repository, branchName));

    assertEquals(1, countCommits(repository));
    assertTrue(new File(repository.getDirectory() + "/../" + fileName).exists());
    assertTrue(new File(repository.getDirectory() + "/../" + fileName2).exists());

    Status status = Git.wrap(repository).status().call();
    assertTrue(status.hasUncommittedChanges());
}

From source file:org.eclipse.egit.gitflow.op.FeatureFinishOperationTest.java

License:Open Source License

@Test
public void testFeatureFinish() throws Exception {
    Repository repository = testRepository.getRepository();
    GitFlowRepository gfRepo = init("testFeatureFinish\n\nfirst commit\n");

    new FeatureStartOperation(gfRepo, MY_FEATURE).execute(null);
    addFileAndCommit("foo.txt", "testFeatureFinish\n\nbranch commit 1\n");
    addFileAndCommit("bar.txt", "testFeatureFinish\n\nbranch commit 2\n");
    new FeatureFinishOperation(gfRepo).execute(null);
    assertEquals(gfRepo.getConfig().getDevelopFull(), repository.getFullBranch());

    String branchName = gfRepo.getConfig().getFeatureBranchName(MY_FEATURE);

    assertEquals(formatMergeCommitMessage(branchName) + " into develop", gfRepo.findHead().getFullMessage());
}

From source file:org.eclipse.egit.gitflow.op.FeatureFinishOperationTest.java

License:Open Source License

@Test
public void testFeatureFinishKeepBranch() throws Exception {
    Repository repository = testRepository.getRepository();
    GitFlowRepository gfRepo = init("testFeatureFinishKeepBranch\n\nfirst commit\n");

    new FeatureStartOperation(gfRepo, MY_FEATURE).execute(null);
    addFileAndCommit("foo.txt", "testFeatureFinishKeepBranch\n\nbranch commit 1\n");
    addFileAndCommit("bar.txt", "testFeatureFinishKeepBranch\n\nbranch commit 2\n");
    FeatureFinishOperation featureFinishOperation = new FeatureFinishOperation(gfRepo);
    featureFinishOperation.setKeepBranch(true);
    featureFinishOperation.execute(null);
    assertEquals(gfRepo.getConfig().getDevelopFull(), repository.getFullBranch());

    String branchName = gfRepo.getConfig().getFeatureBranchName(MY_FEATURE);
    assertNotNull(findBranch(repository, branchName));

    assertEquals(formatMergeCommitMessage(branchName) + " into develop", gfRepo.findHead().getFullMessage());
}

From source file:org.eclipse.egit.gitflow.op.FeatureStartOperationTest.java

License:Open Source License

@Test
public void testFeatureStart() throws Exception {
    Repository repository = testRepository.getRepository();
    GitFlowRepository gfRepo = init("testFeatureStart\n\nfirst commit\n");

    FeatureStartOperation featureStartOperation = new FeatureStartOperation(gfRepo, MY_FEATURE);
    assertNull(featureStartOperation.getSchedulingRule());
    featureStartOperation.execute(null);

    assertEquals(gfRepo.getConfig().getFullFeatureBranchName(MY_FEATURE), repository.getFullBranch());
}