import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.BufferedReader;
}
}
+ @Test
+ public void testRecordShallowRecommendation() throws Exception {
+ try (
+ Repository remoteDb = createBareRepository();
+ Repository tempDb = createWorkRepository()) {
+ StringBuilder xmlContent = new StringBuilder();
+ xmlContent
+ .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
+ .append("<manifest>")
+ .append("<remote name=\"remote1\" fetch=\".\" />")
+ .append("<default revision=\"master\" remote=\"remote1\" />")
+ .append("<project path=\"shallow-please\" ")
+ .append("name=\"").append(defaultUri).append("\" ")
+ .append("clone-depth=\"1\" />")
+ .append("<project path=\"non-shallow\" ")
+ .append("name=\"").append(defaultUri).append("\" />")
+ .append("</manifest>");
+ JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
+ xmlContent.toString());
+
+ RepoCommand command = new RepoCommand(remoteDb);
+ command.setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
+ .setURI(rootUri)
+ .setRecommendShallow(true)
+ .call();
+ // Clone it
+ File directory = createTempDirectory("testBareRepo");
+ try (Repository localDb = Git.cloneRepository()
+ .setDirectory(directory)
+ .setURI(remoteDb.getDirectory().toURI().toString()).call()
+ .getRepository();) {
+ // The .gitmodules file should exist
+ File gitmodules = new File(localDb.getWorkTree(),
+ ".gitmodules");
+ assertTrue("The .gitmodules file should exist",
+ gitmodules.exists());
+ FileBasedConfig c = new FileBasedConfig(gitmodules,
+ FS.DETECTED);
+ c.load();
+ assertEquals("Recording shallow configuration should work", "true",
+ c.getString("submodule", "shallow-please", "shallow"));
+ assertNull("Recording non shallow configuration should work",
+ c.getString("submodule", "non-shallow", "shallow"));
+ }
+ }
+ }
+
@Test
public void testRemoteRevision() throws Exception {
StringBuilder xmlContent = new StringBuilder();
attributes.getValue("revision"), //$NON-NLS-1$
attributes.getValue("remote"), //$NON-NLS-1$
attributes.getValue("groups")); //$NON-NLS-1$
+ currentProject.setRecommendShallow(
+ attributes.getValue("clone-depth")); //$NON-NLS-1$
} else if ("remote".equals(qName)) { //$NON-NLS-1$
String alias = attributes.getValue("alias"); //$NON-NLS-1$
String fetch = attributes.getValue("fetch"); //$NON-NLS-1$
private String targetBranch = Constants.HEAD;
private boolean recordRemoteBranch = false;
private boolean recordSubmoduleLabels = false;
+ private boolean recordShallowSubmodules = false;
private PersonIdent author;
private RemoteReader callback;
private InputStream inputStream;
return this;
}
+ /**
+ * Set whether the clone-depth field should be recorded as a shallow
+ * recommendation in .gitmodules.
+ * <p>
+ * Not implemented for non-bare repositories.
+ *
+ * @param enable Whether to record the shallow recommendation.
+ * @return this command
+ * @since 4.4
+ */
+ public RepoCommand setRecommendShallow(boolean enable) {
+ this.recordShallowSubmodules = enable;
+ return this;
+ }
+
/**
* The progress monitor associated with the clone operation. By default,
* this is set to <code>NullProgressMonitor</code>
proj.getPath(),
proj.getRevision(),
proj.getCopyFiles(),
- proj.getGroups());
+ proj.getGroups(),
+ proj.getRecommendShallow());
}
} catch (GitAPIException | IOException e) {
throw new ManifestErrorException(e);
cfg.setString("submodule", name, "branch", //$NON-NLS-1$ //$NON-NLS-2$
proj.getRevision());
}
+
+ if (recordShallowSubmodules && proj.getRecommendShallow() != null) {
+ // The shallow recommendation is losing information.
+ // As the repo manifests stores the recommended
+ // depth in the 'clone-depth' field, while
+ // git core only uses a binary 'shallow = true/false'
+ // hint, we'll map any depth to 'shallow = true'
+ cfg.setBoolean("submodule", name, "shallow", //$NON-NLS-1$ //$NON-NLS-2$
+ true);
+ }
}
if (recordSubmoduleLabels) {
StringBuilder rec = new StringBuilder();
}
private void addSubmodule(String url, String name, String revision,
- List<CopyFile> copyfiles, Set<String> groups)
+ List<CopyFile> copyfiles, Set<String> groups, String recommendShallow)
throws GitAPIException, IOException {
if (repo.isBare()) {
- RepoProject proj = new RepoProject(url, name, revision, null, groups);
+ RepoProject proj = new RepoProject(url, name, revision, null, groups, recommendShallow);
proj.addCopyFiles(copyfiles);
bareProjects.add(proj);
} else {
private final String remote;
private final Set<String> groups;
private final List<CopyFile> copyfiles;
+ private String recommendShallow;
private String url;
private String defaultRevision;
* @since 4.4
*/
public RepoProject(String name, String path, String revision,
- String remote, Set<String> groups) {
+ String remote, Set<String> groups,
+ String recommendShallow) {
if (name == null) {
throw new NullPointerException();
}
this.revision = revision;
this.remote = remote;
this.groups = groups;
+ this.recommendShallow = recommendShallow;
copyfiles = new ArrayList<CopyFile>();
}
*/
public RepoProject(String name, String path, String revision,
String remote, String groups) {
- this(name, path, revision, remote, new HashSet<String>());
+ this(name, path, revision, remote, new HashSet<String>(), null);
if (groups != null && groups.length() > 0)
this.setGroups(groups);
}
return groups;
}
+ /**
+ * Return the recommendation for shallowness.
+ *
+ * @return the String of "clone-depth"
+ * @since 4.4
+ */
+ public String getRecommendShallow() {
+ return recommendShallow;
+ }
+
+ /**
+ * Sets the recommendation for shallowness.
+ *
+ * @since 4.4
+ */
+ public void setRecommendShallow(String recommendShallow) {
+ this.recommendShallow = recommendShallow;
+ }
+
/**
* Add a copy file configuration.
*