Skip to content

Commit

Permalink
✨ The rollback command now show the details of the State that will be…
Browse files Browse the repository at this point in the history
… removed
  • Loading branch information
evrignaud committed Oct 1, 2016
1 parent 2da0d35 commit 6d2cbc1
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 81 deletions.
2 changes: 2 additions & 0 deletions src/main/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
</p>
++++

// text checked using http://www.onlinecorrection.com

= Fim - File Integrity Manager
Version {projectVersion}

Expand Down
37 changes: 35 additions & 2 deletions src/main/asciidoc/simple-example.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -385,15 +385,48 @@ Rollback the last commit.
[source, bash]
----
simple-example$ fim rbk -y
You are going to rollback the last commit. State 3 will be removed
You are going to rollback the last commit. State #3 will be removed
- State #3: 2016/05/09 21:58:39 (14 files - 176 bytes)
Comment: All modifications
Added: file12
Copied: file11 (was file05)
Duplicated: file03.dup1 = file03
Duplicated: file03.dup2 = file03
Duplicated: file07.dup1 = file07
Date modified: file02 last modified: 2016/05/09 21:58:33 -> 2016/05/09 21:58:36
Content modified: file04 last modified: 2016/05/09 21:58:33 -> 2016/05/09 21:58:36
Content modified: file05 last modified: 2016/05/09 21:58:33 -> 2016/05/09 21:58:36
Deleted: file01
Deleted: file06
1 added, 1 copied, 3 duplicated, 1 date modified, 2 content modified, 2 deleted
----

Rollback again.

[source, bash]
----
simple-example$ fim rbk -y
You are going to rollback the last commit. State 2 will be removed
You are going to rollback the last commit. State #2 will be removed
- State #2: 2016/05/09 21:58:38 (11 files - 132 bytes)
Comment: Modifications from dir01
Added: dir01/file01
Added: file01
Added: file02
Added: file03
Added: file04
Added: file05
Added: file06
Added: file07
Added: file08
Added: file09
Added: file10
11 added
----

Nothing more to rollback.
Expand Down
78 changes: 9 additions & 69 deletions src/main/java/org/fim/command/LogCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,15 @@
package org.fim.command;

import org.fim.internal.StateManager;
import org.fim.model.CommitDetails;
import org.fim.model.CompareResult;
import org.fim.model.Context;
import org.fim.model.Difference;
import org.fim.model.LogEntry;
import org.fim.model.LogResult;
import org.fim.model.Modification;
import org.fim.model.State;
import org.fim.util.Console;
import org.fim.util.Logger;

import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;

import static org.fim.model.HashMode.hashAll;
import static org.fim.model.Modification.added;
import static org.fim.model.Modification.attributesModified;
import static org.fim.model.Modification.contentModified;
import static org.fim.model.Modification.copied;
import static org.fim.model.Modification.dateModified;
import static org.fim.model.Modification.deleted;
import static org.fim.model.Modification.duplicated;
import static org.fim.model.Modification.renamed;

