Skip to content

Commit 526e589

Browse files
committed
#128 counter-spike
1 parent 9b0dc9c commit 526e589

File tree

11 files changed

+123
-147
lines changed

11 files changed

+123
-147
lines changed

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ public void CanCheckoutAnArbitraryCommit(string commitPointer)
398398

399399
Assert.False(detachedHead.IsRemote);
400400
Assert.Equal(detachedHead.Name, detachedHead.CanonicalName);
401-
Assert.Equal("(no branch)", detachedHead.CanonicalName);
401+
Assert.True(detachedHead.Name.EndsWith("...)"));
402402
Assert.Equal(repo.Lookup(commitPointer).Sha, detachedHead.Tip.Sha);
403403

404404
Assert.Equal(repo.Head, detachedHead);

LibGit2Sharp.Tests/InteractiveFixture.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using LibGit2Sharp.Interactive;
2-
using LibGit2Sharp.Tests.TestHelpers;
1+
using LibGit2Sharp.Tests.TestHelpers;
32
using Xunit;
43
using Xunit.Extensions;
54

@@ -15,9 +14,8 @@ public void InteractiveStateHasExpectedValuesForNewRepo(bool isBare)
1514
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
1615
using (var repo = Repository.Init(scd.DirectoryPath, isBare))
1716
{
18-
var state = repo.InteractiveState;
19-
Assert.Equal("master", state.HeadName);
20-
Assert.Equal(Operation.None, state.PendingOperation);
17+
Assert.Equal("master", repo.Head.Name);
18+
Assert.Equal(PendingOperation.None, repo.Info.PendingOperation);
2119
}
2220
}
2321

@@ -26,9 +24,8 @@ public void InteractiveStateHasExpectedValuesForABareRepo()
2624
{
2725
using (var repo = new Repository(BareTestRepoPath))
2826
{
29-
var state = repo.InteractiveState;
30-
Assert.Equal("master", state.HeadName);
31-
Assert.Equal(Operation.None, state.PendingOperation);
27+
Assert.Equal("master", repo.Head.Name);
28+
Assert.Equal(PendingOperation.None, repo.Info.PendingOperation);
3229
}
3330
}
3431

@@ -38,12 +35,11 @@ public void InteractiveStateHasExpectedValuesForStandardRepo()
3835
var path = BuildTemporaryCloneOfTestRepo(StandardTestRepoPath);
3936
using (var repo = new Repository(path.RepositoryPath))
4037
{
41-
var state = repo.InteractiveState;
42-
Assert.Equal("master", state.HeadName);
43-
Assert.Equal(Operation.None, state.PendingOperation);
38+
Assert.Equal("master", repo.Head.Name);
39+
Assert.Equal(PendingOperation.None, repo.Info.PendingOperation);
4440

4541
repo.Checkout("track-local");
46-
Assert.Equal("track-local", state.HeadName);
42+
Assert.Equal("track-local", repo.Head.Name);
4743
}
4844
}
4945

@@ -55,9 +51,8 @@ public void InteractiveStateHasExpectedValuesForDetachedHead()
5551
{
5652
repo.Checkout(repo.Head.Tip.Sha);
5753

58-
var state = repo.InteractiveState;
59-
Assert.Equal("(32eab9c...)", state.HeadName);
60-
Assert.Equal(Operation.None, state.PendingOperation);
54+
Assert.Equal("(32eab9c...)", repo.Head.Name);
55+
Assert.Equal(PendingOperation.None, repo.Info.PendingOperation);
6156
}
6257
}
6358

@@ -72,9 +67,8 @@ public void InteractiveStateHasExpectedValuesForInteractiveRebase()
7267
{
7368
repo.Checkout(repo.Head.Tip.Sha);
7469

75-
var state = repo.InteractiveState;
76-
Assert.Equal("master", state.HeadName);
77-
Assert.Equal(Operation.RebaseInteractive, state.PendingOperation);
70+
Assert.Equal("master", repo.Head.Name);
71+
Assert.Equal(PendingOperation.RebaseInteractive, repo.Info.PendingOperation);
7872
}
7973
}
8074
}

