"-6ren"> "-我有很多 Maven 多模块项目(大约 50 个)。每个都有 1 到 4 个子模块。每个多模块项目都位于一个单独的 git 存储库中。由于出现问题,我需要将模块拆分到单独的存储库中。 我有一个 she-6ren">
gpt4 book ai didi

java - 如何使用 JGit 执行 "git filter-branch --subdirectory "

转载 作者:太空宇宙 更新时间:2023-11-04 11:32:57 25 4
gpt4 key购买 nike

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

我有一个 shell 脚本,用于将文件夹(maven 模块)从存储库 A 移动到存储库 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我必须结合两种方式,但说实话我不知道该怎么做。您能否指出正确的方向并给我一些代码示例?

最佳答案

回答我自己的问题:

我使用 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 和参数“--login -i”

关于java - 如何使用 JGit 执行 "git filter-branch --subdirectory <folderName>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43602710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com