Example usage for org.eclipse.jgit.api Status hasUncommittedChanges

List of usage examples for org.eclipse.jgit.api Status hasUncommittedChanges

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Status hasUncommittedChanges.

Prototype

boolean hasUncommittedChanges

To view the source code for org.eclipse.jgit.api Status hasUncommittedChanges.

Click Source Link

Usage

From source file:ch.sbb.releasetrain.git.GitRepoImpl.java

License:Apache License

@Override
public void addCommitPush() {
    try {//from   w  ww. j a  v a2  s .c o  m
        Git git = gitOpen();

        git.add().addFilepattern(".").call();

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

        // rm the deleted ones
        if (status.getMissing().size() > 0) {
            for (String rm : status.getMissing()) {
                git.rm().addFilepattern(rm).call();
            }
        }

        // commit and push if needed
        if (!status.hasUncommittedChanges()) {
            log.debug("not commiting git, because there are no changes");
            return;
        }

        git.commit().setMessage("Automatic commit by releasetrain").call();
        git.push().setCredentialsProvider(credentialsProvider()).call();
    } catch (Exception e) {
        throw new GitException(e.getMessage(), e);
    }
}

From source file:com.google.devtools.build.lib.bazel.repository.GitCloneFunction.java

License:Open Source License

private boolean isUpToDate(GitRepositoryDescriptor descriptor) {
    // Initializing/checking status of/etc submodules cleanly is hard, so don't try for now.
    if (descriptor.initSubmodules) {
        return false;
    }//from  www . j  a v  a  2  s .co m
    Repository repository = null;
    try {
        repository = new FileRepositoryBuilder()
                .setGitDir(descriptor.directory.getChild(Constants.DOT_GIT).getPathFile()).setMustExist(true)
                .build();
        ObjectId head = repository.resolve(Constants.HEAD);
        ObjectId checkout = repository.resolve(descriptor.checkout);
        if (head != null && checkout != null && head.equals(checkout)) {
            Status status = Git.wrap(repository).status().call();
            if (!status.hasUncommittedChanges()) {
                // new_git_repository puts (only) BUILD and WORKSPACE, and
                // git_repository doesn't add any files.
                Set<String> untracked = status.getUntracked();
                if (untracked.isEmpty() || (untracked.size() == 2 && untracked.contains("BUILD")
                        && untracked.contains("WORKSPACE"))) {
                    return true;
                }
            }
        }
    } catch (GitAPIException | IOException e) {
        // Any exceptions here, we'll just blow it away and try cloning fresh.
        // The fresh clone avoids any weirdness due to what's there and has nicer
        // error reporting.
    } finally {
        if (repository != null) {
            repository.close();
        }
    }
    return false;
}

From source file:com.google.devtools.build.lib.bazel.repository.GitCloner.java

License:Open Source License

private static boolean isUpToDate(GitRepositoryDescriptor descriptor) {
    // Initializing/checking status of/etc submodules cleanly is hard, so don't try for now.
    if (descriptor.initSubmodules) {
        return false;
    }/*from   ww  w. j a v a2s  .  c om*/
    Repository repository = null;
    try {
        repository = new FileRepositoryBuilder()
                .setGitDir(descriptor.directory.getChild(Constants.DOT_GIT).getPathFile()).setMustExist(true)
                .build();
        ObjectId head = repository.resolve(Constants.HEAD);
        ObjectId checkout = repository.resolve(descriptor.checkout);
        if (head != null && checkout != null && head.equals(checkout)) {
            Status status = Git.wrap(repository).status().call();
            if (!status.hasUncommittedChanges()) {
                // new_git_repository puts (only) BUILD and WORKSPACE, and
                // git_repository doesn't add any files.
                Set<String> untracked = status.getUntracked();
                if (untracked.isEmpty() || (untracked.size() == 2 && untracked.contains("BUILD")
                        && untracked.contains("WORKSPACE"))) {
                    return true;
                }
            }
        }
    } catch (GitAPIException | IOException e) {
        // Any exceptions here, we'll just blow it away and try cloning fresh.
        // The fresh clone avoids any weirdness due to what's there and has nicer
        // error reporting.
    } finally {
        if (repository != null) {
            repository.close();
        }
    }
    return false;
}

From source file:com.mooregreatsoftware.gitprocess.lib.GitLib.java

License:Apache License

@SuppressWarnings("RedundantCast")
public boolean hasUncommittedChanges() {
    final Status status = (@NonNull Status) e(() -> jgit.status().call());
    return status.hasUncommittedChanges();
}

From source file:com.spotify.docker.Git.java

License:Apache License

public String getCommitId() throws GitAPIException, DockerException, IOException, MojoExecutionException {

    if (repo == null) {
        throw new MojoExecutionException("Cannot tag with git commit ID because directory not a git repo");
    }// w w  w.j  a va 2  s . co m

    final StringBuilder result = new StringBuilder();

    try {
        // get the first 7 characters of the latest commit
        final ObjectId head = repo.resolve("HEAD");
        if (head == null || isNullOrEmpty(head.getName())) {
            return null;
        }

        result.append(head.getName().substring(0, 7));
        final org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git(repo);

        // append first git tag we find
        for (final Ref gitTag : git.tagList().call()) {
            if (gitTag.getObjectId().equals(head)) {
                // name is refs/tag/name, so get substring after last slash
                final String name = gitTag.getName();
                result.append(".");
                result.append(name.substring(name.lastIndexOf('/') + 1));
                break;
            }
        }

        // append '.DIRTY' if any files have been modified
        final Status status = git.status().call();
        if (status.hasUncommittedChanges()) {
            result.append(".DIRTY");
        }
    } finally {
        repo.close();
    }

    return result.length() == 0 ? null : result.toString();
}

