initialize method

  1. @override
List<UndoableCommand> initialize()
override

Implementation

@override
List<UndoableCommand> initialize() {
  Repository repository = Repository(Directory.current.path);
  debugPrintToConsole(message: "Repository path is ${repository.path}");

  State state = repository.state;
  StateData? stateData = state.stateInfo;
  if (stateData == null) {
    return [ShowErrorCommand("Repository has no state data")];
  }

  Branch? thisBranch = thisBranchName == null ? state.getCurrentBranch() : Branch(thisBranchName!, repository);
  if (thisBranch == null) {
    return [ShowErrorCommand("Unable to get current branch info and has not been provided")];
  }

  BranchTreeMetaData? thisBranchMetaData = thisBranch.branchTreeMetaData;
  if (thisBranchMetaData == null) {
    return [ShowErrorCommand("This branch $thisBranchName has no meta data")];
  }

  CommitTreeMetaData? thisCommitMetaData =
      thisCommitSha == null ? thisBranchMetaData.sortedBranchCommitsFromLatest.firstOrNull : thisBranchMetaData.commits[thisCommitSha];
  if (thisCommitMetaData == null) {
    debugPrintToConsole(message: "commitAMetaData == null");
    return [ShowErrorCommand("Commit $thisCommitSha has no meta data")];
  }

  Branch otherBranch = Branch(otherBranchName, repository);
  BranchTreeMetaData? otherBranchMetaData = otherBranch.branchTreeMetaData;
  if (otherBranchMetaData == null) {
    debugPrintToConsole(message: "branchBMetaData == null");

    return [ShowErrorCommand("Branch $otherBranchName has no meta data")];
  }

  CommitTreeMetaData? otherCommitMetaData =
      otherCommitSha == null ? otherBranchMetaData.sortedBranchCommitsFromLatest.firstOrNull : otherBranchMetaData.commits[otherCommitSha];
  if (otherCommitMetaData == null) {
    debugPrintToConsole(message: "commitBMetaData == null");
    return [ShowErrorCommand("Commit $otherCommitSha has no meta data")];
  }

  Commit thisCommit = Commit(
    Sha1(thisCommitMetaData.sha),
    thisBranch,
    thisCommitMetaData.message,
    thisCommitMetaData.commitedObjects,
    Branch(otherCommitMetaData.originalBranch, repository),
    thisCommitMetaData.commitedAt,
  );

  Commit otherCommit = Commit(
    Sha1(otherCommitMetaData.sha),
    otherBranch,
    otherCommitMetaData.message,
    otherCommitMetaData.commitedObjects,
    Branch(otherCommitMetaData.originalBranch, repository),
    otherCommitMetaData.commitedAt,
  );

  return [ShowCommitDiffCommand(repository, thisCommit, otherCommit)];
}