jgit pull问题HEAD DETACHED

时间:2014-10-03 00:03:50

标签: jgit

我正在尝试使用jgit从git存储库中提取更改。

当我执行拉动

时,我遇到了HEAD DETACHED错误的小问题

我在stackoverflow上已经阅读了其他答案。尝试这些解决方案似乎没有帮助。

我有2个远程分支 分期 2.掌握

以下是示例代码:

    public static void main(String [] args){
    final String GIT_STR = "https://github.com/newlifer2/testNewLiferRepo.git";
    final String localRepositoryPath = new File("").getAbsolutePath() + File.separatorChar;

    try{
        //setting up local repository as just testNewLiferRepo
        String repoName = localRepositoryPath + "testNewLiferRepo";
        File f = new File(repoName);
        Repository localRepo = new FileRepository(repoName + File.separatorChar + ".git");
        ObjectId oldObjid = null;
        Git git = null;
        if (f.exists()) {
            oldObjid = localRepo.resolve(Constants.HEAD);
            git = Git.open(f);
            git.pull().call();
            ObjectId currentObjid = localRepo.resolve(Constants.HEAD);
            git.getRepository().close();
        } else {
            git = Git.cloneRepository().setURI(GIT_STR).setDirectory(new File(repoName)).call();
            ObjectId currentObjid =localRepo.resolve(Constants.HEAD);
            if(!localRepo.getBranch().equalsIgnoreCase("staging")){
                git.checkout().setName("refs/remotes/origin/staging").setForce(true).call();
            }
            git.getRepository().close();
        }
    } catch (InvalidRemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransportException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (GitAPIException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (RevisionSyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (AmbiguousObjectException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IncorrectObjectTypeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

您可以更新存储库上testXML文件中的版本以测试拉动。

任何指针都非常受欢迎

由于

1 个答案:

答案 0 :(得分:0)

看起来我的语句被切换了。这是一个新的解决方案

    public static void main(String [] args){
    final String GIT_STR = "https://github.com/newlifer2/testNewLiferRepo.git";
    final String localRepositoryPath = new File("").getAbsolutePath() + File.separatorChar;

    SSHSessionFactory sessionFactory = new SSHSessionFactory();
    sessionFactory.Initialize();
    SshSessionFactory.setInstance(sessionFactory);
    try{
        //setting up local repository as just testNewLiferRepo
        String repoName = localRepositoryPath + "testNewLiferRepo";
        File f = new File(repoName);
        Repository localRepo = new FileRepository(repoName + File.separatorChar + ".git");
        ObjectId oldObjid = null;
        Git git = null;
        if (f.exists()) {
            oldObjid = localRepo.resolve(Constants.HEAD);
            git = Git.open(f);
            Map<String,Ref> m = git.getRepository().getAllRefs();
            System.out.println("Current Branch: "+git.getRepository().getBranch());
            if(git.getRepository().isValidRefName("refs/remotes/origin/staging")){
                System.out.println("Valid");
            }
            git.pull().call();
            git.checkout().setName("staging").call();
            ObjectId currentObjid = localRepo.resolve(Constants.HEAD);

            git.getRepository().close();
        } else {
            git = Git.cloneRepository().setURI(GIT_STR).setDirectory(new File(repoName)).call();
            ObjectId currentObjid =localRepo.resolve(Constants.HEAD);
            if(!localRepo.getBranch().equalsIgnoreCase("staging")){
                git.branchCreate().setForce(true).setName("staging").setStartPoint("refs/remotes/origin/staging").call();
            }
            git.checkout().setName("staging").call();
            git.getRepository().close();
        }
    } catch (InvalidRemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransportException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (GitAPIException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (RevisionSyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (AmbiguousObjectException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IncorrectObjectTypeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}