diff --git a/LibGit2Sharp/Commands/Checkout.cs b/LibGit2Sharp/Commands/Checkout.cs new file mode 100644 index 000000000..9e61a3e7a --- /dev/null +++ b/LibGit2Sharp/Commands/Checkout.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace LibGit2Sharp.Commands +{ + enum CheckoutMode + { + DetachHead, + CheckoutTree, + } + + class Checkout + { + readonly Repository repo; + readonly Commit commit; + readonly Tree tree; + readonly IEnumerable paths; + readonly CheckoutOptions options; + readonly CheckoutMode mode; + + /// + /// Checkout a commit + /// + /// This will detach HEAD to the given commit. + /// + /// The repository in which to act + /// The commit to checkout + /// The options for the checkout operation + public Checkout(Repository repo, Commit commit, CheckoutOptions options) + { + this.repo = repo; + this.commit = commit; + this.options = options; + this.mode = CheckoutMode.DetachHead; + } + + /// + /// Checkout files from a commit + /// + /// This will put the files from the commit into the working directory and the index. + /// + /// The repository in which to act + /// The commit to checkout the files from + /// List of paths to checkout. Leave null or empty for all + /// The options for the checkout operation + public Checkout(Repository repo, Commit commit, IEnumerable paths, CheckoutOptions options) + { + this.repo = repo; + this.tree = commit.Tree; + this.paths = paths; + this.options = options; + this.mode = CheckoutMode.CheckoutTree; + } + + /// + /// Checkout a tree + /// + /// This will put the files from the tree into the working directory and the index. + /// + /// The repository in which to act + /// The tree to checkout + /// List of paths to checkout. Leave null or empty for all + /// The options for the checkout operation + public Checkout(Repository repo, Tree tree, IEnumerable paths, CheckoutOptions options) + { + this.repo = repo; + this.tree = tree; + this.paths = paths; + this.options = options; + this.mode = CheckoutMode.CheckoutTree; + } + + void RunCheckoutTree() + { + repo.CheckoutTree(tree, null, options); + } + + void RunDetachHead() + { + repo.Checkout(commit, options); + } + + public void Run() + { + switch (mode) + { + case CheckoutMode.DetachHead: + RunDetachHead(); + break; + case CheckoutMode.CheckoutTree: + RunCheckoutTree(); + break; + default: + throw new NotImplementedException("Unimplemented and undefined checkout mode"); + } + } + } +} diff --git a/LibGit2Sharp/Commands/CommandBase.cs b/LibGit2Sharp/Commands/CommandBase.cs new file mode 100644 index 000000000..f0a330117 --- /dev/null +++ b/LibGit2Sharp/Commands/CommandBase.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace LibGit2Sharp.Commands +{ + abstract class CommandBase + { + /// + /// Run the command + /// + public abstract void Run(); + } +} diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index c2fc74e2e..703c7a753 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -65,6 +65,8 @@ + + diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index 6c55a1eae..617c8bd63 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -960,7 +960,7 @@ private void Checkout( /// The to checkout. /// The paths to checkout. /// Collection of parameters controlling checkout behavior. - private void CheckoutTree( + internal void CheckoutTree( Tree tree, IList paths, IConvertableToGitCheckoutOpts opts)