protected ScmResult executeBranchCommand()

in maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/branch/JGitBranchCommand.java [59:109]


    protected ScmResult executeBranchCommand(
            ScmProviderRepository repo, ScmFileSet fileSet, String branch, String message) throws ScmException {
        if (branch == null || StringUtils.isEmpty(branch.trim())) {
            throw new ScmException("branch name must be specified");
        }

        if (!fileSet.getFileList().isEmpty()) {
            throw new ScmException("This provider doesn't support branching subsets of a directory");
        }

        Git git = null;
        try {
            git = JGitUtils.openRepo(fileSet.getBasedir());
            Ref branchResult = git.branchCreate().setName(branch).call();
            logger.info("created [" + branchResult.getName() + "]");

            if (logger.isDebugEnabled()) {
                for (String branchName : getShortLocalBranchNames(git)) {
                    logger.debug("local branch available: " + branchName);
                }
            }

            if (repo.isPushChanges()) {
                logger.info("push branch [" + branch + "] to remote...");
                JGitUtils.push(git, (GitScmProviderRepository) repo, new RefSpec(Constants.R_HEADS + branch));
            }

            // search for the tagged files
            final RevWalk revWalk = new RevWalk(git.getRepository());
            RevCommit commit = revWalk.parseCommit(branchResult.getObjectId());
            revWalk.close();

            final TreeWalk walk = new TreeWalk(git.getRepository());
            walk.reset(); // drop the first empty tree, which we do not need here
            walk.setRecursive(true);
            walk.addTree(commit.getTree());

            List<ScmFile> files = new ArrayList<>();
            while (walk.next()) {
                files.add(new ScmFile(walk.getPathString(), ScmFileStatus.CHECKED_OUT));
            }
            walk.close();

            return new BranchScmResult("JGit branch", files);

        } catch (Exception e) {
            throw new ScmException("JGit branch failed!", e);
        } finally {
            JGitUtils.closeRepo(git);
        }
    }