From ddd0fe257bc78dc9646407e7121769d0c03e0d94 Mon Sep 17 00:00:00 2001 From: Stefan Beller Date: Tue, 31 May 2016 15:18:20 -0700 Subject: [PATCH] RepoCommand: record manifest groups as submodule labels Git core learned about attributes in pathspecs: pathspec: allow querying for attributes The pathspec mechanism is extended via the new ":(attr:eol=input)pattern/to/match" syntax to filter paths so that it requires paths to not just match the given pattern but also have the specified attrs attached for them to be chosen. (177161a5f7, 2016-05-20) We intend to use these pathspec attribute patterns for submodule grouping, similar to the grouping in repo. So the RepoCommand which translates repo manifest files into submodules should propagate this information along. This requires writing information to the .gitattributes file instead of the .gitmodules file. For now we just overwrite any existing .gitattributes file and do not care about prior attributes set. If this becomes an issue we need to figure out how to correctly amend the grouping information to an existing .gitattributes file. Change-Id: I0f55b45786b6b8fc3d5be62d7f6aab9ac00ed60e Signed-off-by: Stefan Beller --- .../eclipse/jgit/gitrepo/RepoCommandTest.java | 49 ++++++++++++++++++ .../org/eclipse/jgit/gitrepo/RepoCommand.java | 48 +++++++++++++++-- .../org/eclipse/jgit/gitrepo/RepoProject.java | 51 +++++++++++++++++-- 3 files changed, 140 insertions(+), 8 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java index 6e6f4ed57..d12e5fc0e 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java @@ -49,6 +49,9 @@ import static org.junit.Assert.assertTrue; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.junit.JGitTestUtil; @@ -751,6 +754,52 @@ public class RepoCommandTest extends RepositoryTestCase { } } + + @Test + public void testRecordSubmoduleLabels() throws Exception { + try ( + Repository remoteDb = createBareRepository(); + Repository tempDb = createWorkRepository()) { + StringBuilder xmlContent = new StringBuilder(); + xmlContent + .append("\n") + .append("") + .append("") + .append("") + .append("") + .append(""); + JGitTestUtil.writeTrashFile(tempDb, "manifest.xml", + xmlContent.toString()); + + RepoCommand command = new RepoCommand(remoteDb); + command.setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml") + .setURI(rootUri) + .setRecordSubmoduleLabels(true) + .call(); + // Clone it + File directory = createTempDirectory("testBareRepo"); + try (Repository localDb = Git.cloneRepository() + .setDirectory(directory) + .setURI(remoteDb.getDirectory().toURI().toString()).call() + .getRepository();) { + // The .gitattributes file should exist + File gitattributes = new File(localDb.getWorkTree(), + ".gitattributes"); + assertTrue("The .gitattributes file should exist", + gitattributes.exists()); + try (BufferedReader reader = new BufferedReader( + new FileReader(gitattributes));) { + String content = reader.readLine(); + assertEquals(".gitattributes content should be as expected", + "/test a1 a2", content); + } + } + } + } + @Test public void testRemoteRevision() throws Exception { StringBuilder xmlContent = new StringBuilder(); 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 ee937f585..6f682ee98 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java @@ -51,8 +51,10 @@ import java.io.IOException; import java.io.InputStream; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.Set; import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.api.Git; @@ -110,6 +112,7 @@ public class RepoCommand extends GitCommand { private String branch; private String targetBranch = Constants.HEAD; private boolean recordRemoteBranch = false; + private boolean recordSubmoduleLabels = false; private PersonIdent author; private RemoteReader callback; private InputStream inputStream; @@ -344,6 +347,21 @@ public class RepoCommand extends GitCommand { return this; } + /** + * Set whether the labels field should be recorded as a label in + * .gitattributes. + *

+ * Not implemented for non-bare repositories. + * + * @param enable Whether to record the labels in the .gitattributes + * @return this command + * @since 4.4 + */ + public RepoCommand setRecordSubmoduleLabels(boolean enable) { + this.recordSubmoduleLabels = enable; + return this; + } + /** * The progress monitor associated with the clone operation. By default, * this is set to NullProgressMonitor @@ -452,7 +470,8 @@ public class RepoCommand extends GitCommand { addSubmodule(proj.getUrl(), proj.getPath(), proj.getRevision(), - proj.getCopyFiles()); + proj.getCopyFiles(), + proj.getGroups()); } } catch (GitAPIException | IOException e) { throw new ManifestErrorException(e); @@ -472,6 +491,7 @@ public class RepoCommand extends GitCommand { ObjectInserter inserter = repo.newObjectInserter(); try (RevWalk rw = new RevWalk(repo)) { Config cfg = new Config(); + StringBuilder attributes = new StringBuilder(); for (RepoProject proj : bareProjects) { String name = proj.getPath(); String nameUri = proj.getName(); @@ -493,6 +513,17 @@ public class RepoCommand extends GitCommand { proj.getRevision()); } } + if (recordSubmoduleLabels) { + StringBuilder rec = new StringBuilder(); + rec.append("/"); //$NON-NLS-1$ + rec.append(name); + for (String group : proj.getGroups()) { + rec.append(" "); //$NON-NLS-1$ + rec.append(group); + } + rec.append("\n"); //$NON-NLS-1$ + attributes.append(rec.toString()); + } cfg.setString("submodule", name, "path", name); //$NON-NLS-1$ //$NON-NLS-2$ cfg.setString("submodule", name, "url", nameUri); //$NON-NLS-1$ //$NON-NLS-2$ @@ -522,6 +553,16 @@ public class RepoCommand extends GitCommand { dcEntry.setFileMode(FileMode.REGULAR_FILE); builder.add(dcEntry); + if (recordSubmoduleLabels) { + // create a new DirCacheEntry for .gitattributes file. + final DirCacheEntry dcEntryAttr = new DirCacheEntry(Constants.DOT_GIT_ATTRIBUTES); + ObjectId attrId = inserter.insert(Constants.OBJ_BLOB, + attributes.toString().getBytes(Constants.CHARACTER_ENCODING)); + dcEntryAttr.setObjectId(attrId); + dcEntryAttr.setFileMode(FileMode.REGULAR_FILE); + builder.add(dcEntryAttr); + } + builder.finish(); ObjectId treeId = index.writeTree(inserter); @@ -575,9 +616,10 @@ public class RepoCommand extends GitCommand { } private void addSubmodule(String url, String name, String revision, - List copyfiles) throws GitAPIException, IOException { + List copyfiles, Set groups) + throws GitAPIException, IOException { if (repo.isBare()) { - RepoProject proj = new RepoProject(url, name, revision, null, null); + RepoProject proj = new RepoProject(url, name, revision, null, groups); proj.addCopyFiles(copyfiles); bareProjects.add(proj); } else { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoProject.java b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoProject.java index f6d1209cb..1b251368d 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoProject.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoProject.java @@ -134,10 +134,11 @@ public class RepoProject implements Comparable { * @param remote * name of the remote definition * @param groups - * comma separated group list + * set of groups + * @since 4.4 */ public RepoProject(String name, String path, String revision, - String remote, String groups) { + String remote, Set groups) { if (name == null) { throw new NullPointerException(); } @@ -148,12 +149,29 @@ public class RepoProject implements Comparable { this.path = name; this.revision = revision; this.remote = remote; - this.groups = new HashSet(); - if (groups != null && groups.length() > 0) - this.groups.addAll(Arrays.asList(groups.split(","))); //$NON-NLS-1$ + this.groups = groups; copyfiles = new ArrayList(); } + /** + * @param name + * the relative path to the {@code remote} + * @param path + * the relative path to the super project + * @param revision + * a SHA-1 or branch name or tag name + * @param remote + * name of the remote definition + * @param groups + * comma separated group list + */ + public RepoProject(String name, String path, String revision, + String remote, String groups) { + this(name, path, revision, remote, new HashSet()); + if (groups != null && groups.length() > 0) + this.setGroups(groups); + } + /** * Set the url of the sub repo. * @@ -165,6 +183,19 @@ public class RepoProject implements Comparable { return this; } + /** + * Set the url of the sub repo. + * + * @param url + * @return this for chaining. + * @since 4.4 + */ + public RepoProject setGroups(String groups) { + this.groups.clear(); + this.groups.addAll(Arrays.asList(groups.split(","))); //$NON-NLS-1$ + return this; + } + /** * Set the default revision for the sub repo. * @@ -240,6 +271,16 @@ public class RepoProject implements Comparable { return groups.contains(group); } + /** + * Return the set of groups. + * + * @return a Set of groups. + * @since 4.4 + */ + public Set getGroups() { + return groups; + } + /** * Add a copy file configuration. *