From source file:com.spotify.docker.Utils.java

License:Apache License

public static String getGitCommitId()
        throws GitAPIException, DockerException, IOException, MojoExecutionException {

    final FileRepositoryBuilder builder = new FileRepositoryBuilder();
    builder.readEnvironment(); // scan environment GIT_* variables
    builder.findGitDir(); // scan up the file system tree

    if (builder.getGitDir() == null) {
        throw new MojoExecutionException("Cannot tag with git commit ID because directory not a git repo");
    }/*from ww  w .  j  a va  2 s. com*/

    final StringBuilder result = new StringBuilder();
    final Repository repo = builder.build();

    try {
        // get the first 7 characters of the latest commit
        final ObjectId head = repo.resolve("HEAD");
        result.append(head.getName().substring(0, 7));
        final Git git = new Git(repo);

        // append first git tag we find
        for (Ref gitTag : git.tagList().call()) {
            if (gitTag.getObjectId().equals(head)) {
                // name is refs/tag/name, so get substring after last slash
                final String name = gitTag.getName();
                result.append(".");
                result.append(name.substring(name.lastIndexOf('/') + 1));
                break;
            }
        }

        // append '.DIRTY' if any files have been modified
        final Status status = git.status().call();
        if (status.hasUncommittedChanges()) {
            result.append(".DIRTY");
        }
    } finally {
        repo.close();
    }

    return result.length() == 0 ? null : result.toString();
}

From source file:org.apache.nifi.registry.provider.flow.git.GitFlowMetaData.java

License:Apache License

boolean isGitDirectoryClean() throws GitAPIException {
    final Status status = new Git(gitRepo).status().call();
    return status.isClean() && !status.hasUncommittedChanges();
}

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

License:Open Source License

/**
 * bootstrap the repository/*from   w  ww .jav a 2  s  . c o  m*/
 */
public void bootstrap() throws Exception {
    // Initialize the helper
    helper = new GitContentRepositoryHelper(studioConfiguration, securityProvider);

    if (Boolean.parseBoolean(studioConfiguration.getProperty(BOOTSTRAP_REPO))) {
        if (helper.createGlobalRepo()) {
            // Copy the global config defaults to the global site
            // Build a path to the bootstrap repo (the repo that ships with Studio)
            String bootstrapFolderPath = this.ctx.getRealPath(
                    File.separator + BOOTSTRAP_REPO_PATH + File.separator + BOOTSTRAP_REPO_GLOBAL_PATH);
            Path source = java.nio.file.FileSystems.getDefault().getPath(bootstrapFolderPath);

            logger.info("Bootstrapping with baseline @ " + source.toFile().toString());

            // Copy the bootstrap repo to the global repo
            Path globalConfigPath = helper.buildRepoPath(GitRepositories.GLOBAL);
            TreeCopier tc = new TreeCopier(source, globalConfigPath);
            EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
            Files.walkFileTree(source, opts, Integer.MAX_VALUE, tc);

            Repository globalConfigRepo = helper.getRepository(StringUtils.EMPTY, GitRepositories.GLOBAL);
            try (Git git = new Git(globalConfigRepo)) {

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

                if (status.hasUncommittedChanges() || !status.isClean()) {
                    // Commit everything
                    // TODO: Consider what to do with the commitId in the future
                    git.add().addFilepattern(GIT_COMMIT_ALL_ITEMS).call();
                    git.commit().setMessage(INITIAL_COMMIT).call();
                }

                git.close();
            } catch (GitAPIException err) {
                logger.error("error creating initial commit for global configuration", err);
            }
        }
    }

    // Create global repository object
    if (!helper.buildGlobalRepo()) {
        logger.error("Failed to create global repository!");
    }
}

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  ww  w .  j  av a 2  s .  c om*/
 * @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.craftercms.studio.impl.v1.repository.git.GitContentRepositoryHelper.java

License:Open Source License

public String commitFile(Repository repo, String site, String path, String comment, PersonIdent user) {
    String commitId = null;// w w  w .  j  a  v  a  2  s.  c  o  m
    String gitPath = getGitPath(path);
    Status status;

    try (Git git = new Git(repo)) {
        status = git.status().addPath(gitPath).call();

        // TODO: SJ: Below needs more thought and refactoring to detect issues with git repo and report them
        if (status.hasUncommittedChanges() || !status.isClean()) {
            RevCommit commit;
            commit = git.commit().setOnly(gitPath).setAuthor(user).setCommitter(user).setMessage(comment)
                    .call();
            commitId = commit.getName();
        }

        git.close();
    } catch (GitAPIException e) {
        logger.error("error adding and committing file to git: site: " + site + " path: " + path, e);
    }

    return commitId;
}