Ref HEAD can not be resolved [message #1690221] |
Wed, 25 March 2015 18:55 |
Derrick McKee 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 |
Rüdiger Herrmann 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
|
|
|
|
Powered by
FUDForum. Page generated in 0.03351 seconds