Search code examples
javajgit

jgit pull issue HEAD DETACHED


I am trying to use JGit to pull the changes from a git repository.

I am facing minor issues with HEAD DETACHED error when I do a pull

I have read other answers here on StackOverflow. Trying those solutions does not seem to help.

I have 2 remote branches

  1. staging
  2. master

Here is the sample code:

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();
    }
}

You can update the version in the testXML file on the repository to test the pull.

Any pointers are highly appreciated

Thanks


Solution

  • Looks like I had my statements switched. Here is a new solution

    
    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();
        }
    }