gpt4 book ai didi

java - 使用 JGit 获取不显示最后一次提交

转载 作者:太空宇宙 更新时间:2023-11-04 14:43:43 26 4
gpt4 key购买 nike

我正在尝试编写一个简单的程序来使用JGit的基本功能。我不想使用默认的 git merge 工具,但我想自己获取并进行 merge 。

我正在使用下面的代码,但遇到了一个奇怪的问题,当我在推送提交后调用更新方法时,FETCH_HEAD 不包含我刚刚推送的提交(请参阅输出)。但是在 git bash 中使用“git fetch”后,获取结果包含最后一次提交。所以看来库的fetch和实际的git fetch是不一样的。

我认为它来自于我使用 git.fetch() 的方式,但我尝试过使用和不使用 FetchCommand 的许多选项,但它不会改变任何东西。也许我在某处忘记了某种更新...它可能来自 getFetchCommit() 吗?

输出:

> updateFetch : git@github.com:XXX/XXX.gitRepository C:\XXX\XXX\.git is up to date.    last commit : commit2 - fcddf71c214784e511ccef73b2c083c0fcacd653// Making some changes> updateFetch : git@github.com:XXX/XXX.gitYou have uncommited changes, commit them (y/n)? : yAdded :     README.mdmessage : commit3Commit : commit3 d50209b080bd0d474b79801bfdc808dd494d83felocal commit : commit3 - d50209b080bd0d474b79801bfdc808dd494d83fefetch commit : commit2 - fcddf71c214784e511ccef73b2c083c0fcacd653Not up to date, do you wish to : fpush - fpull - merge? : fpushPush : git@github.com:XXX/XXX.git   // The push appears as excepted on the repo> updateFetch : git@github.com:XXX/XXX.gitlocal commit : commit3 - d50209b080bd0d474b79801bfdc808dd494d83fefetch commit : commit2 - fcddf71c214784e511ccef73b2c083c0fcacd653Not up to date, do you wish to : fpush - fpull - merge? :    // Should to be up to date>// git fetch on git bash> updateFetch : git@github.com:XXX/XXX.gitRepository C:\XXX\XXX\.git is up to date.    last commit : commit3 - d50209b080bd0d474b79801bfdc808dd494d83fe

CODE:

public void gitFetch() throws Exception
{
FetchResult fetch = git.fetch().setRemote("origin").call();
System.out.println("Fetch : " + fetch.getURI().toString());
}

public void gitPush(boolean force) throws Exception
{
Iterable<PushResult> push = git.push().setForce(force).call();
System.out.println("Push : " + push.iterator().next().getURI().toString());
}

public RevCommit getLocalCommit() throws Exception
{
Iterable<RevCommit> logL = git.log().call();
return logL.iterator().next();
}

public RevCommit getFetchCommit() throws Exception
{
gitFetch();
Iterable<RevCommit> log = git.log().add(git.getRepository().resolve("FETCH_HEAD")).call();
return log.iterator().next();
}

public void gitUpdate() throws Exception
{
RevCommit localCommit = getLocalCommit();
RevCommit fetchCommit = getFetchCommit();

if (fetchCommit.getId().equals(localCommit.getId()))
{
System.out.println("Repository " + localRepo.getDirectory().getAbsolutePath() + " is up to date.");
printCommit(localCommit, "\tlast");
}
else
{
printCommit(localCommit, "local");
printCommit(fetchCommit, "fetch");
String input = readInput("Not up to date, do you wish to : fpush - fpull - merge?");

if (input.equals("fpush"))
gitPush(true);
else if (input.equals("fpull"))
gitPull();
else if (input.equals("merge"))
// Do my own merge
}
}

最佳答案

哇,它与 resolve("origin/master") 一起工作!谢谢罗宾斯特。其实当你回答替换 getFetchCommit() 时, friend 也找到了另一个答案。通过新 gitFetch()

public RevCommit gitFetch() throws Exception
{
FetchResult result = git.fetch().setRemote("origin").call();
System.out.println("Fetch : " + result.getURI().toString());
return revWalk.parseCommit(result.getAdvertisedRef("refs/heads/" + localRepo.getBranch()).getTarget().getObjectId());
}

但是你的解决方案 robinst 似乎更清晰,所以我会接受它!

关于java - 使用 JGit 获取不显示最后一次提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24682310/

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