Browse Source

Merge changes Id616611e,Ib6509e81,I52f5d3f2

* changes:
  RepoCommand: generate relative submodule URLs from absolute URLs.
  RepoCommand: don't record new commit if tree did not change
  RepoCommand: persist unreadable submodules in .gitmodules
stable-4.11
David Pursehouse 7 years ago committed by Gerrit Code Review @ Eclipse.org
parent
commit
0e20df710a
  1. 187
      org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java
  2. 72
      org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java

187
org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java

@ -185,6 +185,59 @@ public class RepoCommandTest extends RepositoryTestCase {
} }
} }
@Test
public void runTwiceIsNOP() throws Exception {
Repository child = Git.cloneRepository()
.setURI(groupADb.getDirectory().toURI().toString())
.setDirectory(createUniqueTestGitDir(true)).setBare(true).call()
.getRepository();
Repository dest = Git.cloneRepository()
.setURI(db.getDirectory().toURI().toString())
.setDirectory(createUniqueTestGitDir(true)).setBare(true).call()
.getRepository();
assertTrue(dest.isBare());
assertTrue(child.isBare());
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=\"base\" name=\"platform/base\" />")
.append("</manifest>");
RepoCommand cmd = new RepoCommand(dest);
IndexedRepos repos = new IndexedRepos();
repos.put("platform/base", child);
RevCommit commit = cmd
.setInputStream(new ByteArrayInputStream(
xmlContent.toString().getBytes(UTF_8)))
.setRemoteReader(repos)
.setURI("platform/")
.setTargetURI("platform/superproject")
.setRecordRemoteBranch(true)
.setRecordSubmoduleLabels(true)
.call();
String firstIdStr = commit.getId().name() + ":" + ".gitmodules";
commit = new RepoCommand(dest)
.setInputStream(new ByteArrayInputStream(
xmlContent.toString().getBytes(UTF_8)))
.setRemoteReader(repos)
.setURI("platform/")
.setTargetURI("platform/superproject")
.setRecordRemoteBranch(true)
.setRecordSubmoduleLabels(true)
.call();
String idStr = commit.getId().name() + ":" + ".gitmodules";
assertEquals(firstIdStr, idStr);
child.close();
dest.close();
}
@Test @Test
public void androidSetup() throws Exception { public void androidSetup() throws Exception {
Repository child = Git.cloneRepository() Repository child = Git.cloneRepository()
@ -213,8 +266,7 @@ public class RepoCommandTest extends RepositoryTestCase {
repos.put("platform/base", child); repos.put("platform/base", child);
RevCommit commit = cmd RevCommit commit = cmd
.setInputStream(new ByteArrayInputStream( .setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(UTF_8)))
xmlContent.toString().getBytes(UTF_8)))
.setRemoteReader(repos) .setRemoteReader(repos)
.setURI("platform/") .setURI("platform/")
.setTargetURI("platform/superproject") .setTargetURI("platform/superproject")
@ -237,6 +289,48 @@ public class RepoCommandTest extends RepositoryTestCase {
dest.close(); dest.close();
} }
@Test
public void recordUnreachableRemotes() throws Exception {
StringBuilder xmlContent = new StringBuilder();
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<manifest>")
.append("<remote name=\"remote1\" fetch=\"https://host.com/\" />")
.append("<default revision=\"master\" remote=\"remote1\" />")
.append("<project path=\"base\" name=\"platform/base\" />")
.append("</manifest>");
Repository dest = Git.cloneRepository()
.setURI(db.getDirectory().toURI().toString())
.setDirectory(createUniqueTestGitDir(true)).setBare(true).call()
.getRepository();
assertTrue(dest.isBare());
RevCommit commit = new RepoCommand(dest)
.setInputStream(new ByteArrayInputStream(
xmlContent.toString().getBytes(UTF_8)))
.setRemoteReader(new IndexedRepos())
.setURI("platform/")
.setTargetURI("platform/superproject")
.setRecordRemoteBranch(true)
.setIgnoreRemoteFailures(true)
.setRecordSubmoduleLabels(true)
.call();
String idStr = commit.getId().name() + ":" + ".gitmodules";
ObjectId modId = dest.resolve(idStr);
try (ObjectReader reader = dest.newObjectReader()) {
byte[] bytes = reader.open(modId).getCachedBytes(Integer.MAX_VALUE);
Config base = new Config();
BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
String subUrl = cfg.getString("submodule", "base", "url");
assertEquals(subUrl, "https://host.com/platform/base");
}
dest.close();
}
@Test @Test
public void gerritSetup() throws Exception { public void gerritSetup() throws Exception {
Repository child = Repository child =
@ -344,6 +438,63 @@ public class RepoCommandTest extends RepositoryTestCase {
dest.close(); dest.close();
} }
@Test
public void absoluteRemoteURLAbsoluteTargetURL() throws Exception {
Repository child =
Git.cloneRepository().setURI(groupADb.getDirectory().toURI().toString())
.setDirectory(createUniqueTestGitDir(true))
.setBare(true).call().getRepository();
Repository dest = Git.cloneRepository()
.setURI(db.getDirectory().toURI().toString()).setDirectory(createUniqueTestGitDir(true))
.setBare(true).call().getRepository();
String abs = "https://chromium.googlesource.com";
String repoUrl = "https://chromium.googlesource.com/chromium/src";
boolean fetchSlash = false;
boolean baseSlash = false;
do {
do {
String fetchUrl = fetchSlash ? abs + "/" : abs;
String baseUrl = baseSlash ? abs + "/" : abs;
StringBuilder xmlContent = new StringBuilder();
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<manifest>")
.append("<remote name=\"origin\" fetch=\"" + fetchUrl + "\" />")
.append("<default revision=\"master\" remote=\"origin\" />")
.append("<project path=\"src\" name=\"chromium/src\" />")
.append("</manifest>");
RepoCommand cmd = new RepoCommand(dest);
IndexedRepos repos = new IndexedRepos();
repos.put(repoUrl, child);
RevCommit commit = cmd
.setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(UTF_8)))
.setRemoteReader(repos)
.setURI(baseUrl)
.setTargetURI(abs + "/superproject")
.setRecordRemoteBranch(true)
.setRecordSubmoduleLabels(true)
.call();
String idStr = commit.getId().name() + ":" + ".gitmodules";
ObjectId modId = dest.resolve(idStr);
try (ObjectReader reader = dest.newObjectReader()) {
byte[] bytes = reader.open(modId).getCachedBytes(Integer.MAX_VALUE);
Config base = new Config();
BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
String subUrl = cfg.getString("submodule", "src", "url");
assertEquals("../chromium/src", subUrl);
}
fetchSlash = !fetchSlash;
} while (fetchSlash);
baseSlash = !baseSlash;
} while (baseSlash);
child.close();
dest.close();
}
@Test @Test
public void testAddRepoManifest() throws Exception { public void testAddRepoManifest() throws Exception {
StringBuilder xmlContent = new StringBuilder(); StringBuilder xmlContent = new StringBuilder();
@ -814,37 +965,6 @@ public class RepoCommandTest extends RepositoryTestCase {
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"master world", content); "master world", content);
} }
@Test
public void testNonDefaultRemotes() throws Exception {
StringBuilder xmlContent = new StringBuilder();
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<manifest>")
.append("<remote name=\"remote1\" fetch=\".\" />")
.append("<remote name=\"remote2\" fetch=\"")
.append(notDefaultUri)
.append("\" />")
.append("<default revision=\"master\" remote=\"remote1\" />")
.append("<project path=\"foo\" name=\"")
.append(defaultUri)
.append("\" />")
.append("<project path=\"bar\" name=\".\" remote=\"remote2\" />")
.append("</manifest>");
Repository localDb = createWorkRepository();
JGitTestUtil.writeTrashFile(
localDb, "manifest.xml", xmlContent.toString());
RepoCommand command = new RepoCommand(localDb);
command
.setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
.setURI(rootUri)
.call();
File file = new File(localDb.getWorkTree(), "foo/hello.txt");
assertTrue("We should have foo", file.exists());
file = new File(localDb.getWorkTree(), "bar/world.txt");
assertTrue("We should have bar", file.exists());
}
@Test @Test
public void testRemoteAlias() throws Exception { public void testRemoteAlias() throws Exception {
StringBuilder xmlContent = new StringBuilder(); StringBuilder xmlContent = new StringBuilder();
@ -1125,5 +1245,6 @@ public class RepoCommandTest extends RepositoryTestCase {
testRelative("abc", "/bcd", "/bcd"); testRelative("abc", "/bcd", "/bcd");
testRelative("http://a", "a/b", "a/b"); testRelative("http://a", "a/b", "a/b");
testRelative("http://base.com/a/", "http://child.com/a/b", "http://child.com/a/b"); testRelative("http://base.com/a/", "http://child.com/a/b", "http://child.com/a/b");
testRelative("http://base.com/a/", "http://base.com/a/b", "b");
} }
} }

