Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View SubmoduleLearningTest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
/*******************************************************************************
* Copyright (c) 2014 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.ConfigConstants.CONFIG_SUBMODULE_SECTION;
import static org.eclipse.jgit.lib.Constants.DOT_GIT_MODULES;
import static org.eclipse.jgit.submodule.SubmoduleStatusType.INITIALIZED;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
 
import java.io.File;
import java.io.IOException;
import java.util.Map;
 
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.SubmoduleAddCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.submodule.SubmoduleStatus;
import org.eclipse.jgit.submodule.SubmoduleWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
 
 
public class SubmoduleLearningTest {
 
@Rule
public final TemporaryFolder tempFolder = new TemporaryFolder();
private Git library;
private Git parent;
@Test
public void testAddSubmodule() throws Exception {
String uri = library.getRepository().getDirectory().getCanonicalPath();
SubmoduleAddCommand addCommand = parent.submoduleAdd();
addCommand.setURI( uri );
addCommand.setPath( "modules/library" );
Repository repository = addCommand.call();
repository.close();
File parentWorkDir = parent.getRepository().getWorkTree();
assertTrue( new File( parentWorkDir, "modules/library" ).isDirectory() );
assertTrue( new File( parentWorkDir, "modules/library/readme.txt" ).isFile() );
assertTrue( new File( parentWorkDir, ".gitmodules" ).isFile() );
}
@Test
public void testListSubmodules() throws Exception {
addLibrarySubmodule();
Map<String, SubmoduleStatus> submodules = parent.submoduleStatus().call();
assertEquals( 1, submodules.size() );
SubmoduleStatus submoduleStatus = submodules.get( "modules/library" );
assertEquals( INITIALIZED, submoduleStatus.getType() );
}
 
@Test
public void testUpdateSubmodule() throws Exception {
addLibrarySubmodule();
ObjectId newHead = library.commit().setMessage( "Another change" ).call();
File workDir = parent.getRepository().getWorkTree();
Git librarySubmodule = Git.open( new File( workDir, "modules/library" ) );
librarySubmodule.pull().call();
librarySubmodule.close();
parent.add().addFilepattern( "modules/library" ).call();
parent.commit().setMessage( "Update submodule" ).call();
assertEquals( newHead, getSubmoduleHead( "modules/library" ) );
}
 
@Test
public void testRemoveSubmodule() throws Exception {
String uri = library.getRepository().getDirectory().getCanonicalPath();
Repository repository = parent.submoduleAdd().setURI( uri ).setPath( "modules/library" ).call();
repository.close();
removeSubmodule( parent.getRepository(), "modules/library" );
Config gitSubmodulesConfig = getGitSubmodulesConfig( parent.getRepository() );
StoredConfig repositoryConfig = parent.getRepository().getConfig();
assertEquals( 0, gitSubmodulesConfig.getNames( CONFIG_SUBMODULE_SECTION, "modules/library" ).size() );
assertEquals( 0, repositoryConfig.getNames( CONFIG_SUBMODULE_SECTION, "modules/library" ).size() );
assertEquals( ObjectId.zeroId(), getSubmoduleHead( "modules/library" ) );
assertFalse( new File( repository.getWorkTree(), "modules/library" ).exists() );
assertTrue( parent.status().call().isClean() );
}
@Test
public void testSubmoduleWalk() throws Exception {
addLibrarySubmodule();
int submoduleCount = 0;
SubmoduleWalk walk = SubmoduleWalk.forIndex( parent.getRepository() );
while( walk.next() ) {
Repository submoduleRepository = walk.getRepository();
Git.wrap( submoduleRepository ).fetch().call();
submoduleRepository.close();
submoduleCount++;
}
walk.release();
assertEquals( 1, submoduleCount );
}
 
@Test
public void testSubmoduleSync() throws Exception {
addLibrarySubmodule();
parent.getRepository().getConfig().unsetSection( "submodule", "modules/library" );
parent.submoduleSync().call();
 
SubmoduleWalk walk = SubmoduleWalk.forIndex( parent.getRepository() );
walk.next();
String configUrl = walk.getConfigUrl();
walk.release();
assertEquals( library.getRepository().getDirectory(), new File( configUrl ) );
}
@Before
public void setUp() throws Exception {
library = initRepository( "library" );
populateLibraryRepository();
parent = initRepository( "parent" );
}
 
@After
public void tearDown() {
library.close();
parent.close();
}
 
private Git initRepository( String name ) throws Exception {
return Git.init().setDirectory( tempFolder.newFolder( name ) ).call();
}
 
private void populateLibraryRepository() throws Exception {
new File( library.getRepository().getWorkTree(), "readme.txt" ).createNewFile();
library.add().addFilepattern( "." ).call();
library.commit().setMessage( "Initial commit" ).call();
}
 
private ObjectId getSubmoduleHead( String path ) throws Exception {
SubmoduleWalk walk = SubmoduleWalk.forIndex( parent.getRepository() );
walk.setFilter( PathFilter.create( path ) );
walk.next();
ObjectId result = walk.getObjectId();
walk.release();
return result;
}
 
private Repository addLibrarySubmodule() throws IOException, GitAPIException {
String uri = library.getRepository().getDirectory().getCanonicalPath();
Repository repository = parent.submoduleAdd().setURI( uri ).setPath( "modules/library" ).call();
repository.close();
return repository;
}
 
private static void removeSubmodule( Repository repository, String submodulePath )
throws Exception
{
StoredConfig gitSubmodulesConfig = getGitSubmodulesConfig( repository );
gitSubmodulesConfig.unsetSection( CONFIG_SUBMODULE_SECTION, submodulePath );
gitSubmodulesConfig.save();
StoredConfig repositoryConfig = repository.getConfig();
repositoryConfig.unsetSection( CONFIG_SUBMODULE_SECTION, submodulePath );
repositoryConfig.save();
Git git = Git.wrap( repository );
git.add().addFilepattern( DOT_GIT_MODULES ).call();
git.rm().setCached( true ).addFilepattern( submodulePath ).call();
git.commit().setMessage( "Remove submodule" ).call();
FileUtils.delete( new File( repository.getWorkTree(), submodulePath ), FileUtils.RECURSIVE );
}
 
private static StoredConfig getGitSubmodulesConfig( Repository repository ) {
File gitSubmodulesFile = new File( repository.getWorkTree(), DOT_GIT_MODULES );
return new FileBasedConfig( null, gitSubmodulesFile, FS.DETECTED );
}
 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.