br.com.objectos.git
Class StageGitCommand<V>

java.lang.Object
  extended by br.com.objectos.concurrent.StageComputationTask<V>
      extended by br.com.objectos.git.StageGitCommand<V>
Type Parameters:
V - the type of this command's result
All Implemented Interfaces:
Computation<V>, CpuTask, br.com.objectos.core.object.ToStringObject, GitCommand<V>
Direct Known Subclasses:
MaterializeCommand

public abstract class StageGitCommand<V>
extends StageComputationTask<V>
implements Computation<V>, GitCommand<V>, br.com.objectos.core.object.ToStringObject

Base class for creating high-level stage-based (porcelain) Git commands. This class is useful for chaining many low-level (plumbing) asynchronous tasks and other high-level asynchronous commands. It does so offering a (mostly) imperative programming style.

This class is an instance of GitCommand and is intended to be executed by a GitService instance.

Warning

This class extends StageComputationTask and, therefore, inherits all of its drawbacks. Please understand all of the drawbacks by reading the superclass' javadocs before using this class.

Usage example

The following copies a tree object from one repository to another repository. This the actual implementation for the internal CopyTreeCommand:
final class CopyTreeCommand extends StageGitCommand<ObjectId> {
  private final MutableSet<ObjectId> objectsToCopy = MutableSet.create();

  private final Repository source;

  private final ObjectId sourceId;

  private final Repository target;

  CopyTreeCommand(Repository source, ObjectId tree, Repository destination) {
    this.source = Checks.checkNotNull(source, "source == null");

    this.sourceId = Checks.checkNotNull(tree, "tree == null");

    this.target = Checks.checkNotNull(destination, "destination == null");
  }

  protected final void executeStage(int stage) throws Exception {
    switch (stage) {
      case 0:
        readTree(source, sourceId);

      case 1:
        if (isComputing()) {
          return;
        }

        final ImmutableList<Tree> trees;
        trees = getAll();

        for (Tree tree : trees) {
          visitTree(tree);
        }

        if (hasComputations()) {
          goTo(1);

          return;
        }

        final ImmutableSet<ObjectId> set;
        set = objectsToCopy.toImmutableSet();

        objectsToCopy.clear();

        copyObjects(source, set, target);

      case 2:
        if (isComputing()) {
          return;
        }

        setResult(sourceId);

        return;

      default:
        throw new AssertionError("Unexpected step=" + stage);
    }
  }

  private void visitTree(Tree tree) {
    ObjectId treeId;
    treeId = tree.getObjectId();

    objectsToCopy.add(treeId);

    ImmutableList<Entry> entries;
    entries = tree.getEntries();

    for (Entry entry : entries) {
      ObjectId entryId;
      entryId = entry.getObjectId();

      if (entry.isBlob()) {
        objectsToCopy.add(entryId);
      } else {
        readTree(source, entryId);
      }
    }
  }
}
 

Since:
3

Constructor Summary
protected StageGitCommand()
          Sole constructor.
 
Method Summary
protected  void copyObjects(Repository source, ImmutableSet<ObjectId> objects, Repository destination)
          Adds a single asynchronous computation by invoking GitCommandExecutor.copyObjects(Repository, ImmutableSet, Repository).
protected  void copyTree(Repository source, ObjectId tree, Repository destination)
          Adds a single asynchronous computation that copies from the source repository the specified tree object to the destination repository (as loose objects).
protected  void executeFinally()
          
 void formatToString(StringBuilder toString, int level)
          
protected  void materializeEntry(Repository repository, Entry entry, Directory target)
          Adds a single asynchronous computation by invoking GitCommandExecutor.materializeEntry(Repository, Entry, Directory).
protected  void openRepository(Directory directory)
          Adds a single asynchronous computation by invoking GitCommandExecutor.openRepository(Directory).
protected  void readBlob(Repository repository, ObjectId id)
          Adds a single asynchronous computation by invoking GitCommandExecutor.readBlob(Repository, ObjectId).
protected  void readCommit(Repository repository, ObjectId id)
          Adds a single asynchronous computation by invoking GitCommandExecutor.readCommit(Repository, ObjectId).
protected  void readTree(Repository repository, ObjectId id)
          Adds a single asynchronous computation by invoking GitCommandExecutor.readTree(Repository, ObjectId).
protected  void resolve(Repository repository, RefName ref)
          Adds a single asynchronous computation by invoking GitCommandExecutor.resolve(Repository, RefName).
protected  void submit(GitCommand<?> command)
          Adds a single asynchronous computation by invoking GitExecutor.submit(GitCommand).
 Computation<V> submitAsync(GitCommandExecutor executor, CpuWorker cpuWorker)
          Returns a possibly new and possibly asynchronous git computation representing the pending result of this command.
 String toString()
           
protected  void updateRef(Repository repository, RefName ref, ObjectId newValue)
          Adds a single asynchronous computation by invoking GitCommandExecutor.updateRef(Repository, RefName, ObjectId).
protected  void writeCommit(Repository repository, MutableCommit commit)
          Adds a single asynchronous computation by invoking GitCommandExecutor.writeCommit(Repository, MutableCommit).
protected  void writeTree(Repository repository, MutableTree tree)
          Adds a single asynchronous computation by invoking GitCommandExecutor.writeTree(GitCommand, Repository, MutableTree).
 
Methods inherited from class br.com.objectos.concurrent.StageComputationTask
addComputation, executeOne, executeStage, get, getAll, getResult, goTo, hasComputations, isActive, isComputing, reset, setResult
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface br.com.objectos.concurrent.Computation
getResult, isActive
 

Constructor Detail

StageGitCommand

protected StageGitCommand()
Sole constructor.

Method Detail

formatToString

public void formatToString(StringBuilder toString,
                           int level)

