Skip to content

Commit

Permalink
✅ Test more efficiently super fast commit
Browse files Browse the repository at this point in the history
  • Loading branch information
evrignaud committed May 16, 2016
1 parent d7ab1ed commit c223e97
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 24 deletions.
60 changes: 38 additions & 22 deletions src/main/java/org/fim/command/CommitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
import static org.fim.util.FileStateUtil.buildFileNamesMap;

public class CommitCommand extends AbstractCommand {
SettingsManager settingsManager;
StateManager manager;

@Override
public String getCmdName() {
return "commit";
Expand All @@ -63,7 +66,7 @@ public String getDescription() {

@Override
public Object execute(Context context) throws Exception {
SettingsManager settingsManager = new SettingsManager(context);
settingsManager = new SettingsManager(context);
HashMode globalHashMode = settingsManager.getGlobalHashMode();
if (context.getHashMode() == dontHash && globalHashMode != dontHash) {
Logger.error("Computing hash is mandatory");
Expand All @@ -78,7 +81,7 @@ public Object execute(Context context) throws Exception {
}
}

StateManager manager = new StateManager(context);
manager = new StateManager(context);
State currentState = new StateGenerator(context).generateState(context.getComment(), context.getRepositoryRootDir(), context.getCurrentDirectory());
State lastState = manager.loadLastState();
State lastStateToCompare = lastState;
Expand All @@ -96,26 +99,7 @@ public Object execute(Context context) throws Exception {
if (result.somethingModified()) {
Console.newLine();
if (confirmAction(context, "commit")) {
currentState.setModificationCounts(result.getModificationCounts());

if (context.getHashMode() != dontHash && context.getHashMode() != globalHashMode) {
// Reload the last state with the globalHashMode in order to get a complete state.
context.setHashMode(globalHashMode);
currentState.setHashMode(globalHashMode);
lastState = manager.loadLastState();
retrieveMissingHash(context, currentState, lastState);
}

if (context.isInvokedFromSubDirectory()) {
currentState = createConsolidatedState(context, lastState, currentState);
}

manager.createNewState(currentState);

if (context.isPurgeStates()) {
PurgeStatesCommand purgeStatesCommand = new PurgeStatesCommand();
purgeStatesCommand.execute(context);
}
commitModifications(context, currentState, lastState, result);
} else {
Logger.info("Nothing committed");
}
Expand All @@ -124,6 +108,38 @@ public Object execute(Context context) throws Exception {
return result;
}

private void commitModifications(Context context, State originalCurrentState, State originalLastState, CompareResult result) throws Exception {
State currentState = originalCurrentState;
State lastState = originalLastState;

HashMode initialHashMode = context.getHashMode();
try {
currentState.setModificationCounts(result.getModificationCounts());

HashMode globalHashMode = settingsManager.getGlobalHashMode();
if (initialHashMode != dontHash && initialHashMode != globalHashMode) {
// Reload the last state with the globalHashMode in order to get a complete state.
context.setHashMode(globalHashMode);
currentState.setHashMode(globalHashMode);
lastState = manager.loadLastState();
retrieveMissingHash(context, currentState, lastState);
}

if (context.isInvokedFromSubDirectory()) {
currentState = createConsolidatedState(context, lastState, currentState);
}

manager.createNewState(currentState);

if (context.isPurgeStates()) {
PurgeStatesCommand purgeStatesCommand = new PurgeStatesCommand();
purgeStatesCommand.execute(context);
}
} finally {
context.setHashMode(initialHashMode);
}
}

private void retrieveMissingHash(Context context, State currentState, State lastState) throws NoSuchAlgorithmException, IOException {
Map<String, FileState> lastFileStateMap = buildFileNamesMap(lastState.getFileStates());

Expand Down
18 changes: 16 additions & 2 deletions src/test/java/org/fim/FullScenarioTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,25 @@ public void fullScenario() throws Exception {
assertThat(modificationCounts.getRenamed()).isEqualTo(1);
assertThat(modificationCounts.getDeleted()).isEqualTo(1);

// Check that using normal hashMode there is no modification detected
commit_AndAssertFilesModifiedCountEqualsTo(context, 0);
assertThatUsingNormalHashModeNoModificationIsDetected(context);

tool.setFileContent("file13", "New file 13");
tool.setFileContent("file14", "New file 14");

// Commit again using super-fast mode (hashSmallBlock)
compareResult = (CompareResult) commitCommand.execute(superFastModeContext);
assertThat(compareResult.modifiedCount()).isEqualTo(2);
modificationCounts = compareResult.getModificationCounts();
assertThat(modificationCounts.getAdded()).isEqualTo(2);

assertThatUsingNormalHashModeNoModificationIsDetected(context);
}
}

private void assertThatUsingNormalHashModeNoModificationIsDetected(Context context) throws Exception {
commit_AndAssertFilesModifiedCountEqualsTo(context, 0);
}

private void doSomeModifications() throws IOException {
Files.createDirectories(dir01);

Expand Down

0 comments on commit c223e97

Please sign in to comment.