diff options
author | Sascha Scholz | 2012-03-02 12:31:09 -0500 |
---|---|---|
committer | Kevin Sawicki | 2012-03-02 12:31:09 -0500 |
commit | a1f877e312bd63c63c6b9d6ba32b5c68bc43a10d (patch) | |
tree | ade1284f02aef96ec087f98237fceb75534d5532 | |
parent | 1515d7b7d54534a2ac44a274d4407654fb3ec512 (diff) | |
download | jgit-a1f877e312bd63c63c6b9d6ba32b5c68bc43a10d.zip jgit-a1f877e312bd63c63c6b9d6ba32b5c68bc43a10d.tar.gz jgit-a1f877e312bd63c63c6b9d6ba32b5c68bc43a10d.tar.xz |
Load .gitmodules config before adding values to it
This prevents existing entries from being cleared when the
.gitmodules config is saved after the new submodule configuration
is added.
Change-Id: I66841f5e758a7527e2e6e25cf1318e5fea91a909
Signed-off-by: Kevin Sawicki <kevin@github.com>
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java | 41 | ||||
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleAddCommand.java | 12 |
2 files changed, 49 insertions, 4 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java index dee2acc..4c5a2c1 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java @@ -66,6 +66,7 @@ import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryTestCase; import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.storage.file.FileBasedConfig; import org.junit.Test; /** @@ -212,4 +213,44 @@ public class SubmoduleAddTest extends RepositoryTestCase { assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES)); assertTrue(status.getAdded().contains(path)); } + + @Test + public void addSubmoduleWithExistingSubmoduleDefined() throws Exception { + String path1 = "sub1"; + String url1 = "git://server/repo1.git"; + String path2 = "sub2"; + + FileBasedConfig modulesConfig = new FileBasedConfig(new File( + db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS()); + modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, + path1, ConfigConstants.CONFIG_KEY_PATH, path1); + modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, + path1, ConfigConstants.CONFIG_KEY_URL, url1); + modulesConfig.save(); + + Git git = new Git(db); + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + assertNotNull(git.commit().setMessage("create file").call()); + + SubmoduleAddCommand command = new SubmoduleAddCommand(db); + command.setPath(path2); + String url2 = db.getDirectory().toURI().toString(); + command.setURI(url2); + assertNotNull(command.call()); + + modulesConfig.load(); + assertEquals(path1, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path1, + ConfigConstants.CONFIG_KEY_PATH)); + assertEquals(url1, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path1, + ConfigConstants.CONFIG_KEY_URL)); + assertEquals(path2, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path2, + ConfigConstants.CONFIG_KEY_PATH)); + assertEquals(url2, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path2, + ConfigConstants.CONFIG_KEY_URL)); + } }
\ No newline at end of file diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleAddCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleAddCommand.java index e1b293c..a983134 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleAddCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleAddCommand.java @@ -49,6 +49,7 @@ import java.text.MessageFormat; import org.eclipse.jgit.JGitText; import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.NoFilepatternException; +import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.NullProgressMonitor; @@ -177,14 +178,17 @@ public class SubmoduleAddCommand extends // Save path and URL to parent repository's .gitmodules file FileBasedConfig modulesConfig = new FileBasedConfig(new File( repo.getWorkTree(), Constants.DOT_GIT_MODULES), repo.getFS()); - modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, - ConfigConstants.CONFIG_KEY_PATH, path); - modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, - ConfigConstants.CONFIG_KEY_URL, uri); try { + modulesConfig.load(); + modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, + path, ConfigConstants.CONFIG_KEY_PATH, path); + modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, + path, ConfigConstants.CONFIG_KEY_URL, uri); modulesConfig.save(); } catch (IOException e) { throw new JGitInternalException(e.getMessage(), e); + } catch (ConfigInvalidException e) { + throw new JGitInternalException(e.getMessage(), e); } AddCommand add = new AddCommand(repo); |