-
Notifications
You must be signed in to change notification settings - Fork 897
/
Copy pathCheckout.cs
189 lines (165 loc) · 8.72 KB
/
Checkout.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Linq;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
public static partial class Commands
{
/// <summary>
/// Checkout the specified <see cref="Branch"/>, reference or SHA.
/// <para>
/// If the committishOrBranchSpec parameter resolves to a branch name, then the checked out HEAD will
/// will point to the branch. Otherwise, the HEAD will be detached, pointing at the commit sha.
/// </para>
/// </summary>
/// <param name="repository">The repository to act on</param>
/// <param name="committishOrBranchSpec">A revparse spec for the commit or branch to checkout.</param>
/// <returns>The <see cref="Branch"/> that was checked out.</returns>
public static Branch Checkout(IRepository repository, string committishOrBranchSpec)
{
return Checkout(repository, committishOrBranchSpec, new CheckoutOptions());
}
/// <summary>
/// Checkout the specified <see cref="Branch"/>, reference or SHA.
/// <para>
/// If the committishOrBranchSpec parameter resolves to a branch name, then the checked out HEAD will
/// will point to the branch. Otherwise, the HEAD will be detached, pointing at the commit sha.
/// </para>
/// </summary>
/// <param name="repository">The repository to act on</param>
/// <param name="committishOrBranchSpec">A revparse spec for the commit or branch to checkout.</param>
/// <param name="options"><see cref="CheckoutOptions"/> controlling checkout behavior.</param>
/// <returns>The <see cref="Branch"/> that was checked out.</returns>
public static Branch Checkout(IRepository repository, string committishOrBranchSpec, CheckoutOptions options)
{
Ensure.ArgumentNotNull(repository, nameof(repository));
Ensure.ArgumentNotNullOrEmptyString(committishOrBranchSpec, nameof(committishOrBranchSpec));
Ensure.ArgumentNotNull(options, nameof(options));
Reference reference = null;
GitObject obj = null;
Branch branch = null;
try
{
repository.RevParse(committishOrBranchSpec, out reference, out obj);
}
catch (NotFoundException)
{
// If committishOrBranchSpec is not a local branch but matches a tracking branch
// in exactly one remote, use it. This is the "git checkout" command's default behavior.
// https://git-scm.com/docs/git-checkout#Documentation/git-checkout.txt-emgitcheckoutemltbranchgt
var remoteBranches = repository.Network.Remotes
.SelectMany(r => repository.Branches.Where(b =>
b.IsRemote &&
b.CanonicalName == $"refs/remotes/{r.Name}/{committishOrBranchSpec}"))
.ToList();
if (remoteBranches.Count == 1)
{
branch = repository.CreateBranch(committishOrBranchSpec, remoteBranches[0].Tip);
repository.Branches.Update(branch, b => b.TrackedBranch = remoteBranches[0].CanonicalName);
return Checkout(repository, branch, options);
}
if (remoteBranches.Count > 1)
{
throw new AmbiguousSpecificationException($"'{committishOrBranchSpec}' matched multiple ({remoteBranches.Count}) remote tracking branches");
}
throw;
}
if (reference != null && reference.IsLocalBranch)
{
branch = repository.Branches[reference.CanonicalName];
return Checkout(repository, branch, options);
}
Commit commit = obj.Peel<Commit>(true);
Checkout(repository, commit.Tree, options, committishOrBranchSpec);
return repository.Head;
}
/// <summary>
/// Checkout the tip commit of the specified <see cref="Branch"/> object. If this commit is the
/// current tip of the branch, will checkout the named branch. Otherwise, will checkout the tip commit
/// as a detached HEAD.
/// </summary>
/// <param name="repository">The repository to act on</param>
/// <param name="branch">The <see cref="Branch"/> to check out.</param>
/// <returns>The <see cref="Branch"/> that was checked out.</returns>
public static Branch Checkout(IRepository repository, Branch branch)
{
return Checkout(repository, branch, new CheckoutOptions());
}
/// <summary>
/// Checkout the tip commit of the specified <see cref="Branch"/> object. If this commit is the
/// current tip of the branch, will checkout the named branch. Otherwise, will checkout the tip commit
/// as a detached HEAD.
/// </summary>
/// <param name="repository">The repository to act on</param>
/// <param name="branch">The <see cref="Branch"/> to check out.</param>
/// <param name="options"><see cref="CheckoutOptions"/> controlling checkout behavior.</param>
/// <returns>The <see cref="Branch"/> that was checked out.</returns>
public static Branch Checkout(IRepository repository, Branch branch, CheckoutOptions options)
{
Ensure.ArgumentNotNull(repository, nameof(repository));
Ensure.ArgumentNotNull(branch, nameof(branch));
Ensure.ArgumentNotNull(options, nameof(options));
// Make sure this is not an unborn branch.
if (branch.Tip == null)
{
throw new UnbornBranchException("The tip of branch '{0}' is null. There's nothing to checkout.",
branch.FriendlyName);
}
if (!branch.IsRemote && !(branch is DetachedHead) &&
string.Equals(repository.Refs[branch.CanonicalName].TargetIdentifier, branch.Tip.Id.Sha,
StringComparison.OrdinalIgnoreCase))
{
Checkout(repository, branch.Tip.Tree, options, branch.CanonicalName);
}
else
{
Checkout(repository, branch.Tip.Tree, options, branch.Tip.Id.Sha);
}
return repository.Head;
}
/// <summary>
/// Checkout the specified <see cref="LibGit2Sharp.Commit"/>.
/// <para>
/// Will detach the HEAD and make it point to this commit sha.
/// </para>
/// </summary>
/// <param name="repository">The repository to act on</param>
/// <param name="commit">The <see cref="LibGit2Sharp.Commit"/> to check out.</param>
/// <returns>The <see cref="Branch"/> that was checked out.</returns>
public static Branch Checkout(IRepository repository, Commit commit)
{
return Checkout(repository, commit, new CheckoutOptions());
}
/// <summary>
/// Checkout the specified <see cref="LibGit2Sharp.Commit"/>.
/// <para>
/// Will detach the HEAD and make it point to this commit sha.
/// </para>
/// </summary>
/// <param name="repository">The repository to act on</param>
/// <param name="commit">The <see cref="LibGit2Sharp.Commit"/> to check out.</param>
/// <param name="options"><see cref="CheckoutOptions"/> controlling checkout behavior.</param>
/// <returns>The <see cref="Branch"/> that was checked out.</returns>
public static Branch Checkout(IRepository repository, Commit commit, CheckoutOptions options)
{
Ensure.ArgumentNotNull(repository, nameof(repository));
Ensure.ArgumentNotNull(commit, nameof(commit));
Ensure.ArgumentNotNull(options, nameof(options));
Checkout(repository, commit.Tree, options, commit.Id.Sha);
return repository.Head;
}
/// <summary>
/// Internal implementation of Checkout that expects the ID of the checkout target
/// to already be in the form of a canonical branch name or a commit ID.
/// </summary>
/// <param name="repository">The repository to act on</param>
/// <param name="tree">The <see cref="Tree"/> to checkout.</param>
/// <param name="checkoutOptions"><see cref="CheckoutOptions"/> controlling checkout behavior.</param>
/// <param name="refLogHeadSpec">The spec which will be written as target in the reflog.</param>
public static void Checkout(IRepository repository, Tree tree, CheckoutOptions checkoutOptions, string refLogHeadSpec)
{
repository.Checkout(tree, null, checkoutOptions);
repository.Refs.MoveHeadTarget(refLogHeadSpec);
}
}
}