Skip to content

Commit

Permalink
Address comments by @ikhoon
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoox committed Oct 5, 2022
1 parent 5472052 commit e2c8db4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public final class GitMirror extends AbstractMirror {
// We are going to hide this file from CD UI after we implement UI for mirroring.
private static final String MIRROR_STATE_FILE_NAME = "mirror_state.json";

// Prepend '.' because this file is a metadata.
// Prepend '.' because this file is metadata.
private static final String LOCAL_TO_REMOTE_MIRROR_STATE_FILE_NAME = '.' + MIRROR_STATE_FILE_NAME;

private static final Pattern CR = Pattern.compile("\r", Pattern.LITERAL);
Expand Down Expand Up @@ -269,13 +269,15 @@ protected void mirrorRemoteToLocal(File workDir, CommandExecutor executor,
}

if (++numFiles > maxNumFiles) {
throw new MirrorException("mirror contains more than " + maxNumFiles + " file(s)");
throwMirrorException(maxNumFiles, "files");
return;
}

final ObjectId objectId = treeWalk.getObjectId(0);
final long contentLength = reader.getObjectSize(objectId, ObjectReader.OBJ_ANY);
if (numBytes > maxNumBytes - contentLength) {
throw new MirrorException("mirror contains more than " + maxNumBytes + " byte(s)");
throwMirrorException(maxNumBytes, "bytes");
return;
}
numBytes += contentLength;

Expand Down Expand Up @@ -428,17 +430,16 @@ private Map<String, Entry<?>> localHeadEntries(Revision localHead) {
final Map<String, Entry<?>> localRawHeadEntries = localRepo().find(localHead, localPath() + "**")
.join();

final Stream<Map.Entry<String, Entry<?>>> filteredStream =
final Stream<Map.Entry<String, Entry<?>>> entryStream =
localRawHeadEntries.entrySet()
.stream()
.filter(e -> e.getKey().startsWith(localPath()));
.stream();
if (ignoreNode == null) {
// Use HashMap to manipulate it.
return filteredStream.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return entryStream.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

final Map<String, Entry<?>> sortedMap =
filteredStream.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
entryStream.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(v1, v2) -> v1, LinkedHashMap::new));
// Use HashMap to manipulate it.
final HashMap<String, Entry<?>> result = new HashMap<>(sortedMap.size());
Expand Down Expand Up @@ -474,11 +475,11 @@ private void addModifiedEntryToCache(Revision localHead, DirCache dirCache, Obje
while (treeWalk.next()) {
final FileMode fileMode = treeWalk.getFileMode();
final String pathString = treeWalk.getPathString();
final String path = '/' + pathString;
final String remoteFilePath = '/' + pathString;

// Recurse into a directory if necessary.
if (fileMode == FileMode.TREE) {
maybeEnterSubtree(treeWalk, remotePath(), path);
maybeEnterSubtree(treeWalk, remotePath(), remoteFilePath);
continue;
}

Expand All @@ -488,11 +489,11 @@ private void addModifiedEntryToCache(Revision localHead, DirCache dirCache, Obje
}

// Skip the entries that are not under the remote path.
if (!path.startsWith(remotePath())) {
if (!remoteFilePath.startsWith(remotePath())) {
continue;
}

final String localFilePath = localPath() + path.substring(remotePath().length());
final String localFilePath = localPath() + remoteFilePath.substring(remotePath().length());

// Skip the entry whose path does not conform to CD's path rule.
if (!Util.isValidFilePath(localFilePath)) {
Expand All @@ -507,14 +508,16 @@ private void addModifiedEntryToCache(Revision localHead, DirCache dirCache, Obje
}

if (++numFiles > maxNumFiles) {
throw new MirrorException("mirror contains more than " + maxNumFiles + " file(s)");
throwMirrorException(maxNumFiles, "files");
return;
}

final byte[] oldContent = currentEntryContent(reader, treeWalk);
final long contentLength = applyPathEdit(dirCache, inserter, pathString, entry, oldContent);
numBytes += contentLength;
if (numBytes > maxNumBytes) {
throw new MirrorException("mirror contains more than " + maxNumBytes + " byte(s)");
throwMirrorException(maxNumBytes, "bytes");
return;
}
}

Expand All @@ -529,15 +532,16 @@ private void addModifiedEntryToCache(Revision localHead, DirCache dirCache, Obje
}

if (++numFiles > maxNumFiles) {
throw new MirrorException("mirror contains more than " + maxNumFiles + " file(s)");
throwMirrorException(maxNumFiles, "files");
return;
}

final String convertedPath = remotePath().substring(1) + // Strip the leading '/'
entry.getKey().substring(localPath().length());
final long contentLength = applyPathEdit(dirCache, inserter, convertedPath, value, null);
numBytes += contentLength;
if (numBytes > maxNumBytes) {
throw new MirrorException("mirror contains more than " + maxNumBytes + " byte(s)");
throwMirrorException(maxNumBytes, "bytes");
}
}
}
Expand All @@ -561,7 +565,7 @@ private static long applyPathEdit(DirCache dirCache, ObjectInserter inserter, St
case TEXT:
final String sanitizedOldText = oldContent != null ?
sanitizeText(new String(oldContent, UTF_8)) : null;
final String sanitizedNewText = sanitizeText(entry.contentAsText());
final String sanitizedNewText = entry.contentAsText(); // Already sanitized when committing.
// Upsert only when the contents are really different.
if (!sanitizedNewText.equals(sanitizedOldText)) {
applyPathEdit(dirCache, new InsertText(pathString, inserter, sanitizedNewText));
Expand Down Expand Up @@ -655,6 +659,11 @@ private ObjectId commit(org.eclipse.jgit.lib.Repository gitRepository, DirCache
}
}

private <T> T throwMirrorException(long number, String filesOrBytes) {
throw new MirrorException("mirror (" + remoteRepoUri() + '#' + remoteBranch() +
") contains more than " + number + ' ' + filesOrBytes);
}

static void updateRef(org.eclipse.jgit.lib.Repository jGitRepository, RevWalk revWalk,
String ref, ObjectId commitId) throws IOException {
final RefUpdate refUpdate = jGitRepository.updateRef(ref);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static String normalizePath(String path) {
*
* <p>e.g. git+ssh://foo.com/bar.git/some-path#master is split into:
* - remoteRepoUri: git+ssh://foo.com/bar.git
* - remotePath: /some-path
* - remotePath: /some-path/
* - remoteBranch: master
*
* <p>e.g. dogma://foo.com/bar/qux.dogma is split into:
Expand Down

0 comments on commit e2c8db4

Please sign in to comment.