LibGit2Sharp.Tests/ResetHeadFixture.cs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,41 @@ public void ResettingWithBadParamsThrows()
6262
public void SoftResetSetsTheHeadToTheSpecifiedCommit()
6363
{
6464
/* Make the Head point to a branch through its name */
65-
AssertSoftReset(b => b.Name, false, b => b.Name);
65+
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
66+
67+
using (var repo = Repository.Init(scd.DirectoryPath))
68+
{
69+
FeedTheRepository(repo);
70+
71+
Tag tag = repo.Tags["mytag"];
72+
Branch branch = repo.Branches["mybranch"];
73+
74+
repo.Checkout(branch.Name);
75+
Assert.Equal(false, repo.Info.IsHeadDetached);
76+
77+
Assert.Equal(branch.Name, repo.Head.Name);
78+
Assert.Equal(branch.Tip.Sha, repo.Head.Tip.Sha);
79+
80+
/* Reset --soft the Head to a tag through its canonical name */
81+
repo.Reset(ResetOptions.Soft, tag.CanonicalName);
82+
Assert.Equal(branch.Name, repo.Head.Name);
83+
Assert.Equal(tag.Target.Id, repo.Head.Tip.Id);
84+
85+
Assert.Equal(FileStatus.Staged, repo.Index.RetrieveStatus("a.txt"));
86+
87+
/* Reset --soft the Head to a commit through its sha */
88+
repo.Reset(ResetOptions.Soft, branch.Tip.Sha);
89+
Assert.Equal(branch.Name, repo.Head.Name);
90+
Assert.Equal(branch.Tip.Sha, repo.Head.Tip.Sha);
91+
92+
Assert.Equal(FileStatus.Unaltered, repo.Index.RetrieveStatus("a.txt"));
93+
}
6694
}
6795

6896
[Fact]
6997
public void SoftResetSetsTheDetachedHeadToTheSpecifiedCommit()
7098
{
7199
/* Make the Head point to a commit through its sha (Detaches the Head) */
72-
AssertSoftReset(b => b.Tip.Sha, true, b => "(no branch)");
73-
}
74-
75-
private void AssertSoftReset(Func<Branch, string> branchIdentifierRetriever, bool shouldHeadBeDetached, Func<Branch, string> expectedHeadNameRetriever)
76-
{
77100
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
78101

79102
using (var repo = Repository.Init(scd.DirectoryPath))
@@ -83,17 +106,16 @@ private void AssertSoftReset(Func<Branch, string> branchIdentifierRetriever, boo
83106
Tag tag = repo.Tags["mytag"];
84107
Branch branch = repo.Branches["mybranch"];
85108

86-
string branchIdentifier = branchIdentifierRetriever(branch);
87-
repo.Checkout(branchIdentifier);
88-
Assert.Equal(shouldHeadBeDetached, repo.Info.IsHeadDetached);
109+
repo.Checkout(branch.Tip.Sha);
110+
Assert.Equal(true, repo.Info.IsHeadDetached);
89111

90-
string expectedHeadName = expectedHeadNameRetriever(branch);
112+
const string expectedHeadName = "(a5ed7f8...)";
91113
Assert.Equal(expectedHeadName, repo.Head.Name);
92114
Assert.Equal(branch.Tip.Sha, repo.Head.Tip.Sha);
93115

94116
/* Reset --soft the Head to a tag through its canonical name */
95117
repo.Reset(ResetOptions.Soft, tag.CanonicalName);
96-
Assert.Equal(expectedHeadName, repo.Head.Name);
118+
Assert.Equal("(653c177...)", repo.Head.Name);
97119
Assert.Equal(tag.Target.Id, repo.Head.Tip.Id);
98120

99121
Assert.Equal(FileStatus.Staged, repo.Index.RetrieveStatus("a.txt"));

LibGit2Sharp/Branch.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ public class Branch : ReferenceWrapper<Commit>
1717
/// </summary>
1818
/// <param name = "repo">The repo.</param>
1919
/// <param name = "reference">The reference.</param>
20-
/// <param name = "canonicalName">The full name of the reference</param>
21-
internal Branch(Repository repo, Reference reference, string canonicalName)
22-
: this(repo, reference, _ => canonicalName)
20+
/// <param name = "canonicalNameSelector">Provider of full name of the reference</param>
21+
internal Branch(Repository repo, Reference reference, Func<Reference, string> canonicalNameSelector)
22+
: base(repo, reference, canonicalNameSelector)
2323
{
24+
trackedBranch = new Lazy<Branch>(ResolveTrackedBranch);
2425
}
2526

2627
/// <summary>
@@ -36,12 +37,6 @@ internal Branch(Repository repo, Reference reference)
3637
{
3738
}
3839

39-
private Branch(Repository repo, Reference reference, Func<Reference, string> canonicalNameSelector)
40-
: base(repo, reference, canonicalNameSelector)
41-
{
42-
trackedBranch = new Lazy<Branch>(ResolveTrackedBranch);
43-
}
44-
4540
/// <summary>
4641
/// Gets the <see cref = "TreeEntry" /> pointed at by the <paramref name = "relativePath" /> in the <see cref = "Tip" />.
4742
/// </summary>

LibGit2Sharp/BranchCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private static string ShortToRemoteName(string name)
6060
private Branch BuildFromReferenceName(string canonicalName)
6161
{
6262
var reference = repo.Refs.Resolve<Reference>(canonicalName);
63-
return reference == null ? null : new Branch(repo, reference, canonicalName);
63+
return reference == null ? null : new Branch(repo, reference, _ => canonicalName);
6464
}
6565

6666
#region IEnumerable<Branch> Members

LibGit2Sharp/DetachedHead.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
namespace LibGit2Sharp
1+
using System.IO;
2+
3+
namespace LibGit2Sharp
24
{
35
internal class DetachedHead : Branch
46
{
5-
internal DetachedHead(Repository repo, Reference reference)
6-
: base(repo, reference, "(no branch)")
7+
internal DetachedHead(Repository repo, Reference reference, string sha)
8+
: base(repo, reference, _ => HeadName(repo.Info.Path, sha))
79
{
810
}
911

1012
protected override string Shorten(string branchName)
1113
{
1214
return branchName;
1315
}
16+
17+
public static string HeadName(string path, string tipSha)
18+
{
19+
if (File.Exists(Path.Combine(path, "rebase-merge/head-name")))
20+
{
21+
return File.ReadAllText(Path.Combine(path, "rebase-merge/head-name")).Replace("refs/heads/", "");
22+
}
23+
24+
return string.Format("({0}...)", tipSha.Substring(0, 7));
25+
}
1426
}
15-
}
27+
}

LibGit2Sharp/Interactive/State.cs

Lines changed: 0 additions & 85 deletions
This file was deleted.

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@
9898
<Compile Include="TreeChanges.cs" />
9999
<Compile Include="TreeEntryChanges.cs" />
100100
<Compile Include="LibGit2SharpException.cs" />
101-
<Compile Include="Interactive\Operation.cs" />
102-
<Compile Include="Interactive\State.cs" />
101+
<Compile Include="PendingOperation.cs" />
103102
<Compile Include="Core\Handles\ConfigurationSafeHandle.cs" />
104103
<Compile Include="Core\Ensure.cs" />
105104
<Compile Include="Core\Epoch.cs" />

LibGit2Sharp/Interactive/Operation.cs renamed to LibGit2Sharp/PendingOperation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
namespace LibGit2Sharp.Interactive
1+
namespace LibGit2Sharp
22
{
3-
public enum Operation
3+
public enum PendingOperation
44
{
55
None = 0,
66
RebaseInteractive = 1,
@@ -12,4 +12,4 @@ public enum Operation
1212
CherryPick = 7,
1313
Bisect = 8,
1414
}
15-
}
15+
}

