Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » Ref HEAD can not be resolved
Ref HEAD can not be resolved [message #1690221] Wed, 25 March 2015 18:55 Go to next message
Derrick McKee is currently offline Derrick McKeeFriend
Messages: 2
Registered: March 2015
Junior Member
I am trying to create an empty git, then immediately create a new branch, check in files from an Eclipse IProject, and commit to the new branch. However, when I try to create the new branch, I get an Exception with the message, "Ref HEAD can not be resolved".

Looking at the git directory that my code creates seems to be in order, and I can't figure out what is wrong with my code. Here's the relevant parts:

Code to create the empty git repository
  private Git createGit() {
    try {
      Git result = Git.init().setDirectory(new File(location)).call();
      return result;
    } catch (GitAPIException e) {
      e.printStackTrace();
      return null;
    }
  }


Code to create the new branch, copy files from IProject to the git repository, and check in the new files.

  public RevCommit copyProjectToRepo(Git git, IProject project) throws CoreException,
      NoFilepatternException, GitAPIException, IOException {
    if (project != null) {
      git.checkout().setName(Long.toString(new Date().getTime())).setCreateBranch(true).call();
      IResource[] resources = project.members();
      for (IResource resource : resources) {
        if (resource instanceof IFile) {
          File orig = new File(resource.getLocationURI());
          File gitFile =
              new File(git.getRepository().getWorkTree() + File.separator + orig.getName());
          CopyOption[] options = new CopyOption[] {StandardCopyOption.REPLACE_EXISTING};
          Files.copy(orig.toPath(), gitFile.toPath(), options);
        }
      }

      // TODO: Add in pull command first
      git.add().addFilepattern("*").call();
      RevCommit result = git.commit().setMessage("initial commit").call();
      git.push().call();
      return result;
    } else {
      return null;
    }
  }
Re: Ref HEAD can not be resolved [message #1690323 is a reply to message #1690221] Thu, 26 March 2015 15:27 Go to previous messageGo to next message
Rüdiger Herrmann is currently offline Rüdiger HerrmannFriend
Messages: 581
Registered: July 2009
Senior Member
Derrick,

an empty repository does not have a HEAD yet (at least when created by
JGit).

If you add and commit the files right away, a HEAD that points to branch
'master' will be created. You can then rename that branch with

git.branchRename().setNewName( "foo" ).call()

HTH
Rüdiger
http://codeaffine.com

On 26.03.2015 15:52, Derrick McKee wrote:
> I am trying to create an empty git, then immediately create a new
> branch, check in files from an Eclipse IProject, and commit to the new
> branch. However, when I try to create the new branch, I get an
> Exception with the message, "Ref HEAD can not be resolved".
>
> Looking at the git directory that my code creates seems to be in order,
> and I can't figure out what is wrong with my code. Here's the relevant
> parts:
>
> Code to create the empty git repository
> private Git createGit() {
> try {
> Git result = Git.init().setDirectory(new File(location)).call();
> return result;
> } catch (GitAPIException e) {
> e.printStackTrace();
> return null;
> }
> }
>
> Code to create the new branch, copy files from IProject to the git
> repository, and check in the new files.
>
>
> public RevCommit copyProjectToRepo(Git git, IProject project) throws
> CoreException,
> NoFilepatternException, GitAPIException, IOException {
> if (project != null) {
> git.checkout().setName(Long.toString(new
> Date().getTime())).setCreateBranch(true).call();
> IResource[] resources = project.members();
> for (IResource resource : resources) {
> if (resource instanceof IFile) {
> File orig = new File(resource.getLocationURI());
> File gitFile =
> new File(git.getRepository().getWorkTree() +
> File.separator + orig.getName());
> CopyOption[] options = new CopyOption[]
> {StandardCopyOption.REPLACE_EXISTING};
> Files.copy(orig.toPath(), gitFile.toPath(), options);
> }
> }
>
> // TODO: Add in pull command first
> git.add().addFilepattern("*").call();
> RevCommit result = git.commit().setMessage("initial commit").call();
> git.push().call();
> return result;
> } else {
> return null;
> }
> }


--
Rüdiger Herrmann
http://codeaffine.com
Re: Ref HEAD can not be resolved [message #1690325 is a reply to message #1690221] Thu, 26 March 2015 15:42 Go to previous message
Derrick McKee is currently offline Derrick McKeeFriend
Messages: 2
Registered: March 2015
Junior Member
Yeah, I finally got that figured out. Thanks for the response.
Previous Topic:JGit clone fails with "Auth fail"
Next Topic:Problems starting with EGit development
Goto Forum:
  


Current Time: Wed Apr 24 21:14:59 GMT 2024

Powered by FUDForum. Page generated in 0.03351 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top