怎么做&#34; git filter-branch --subdirectory <foldername>&#34;与JGit

时间:2017-04-25 05:55:45

标签: java git jgit

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

我有一个shell脚本将文件夹(maven模块)从Repo A移动到Repo B,保留其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

当我必须为少数项目执行此操作时,我可以手动执行此操作。但是因为我喜欢50个多模块项目,所以我想让它自动化。

我可以使用ProcessBuilder来执行此脚本。但拆分所有模块的过程将在Windows机器上完成 (当我手动执行此操作时,我使用Git Bash)

所以我发现JGit用java自动化我的整个过程。我的问题在于git filter-branch -f --subdirectory-filder $2 HEAD 到目前为止,我有以下几点。

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());
            }
        }
    }

public static void fileWalk(final Git repo, final String modulePath) throws IOException {
        final Ref head = repo.getRepository().findRef("HEAD");

        final RevWalk walk = new RevWalk(repo.getRepository());

        final RevCommit commit = walk.parseCommit(head.getObjectId());
        final RevTree tree = commit.getTree();
        System.out.println("Having tree: " + tree);

        final TreeWalk treeWalk = new TreeWalk(repo.getRepository());
        treeWalk.addTree(tree);
        treeWalk.setRecursive(true);
        treeWalk.setFilter(PathFilterGroup.createFromStrings(modulePath));
        while (treeWalk.next()) {
            System.out.println("found: " + treeWalk.getPathString());
        }
        walk.close();
        treeWalk.close();
    }

revWalk检出一个新的tmpBranch,然后列出所有提交时间和消息。

fileWalk获取HEAD,并按modulePath

列出与给定过滤器匹配的所有文件

我想实现git filter-branch -f --subdirectory-filder $2 HEAD我必须结合两种方式,但说实话,我不知道如何做到这一点。 能否请您指出正确的方向并给我一些代码示例?

1 个答案:

答案 0 :(得分:0)

回答我自己的问题:

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

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());
        }
    }

要使用Gitbash执行它我需要提供gitbash / sh.exe和参数&#34; - login -i&#34;

相关问题