public ScmResult executeCommand()

in maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/checkout/GitCheckOutCommand.java [68:149]


    public ScmResult executeCommand(ScmProviderRepository repo, ScmFileSet fileSet, CommandParameters parameters)
            throws ScmException {
        ScmVersion version = parameters.getScmVersion(CommandParameter.SCM_VERSION, null);
        boolean binary = parameters.getBoolean(CommandParameter.BINARY, false);
        boolean shallow = parameters.getBoolean(CommandParameter.SHALLOW, false);

        GitScmProviderRepository repository = (GitScmProviderRepository) repo;

        if (GitScmProviderRepository.PROTOCOL_FILE.equals(
                        repository.getFetchInfo().getProtocol())
                && repository
                                .getFetchInfo()
                                .getPath()
                                .indexOf(fileSet.getBasedir().getPath())
                        >= 0) {
            throw new ScmException("remote repository must not be the working directory");
        }

        int exitCode;

        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();

        String lastCommandLine = "git-nothing-to-do";

        if (!fileSet.getBasedir().exists() || !(new File(fileSet.getBasedir(), ".git").exists())) {
            if (fileSet.getBasedir().exists()) {
                // git refuses to clone otherwise
                fileSet.getBasedir().delete();
            }

            // no git repo seems to exist, let's clone the original repo
            Commandline gitClone = createCloneCommand(repository, fileSet.getBasedir(), version, binary, shallow);

            exitCode = GitCommandLineUtils.execute(gitClone, stdout, stderr);
            if (exitCode != 0) {
                return new CheckOutScmResult(
                        gitClone.toString(), "The git clone command failed.", stderr.getOutput(), false);
            }
            lastCommandLine = gitClone.toString();
        }

        GitRemoteInfoCommand gitRemoteInfoCommand = new GitRemoteInfoCommand(environmentVariables);

        RemoteInfoScmResult result = gitRemoteInfoCommand.executeRemoteInfoCommand(repository, null, null);

        if (fileSet.getBasedir().exists()
                && new File(fileSet.getBasedir(), ".git").exists()
                && result.getBranches().size() > 0) {
            // git repo exists, so we must git-pull the changes
            Commandline gitPull = createPullCommand(repository, fileSet.getBasedir(), version);

            exitCode = GitCommandLineUtils.execute(gitPull, stdout, stderr);
            if (exitCode != 0) {
                return new CheckOutScmResult(
                        gitPull.toString(), "The git pull command failed.", stderr.getOutput(), false);
            }

            // and now let's do the git-checkout itself
            Commandline gitCheckout = createCommandLine(repository, fileSet.getBasedir(), version);

            exitCode = GitCommandLineUtils.execute(gitCheckout, stdout, stderr);
            if (exitCode != 0) {
                return new CheckOutScmResult(
                        gitCheckout.toString(), "The git checkout command failed.", stderr.getOutput(), false);
            }
            lastCommandLine = gitCheckout.toString();
        }

        // and now search for the files
        GitListConsumer listConsumer = new GitListConsumer(fileSet.getBasedir(), ScmFileStatus.CHECKED_IN);

        Commandline gitList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());

        exitCode = GitCommandLineUtils.execute(gitList, listConsumer, stderr);
        if (exitCode != 0) {
            return new CheckOutScmResult(
                    gitList.toString(), "The git ls-files command failed.", stderr.getOutput(), false);
        }

        return new CheckOutScmResult(lastCommandLine, listConsumer.getListedFiles());
    }