]> source.dussan.org Git - jgit.git/commitdiff
Enhance RepositoryTestCase.commitFile() to work on empty repository 18/17618/2
authorMatthias Sohn <matthias.sohn@sap.com>
Sun, 20 Oct 2013 23:22:08 +0000 (01:22 +0200)
committerGerrit Code Review @ Eclipse.org <gerrit@eclipse.org>
Thu, 31 Oct 2013 12:28:46 +0000 (08:28 -0400)
Change-Id: Ic64497f0eedf8996ba593ca52dc9a040732a5b24
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/RepositoryTestCase.java

index 7b12ce1b213c49b93ae533aa5cd6118ab9e02b44..15334ee39611794af3588a7614e0e08a7e2441ea 100644 (file)
@@ -463,16 +463,25 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {
        protected RevCommit commitFile(String filename, String contents, String branch) {
                try {
                        Git git = new Git(db);
-                       String originalBranch = git.getRepository().getFullBranch();
-                       if (git.getRepository().getRef(branch) == null)
-                               git.branchCreate().setName(branch).call();
-                       git.checkout().setName(branch).call();
+                       Repository repo = git.getRepository();
+                       String originalBranch = repo.getFullBranch();
+                       boolean empty = repo.resolve(Constants.HEAD) == null;
+                       if (!empty) {
+                               if (repo.getRef(branch) == null)
+                                       git.branchCreate().setName(branch).call();
+                               git.checkout().setName(branch).call();
+                       }
+
                        writeTrashFile(filename, contents);
                        git.add().addFilepattern(filename).call();
                        RevCommit commit = git.commit()
                                        .setMessage(branch + ": " + filename).call();
+
                        if (originalBranch != null)
                                git.checkout().setName(originalBranch).call();
+                       else if (empty)
+                               git.branchCreate().setName(branch).setStartPoint(commit).call();
+
                        return commit;
                } catch (IOException e) {
                        throw new RuntimeException(e);