Last active
July 29, 2024 09:00
-
-
Save rherrmann/4bacb68b23be1f12c73d to your computer and use it in GitHub Desktop.
'Learning Tests' that use the JGit API to create new Git repositories: http://www.codeaffine.com/2015/05/06/jgit-initialize-repository/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************************************************************** | |
* Copyright (c) 2015 Rüdiger Herrmann | |
* All rights reserved. This program and the accompanying materials are made available under the | |
* terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available | |
* at http://www.eclipse.org/legal/epl-v10.html | |
* | |
* Contributors: | |
* Rüdiger Herrmann - initial API and implementation | |
**************************************************************************************************/ | |
package com.codeaffine.jgit.example; | |
import static org.eclipse.jgit.lib.Constants.HEAD; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNotNull; | |
import static org.junit.Assert.assertTrue; | |
import java.io.File; | |
import org.eclipse.jgit.api.Git; | |
import org.eclipse.jgit.lib.Repository; | |
import org.eclipse.jgit.lib.StoredConfig; | |
import org.eclipse.jgit.revwalk.RevCommit; | |
import org.eclipse.jgit.storage.file.FileRepositoryBuilder; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.TemporaryFolder; | |
public class InitLearningTest { | |
@Rule | |
public final TemporaryFolder tempFolder = new TemporaryFolder(); | |
@Test | |
public void testInit() throws Exception { | |
File directory = new File ( tempFolder.getRoot(), "repo" ); | |
Git git = Git.init().setDirectory( directory ).call(); | |
assertNotNull( git.getRepository().getRef( HEAD ) ); | |
assertTrue( git.status().call().isClean() ); | |
} | |
@Test | |
public void testInitExistingRepository() throws Exception { | |
File directory = new File ( tempFolder.getRoot(), "repo" ); | |
Git git1 = Git.init().setDirectory( directory ).call(); | |
File file = new File( git1.getRepository().getWorkTree(), "new-file" ); | |
file.createNewFile(); | |
git1.add().addFilepattern( "." ).call(); | |
RevCommit commit = git1.commit().setMessage( "First commit" ).call(); | |
Git git2 = Git.init().setDirectory( directory ).call(); | |
assertTrue( file.exists() ); | |
assertEquals( commit.getId(), git2.getRepository().resolve( HEAD ) ); | |
assertEquals( git1.getRepository().getDirectory(), git2.getRepository().getDirectory() ); | |
} | |
@Test | |
public void testInitNonEmptyDirectory() throws Exception { | |
File directory = new File ( tempFolder.getRoot(), "repo" ); | |
directory.mkdir(); | |
File file = new File( directory, "new-file" ); | |
file.createNewFile(); | |
Git git = Git.init().setDirectory( directory ).call(); | |
assertNotNull( git.getRepository().getRef( HEAD ) ); | |
assertTrue( git.status().call().getUntracked().contains( file.getName() ) ); | |
} | |
@Test | |
public void testInitRepositoryWithSeparateWorkDir() throws Exception { | |
File gitDir = new File ( tempFolder.getRoot(), "repo" ); | |
File workDir = new File ( tempFolder.getRoot(), "workdir" ); | |
Git git = Git.init().setDirectory( workDir ).setGitDir( gitDir ).call(); | |
assertNotNull( git.getRepository().getRef( HEAD ) ); | |
assertEquals( gitDir, git.getRepository().getDirectory() ); | |
assertEquals( workDir, git.getRepository().getWorkTree() ); | |
} | |
@Test | |
public void testInitBareRepositoryWithSetDirectory() throws Exception { | |
File directory = new File ( tempFolder.getRoot(), "repo" ); | |
Git git = Git.init().setDirectory( directory ).setBare( true ).call(); | |
assertTrue( git.getRepository().isBare() ); | |
assertNotNull( git.getRepository().getRef( HEAD ) ); | |
assertEquals( directory, git.getRepository().getDirectory() ); | |
} | |
@Test | |
public void testInitBareRepositoryWithSetGitDir() throws Exception { | |
File directory = new File ( tempFolder.getRoot(), "repo" ); | |
Git git = Git.init().setGitDir( directory ).setBare( true ).call(); | |
assertTrue( git.getRepository().isBare() ); | |
assertNotNull( git.getRepository().getRef( HEAD ) ); | |
assertEquals( directory, git.getRepository().getDirectory() ); | |
} | |
@Test | |
public void testInitNonExistingDirectory() throws Exception { | |
File directory = new File ( tempFolder.getRoot(), "non-existing/path/to/repo" ); | |
Git git = Git.init().setDirectory( directory ).call(); | |
assertNotNull( git.getRepository().getRef( HEAD ) ); | |
assertTrue( git.status().call().isClean() ); | |
} | |
@Test | |
public void testChangeWorkDir() throws Exception { | |
File workDir = tempFolder.newFolder( "workdir" ); | |
File directory = new File ( tempFolder.getRoot(), "repo" ); | |
Git git = Git.init().setDirectory( directory ).call(); | |
StoredConfig config = git.getRepository().getConfig(); | |
config.setString( "core", null, "worktree", workDir.getCanonicalPath() ); | |
config.save(); | |
assertEquals( workDir, Git.open( directory ).getRepository().getWorkTree() ); | |
} | |
@Test | |
public void testRepositoryCreate() throws Exception { | |
File workDir = new File ( tempFolder.getRoot(), "repo" ); | |
File gitDir = new File ( workDir, ".git" ); | |
Repository repository = new FileRepositoryBuilder().setGitDir( gitDir ).build(); | |
repository.create(); | |
assertNotNull( repository.getRef( HEAD ) ); | |
assertTrue( Git.wrap( repository ).status().call().isClean() ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment