Skip to content

Commit

Permalink
🪲 Fix Super-fast commit
Browse files Browse the repository at this point in the history
  • Loading branch information
evrignaud committed Sep 28, 2016
1 parent 6f1d272 commit 2ee84f7
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 23 deletions.
9 changes: 7 additions & 2 deletions src/main/java/org/fim/command/CommitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.fim.model.FileState;
import org.fim.model.HashMode;
import org.fim.model.Modification;
import org.fim.model.ModificationCounts;
import org.fim.model.State;
import org.fim.util.Console;
import org.fim.util.Logger;
Expand All @@ -41,9 +42,11 @@
import java.util.Map;
import java.util.stream.Collectors;

import static org.fim.internal.StateComparator.resetFileStates;
import static org.fim.model.HashMode.dontHash;
import static org.fim.model.Modification.attributesModified;
import static org.fim.model.Modification.dateModified;
import static org.fim.model.Modification.deleted;
import static org.fim.util.FileStateUtil.buildFileNamesMap;

public class CommitCommand extends AbstractCommand {
Expand Down Expand Up @@ -162,7 +165,7 @@ private void retrieveMissingHash(Context context, State currentState, State last
throw new IllegalStateException(String.format("Not able to find file '%s' into the previous state", fileState.getFileName()));
}
fileState.setFileHash(lastFileState.getFileHash());
} else {
} else if (modification != deleted) {
// Hash changed, we need to compute all the mandatory hash
toReHash.add(fileState);
}
Expand All @@ -175,9 +178,11 @@ private void retrieveMissingHash(Context context, State currentState, State last
private State createConsolidatedState(Context context, State lastState, State currentState) {
State filteredState = lastState.filterDirectory(context.getRepositoryRootDir(), context.getCurrentDirectory(), false);

resetFileStates(filteredState.getFileStates());

State consolidatedState = currentState.clone();
consolidatedState.getFileStates().addAll(filteredState.getFileStates());
consolidatedState.getModificationCounts().add(filteredState.getModificationCounts());
consolidatedState.setModificationCounts(new ModificationCounts(consolidatedState.getFileStates()));
consolidatedState.getIgnoredFiles().addAll(lastState.getIgnoredFiles());

return consolidatedState;
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/org/fim/internal/StateComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ public StateComparator(Context context, State lastState, State currentState) {
init();
}

/**
* Remove modification, previousFileState and deleted entries
*/
public static void resetFileStates(List<FileState> fileStates) {
for (Iterator<FileState> iter = fileStates.iterator(); iter.hasNext(); ) {
FileState fileState = iter.next();
if (fileState.getModification() == deleted) {
iter.remove();
} else {
fileState.setModification(null);
fileState.setPreviousFileState(null);
}
}
}

private void init() {
if (lastState != null && !lastState.getModelVersion().equals(currentState.getModelVersion())) {
Logger.warning("Not able to compare with a State that have a different model version.");
Expand Down Expand Up @@ -104,14 +119,7 @@ private void makeLastStateComparable() {
filterOut(lastState, SELinuxLabel.name());
}

// Remove previousFileState and deleted entries
for (Iterator<FileState> iter = lastState.getFileStates().iterator(); iter.hasNext(); ) {
FileState fileState = iter.next();
fileState.setPreviousFileState(null);
if (fileState.getModification() == deleted) {
iter.remove();
}
}
resetFileStates(lastState.getFileStates());

if (context.isDatesIgnored()) {
FileTime noTime = new FileTime(0, 0);
Expand Down Expand Up @@ -294,6 +302,7 @@ private void searchForDeleted() {
.filter(fileState -> !isFileIgnored(fileState))
.forEach(fileState -> {
fileState.setModification(deleted);
fileState.restoreOriginalHash();
result.getDeleted().add(new Difference(null, fileState));
});
}
Expand Down
60 changes: 49 additions & 11 deletions src/main/java/org/fim/model/ModificationCounts.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.fim.model;

import java.util.List;

public class ModificationCounts {
private int added;
private int copied;
Expand All @@ -28,6 +30,53 @@ public class ModificationCounts {
private int renamed;
private int deleted;

public ModificationCounts() {
// Use the default values
}

public ModificationCounts(List<FileState> fileStates) {
fileStates.stream()
.filter(fileState -> fileState.getModification() != null)
.forEach(fileState -> {
switch (fileState.getModification()) {
case added:
added++;
break;

case copied:
copied++;
break;

case duplicated:
duplicated++;
break;

case dateModified:
dateModified++;
break;

case contentModified:
contentModified++;
break;

case attributesModified:
attributesModified++;
break;

case renamed:
renamed++;
break;

case deleted:
deleted++;
break;

default:
break;
}
});
}

public int getAdded() {
return added;
}
Expand Down Expand Up @@ -91,15 +140,4 @@ public int getDeleted() {
public void setDeleted(int deleted) {
this.deleted = deleted;
}

public void add(ModificationCounts modificationCounts) {
added += modificationCounts.getAdded();
copied += modificationCounts.getCopied();
duplicated += modificationCounts.getDuplicated();
dateModified += modificationCounts.getDateModified();
contentModified += modificationCounts.getContentModified();
attributesModified += modificationCounts.getAttributesModified();
renamed += modificationCounts.getRenamed();
deleted += modificationCounts.getDeleted();
}
}
22 changes: 20 additions & 2 deletions src/test/java/org/fim/FullScenarioTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ public void fullScenario() throws Exception {
assertWeCanRollbackLastCommit(context, 1, 0);

if (hashMode == hashAll || hashMode == hashMediumBlock) {
Context superFastModeContext = context.clone();
superFastModeContext.setHashMode(hashSmallBlock);
Context superFastModeContext = tool.createContext(hashSmallBlock, hashMode == hashAll);
superFastModeContext.setComment("Using hash mode " + hashSmallBlock);

// Commit using super-fast mode (hashSmallBlock)
Expand All @@ -202,6 +201,18 @@ public void fullScenario() throws Exception {

assertThatUsingNormalHashModeNoModificationIsDetected(context);

// Commit a new file from the dir01 sub directory
tool.createFile(dir01.resolve("file15"));

Context fromSubDirectoryContext = tool.createInvokedFromSubDirContext(hashSmallBlock, "dir01", hashMode == hashAll);
superFastModeContext.setComment("From from sub directory dir01");

compareResult = (CompareResult) commitCommand.execute(fromSubDirectoryContext);
assertThat(compareResult.modifiedCount()).isEqualTo(1);
modificationCounts = compareResult.getModificationCounts();
assertThat(modificationCounts.getAdded()).isEqualTo(1);
assertLastStateContainSameModifications(context, modificationCounts);

// Add two files
tool.setFileContent("file13", "New file 13");
tool.setFileContent("file14", "New file 14");
Expand All @@ -214,6 +225,13 @@ public void fullScenario() throws Exception {
assertLastStateContainSameModifications(context, modificationCounts);

assertThatUsingNormalHashModeNoModificationIsDetected(context);

logResult = (LogResult) logCommand.execute(context);
assertThat(logResult.getLogEntries().size()).isEqualTo(4);
modificationCounts = logResult.getLogEntries().get(2).getModificationCounts();
assertThat(modificationCounts.getAdded()).isEqualTo(1);
assertThat(modificationCounts.getDeleted()).isEqualTo(0);
assertThat(modificationCounts.getCopied()).isEqualTo(0);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/fim/tooling/RepositoryTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public Context createContext(HashMode hashMode, boolean verbose) {
return context;
}

public Context createInvokedFromSubDirContext(HashMode hashMode, String subDirectory, boolean verbose) {
Context context = createContext(hashMode, verbose);
context.setCurrentDirectory(context.getCurrentDirectory().resolve(subDirectory));
context.setInvokedFromSubDirectory(true);
return context;
}

public void createASetOfFiles(int count) throws IOException {
for (int index = 1; index <= count; index++) {
createOneFile();
Expand Down

0 comments on commit 2ee84f7

Please sign in to comment.