Skip to content

Explore moving the command-simulating methods to their own namespace #1248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions LibGit2Sharp/Commands/Checkout.cs
Original file line number Diff line number Diff line change
@@ -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<string> paths;
readonly CheckoutOptions options;
readonly CheckoutMode mode;

/// <summary>
/// Checkout a commit
///
/// This will detach HEAD to the given commit.
/// </summary>
/// <param name="repo">The repository in which to act</param>
/// <param name="commit">The commit to checkout</param>
/// <param name="options">The options for the checkout operation</param>
public Checkout(Repository repo, Commit commit, CheckoutOptions options)
{
this.repo = repo;
this.commit = commit;
this.options = options;
this.mode = CheckoutMode.DetachHead;
}

/// <summary>
/// Checkout files from a commit
///
/// This will put the files from the commit into the working directory and the index.
/// </summary>
/// <param name="repo">The repository in which to act</param>
/// <param name="commit">The commit to checkout the files from</param>
/// <param name="paths">List of paths to checkout. Leave null or empty for all</param>
/// <param name="options">The options for the checkout operation</param>
public Checkout(Repository repo, Commit commit, IEnumerable<string> paths, CheckoutOptions options)
{
this.repo = repo;
this.tree = commit.Tree;
this.paths = paths;
this.options = options;
this.mode = CheckoutMode.CheckoutTree;
}

/// <summary>
/// Checkout a tree
///
/// This will put the files from the tree into the working directory and the index.
/// </summary>
/// <param name="repo">The repository in which to act</param>
/// <param name="tree">The tree to checkout</param>
/// <param name="paths">List of paths to checkout. Leave null or empty for all</param>
/// <param name="options">The options for the checkout operation</param>
public Checkout(Repository repo, Tree tree, IEnumerable<string> 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");
}
}
}
}
15 changes: 15 additions & 0 deletions LibGit2Sharp/Commands/CommandBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LibGit2Sharp.Commands
{
abstract class CommandBase
{
/// <summary>
/// Run the command
/// </summary>
public abstract void Run();
}
}
2 changes: 2 additions & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
<Compile Include="CherryPickOptions.cs" />
<Compile Include="CherryPickResult.cs" />
<Compile Include="CloneOptions.cs" />
<Compile Include="Commands\Checkout.cs" />
<Compile Include="Commands\CommandBase.cs" />
<Compile Include="CommitFilter.cs" />
<Compile Include="CommitOptions.cs" />
<Compile Include="CommitSortStrategies.cs" />
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ private void Checkout(
/// <param name="tree">The <see cref="Tree"/> to checkout.</param>
/// <param name="paths">The paths to checkout.</param>
/// <param name="opts">Collection of parameters controlling checkout behavior.</param>
private void CheckoutTree(
internal void CheckoutTree(
Tree tree,
IList<string> paths,
IConvertableToGitCheckoutOpts opts)
Expand Down