public class LogCommand extends AbstractCommand {
@Override
Expand All @@ -64,72 +47,29 @@ public String getDescription() {

@Override
public Object execute(Context context) throws Exception {
StateManager manager = new StateManager(context);
StateManager stateManager = new StateManager(context);

int lastStateNumber = manager.getLastStateNumber();
int lastStateNumber = stateManager.getLastStateNumber();
if (lastStateNumber == -1) {
Logger.error("No State found");
return null;
}

LogResult logResult = new LogResult();
for (int stateNumber = 1; stateNumber <= lastStateNumber; stateNumber++) {
Path statFile = manager.getStateFile(stateNumber);
Path statFile = stateManager.getStateFile(stateNumber);
if (Files.exists(statFile)) {
State state = manager.loadState(stateNumber, false);
LogEntry logEntry = new LogEntry();
logEntry.setStateNumber(stateNumber);
logEntry.setComment(state.getComment());
logEntry.setTimestamp(state.getTimestamp());
logEntry.setFileCount(state.getFileCount());
logEntry.setFilesContentLength(state.getFilesContentLength());
logEntry.setModificationCounts(state.getModificationCounts());
logEntry.setCommitDetails(getCommitDetails(state));
State state = stateManager.loadState(stateNumber, false);
LogEntry logEntry = new LogEntry(context, state, stateNumber);
logResult.add(logEntry);

CompareResult compareResult = buildCompareResult(context, state);
displayEntry(compareResult, logEntry, System.out);
logEntry.displayEntryHeader(System.out);
Console.newLine();
logEntry.getCompareResult().displayChanges(System.out);
Console.newLine();
}
}

return logResult;
}

private CommitDetails getCommitDetails(State state) {
if (state.getCommitDetails() != null) {
return state.getCommitDetails();
}
// For backward compatibility
return new CommitDetails(hashAll, null);
}

private void displayEntry(CompareResult compareResult, LogEntry logEntry, PrintStream out) {
logEntry.displayEntryHeader(out);
Console.newLine();
compareResult.displayChanges(out);
Console.newLine();
}

private CompareResult buildCompareResult(Context context, State state) {
CompareResult result = new CompareResult(context, null);

addModifications(state, added, result.getAdded());
addModifications(state, copied, result.getCopied());
addModifications(state, duplicated, result.getDuplicated());
addModifications(state, dateModified, result.getDateModified());
addModifications(state, contentModified, result.getContentModified());
addModifications(state, attributesModified, result.getAttributesModified());
addModifications(state, renamed, result.getRenamed());
addModifications(state, deleted, result.getDeleted());

return result;
}

private void addModifications(State state, Modification modification, List<Difference> differences) {
List<Difference> newDifferences = state.getFileStates().stream()
.filter(fileState -> fileState.getModification() == modification)
.map(Difference::new)
.collect(Collectors.toList());
differences.addAll(newDifferences);
}
}
21 changes: 20 additions & 1 deletion src/main/java/org/fim/command/RollbackCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@

import org.fim.internal.StateManager;
import org.fim.model.Context;
import org.fim.model.LogEntry;
import org.fim.model.State;
import org.fim.util.Console;
import org.fim.util.Logger;

import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;

Expand Down Expand Up @@ -58,7 +63,10 @@ public Object execute(Context context) throws Exception {

Path stateFile = stateManager.getStateFile(lastStateNumber);
if (Files.exists(stateFile)) {
System.out.printf("You are going to rollback the last commit. State %d will be removed%n", lastStateNumber);
System.out.printf("You are going to rollback the last commit. State #%d will be removed%n", lastStateNumber);

displayStateSummary(context, stateManager, lastStateNumber, System.out);

if (confirmAction(context, "remove it")) {
Files.delete(stateFile);

Expand All @@ -67,4 +75,15 @@ public Object execute(Context context) throws Exception {
}
return null;
}

private void displayStateSummary(Context context, StateManager stateManager, int stateNumber, PrintStream out) throws IOException {
State state = stateManager.loadState(stateNumber, false);
LogEntry logEntry = new LogEntry(context, state, stateNumber);

Console.newLine();
logEntry.displayEntryHeader(out);
Console.newLine();
logEntry.getCompareResult().displayChanges(out);
Console.newLine();
}
}
36 changes: 27 additions & 9 deletions src/main/java/org/fim/model/CompareResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static org.atteo.evo.inflector.English.plural;
import static org.fim.util.FormatUtil.formatCreationTime;
Expand All @@ -54,19 +55,36 @@ public class CompareResult {
private boolean searchForHardwareCorruption;

public CompareResult(Context context, State lastState) {
this(context, lastState, null);
}

public CompareResult(Context context, State lastState, State currentState) {
this.context = context;
this.lastState = lastState;
this.searchForHardwareCorruption = false;

added = new ArrayList<>();
copied = new ArrayList<>();
duplicated = new ArrayList<>();
dateModified = new ArrayList<>();
contentModified = new ArrayList<>();
attributesModified = new ArrayList<>();
renamed = new ArrayList<>();
deleted = new ArrayList<>();
corrupted = new ArrayList<>();
added = buildModifications(currentState, Modification.added);
copied = buildModifications(currentState, Modification.copied);
duplicated = buildModifications(currentState, Modification.duplicated);
dateModified = buildModifications(currentState, Modification.dateModified);
contentModified = buildModifications(currentState, Modification.contentModified);
attributesModified = buildModifications(currentState, Modification.attributesModified);
renamed = buildModifications(currentState, Modification.renamed);
deleted = buildModifications(currentState, Modification.deleted);
corrupted = buildModifications(currentState, Modification.corrupted);
}

private List<Difference> buildModifications(State state, Modification modification) {
List<Difference> differences;
if (state != null) {
differences = state.getFileStates().stream()
.filter(fileState -> fileState.getModification() == modification)
.map(Difference::new)
.collect(Collectors.toList());
} else {
differences = new ArrayList<>();
}
return differences;
}

public boolean isSearchForHardwareCorruption() {
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/fim/model/LogEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.PrintStream;

import static org.atteo.evo.inflector.English.plural;
import static org.fim.model.HashMode.hashAll;
import static org.fim.util.FormatUtil.formatDate;

public class LogEntry {
Expand All @@ -32,8 +33,20 @@ public class LogEntry {
private int fileCount;
private ModificationCounts modificationCounts;
private CommitDetails commitDetails;
private CompareResult compareResult;
private long filesContentLength;

public LogEntry(Context context, State state, int stateNumber) {
setStateNumber(stateNumber);
setComment(state.getComment());
setTimestamp(state.getTimestamp());
setFileCount(state.getFileCount());
setFilesContentLength(state.getFilesContentLength());
setModificationCounts(state.getModificationCounts());
setCommitDetails(getStateCommitDetails(state));
setCompareResult(new CompareResult(context, null, state));
}

public int getStateNumber() {
return stateNumber;
}
Expand Down Expand Up @@ -90,6 +103,14 @@ public void setCommitDetails(CommitDetails commitDetails) {
this.commitDetails = commitDetails;
}

public CompareResult getCompareResult() {
return compareResult;
}

public void setCompareResult(CompareResult compareResult) {
this.compareResult = compareResult;
}

public void displayEntryHeader(PrintStream out) {
out.printf("- State #%d: %s (%d %s - %s - generated%s using hash mode %s)%n", getStateNumber(), formatDate(getTimestamp()),
getFileCount(), plural("file", getFileCount()), FileUtils.byteCountToDisplaySize(getFilesContentLength()),
Expand All @@ -99,4 +120,12 @@ public void displayEntryHeader(PrintStream out) {
out.printf("\tComment: %s%n", getComment());
}
}

private CommitDetails getStateCommitDetails(State state) {
if (state.getCommitDetails() != null) {
return state.getCommitDetails();
}
// For backward compatibility
return new CommitDetails(hashAll, null);
}
}

0 comments on commit 6d2cbc1

Please sign in to comment.