LibGit2Sharp/Repository.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using LibGit2Sharp.Core;
77
using LibGit2Sharp.Core.Compat;
88
using LibGit2Sharp.Core.Handles;
9-
using LibGit2Sharp.Interactive;
109

1110
namespace LibGit2Sharp
1211
{
@@ -24,7 +23,6 @@ public class Repository : IDisposable
2423
private readonly Lazy<RemoteCollection> remotes;
2524
private readonly TagCollection tags;
2625
private readonly Lazy<RepositoryInformation> info;
27-
private readonly Lazy<State> interactiveState;
2826
private readonly Diff diff;
2927
private readonly NoteCollection notes;
3028
private readonly Lazy<ObjectDatabase> odb;
@@ -92,7 +90,6 @@ public Repository(string path, RepositoryOptions options = null)
9290
branches = new BranchCollection(this);
9391
tags = new TagCollection(this);
9492
info = new Lazy<RepositoryInformation>(() => new RepositoryInformation(this, isBare));
95-
interactiveState = new Lazy<State>(() => new State(this));
9693
config = new Lazy<Configuration>(() => RegisterForCleanup(new Configuration(this, configurationGlobalFilePath, configurationSystemFilePath)));
9794
remotes = new Lazy<RemoteCollection>(() => new RemoteCollection(this));
9895
odb = new Lazy<ObjectDatabase>(() => new ObjectDatabase(this));
@@ -128,7 +125,7 @@ public Branch Head
128125
return new Branch(this, reference);
129126
}
130127

131-
return new DetachedHead(this, reference);
128+
return new DetachedHead(this, reference, reference.TargetIdentifier);
132129
}
133130
}
134131

@@ -216,14 +213,6 @@ public RepositoryInformation Info
216213
get { return info.Value; }
217214
}
218215

219-
/// <summary>
220-
/// Provides information about this repository's interactive state (merge, rebase, etc).
221-
/// </summary>
222-
public State InteractiveState
223-
{
224-
get { return interactiveState.Value; }
225-
}
226-
227216
/// <summary>
228217
/// Provides access to diffing functionalities to show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, or changes between two files on disk.
229218
/// </summary>

0 commit comments

Comments
 (0)