protected CheckInScmResult executeCheckInCommand()

in maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/checkin/GitCheckInCommand.java [65:181]


    protected CheckInScmResult executeCheckInCommand(
            ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion version) throws ScmException {
        GitScmProviderRepository repository = (GitScmProviderRepository) repo;

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

        int exitCode = -1;

        File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
        try {
            FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8", message);
        } catch (IOException ex) {
            return new CheckInScmResult(
                    null,
                    "Error while making a temporary file for the commit message: " + ex.getMessage(),
                    null,
                    false);
        }

        try {
            if (!fileSet.getFileList().isEmpty()) {
                // if specific fileSet is given, we have to git-add them first
                // otherwise we will use 'git-commit -a' later

                Commandline clAdd = null;

                // SCM-714: Workaround for the Windows terminal command limit
                if (Os.isFamily(Os.FAMILY_WINDOWS)) {
                    for (File file : fileSet.getFileList()) {
                        clAdd = GitAddCommand.createCommandLine(fileSet.getBasedir(), Collections.singletonList(file));
                        exitCode = GitCommandLineUtils.execute(clAdd, stdout, stderr);

                        if (exitCode != 0) {
                            break;
                        }
                    }
                } else {
                    clAdd = GitAddCommand.createCommandLine(fileSet.getBasedir(), fileSet.getFileList());
                    exitCode = GitCommandLineUtils.execute(clAdd, stdout, stderr);
                }

                if (exitCode != 0) {
                    return new CheckInScmResult(
                            clAdd.toString(), "The git-add command failed.", stderr.getOutput(), false);
                }
            }

            // SCM-709: statusCommand uses repositoryRoot instead of workingDirectory, adjust it with
            // relativeRepositoryPath
            URI relativeRepositoryPath = GitStatusCommand.getRelativeCWD(logger, fileSet);

            // git-commit doesn't show single files, but only summary :/
            // so we must run git-status and consume the output
            // borrow a few things from the git-status command
            Commandline clStatus = GitStatusCommand.createCommandLine(repository, fileSet);

            GitStatusConsumer statusConsumer =
                    new GitStatusConsumer(fileSet.getBasedir(), relativeRepositoryPath, fileSet);
            exitCode = GitCommandLineUtils.execute(clStatus, statusConsumer, stderr);
            if (exitCode != 0) {
                // git-status returns non-zero if nothing to do
                if (logger.isInfoEnabled()) {
                    logger.info("nothing added to commit but untracked files present (use \"git add\" to " + "track)");
                }
            }

            if (statusConsumer.getChangedFiles().isEmpty()) {
                return new CheckInScmResult(null, statusConsumer.getChangedFiles());
            }

            Commandline clCommit = createCommitCommandLine(repository, fileSet, messageFile, environmentVariables);

            exitCode = GitCommandLineUtils.execute(clCommit, stdout, stderr);
            if (exitCode != 0) {
                return new CheckInScmResult(
                        clCommit.toString(), "The git-commit command failed.", stderr.getOutput(), false);
            }

            if (repo.isPushChanges()) {
                Commandline cl = createPushCommandLine(repository, fileSet, version);

                exitCode = GitCommandLineUtils.execute(cl, stdout, stderr);
                if (exitCode != 0) {
                    return new CheckInScmResult(
                            cl.toString(), "The git-push command failed.", stderr.getOutput(), false);
                }
            }

            List<ScmFile> checkedInFiles =
                    new ArrayList<>(statusConsumer.getChangedFiles().size());

            // rewrite all detected files to now have status 'checked_in'
            for (ScmFile changedFile : statusConsumer.getChangedFiles()) {
                ScmFile scmfile = new ScmFile(changedFile.getPath(), ScmFileStatus.CHECKED_IN);

                if (fileSet.getFileList().isEmpty()) {
                    checkedInFiles.add(scmfile);
                } else {
                    // if a specific fileSet is given, we have to check if the file is really tracked
                    for (File f : fileSet.getFileList()) {
                        if (FilenameUtils.separatorsToUnix(f.getPath()).equals(scmfile.getPath())) {
                            checkedInFiles.add(scmfile);
                        }
                    }
                }
            }

            return new CheckInScmResult(clCommit.toString(), checkedInFiles);
        } finally {
            try {
                FileUtils.forceDelete(messageFile);
            } catch (IOException ex) {
                // ignore
            }
        }
    }