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

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

Introduction

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

Prototype

public StatusCommand status() 

Source Link

Document

Return a command object to execute a status command

Usage

From source file:org.basinmc.maven.plugins.minecraft.patch.GeneratePatchesMojo.java

License:Apache License

/**
 * {@inheritDoc}/*from  w w  w . ja v  a 2  s . c o  m*/
 */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    super.execute();

    this.getLog().info("Purging previous patch files");

    try {
        Files.walk(this.getPatchDirectory().toPath())
                .filter((p) -> p.getFileName().toString().endsWith(".patch")).forEach((p) -> {
                    try {
                        Files.delete(p);
                    } catch (IOException ex) {
                        throw new WrappedException(ex);
                    }
                });
    } catch (IOException ex) {
        throw new MojoFailureException("Failed to access patch directory: " + ex.getMessage(), ex);
    } catch (WrappedException ex) {
        if (ex.getCause() instanceof IOException) {
            throw new MojoFailureException("Failed to delete patch file: " + ex.getMessage(), ex);
        }

        throw new MojoFailureException("Caught unexpected exception: " + ex.getMessage(), ex);
    }

    try {
        Repository repository = new FileRepositoryBuilder().setWorkTree(this.getSourceDirectory())
                .setMustExist(true).build();

        Git git = new Git(repository);

        if (!git.status().call().isClean()) {
            this.getLog().warn("One or more uncommited changes present within source directory\n");
            this.getLog().warn("Only commited changes will be considered in patch generation");
        }
    } catch (GitAPIException ex) {
        throw new MojoFailureException("Failed to invoke git: " + ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new MojoFailureException("Could not access module repository: " + ex.getMessage(), ex);
    }

    Path workingDirectory = this.getSourceDirectory().toPath().toAbsolutePath();
    Path relativePatchDirectory = workingDirectory.relativize(this.getPatchDirectory().toPath());

    try {
        if (this.execute(new ProcessBuilder()
                .command("git", "format-patch", "--minimal", "--no-stat", "-N", "-o",
                        relativePatchDirectory.toString(), "upstream")
                .directory(this.getSourceDirectory().getAbsoluteFile())) != 0) {
            throw new MojoFailureException("Git reported an unexpected error");
        }
    } catch (InterruptedException ex) {
        throw new MojoFailureException("Interrupted while awaiting git result: " + ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new MojoFailureException("Failed to invoke git: " + ex.getMessage(), ex);
    }
}

From source file:org.basinmc.maven.plugins.minecraft.patch.SafeguardMojo.java

License:Apache License

/**
 * {@inheritDoc}//from  w w  w  .j av a  2  s.  com
 */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    this.verifyProperties("sourceDirectory", "force");

    this.getLog().info("Running repository safeguard checks");

    if (this.isForced()) {
        this.getLog().warn("Skipping safeguard - Override is enabled\n" + "\n"
                + "+--------------------------------------+\n" + "|                                      |\n"
                + "|    THIS MAY CAUSE LOSS OF CHANGES    |\n" + "|         USE AT YOUR OWN RISK         |\n"
                + "|                                      |\n" + "+--------------------------------------+\n");
        return;
    }

    try {
        Repository repository = new FileRepositoryBuilder().setWorkTree(this.getSourceDirectory()).build();

        if (!repository.getObjectDatabase().exists()) {
            this.getLog().info("Skipping safeguard - No repository in source path");
            return;
        }

        Git git = new Git(repository);

        if (!git.status().call().isClean()) {
            this.getLog().error(
                    "The repository at " + this.getSourceDirectory().toString() + " is not in a clean state");
            this.getLog().error(
                    "As such the build will be halted - Please verify that all changes you wish to retain have been commited and turned into patch files");
            this.getLog().error(
                    "If you wish to override this behavior, start the build by passing -Dminecraft.force=true");

            throw new MojoFailureException("Repository is in a dirty state");
        }

        // TODO: Compare number of commits since first commit against amount of patches to
        // prevent accidental loss of changes due to the pending git reset operation
    } catch (GitAPIException ex) {
        throw new MojoFailureException("Failed to execute git command: " + ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new MojoFailureException("Could not access source repository: " + ex.getMessage(), ex);
    }
}

From source file:org.craftercms.commons.git.impl.GitRepositoryImplTest.java

License:Open Source License

@Test
public void testAdd() throws Exception {
    Git git = Git.init().setDirectory(tmpDir.getRoot()).call();
    GitRepositoryImpl repository = new GitRepositoryImpl(git);

    File testFile = new File(tmpDir.getRoot(), "test");
    testFile.createNewFile();/* w ww  .j a  va2  s.c o  m*/

    repository.add("test");

    Status status = git.status().call();

    assertNotNull(status);
    assertEquals(Collections.singleton("test"), status.getAdded());
}

From source file:org.craftercms.commons.git.impl.GitRepositoryImplTest.java

License:Open Source License

@Test
public void testRemove() throws Exception {
    Git git = Git.init().setDirectory(tmpDir.getRoot()).call();
    GitRepositoryImpl repository = new GitRepositoryImpl(git);

    File testFile = new File(tmpDir.getRoot(), "test");
    testFile.createNewFile();//from ww  w .  j av a  2 s  .c om

    git.add().addFilepattern("test").call();
    git.commit().setMessage("Test commit").call();

    repository.remove("test");

    Status status = git.status().call();

    assertNotNull(status);
    assertEquals(Collections.singleton("test"), status.getRemoved());
}

From source file:org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryHelper.java

License:Open Source License

/**
 * Perform an initial commit after large changes to a site. Will not work against the global config repo.
 * @param site/*from   w  w  w .  j  a v a  2s .c  o  m*/
 * @param message
 * @return true if successful, false otherwise
 */
public boolean performInitialCommit(String site, String message) {
    boolean toReturn = true;

    try {
        Repository repo = getRepository(site, GitRepositories.SANDBOX);

        Git git = new Git(repo);

        Status status = git.status().call();

        if (status.hasUncommittedChanges() || !status.isClean()) {
            DirCache dirCache = git.add().addFilepattern(GIT_COMMIT_ALL_ITEMS).call();
            RevCommit commit = git.commit().setMessage(message).call();
            // TODO: SJ: Do we need the commit id?
            // commitId = commit.getName();
        }
    } catch (GitAPIException err) {
        logger.error("error creating initial commit for site:  " + site, err);
        toReturn = false;
    }

    return toReturn;
}

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

License:Apache License

/**
 * We should be able to create a git repo over top of other version control systems, public void testInitOtherVcs() throws
 * Exception { }/*  ww  w .  j a va  2 s  . co  m*/
 *
 * /** When cleaning the source, we must take into account all types of dirty file states.
 */
@Test
public void testClean() throws Exception {
    Git git = Git.open(directory);

    File.createTempFile("unchanged", "file", directory);

    File subChanged = new File(new File(directory, "directory"), "changed");
    subChanged.getParentFile().mkdirs();
    subChanged.createNewFile();
    File changed = File.createTempFile("changed", "file", directory);

    git.add().addFilepattern(".").call();
    git.commit().setMessage("Prepare for test").call();

    source.initialize(directory, ORIGIN);

    Files.write("some data", subChanged, Charset.defaultCharset());
    Files.write("more data", changed, Charset.defaultCharset());
    File.createTempFile("new", "file", directory);

    File untrackedDir = new File(directory, "untracked");
    untrackedDir.mkdirs();
    File.createTempFile("another", "file", untrackedDir);

    source.clean();

    Status status = git.status().call();
    assertThat(status.getUntracked(), hasSize(0));
    assertThat(status.getUntrackedFolders(), hasSize(0));
    assertThat(status.getIgnoredNotInIndex(), hasSize(0));
    assertTrue("No changes", status.isClean());
}

From source file:org.eclipse.egit.core.test.SubmoduleAndContainerTreeIteratorTest.java

License:Open Source License

@Test
public void testCleanStateAfterInit() throws NoWorkTreeException, GitAPIException {
    Git parentGit = Git.wrap(parentRepository.getRepository());

    Status status = parentGit.status().setWorkingTreeIt(treeIt).call();
    assertTrue(status.isClean());//from  ww  w. j a  v a2  s  .  co  m
}

From source file:org.eclipse.egit.core.test.SubmoduleAndContainerTreeIteratorTest.java

License:Open Source License

@Test
public void testCleanStateFirstCommit()
        throws NoWorkTreeException, GitAPIException, IOException, CoreException, InterruptedException {
    testUtils.changeContentOfFile(parentProject, parentFile, "new content");
    RepositoryTestCase.fsTick(null);// w w w  .  j av a  2s  . co  m
    parentRepository.trackAllFiles(parentProject);
    parentRepository.commit("modified parent.txt");
    Git parentGit = Git.wrap(parentRepository.getRepository());

    Status status = parentGit.status().setWorkingTreeIt(treeIt).call();
    assertTrue(status.isClean());
}

From source file:org.eclipse.egit.ui.gitflow.FeatureRebaseHandlerTest.java

License:Open Source License

@Test
public void testRebaseFailOnConflict() throws Exception {
    disableAutomatedMode();//from  ww w.  j  av a2 s.c o m

    Git git = Git.wrap(repository);

    init();

    createFeature(FEATURE_NAME);
    setContentAddAndCommit("foo");

    checkoutBranch(DEVELOP);
    setContentAddAndCommit("bar");

    checkoutFeature(FEATURE_NAME);

    rebaseFeature();
    acceptError(RebaseResult.Status.STOPPED);

    Status status = git.status().call();
    Object[] conflicting = status.getConflicting().toArray();
    assertEquals(1, conflicting.length);
    assertEquals(FILE1_PATH, conflicting[0]);

    assertEquals("org.eclipse.egit.ui.InteractiveRebaseView", bot.activeView().getReference().getId());
}

From source file:org.eclipse.egit.ui.gitflow.FeatureRebaseHandlerTest.java

License:Open Source License

@Test
public void testRebaseFailOnDirtyWorkingDirectory() throws Exception {
    disableAutomatedMode();/*from  ww w  .j a v a2 s .co m*/

    Git git = Git.wrap(repository);

    init();
    setContentAddAndCommit("bar");

    createFeature(FEATURE_NAME);
    setContentAddAndCommit("foo");

    setTestFileContent("foobar");

    rebaseFeature();
    acceptError(RebaseResult.Status.UNCOMMITTED_CHANGES);

    Status status = git.status().call();
    Object[] uncommitted = status.getUncommittedChanges().toArray();
    assertEquals(1, uncommitted.length);
    assertEquals(FILE1_PATH, uncommitted[0]);
}