Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 339610 - NPE invoking ResetCommand in a new repository
Summary: NPE invoking ResetCommand in a new repository
Status: RESOLVED FIXED
Alias: None
Product: JGit
Classification: Technology
Component: JGit (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows 7
: P3 minor (vote)
Target Milestone: 1.1-M3   Edit
Assignee: Christian Halstrick CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 339609
  Show dependency tree
 
Reported: 2011-03-10 17:27 EST by John Arthorne CLA
Modified: 2011-08-21 17:15 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description John Arthorne CLA 2011-03-10 17:27:03 EST
1) Create a new repository
2) Perform AddCommand to add a file to the index
3) Perform ResetCommand to remove the file from index

NPE occurs. It is trying to obtain the id of the last commit, but since there are no commits it gets an NPE:

java.lang.NullPointerException
	at org.eclipse.jgit.lib.ObjectIdSubclassMap.index(ObjectIdSubclassMap.java:213)
	at org.eclipse.jgit.lib.ObjectIdSubclassMap.get(ObjectIdSubclassMap.java:89)
	at org.eclipse.jgit.revwalk.RevWalk.parseAny(RevWalk.java:809)
	at org.eclipse.jgit.revwalk.RevWalk.parseCommit(RevWalk.java:724)
	at org.eclipse.jgit.api.ResetCommand.call(ResetCommand.java:149)
	at org.eclipse.orion.server.git.servlets.GitIndexHandlerV1.handlePost(GitIndexHandlerV1.java:130)
Comment 1 Christian Halstrick CLA 2011-06-28 04:34:57 EDT
If you want to remove the file from the index using the ResetCommand in this case is not correct since the ResetCommand tries to reset the repository to the last commit, which does not exist (as you already said). Also on the git command line reset would not work in this case, but fail with an error message, saying that HEAD doesn't exist.

If you just want to remove the file from the index you should use (as in git command line) the rm command. 

    File tmpDir =
      new File(System.getProperty("java.io.tmpdir"), "tmp"
          + System.currentTimeMillis());
    
    Git repo = Git.init().setDirectory(new File(tmpDir, "repo1")).call();
    new FileWriter(new File(repo.getRepository().getWorkTree(), "f.txt"))
        .write("hello world");
    repo.add().addFilepattern("f.txt").call();
    repo.rm().addFilepattern("f.txt").call();

But I agree that your usage of resetCommand should not result in NPE. We should throw a better exception.
Comment 2 Chris Aniszczyk CLA 2011-08-21 17:15:53 EDT
Fixed in master.