diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/BareSuperprojectWriterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/BareSuperprojectWriterTest.java index d52292dc9..17fc8db35 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/BareSuperprojectWriterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/BareSuperprojectWriterTest.java @@ -50,7 +50,7 @@ public void write_setGitModulesContents() throws Exception { BareSuperprojectWriter w = new BareSuperprojectWriter(bareRepo, null, "refs/heads/master", author, mockRemoteReader, - BareWriterConfig.getDefault()); + BareWriterConfig.getDefault(), List.of()); RevCommit commit = w.write(Arrays.asList(repoProject)); @@ -66,6 +66,31 @@ public void write_setGitModulesContents() throws Exception { } } + @Test + public void write_setExtraContents() throws Exception { + try (Repository bareRepo = createBareRepository()) { + RepoProject repoProject = new RepoProject("subprojectX", "path/to", + "refs/heads/branch-x", "remote", ""); + repoProject.setUrl("http://example.com/a"); + + RemoteReader mockRemoteReader = mock(RemoteReader.class); + when(mockRemoteReader.sha1("http://example.com/a", + "refs/heads/branch-x")) + .thenReturn(ObjectId.fromString(SHA1_A)); + + BareSuperprojectWriter w = new BareSuperprojectWriter(bareRepo, + null, "refs/heads/master", author, mockRemoteReader, + BareWriterConfig.getDefault(), + List.of(new BareSuperprojectWriter.ExtraContent("x", + "extra-content"))); + + RevCommit commit = w.write(Arrays.asList(repoProject)); + + String contents = readContents(bareRepo, commit, "x"); + assertThat(contents, is("extra-content")); + } + } + private String readContents(Repository repo, RevCommit commit, String path) throws Exception { String idStr = commit.getId().name() + ":" + path; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/BareSuperprojectWriter.java b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/BareSuperprojectWriter.java index d6482968f..e6626aece 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/BareSuperprojectWriter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/BareSuperprojectWriter.java @@ -71,6 +71,8 @@ class BareSuperprojectWriter { private final PersonIdent author; + private List extraContents; + static class BareWriterConfig { boolean ignoreRemoteFailures = false; @@ -88,10 +90,22 @@ private BareWriterConfig() { } } + static class ExtraContent { + final String path; + + final String content; + + ExtraContent(String path, String content) { + this.path = path; + this.content = content; + } + } + BareSuperprojectWriter(Repository repo, URI targetUri, String targetBranch, PersonIdent author, RemoteReader callback, - BareWriterConfig config) { + BareWriterConfig config, + List extraContents) { assert (repo.isBare()); this.repo = repo; this.targetUri = targetUri; @@ -99,6 +113,7 @@ private BareWriterConfig() { this.author = author; this.callback = callback; this.config = config; + this.extraContents = extraContents; } RevCommit write(List repoProjects) @@ -244,6 +259,16 @@ private void prepareIndex(List projects, DirCache index, builder.add(dcEntryAttr); } + for (ExtraContent ec : extraContents) { + DirCacheEntry extraDcEntry = new DirCacheEntry(ec.path); + + ObjectId oid = inserter.insert(Constants.OBJ_BLOB, + ec.content.getBytes(UTF_8)); + extraDcEntry.setObjectId(oid); + extraDcEntry.setFileMode(FileMode.REGULAR_FILE); + builder.add(extraDcEntry); + } + builder.finish(); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java index 30b862673..6e943e5d3 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java @@ -28,6 +28,7 @@ import org.eclipse.jgit.api.GitCommand; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.InvalidRefNameException; +import org.eclipse.jgit.gitrepo.BareSuperprojectWriter.ExtraContent; import org.eclipse.jgit.gitrepo.ManifestParser.IncludedFileReader; import org.eclipse.jgit.gitrepo.internal.RepoText; import org.eclipse.jgit.internal.JGitText; @@ -79,6 +80,8 @@ public class RepoCommand extends GitCommand { private ProgressMonitor monitor; + private final List extraContents = new ArrayList<>(); + /** * A callback to get ref sha1 of a repository from its uri. * @@ -509,6 +512,22 @@ public RepoCommand setIncludedFileReader(IncludedFileReader reader) { return this; } + /** + * Create a file with the given content in the destination repository + * + * @param path + * where to create the file in the destination repository + * @param contents + * content for the create file + * @return this command + * + * @since 6.1 + */ + public RepoCommand addToDestination(String path, String contents) { + this.extraContents.add(new ExtraContent(path, contents)); + return this; + } + /** {@inheritDoc} */ @Override public RevCommit call() throws GitAPIException { @@ -550,7 +569,7 @@ public RevCommit call() throws GitAPIException { targetBranch, author == null ? new PersonIdent(repo) : author, callback == null ? new DefaultRemoteReader() : callback, - bareWriterConfig); + bareWriterConfig, extraContents); return writer.write(renamedProjects); } @@ -663,5 +682,4 @@ static URI relativize(URI current, URI target) { return URI.create(j.toString()); } - }