代码迁移过程– GitHub-概述
问题:
我正在尝试从语法上检查开发人员是否已将其代码合并到主代码中。我无法执行此步骤。我审阅过的所有示例均已提交给分支机构和master。此后,示例随后检查分支是否已提交。这对我的程序而言不是可行的选择,因为合并是在批准完成后在GitHub中进行的。
当我克隆存储库JGit并尝试检查其他分支是否已合并时,解析提交失败并返回null。反之亦然,如果我克隆了分支并检查master是否有提交,它也会失败。很明显,我在这里做错了。我正在谈论的示例代码:
try( RevWalk revWalk = new RevWalk( repository ) ) {
RevCommit masterHead = revWalk.parseCommit( repository.resolve( "refs/heads/master" );
RevCommit otherHead = revWalk.parseCommit( repository.resolve( "refs/heads/other-branch" );
if( revWalk.isMergedInto( otherHead, masterHead ) ) {
...
}
}
我还尝试查看存储库状态,但这也不起作用。如果除了使用git命令行工具之外还有其他想法,我将不胜感激。
其他信息
Java 1.7版-这就是我们的产品。
Jar版本-org.eclipse.jgit-4.5.4.201711221230-r.jar
我查看的示例:
https://gist.github.com/rherrmann/433adb44b3d15ed0f0c7
https://www.codeaffine.com/2015/12/15/getting-started-with-jgit/
How to determine with JGit which branches have been merged to master?
我的代码
public class GitHubTest
{
public Repository repository;
public GitHubTest()
{
}
public static ArrayList<String> fetchGitBranches(String gitUrl)
{
Collection<Ref> refs;
ArrayList<String> branches = new ArrayList<String>();
try {
refs = Git.lsRemoteRepository().setHeads(true)
.setRemote(gitUrl)
.setCredentialsProvider(new UsernamePasswordCredentialsProvider("token", "not applicable stack exchange"))
.call();
for (Ref ref: refs) {
branches.add(ref.getName().substring(ref.getName().lastIndexOf("/") + 1, ref.getName().length()));
System.out.println(ref.getName());
}
Collections.sort(branches);
} catch (InvalidRemoteException e) {
e.printStackTrace();
} catch (TransportException e) {
e.printStackTrace();
} catch (GitAPIException e) {
e.printStackTrace();
}
return branches;
}
public void getRepositoryCode()
{
try {
Git git =
Git.cloneRepository()
.setURI("https://github.com/FICOCorp/iww-test-create-1552931848516.git")
.setCredentialsProvider(new UsernamePasswordCredentialsProvider("token", "not applicable stack exchange"))
.setBranch("IKE_TEST2")
.setDirectory(new File("C:\\z_git")).call();
} catch (TransportException e) {
e.printStackTrace();
} catch (InvalidRemoteException e) {
e.printStackTrace();
} catch (GitAPIException e) {
e.printStackTrace();
}
}
public void setRespository(String repo)
{
try {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
repository =
builder.findGitDir(new File(repo)).readEnvironment()
.build();
} catch (Exception e) {
e.printStackTrace();
}
}
public void isBranchMerged( String branch)
{
try {
Git git = new Git(repository);
System.out.println("git.getRepository().getFullBranch() ="+git.getRepository().getFullBranch() );
RepositoryState r = git.getRepository().getRepositoryState();
System.out.println("r.canCommit(); =" + r.canCommit());
//System.out.println("r.canCommit(); ="+r. );
if (r == RepositoryState.MERGING) {
System.out.println("RepositoryState.MERGING = Y");
}
else {
System.out.println("RepositoryState.MERGING = N");
}
if (r == RepositoryState.MERGING_RESOLVED) {
System.out.println("RepositoryState.MERGING_RESOLVED = Y");
}
else {
System.out.println("RepositoryState.MERGING_RESOLVED = N");
}
Ref master = repository.findRef("master");
// Get the object the reference points to
ObjectId masterTip = master.getObjectId();
} catch (Exception e) {
e.printStackTrace();
System.out.println( e.getMessage());
}
}
public static void main(String[] args)
{
GitHubTest gitHubTest = new GitHubTest();
gitHubTest.getRepositoryCode();
ArrayList<String> al = gitHubTest.fetchGitBranches("https://github.com/FICOCorp/iww-test-create-1552931848516.git");
for (String s: al) {
System.out.println("Known Branches " + s);
}
//gitHubTest.getRepositoryDetails();
gitHubTest.setRespository("C:\\z_git\\.git");
gitHubTest.isBranchMerged("IKE_TEST2");
//gitHubTest.listUncommittedChanges();
}
}
屏幕截图
答案 0 :(得分:0)
JGit无法做到这一点。解决方案是在GitHub中调用rest API,特别是“拉”请求服务。