Specified by:
formatToString in interface br.com.objectos.core.object.ToStringObject

submitAsync

public final Computation<V> submitAsync(GitCommandExecutor executor,
                                        CpuWorker cpuWorker)
Returns a possibly new and possibly asynchronous git computation representing the pending result of this command.

Please

Specified by:
submitAsync in interface GitCommand<V>
Parameters:
executor - the executor providing low-level git tasks
cpuWorker - the cpu worker to execute the command
Returns:
a computation object representing the pending result of this command

toString

public final String toString()
Overrides:
toString in class Object

copyObjects

protected final void copyObjects(Repository source,
                                 ImmutableSet<ObjectId> objects,
                                 Repository destination)
Adds a single asynchronous computation by invoking GitCommandExecutor.copyObjects(Repository, ImmutableSet, Repository).

Parameters:
source - the source repository containing the objects. Git objects will be read from this repository.
objects - the ids (hash values) of the objects to be copied
destination - the destination repository. Git objects will (possibly) be written to this repository
See Also:
StageComputationTask.addComputation(br.com.objectos.concurrent.Computation), openRepository(Directory)

copyTree

protected final void copyTree(Repository source,
                              ObjectId tree,
                              Repository destination)
Adds a single asynchronous computation that copies from the source repository the specified tree object to the destination repository (as loose objects).

The computation, once completed, returns the hash value of the tree object created in the destination repository.

The computation will fail if the source repository does not contain the specified tree object or if an I/O error occurs.

Parameters:
source - the repository from where to read the tree object
tree - the hash value of the tree object to copy
destination - the repository where the tree will be copied to
See Also:
StageComputationTask.addComputation(br.com.objectos.concurrent.Computation), openRepository(Directory)

executeFinally

protected void executeFinally()

Overrides:
executeFinally in class StageComputationTask<V>

materializeEntry

protected final void materializeEntry(Repository repository,
                                      Entry entry,
                                      Directory target)
Adds a single asynchronous computation by invoking GitCommandExecutor.materializeEntry(Repository, Entry, Directory).

Parameters:
repository - repository to read blob contents from (if necessary)
entry - the entry to be materialized
target - the target directory
See Also:
StageComputationTask.addComputation(br.com.objectos.concurrent.Computation), openRepository(Directory), readTree(Repository, ObjectId)

openRepository

protected final void openRepository(Directory directory)
Adds a single asynchronous computation by invoking GitCommandExecutor.openRepository(Directory).

Parameters:
directory - the directory where a Git repository is located
See Also:
StageComputationTask.addComputation(br.com.objectos.concurrent.Computation)

readBlob

protected final void readBlob(Repository repository,
                              ObjectId id)
Adds a single asynchronous computation by invoking GitCommandExecutor.readBlob(Repository, ObjectId).

Parameters:
repository - the git repository from which to read the commit
id - the unique hash value of the blob to read
See Also:
StageComputationTask.addComputation(br.com.objectos.concurrent.Computation), openRepository(Directory)

readCommit

protected final void readCommit(Repository repository,
                                ObjectId id)
Adds a single asynchronous computation by invoking GitCommandExecutor.readCommit(Repository, ObjectId).

Parameters:
repository - the git repository from which to read the commit
id - the unique hash value of the commit to read
See Also:
StageComputationTask.addComputation(br.com.objectos.concurrent.Computation), openRepository(Directory)

readTree

protected final void readTree(Repository repository,
                              ObjectId id)
Adds a single asynchronous computation by invoking GitCommandExecutor.readTree(Repository, ObjectId).

Parameters:
repository - the git repository from which to read the tree
id - the unique hash value of the tree to read
See Also:
StageComputationTask.addComputation(br.com.objectos.concurrent.Computation), openRepository(Directory)

resolve

protected final void resolve(Repository repository,
                             RefName ref)
Adds a single asynchronous computation by invoking GitCommandExecutor.resolve(Repository, RefName).

Parameters:
repository - the repository for which the reference will be resolved
ref - the Git reference to be resolved
See Also:
StageComputationTask.addComputation(br.com.objectos.concurrent.Computation), openRepository(Directory)

submit

protected final void submit(GitCommand<?> command)
Adds a single asynchronous computation by invoking GitExecutor.submit(GitCommand).

Parameters:
command - the command to be submitted
See Also:
StageComputationTask.addComputation(br.com.objectos.concurrent.Computation)

updateRef

protected final void updateRef(Repository repository,
                               RefName ref,
                               ObjectId newValue)
Adds a single asynchronous computation by invoking GitCommandExecutor.updateRef(Repository, RefName, ObjectId).

Parameters:
repository - the repository for which the reference will be updated
ref - the Git reference to be updated
newValue - the new value for the reference
See Also:
StageComputationTask.addComputation(br.com.objectos.concurrent.Computation), openRepository(Directory)

writeCommit

protected final void writeCommit(Repository repository,
                                 MutableCommit commit)
Adds a single asynchronous computation by invoking GitCommandExecutor.writeCommit(Repository, MutableCommit).

Parameters:
repository - the git repository where the commit is to be written
commit - a MutableCommit instance containing all the information to be recorded as a commit object
See Also:
StageComputationTask.addComputation(br.com.objectos.concurrent.Computation), openRepository(Directory)

writeTree

protected final void writeTree(Repository repository,
                               MutableTree tree)
Adds a single asynchronous computation by invoking GitCommandExecutor.writeTree(GitCommand, Repository, MutableTree).

Parameters:
repository - the git repository where the tree is to be written
tree - a MutableTree to be written
See Also:
StageComputationTask.addComputation(br.com.objectos.concurrent.Computation), openRepository(Directory)


Copyright © 2020–2022 Objectos Software LTDA. All rights reserved.