简体   繁体   English

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

[英]Fetching with JGit doesn't show the last commit

I'm trying to write a simple program to use the basic functions of JGit.我正在尝试编写一个简单的程序来使用 JGit 的基本功能。 I don't want to use the default git merge tool but I'd like to fetch and then make the merge on my own.我不想使用默认的 git 合并工具,但我想获取然后自己进行合并。

I'm using the code below and I'm having a weird problem, when I call the update method just after pushing a commit, the FETCH_HEAD does not contain the commit I just pushed (see output).我正在使用下面的代码,但我遇到了一个奇怪的问题,当我在推送提交后调用 update 方法时,FETCH_HEAD 不包含我刚刚推送的提交(参见输出)。 But after using "git fetch" in git bash the fetching result contains the last commit.但是在 git bash 中使用“git fetch”后,获取结果包含最后一次提交。 So it seems that the fetch of the library is different from the actual git fetch.所以看起来库的fetch和实际的git fetch是不一样的。

I think it comes from the way I'm using git.fetch() but I've try with and without many options for the FetchCommand and it doesn't change anything.我认为它来自我使用git.fetch() 的方式,但我已经尝试过使用和不使用 FetchCommand 的很多选项,但它没有改变任何东西。 Maybe I've forgotten a kind of update somewhere... Could it come from getFetchCommit() ?也许我在某处忘记了某种更新......它可能来自getFetchCommit()吗?

OUTPUT:输出:

> update
Fetch : git@github.com:XXX/XXX.git
Repository C:\XXX\XXX\.git is up to date.
    last commit : commit2 - fcddf71c214784e511ccef73b2c083c0fcacd653

// Making some changes

> update
Fetch : git@github.com:XXX/XXX.git
You have uncommited changes, commit them (y/n)? : y
Added : 
    README.md
message : commit3
Commit : commit3 d50209b080bd0d474b79801bfdc808dd494d83fe
local commit : commit3 - d50209b080bd0d474b79801bfdc808dd494d83fe
fetch commit : commit2 - fcddf71c214784e511ccef73b2c083c0fcacd653
Not up to date, do you wish to : fpush - fpull - merge? : fpush
Push : git@github.com:XXX/XXX.git   // The push appears as excepted on the repo
> update
Fetch : git@github.com:XXX/XXX.git
local commit : commit3 - d50209b080bd0d474b79801bfdc808dd494d83fe
fetch commit : commit2 - fcddf71c214784e511ccef73b2c083c0fcacd653
Not up to date, do you wish to : fpush - fpull - merge? :    // Should to be up to date
>

// git fetch on git bash

> update
Fetch : git@github.com:XXX/XXX.git
Repository 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
    }
}

Wow it worked with resolve("origin/master") !哇它与resolve("origin/master") Thank you robinst.谢谢罗宾斯特。 Actually a friend also found another answer just when you answered by replacing getFetchCommit() by a new gitFetch()实际上,当您通过将getFetchCommit()替换为新的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());
}

But your solution robinst seems clearer so I'll take it!但是您的解决方案 robinst 似乎更清晰,所以我会接受!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM