Java “怎么做?”;git筛选器分支--子目录<;folderName>&引用;用JGit

Java “怎么做?”;git筛选器分支--子目录<;folderName>&引用;用JGit,java,git,jgit,Java,Git,Jgit,我有很多maven多模块项目(大约50个)。每个模块都有1到4个子模块。每个多模块项目都位于一个单独的git存储库中。由于一个问题,我需要将模块拆分为单独的存储库。 我有一个shell脚本,可以将一个文件夹(一个maven模块)从RepoA移动到RepoB,保存它的git历史 #!/bin/sh # moves a folder from one git repository to another # moveFolder <absolute repository one path>

我有很多maven多模块项目(大约50个)。每个模块都有1到4个子模块。每个多模块项目都位于一个单独的git存储库中。由于一个问题,我需要将模块拆分为单独的存储库。

我有一个shell脚本,可以将一个文件夹(一个maven模块)从RepoA移动到RepoB,保存它的git历史

#!/bin/sh
# moves a folder from one git repository to another
# moveFolder <absolute repository one path> <repository one folder> <absolute repository two path>
echo "Moving "$2" from Repository: "$1" to Repository:"$3
# prepare repository one
cd $1
git clean -f -d -x
git checkout -b tmpBranch
git filter-branch -f --subdirectory-filter $2 HEAD
mkdir $2
mv * $2
git add .
git commit -a -m "CIRCLE-524: Moved "$2" into new repository"

#import in repository two
cd $3
git remote add repositoryOne $1
git pull repositoryOne tmpBranch
git remote rm repositoryOne

#cleanup
cd $1
git checkout master
git branch -D tmpBranch

revWalk
签出要处理的新tmpBranch,然后列出所有提交的时间和消息

fileWalk
采用标题,并按
modulePath

我想要实现git过滤器分支-f--子目录filder$2 HEAD我必须将这两种方法结合起来,但老实说,我不知道如何实现。
你能给我指出正确的方向并给我一些代码示例吗?

回答我自己的问题:

我使用ProcessBuilder执行给定的脚本,并执行了以下操作

public static void revWalk(final Git repo) throws NoWorkTreeException, GitAPIException, MissingObjectException, IncorrectObjectTypeException, IOException {
        final Ref tmpBranch = checkOutBranch(repo, "tmpBranch");
        final Repository repository = repo.getRepository();
        try (RevWalk revWalk = new RevWalk(repository)) {
            final ObjectId commitId = repository.resolve(tmpBranch.getName());
            revWalk.markStart(revWalk.parseCommit(commitId));
            for (final RevCommit commit : revWalk) {

                System.out.println("Time: " + commit.getAuthorIdent().getWhen() + " Message: " + commit.getShortMessage());
            }
        }
    }
private void executeScript(final String pathToGitBash, final File scriptFileToExecute, final String... arguments) throws IOException, InterruptedException {
        final int offset = 4;
        final String[] command = new String[arguments.length + offset];
        command[0] = pathToGitBash;
        command[1] = "--login";
        command[2] = "-i";
        command[3] = scriptFileToExecute.getAbsolutePath();
        for (int i = 0; i < arguments.length; i++) {
            command[i + offset] = arguments[i];
        }
        final ProcessBuilder builder = new ProcessBuilder(command);
        builder.redirectErrorStream(true);
        final Process process = builder.start();
        IOUtils.copy(process.getInputStream(), System.out);
        IOUtils.copy(process.getErrorStream(), System.err);
        switch (process.waitFor()) {
            case 1:
                LOGGER.error("Parameters for scripts are not correct.");
            break;
            case 0:
                LOGGER.info("Script seemed to execute properly");
            break;
            default:
                LOGGER.info("Unknown returnCode. Script finished with: {}", process.exitValue());
        }
    }
private void executeScript(最终字符串pathToGitBash、最终文件scriptFileToExecute、最终字符串…参数)引发IOException、InterruptedException{
最终整数偏移=4;
final String[]命令=新字符串[arguments.length+offset];
命令[0]=pathToGitBash;
命令[1]=“--login”;
命令[2]=“i”;
命令[3]=scriptFileToExecute.getAbsolutePath();
for(int i=0;i

要使用Gitbash执行它,我需要提供Gitbash/sh.exe和参数--login-I“

来回答我自己的问题:

我使用ProcessBuilder执行给定的脚本,并执行了以下操作

public static void revWalk(final Git repo) throws NoWorkTreeException, GitAPIException, MissingObjectException, IncorrectObjectTypeException, IOException {
        final Ref tmpBranch = checkOutBranch(repo, "tmpBranch");
        final Repository repository = repo.getRepository();
        try (RevWalk revWalk = new RevWalk(repository)) {
            final ObjectId commitId = repository.resolve(tmpBranch.getName());
            revWalk.markStart(revWalk.parseCommit(commitId));
            for (final RevCommit commit : revWalk) {

                System.out.println("Time: " + commit.getAuthorIdent().getWhen() + " Message: " + commit.getShortMessage());
            }
        }
    }
private void executeScript(final String pathToGitBash, final File scriptFileToExecute, final String... arguments) throws IOException, InterruptedException {
        final int offset = 4;
        final String[] command = new String[arguments.length + offset];
        command[0] = pathToGitBash;
        command[1] = "--login";
        command[2] = "-i";
        command[3] = scriptFileToExecute.getAbsolutePath();
        for (int i = 0; i < arguments.length; i++) {
            command[i + offset] = arguments[i];
        }
        final ProcessBuilder builder = new ProcessBuilder(command);
        builder.redirectErrorStream(true);
        final Process process = builder.start();
        IOUtils.copy(process.getInputStream(), System.out);
        IOUtils.copy(process.getErrorStream(), System.err);
        switch (process.waitFor()) {
            case 1:
                LOGGER.error("Parameters for scripts are not correct.");
            break;
            case 0:
                LOGGER.info("Script seemed to execute properly");
            break;
            default:
                LOGGER.info("Unknown returnCode. Script finished with: {}", process.exitValue());
        }
    }
private void executeScript(最终字符串pathToGitBash、最终文件scriptFileToExecute、最终字符串…参数)引发IOException、InterruptedException{
最终整数偏移=4;
final String[]命令=新字符串[arguments.length+offset];
命令[0]=pathToGitBash;
命令[1]=“--login”;
命令[2]=“i”;
命令[3]=scriptFileToExecute.getAbsolutePath();
for(int i=0;i
要使用Gitbash执行它,我需要提供Gitbash/sh.exe和参数“-login-I”