72
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java

@ -54,6 +54,7 @@ import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.StringJoiner; import java.util.StringJoiner;
@ -543,10 +544,7 @@ public class RepoCommand extends GitCommand<RevCommit> {
objectId = ObjectId.fromString(proj.getRevision()); objectId = ObjectId.fromString(proj.getRevision());
} else { } else {
objectId = callback.sha1(nameUri, proj.getRevision()); objectId = callback.sha1(nameUri, proj.getRevision());
if (objectId == null) { if (objectId == null && !ignoreRemoteFailures) {
if (ignoreRemoteFailures) {
continue;
}
throw new RemoteUnavailableException(nameUri); throw new RemoteUnavailableException(nameUri);
} }
if (recordRemoteBranch) { if (recordRemoteBranch) {
@ -585,38 +583,40 @@ public class RepoCommand extends GitCommand<RevCommit> {
cfg.setString("submodule", path, "url", submodUrl.toString()); //$NON-NLS-1$ //$NON-NLS-2$ cfg.setString("submodule", path, "url", submodUrl.toString()); //$NON-NLS-1$ //$NON-NLS-2$
// create gitlink // create gitlink
DirCacheEntry dcEntry = new DirCacheEntry(path); if (objectId != null) {
dcEntry.setObjectId(objectId); DirCacheEntry dcEntry = new DirCacheEntry(path);
dcEntry.setFileMode(FileMode.GITLINK);
builder.add(dcEntry);
for (CopyFile copyfile : proj.getCopyFiles()) {
byte[] src = callback.readFile(
nameUri, proj.getRevision(), copyfile.src);
objectId = inserter.insert(Constants.OBJ_BLOB, src);
dcEntry = new DirCacheEntry(copyfile.dest);
dcEntry.setObjectId(objectId); dcEntry.setObjectId(objectId);
dcEntry.setFileMode(FileMode.REGULAR_FILE); dcEntry.setFileMode(FileMode.GITLINK);
builder.add(dcEntry); builder.add(dcEntry);
}
for (LinkFile linkfile : proj.getLinkFiles()) { for (CopyFile copyfile : proj.getCopyFiles()) {
String link; byte[] src = callback.readFile(
if (linkfile.dest.contains("/")) { //$NON-NLS-1$ nameUri, proj.getRevision(), copyfile.src);
link = FileUtils.relativizeGitPath( objectId = inserter.insert(Constants.OBJ_BLOB, src);
dcEntry = new DirCacheEntry(copyfile.dest);
dcEntry.setObjectId(objectId);
dcEntry.setFileMode(FileMode.REGULAR_FILE);
builder.add(dcEntry);
}
for (LinkFile linkfile : proj.getLinkFiles()) {
String link;
if (linkfile.dest.contains("/")) { //$NON-NLS-1$
link = FileUtils.relativizeGitPath(
linkfile.dest.substring(0, linkfile.dest.substring(0,
linkfile.dest.lastIndexOf('/')), linkfile.dest.lastIndexOf('/')),
proj.getPath() + "/" + linkfile.src); //$NON-NLS-1$ proj.getPath() + "/" + linkfile.src); //$NON-NLS-1$
} else { } else {
link = proj.getPath() + "/" + linkfile.src; //$NON-NLS-1$ link = proj.getPath() + "/" + linkfile.src; //$NON-NLS-1$
} }
objectId = inserter.insert(Constants.OBJ_BLOB, objectId = inserter.insert(Constants.OBJ_BLOB,
link.getBytes( link.getBytes(
Constants.CHARACTER_ENCODING)); Constants.CHARACTER_ENCODING));
dcEntry = new DirCacheEntry(linkfile.dest); dcEntry = new DirCacheEntry(linkfile.dest);
dcEntry.setObjectId(objectId); dcEntry.setObjectId(objectId);
dcEntry.setFileMode(FileMode.SYMLINK); dcEntry.setFileMode(FileMode.SYMLINK);
builder.add(dcEntry); builder.add(dcEntry);
}
} }
} }
String content = cfg.toText(); String content = cfg.toText();
@ -644,6 +644,11 @@ public class RepoCommand extends GitCommand<RevCommit> {
// Create a Commit object, populate it and write it // Create a Commit object, populate it and write it
ObjectId headId = repo.resolve(targetBranch + "^{commit}"); //$NON-NLS-1$ ObjectId headId = repo.resolve(targetBranch + "^{commit}"); //$NON-NLS-1$
if (headId != null && rw.parseCommit(headId).getTree().getId().equals(treeId)) {
// No change. Do nothing.
return rw.parseCommit(headId);
}
CommitBuilder commit = new CommitBuilder(); CommitBuilder commit = new CommitBuilder();
commit.setTreeId(treeId); commit.setTreeId(treeId);
if (headId != null) if (headId != null)
@ -745,12 +750,7 @@ public class RepoCommand extends GitCommand<RevCommit> {
*/ */
private static final String SLASH = "/"; //$NON-NLS-1$ private static final String SLASH = "/"; //$NON-NLS-1$
static URI relativize(URI current, URI target) { static URI relativize(URI current, URI target) {
if (!Objects.equals(current.getHost(), target.getHost())) {
// We only handle bare paths for now.
if (!target.toString().equals(target.getPath())) {
return target;
}
if (!current.toString().equals(current.getPath())) {
return target; return target;
} }

Loading…
Cancel
Save