Skip to content

Commit

Permalink
Add checkout command
Browse files Browse the repository at this point in the history
git tfs checkout changesetId [-b=branch_name]
ex: git-tfs checkout 2365
      git-tfs checkout 2365 -b=bugfix_2365

Solve git-tfs#563
  • Loading branch information
pmiossec committed Jul 10, 2014
1 parent ee86eed commit 0225dd5
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
61 changes: 61 additions & 0 deletions GitTfs/Commands/Checkout.cs
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using NDesk.Options;
using Sep.Git.Tfs.Core;
using StructureMap;

namespace Sep.Git.Tfs.Commands
{
[Pluggable("checkout")]
[RequiresValidGitRepository]
[Description("checkout changesetId [-b=branch_name]\n ex: git-tfs checkout 2365\n git-tfs checkout 2365 -b=bugfix_2365\n")]
public class Checkout : GitTfsCommand
{
private readonly Globals _globals;
private readonly TextWriter _stdout;

public Checkout(Globals globals, TextWriter stdout)
{
_globals = globals;
_stdout = stdout;
}

public OptionSet OptionSet
{
get
{
return new OptionSet
{
{ "b|branch=", "Name of the branch to create", v => BranchName = v },
};
}
}

protected string BranchName { get; set; }

public int Run(string id)
{
long changesetId;
if(!long.TryParse(id, out changesetId))
throw new GitTfsException("error: wrong format for changeset id...");
var sha = _globals.Repository.FindCommitHashByChangesetId(changesetId);
if (string.IsNullOrEmpty(sha))
throw new GitTfsException("error: commit not found for this changeset id...");
string commitishToCheckout = sha;
if (!string.IsNullOrEmpty(BranchName))
{
BranchName = _globals.Repository.AssertValidBranchName(BranchName);
if(!_globals.Repository.CreateBranch(BranchName.ToLocalGitRef(), sha))
throw new GitTfsException("error: can not create branch '" + BranchName + "'");
_stdout.WriteLine("Branch '" + BranchName + "' created...");
commitishToCheckout = BranchName;
}
if(!_globals.Repository.Checkout(commitishToCheckout))
throw new GitTfsException("error: unable to checkout '" + commitishToCheckout + "' due to changes in your workspace!",
new List<string> { "commit or stash your changes before retrying..."});
return GitTfsExitCodes.OK;
}
}
}
8 changes: 7 additions & 1 deletion GitTfs/Commands/Init.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
Expand Down Expand Up @@ -171,5 +171,11 @@ public static string ToGitBranchNameFromTfsRepositoryPath(this string tfsReposit
: tfsRepositoryPath;
return gitBranchNameExpected.ToGitRefName();
}

public static string ToLocalGitRef(this string refName)
{
return "refs/heads/" + refName;
}

}
}
2 changes: 1 addition & 1 deletion GitTfs/Commands/InitBranch.cs
Expand Up @@ -380,7 +380,7 @@ private IFetchResult FetchRemote(IGitTfsRemote tfsRemote, bool stopOnFailMergeCo
if (fetchResult.IsSuccess && createBranch && tfsRemote.Id != GitTfsConstants.DefaultRepositoryId)
{
Trace.WriteLine("Try creating the local branch...");
var branchRef = "refs/heads/" + tfsRemote.Id;
var branchRef = tfsRemote.Id.ToLocalGitRef();
if (!_globals.Repository.HasRef(branchRef))
{
if (!_globals.Repository.CreateBranch(branchRef, tfsRemote.MaxCommitHash))
Expand Down
13 changes: 13 additions & 0 deletions GitTfs/Core/GitRepository.cs
Expand Up @@ -619,5 +619,18 @@ public void GarbageCollect(bool auto, string additionalMessage)
realStdout.WriteLine("Warning: `git gc` failed! " + additionalMessage);
}
}

public bool Checkout(string commitish)
{
try
{
_repository.Checkout(commitish);
return true;
}
catch (MergeConflictException ex)
{
return false;
}
}
}
}
1 change: 1 addition & 0 deletions GitTfs/Core/IGitRepository.cs
Expand Up @@ -51,5 +51,6 @@ public interface IGitRepository : IGitHelpers
void ResetRemote(IGitTfsRemote remoteToReset, string target);
string GetCurrentBranch();
void GarbageCollect(bool auto = false, string additionalMessage = null);
bool Checkout(string commitish);
}
}
1 change: 1 addition & 0 deletions GitTfs/GitTfs.csproj
Expand Up @@ -97,6 +97,7 @@
<Compile Include="..\Version.cs">
<Link>Properties\Version.cs</Link>
</Compile>
<Compile Include="Commands\Checkout.cs" />
<Compile Include="Commands\ResetRemote.cs" />
<Compile Include="Commands\Create.cs" />
<Compile Include="Commands\ListRemoteBranches.cs" />
Expand Down

0 comments on commit 0225dd5

Please sign in to comment.